Half-working container flexibility propagation

This commit is contained in:
2023-01-16 23:34:17 -05:00
parent 76d50bb01a
commit e94e170a04
8 changed files with 91 additions and 34 deletions

View File

@@ -25,17 +25,22 @@ func (layout Vertical) Arrange (entries []tomo.LayoutEntry, width, height int) {
freeSpace := height
expandingElements := 0
// TODO: find the width first, then store the minumum height of
// everything in a list, then arrange everything.
// minimumHeights := make([]int, len(entries))
// count the number of expanding elements and the amount of free space
// for them to collectively occupy
// for them to collectively occupy, while gathering minimum heights.
minimumHeights := make([]int, len(entries))
for index, entry := range entries {
var entryMinHeight int
if child, flexible := entry.Element.(tomo.Flexible); flexible {
entryMinHeight = child.MinimumHeightFor(width)
} else {
_, entryMinHeight = entry.MinimumSize()
}
minimumHeights[index] = entryMinHeight
if entry.Expand {
expandingElements ++
} else {
_, entryMinHeight := entry.MinimumSize()
freeSpace -= entryMinHeight
}
if index > 0 && layout.Gap {
@@ -62,7 +67,7 @@ func (layout Vertical) Arrange (entries []tomo.LayoutEntry, width, height int) {
if entry.Expand {
entryHeight = expandingElementHeight
} else {
_, entryHeight = entry.MinimumSize()
entryHeight = minimumHeights[index]
}
y += entryHeight
entryBounds := entry.Bounds()
@@ -76,7 +81,6 @@ func (layout Vertical) Arrange (entries []tomo.LayoutEntry, width, height int) {
// arrange the given list of entries.
func (layout Vertical) MinimumSize (
entries []tomo.LayoutEntry,
squeeze int,
) (
width, height int,
) {
@@ -97,3 +101,31 @@ func (layout Vertical) MinimumSize (
}
return
}
// MinimumHeightFor Returns the minimum height the layout needs to lay out the
// specified elements at the given width, taking into account flexible elements.
func (layout Vertical) MinimumHeightFor (
entries []tomo.LayoutEntry,
squeeze int,
) (
height int,
) {
for index, entry := range entries {
child, flexible := entry.Element.(tomo.Flexible)
if flexible {
height += child.MinimumHeightFor(squeeze)
} else {
_, entryHeight := entry.MinimumSize()
height += entryHeight
}
if layout.Gap && index > 0 {
height += theme.Padding()
}
}
if layout.Pad {
height += theme.Padding() * 2
}
return
}