This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/layouts/vertical.go

114 lines
2.9 KiB
Go
Raw Normal View History

2023-03-30 21:19:04 -06:00
package layouts
2023-01-09 18:34:19 -07:00
2023-01-10 14:39:37 -07:00
import "image"
import "golang.org/x/image/math/fixed"
2023-03-30 21:19:04 -06:00
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/fixedutil"
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 (
2023-03-30 21:19:04 -06:00
entries []tomo.LayoutEntry,
margin image.Point,
padding artist.Inset,
2023-02-01 23:47:31 -07:00
bounds image.Rectangle,
) {
if layout.Pad { bounds = padding.Apply(bounds) }
2023-01-10 14:39:37 -07:00
// get height of expanding elements
expandingElementHeight, minimumHeights := layout.expandingElementHeight (
entries, margin, padding, bounds.Dy())
2023-01-10 14:39:37 -07:00
// set the size and position of each element
dot := fixedutil.Pt(bounds.Min)
2023-01-10 14:39:37 -07:00
for index, entry := range entries {
if index > 0 && layout.Gap { dot.Y += fixed.I(margin.Y) }
2023-01-10 14:39:37 -07:00
entry.Bounds.Min = fixedutil.FloorPt(dot)
entryHeight := fixed.Int26_6(0)
2023-01-10 14:39:37 -07:00
if entry.Expand {
entryHeight = expandingElementHeight
} else {
entryHeight = fixed.I(minimumHeights[index])
2023-01-10 14:39:37 -07:00
}
2023-01-31 16:04:12 -07:00
dot.Y += entryHeight
entryBounds := entry.Bounds
entry.Bounds.Max = entryBounds.Min.Add (
image.Pt(bounds.Dx(),
entryHeight.Floor()))
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.
func (layout Vertical) MinimumSize (
2023-03-30 21:19:04 -06:00
entries []tomo.LayoutEntry,
margin image.Point,
padding artist.Inset,
) (
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 += margin.Y
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 {
width += padding.Horizontal()
height += padding.Vertical()
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
}
func (layout Vertical) expandingElementHeight (
2023-03-30 21:19:04 -06:00
entries []tomo.LayoutEntry,
margin image.Point,
padding artist.Inset,
freeSpace int,
) (
height fixed.Int26_6,
minimumHeights []int,
) {
// count the number of expanding elements and the amount of free space
// for them to collectively occupy, while gathering minimum heights.
minimumHeights = make([]int, len(entries))
expandingElements := 0
for index, entry := range entries {
_, entryMinHeight := entry.MinimumSize()
minimumHeights[index] = entryMinHeight
if entry.Expand {
expandingElements ++
} else {
freeSpace -= entryMinHeight
}
if index > 0 && layout.Gap {
freeSpace -= margin.Y
}
}
if expandingElements > 0 {
height = fixed.I(freeSpace) / fixed.Int26_6(expandingElements)
}
return
}