2023-02-01 23:47:31 -07:00
|
|
|
package basicLayouts
|
2023-01-09 18:34:19 -07:00
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
import "image"
|
2023-02-01 23:47:31 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/layouts"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements"
|
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.
|
2023-02-01 23:47:31 -07:00
|
|
|
func (layout Vertical) Arrange (
|
|
|
|
entries []layouts.LayoutEntry,
|
|
|
|
margin int,
|
2023-03-02 15:58:42 -07:00
|
|
|
padding int,
|
2023-02-01 23:47:31 -07:00
|
|
|
bounds image.Rectangle,
|
|
|
|
) {
|
2023-03-02 15:58:42 -07:00
|
|
|
if layout.Pad { bounds = bounds.Inset(padding) }
|
2023-01-10 14:39:37 -07:00
|
|
|
|
|
|
|
// count the number of expanding elements and the amount of free space
|
2023-01-16 21:34:17 -07:00
|
|
|
// for them to collectively occupy, while gathering minimum heights.
|
2023-01-31 16:04:12 -07:00
|
|
|
freeSpace := bounds.Dy()
|
2023-01-16 21:34:17 -07:00
|
|
|
minimumHeights := make([]int, len(entries))
|
2023-01-31 16:04:12 -07:00
|
|
|
expandingElements := 0
|
2023-01-10 20:27:25 -07:00
|
|
|
for index, entry := range entries {
|
2023-01-16 21:34:17 -07:00
|
|
|
var entryMinHeight int
|
|
|
|
|
2023-02-01 23:47:31 -07:00
|
|
|
if child, flexible := entry.Element.(elements.Flexible); flexible {
|
2023-01-31 16:04:12 -07:00
|
|
|
entryMinHeight = child.FlexibleHeightFor(bounds.Dx())
|
2023-01-16 21:34:17 -07:00
|
|
|
} else {
|
|
|
|
_, entryMinHeight = entry.MinimumSize()
|
|
|
|
}
|
|
|
|
minimumHeights[index] = entryMinHeight
|
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
if entry.Expand {
|
|
|
|
expandingElements ++
|
|
|
|
} else {
|
|
|
|
freeSpace -= entryMinHeight
|
2023-01-10 20:27:25 -07:00
|
|
|
}
|
2023-01-10 21:20:42 -07:00
|
|
|
if index > 0 && layout.Gap {
|
2023-02-01 23:47:31 -07:00
|
|
|
freeSpace -= margin
|
2023-01-10 09:51:46 -07:00
|
|
|
}
|
|
|
|
}
|
2023-01-31 16:04:12 -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
|
|
|
|
|
|
|
// set the size and position of each element
|
2023-01-31 16:39:17 -07:00
|
|
|
dot := bounds.Min
|
2023-01-10 14:39:37 -07:00
|
|
|
for index, entry := range entries {
|
2023-02-01 23:47:31 -07:00
|
|
|
if index > 0 && layout.Gap { dot.Y += margin }
|
2023-01-10 14:39:37 -07:00
|
|
|
|
2023-01-31 16:04:12 -07:00
|
|
|
entry.Bounds.Min = dot
|
2023-01-10 14:39:37 -07:00
|
|
|
entryHeight := 0
|
|
|
|
if entry.Expand {
|
|
|
|
entryHeight = expandingElementHeight
|
|
|
|
} else {
|
2023-01-16 21:34:17 -07:00
|
|
|
entryHeight = minimumHeights[index]
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
2023-01-31 16:04:12 -07:00
|
|
|
dot.Y += entryHeight
|
2023-01-31 12:48:28 -07:00
|
|
|
entryBounds := entry.Bounds
|
2023-01-31 16:57:29 -07:00
|
|
|
entry.Bounds.Max = entryBounds.Min.Add(image.Pt(bounds.Dx(), entryHeight))
|
2023-01-31 16:04:12 -07:00
|
|
|
entries[index] = entry
|
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 (
|
2023-02-01 23:47:31 -07:00
|
|
|
entries []layouts.LayoutEntry,
|
|
|
|
margin int,
|
2023-03-02 15:58:42 -07:00
|
|
|
padding int,
|
2023-01-15 22:09:01 -07:00
|
|
|
) (
|
|
|
|
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 {
|
2023-02-01 23:47:31 -07:00
|
|
|
height += margin
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
}
|
2023-01-10 09:51:46 -07:00
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
if layout.Pad {
|
2023-03-02 15:58:42 -07:00
|
|
|
width += padding * 2
|
|
|
|
height += 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
|
|
|
}
|
2023-01-16 21:34:17 -07:00
|
|
|
|
2023-01-19 14:54:49 -07:00
|
|
|
// FlexibleHeightFor Returns the minimum height the layout needs to lay out the
|
2023-01-16 21:34:17 -07:00
|
|
|
// specified elements at the given width, taking into account flexible elements.
|
2023-01-19 14:54:49 -07:00
|
|
|
func (layout Vertical) FlexibleHeightFor (
|
2023-02-01 23:47:31 -07:00
|
|
|
entries []layouts.LayoutEntry,
|
|
|
|
margin int,
|
2023-03-02 15:58:42 -07:00
|
|
|
padding int,
|
2023-01-17 08:55:38 -07:00
|
|
|
width int,
|
2023-01-16 21:34:17 -07:00
|
|
|
) (
|
|
|
|
height int,
|
|
|
|
) {
|
2023-01-17 08:55:38 -07:00
|
|
|
if layout.Pad {
|
2023-03-02 15:58:42 -07:00
|
|
|
width -= padding * 2
|
|
|
|
height += padding * 2
|
2023-01-17 08:55:38 -07:00
|
|
|
}
|
|
|
|
|
2023-01-16 21:34:17 -07:00
|
|
|
for index, entry := range entries {
|
2023-02-01 23:47:31 -07:00
|
|
|
child, flexible := entry.Element.(elements.Flexible)
|
2023-01-16 21:34:17 -07:00
|
|
|
if flexible {
|
2023-01-19 14:54:49 -07:00
|
|
|
height += child.FlexibleHeightFor(width)
|
2023-01-16 21:34:17 -07:00
|
|
|
} else {
|
|
|
|
_, entryHeight := entry.MinimumSize()
|
|
|
|
height += entryHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
if layout.Gap && index > 0 {
|
2023-02-01 23:47:31 -07:00
|
|
|
height += margin
|
2023-01-16 21:34:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|