diff --git a/elements/basic/container.go b/elements/basic/container.go index c2e3cf2..158170d 100644 --- a/elements/basic/container.go +++ b/elements/basic/container.go @@ -301,9 +301,9 @@ func (element *Container) HandleSelection (direction tomo.SelectionDirection) (o } // FIXME: fix this! -// func (element *Container) MinimumHeightFor (width int) (height int) { - // return element.layout.MinimumHeightFor(element.children, width) -// } +func (element *Container) MinimumHeightFor (width int) (height int) { + return element.layout.MinimumHeightFor(element.children, width) +} func (element *Container) HandleDeselection () { element.selected = false diff --git a/layouts/horizontal.go b/layouts/horizontal.go index 2450d61..ebe3a2a 100644 --- a/layouts/horizontal.go +++ b/layouts/horizontal.go @@ -99,20 +99,51 @@ func (layout Horizontal) MinimumHeightFor ( ) ( height int, ) { + // TODO: maybe put calculating the expanding element width in a separate + // method 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 { - var entryHeight int + // set the size and position of each element + for index, entry := range entries { + entryWidth, entryHeight := entry.MinimumSize() + if entry.Expand { + entryWidth = expandingElementWidth + } if child, flexible := entry.Element.(tomo.Flexible); flexible { - entryHeight = child.MinimumHeightFor(width) - } else { - _, entryHeight = entry.MinimumSize() - } - if entryHeight > height { - height = entryHeight + entryHeight = child.MinimumHeightFor(entryWidth) } + if entryHeight > height { height = entryHeight } + + x += entryWidth + if index > 0 && layout.Gap { x += theme.Padding() } } if layout.Pad {