Horizontal layouts work nearly perfectly

This commit is contained in:
Sasha Koshka 2023-01-17 01:40:49 -05:00
parent 9459bcd942
commit 40bdffc8be
2 changed files with 43 additions and 12 deletions

View File

@ -301,9 +301,9 @@ func (element *Container) HandleSelection (direction tomo.SelectionDirection) (o
} }
// FIXME: fix this! // FIXME: fix this!
// func (element *Container) MinimumHeightFor (width int) (height int) { func (element *Container) MinimumHeightFor (width int) (height int) {
// return element.layout.MinimumHeightFor(element.children, width) return element.layout.MinimumHeightFor(element.children, width)
// } }
func (element *Container) HandleDeselection () { func (element *Container) HandleDeselection () {
element.selected = false element.selected = false

View File

@ -99,20 +99,51 @@ func (layout Horizontal) MinimumHeightFor (
) ( ) (
height int, height int,
) { ) {
// TODO: maybe put calculating the expanding element width in a separate
// method
if layout.Pad { if layout.Pad {
width -= theme.Padding() * 2 width -= theme.Padding() * 2
}
freeSpace := width
expandingElements := 0
// count the number of expanding elements and the amount of free space
// for them to collectively occupy
for index, entry := range entries {
if entry.Expand {
expandingElements ++
} else {
entryMinWidth, _ := entry.MinimumSize()
freeSpace -= entryMinWidth
}
if index > 0 && layout.Gap {
freeSpace -= theme.Padding()
}
}
expandingElementWidth := 0
if expandingElements > 0 {
expandingElementWidth = freeSpace / expandingElements
}
x, y := 0, 0
if layout.Pad {
x += theme.Padding()
y += theme.Padding()
} }
for _, entry := range entries { // set the size and position of each element
var entryHeight int for index, entry := range entries {
entryWidth, entryHeight := entry.MinimumSize()
if entry.Expand {
entryWidth = expandingElementWidth
}
if child, flexible := entry.Element.(tomo.Flexible); flexible { if child, flexible := entry.Element.(tomo.Flexible); flexible {
entryHeight = child.MinimumHeightFor(width) entryHeight = child.MinimumHeightFor(entryWidth)
} else {
_, entryHeight = entry.MinimumSize()
}
if entryHeight > height {
height = entryHeight
} }
if entryHeight > height { height = entryHeight }
x += entryWidth
if index > 0 && layout.Gap { x += theme.Padding() }
} }
if layout.Pad { if layout.Pad {