From 541d0f42042b6fe31361386ac65a28e6ecee3a68 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 31 Jan 2023 18:06:55 -0500 Subject: [PATCH] Horizontal layouts now work --- layouts/horizontal.go | 32 ++++++++++++++------------------ layouts/vertical.go | 4 +--- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/layouts/horizontal.go b/layouts/horizontal.go index f891348..93339fc 100644 --- a/layouts/horizontal.go +++ b/layouts/horizontal.go @@ -17,37 +17,33 @@ type Horizontal struct { } // Arrange arranges a list of entries horizontally. -func (layout Horizontal) Arrange (entries []tomo.LayoutEntry, width, height int) { - if layout.Pad { - width -= theme.Margin() * 2 - height -= theme.Margin() * 2 - } - // get width of expanding elements - expandingElementWidth := layout.expandingElementWidth(entries, width) +func (layout Horizontal) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle) { + if layout.Pad { bounds = bounds.Inset(theme.Margin()) } - x, y := 0, 0 - if layout.Pad { - x += theme.Margin() - y += theme.Margin() - } + // get width of expanding elements + expandingElementWidth := layout.expandingElementWidth(entries, bounds.Dx()) + + dot := bounds.Min // set the size and position of each element for index, entry := range entries { - if index > 0 && layout.Gap { x += theme.Margin() } + if index > 0 && layout.Gap { dot.X += theme.Margin() } - entries[index].Bounds.Min = image.Pt(x, y) + entry.Bounds.Min = dot entryWidth := 0 if entry.Expand { entryWidth = expandingElementWidth } else { entryWidth, _ = entry.MinimumSize() } - x += entryWidth + dot.X += entryWidth entryBounds := entry.Bounds - if entryBounds.Dy() != height || entryBounds.Dx() != entryWidth { - entries[index].Bounds.Max = entryBounds.Min.Add ( - image.Pt(entryWidth, height)) + if entryBounds.Dy() != bounds.Dy() || entryBounds.Dx() != entryWidth { + entry.Bounds.Max = entryBounds.Min.Add ( + image.Pt(entryWidth, bounds.Dy())) } + + entries[index] = entry } } diff --git a/layouts/vertical.go b/layouts/vertical.go index 56fc23a..a400e46 100644 --- a/layouts/vertical.go +++ b/layouts/vertical.go @@ -18,9 +18,7 @@ type Vertical struct { // Arrange arranges a list of entries vertically. func (layout Vertical) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle) { - if layout.Pad { - bounds = bounds.Inset(theme.Margin()) - } + if layout.Pad { bounds = bounds.Inset(theme.Margin()) } // count the number of expanding elements and the amount of free space // for them to collectively occupy, while gathering minimum heights.