Horizontal layouts now work

This commit is contained in:
Sasha Koshka 2023-01-31 18:06:55 -05:00
parent b0ff1ca0af
commit 541d0f4204
2 changed files with 15 additions and 21 deletions

View File

@ -17,37 +17,33 @@ type Horizontal struct {
} }
// Arrange arranges a list of entries horizontally. // Arrange arranges a list of entries horizontally.
func (layout Horizontal) Arrange (entries []tomo.LayoutEntry, width, height int) { func (layout Horizontal) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle) {
if layout.Pad { if layout.Pad { bounds = bounds.Inset(theme.Margin()) }
width -= theme.Margin() * 2
height -= theme.Margin() * 2
}
// get width of expanding elements
expandingElementWidth := layout.expandingElementWidth(entries, width)
x, y := 0, 0 // get width of expanding elements
if layout.Pad { expandingElementWidth := layout.expandingElementWidth(entries, bounds.Dx())
x += theme.Margin()
y += theme.Margin() dot := bounds.Min
}
// set the size and position of each element // set the size and position of each element
for index, entry := range entries { 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 entryWidth := 0
if entry.Expand { if entry.Expand {
entryWidth = expandingElementWidth entryWidth = expandingElementWidth
} else { } else {
entryWidth, _ = entry.MinimumSize() entryWidth, _ = entry.MinimumSize()
} }
x += entryWidth dot.X += entryWidth
entryBounds := entry.Bounds entryBounds := entry.Bounds
if entryBounds.Dy() != height || entryBounds.Dx() != entryWidth { if entryBounds.Dy() != bounds.Dy() || entryBounds.Dx() != entryWidth {
entries[index].Bounds.Max = entryBounds.Min.Add ( entry.Bounds.Max = entryBounds.Min.Add (
image.Pt(entryWidth, height)) image.Pt(entryWidth, bounds.Dy()))
} }
entries[index] = entry
} }
} }

View File

@ -18,9 +18,7 @@ type Vertical struct {
// Arrange arranges a list of entries vertically. // Arrange arranges a list of entries vertically.
func (layout Vertical) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle) { func (layout Vertical) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle) {
if layout.Pad { if layout.Pad { bounds = bounds.Inset(theme.Margin()) }
bounds = bounds.Inset(theme.Margin())
}
// count the number of expanding elements and the amount of free space // count the number of expanding elements and the amount of free space
// for them to collectively occupy, while gathering minimum heights. // for them to collectively occupy, while gathering minimum heights.