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.
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
}
}

View File

@ -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.