diff --git a/examples/horizontalLayout/main.go b/examples/horizontalLayout/main.go index f35bf54..39ecc0c 100644 --- a/examples/horizontalLayout/main.go +++ b/examples/horizontalLayout/main.go @@ -10,7 +10,7 @@ func main () { } func run () { - window, _ := tomo.NewWindow(256, 2) + window, _ := tomo.NewWindow(360, 2) window.SetTitle("horizontal stack") container := basic.NewContainer(layouts.Horizontal { true, true }) diff --git a/layouts/horizontal.go b/layouts/horizontal.go index ebe3a2a..8207087 100644 --- a/layouts/horizontal.go +++ b/layouts/horizontal.go @@ -22,26 +22,8 @@ func (layout Horizontal) Arrange (entries []tomo.LayoutEntry, width, height int) width -= theme.Padding() * 2 height -= 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 - } + // get width of expanding elements + expandingElementWidth := layout.expandingElementWidth(entries, width) x, y := 0, 0 if layout.Pad { @@ -99,31 +81,11 @@ 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 - } - 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 + width -= theme.Padding() * 2 } + // get width of expanding elements + expandingElementWidth := layout.expandingElementWidth(entries, width) x, y := 0, 0 if layout.Pad { @@ -151,3 +113,31 @@ func (layout Horizontal) MinimumHeightFor ( } return } + +func (layout Horizontal) expandingElementWidth ( + entries []tomo.LayoutEntry, + freeSpace int, +) ( + width int, +) { + 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() + } + } + + if expandingElements > 0 { + width = freeSpace / expandingElements + } + return +}