2023-01-09 18:34:19 -07:00
|
|
|
package layouts
|
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
import "image"
|
2023-01-09 18:34:19 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
2023-01-10 09:51:46 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
2023-01-09 18:34:19 -07:00
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
// Vertical arranges elements vertically. Elements at the start of the entry
|
|
|
|
// list will be positioned at the top, and elements at the end of the entry list
|
|
|
|
// will positioned at the bottom. All elements have the same width.
|
2023-01-09 18:34:19 -07:00
|
|
|
type Vertical struct {
|
2023-01-10 14:39:37 -07:00
|
|
|
// If Gap is true, a gap will be placed between each element.
|
|
|
|
Gap bool
|
2023-01-10 09:51:46 -07:00
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
// If Pad is true, there will be padding running along the inside of the
|
|
|
|
// layout's border.
|
|
|
|
Pad bool
|
2023-01-10 09:51:46 -07:00
|
|
|
}
|
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
// Arrange arranges a list of entries vertically.
|
|
|
|
func (layout Vertical) Arrange (entries []tomo.LayoutEntry, width, height int) {
|
|
|
|
if layout.Pad {
|
|
|
|
width -= theme.Padding() * 2
|
|
|
|
height -= theme.Padding() * 2
|
|
|
|
}
|
|
|
|
freeSpace := height
|
|
|
|
expandingElements := 0
|
|
|
|
|
|
|
|
// count the number of expanding elements and the amount of free space
|
|
|
|
// for them to collectively occupy
|
2023-01-10 20:27:25 -07:00
|
|
|
for index, entry := range entries {
|
2023-01-10 14:39:37 -07:00
|
|
|
if entry.Expand {
|
|
|
|
expandingElements ++
|
|
|
|
} else {
|
|
|
|
_, entryMinHeight := entry.MinimumSize()
|
|
|
|
freeSpace -= entryMinHeight
|
2023-01-10 20:27:25 -07:00
|
|
|
}
|
2023-01-10 21:20:42 -07:00
|
|
|
if index > 0 && layout.Gap {
|
2023-01-10 15:34:40 -07:00
|
|
|
freeSpace -= theme.Padding()
|
2023-01-10 09:51:46 -07:00
|
|
|
}
|
|
|
|
}
|
2023-01-10 14:39:37 -07:00
|
|
|
expandingElementHeight := 0
|
|
|
|
if expandingElements > 0 {
|
|
|
|
expandingElementHeight = freeSpace / expandingElements
|
2023-01-10 09:51:46 -07:00
|
|
|
}
|
2023-01-10 14:39:37 -07:00
|
|
|
|
|
|
|
x, y := 0, 0
|
|
|
|
if layout.Pad {
|
|
|
|
x += theme.Padding()
|
|
|
|
y += theme.Padding()
|
2023-01-10 09:51:46 -07:00
|
|
|
}
|
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
// set the size and position of each element
|
|
|
|
for index, entry := range entries {
|
|
|
|
if index > 0 && layout.Gap { y += theme.Padding() }
|
|
|
|
|
|
|
|
entries[index].Position = image.Pt(x, y)
|
|
|
|
entryHeight := 0
|
|
|
|
if entry.Expand {
|
|
|
|
entryHeight = expandingElementHeight
|
|
|
|
} else {
|
|
|
|
_, entryHeight = entry.MinimumSize()
|
|
|
|
}
|
|
|
|
y += entryHeight
|
|
|
|
entryBounds := entry.Bounds()
|
|
|
|
if entryBounds.Dx() != width || entryBounds.Dy() != entryHeight {
|
2023-01-15 22:09:01 -07:00
|
|
|
entry.Resize(width, entryHeight)
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
2023-01-10 09:51:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 21:20:42 -07:00
|
|
|
// MinimumSize returns the minimum width and height that will be needed to
|
|
|
|
// arrange the given list of entries.
|
2023-01-15 22:09:01 -07:00
|
|
|
func (layout Vertical) MinimumSize (
|
|
|
|
entries []tomo.LayoutEntry,
|
|
|
|
squeeze int,
|
|
|
|
) (
|
|
|
|
width, height int,
|
|
|
|
) {
|
2023-01-10 14:39:37 -07:00
|
|
|
for index, entry := range entries {
|
|
|
|
entryWidth, entryHeight := entry.MinimumSize()
|
|
|
|
if entryWidth > width {
|
|
|
|
width = entryWidth
|
|
|
|
}
|
|
|
|
height += entryHeight
|
|
|
|
if layout.Gap && index > 0 {
|
|
|
|
height += theme.Padding()
|
|
|
|
}
|
|
|
|
}
|
2023-01-10 09:51:46 -07:00
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
if layout.Pad {
|
|
|
|
width += theme.Padding() * 2
|
|
|
|
height += theme.Padding() * 2
|
2023-01-10 09:51:46 -07:00
|
|
|
}
|
2023-01-10 14:39:37 -07:00
|
|
|
return
|
2023-01-09 18:34:19 -07:00
|
|
|
}
|