Bit of refactoring of the horizontal layout
This commit is contained in:
parent
99492874ca
commit
b7fe28f600
@ -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 })
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user