2024-06-12 01:15:38 -06:00
|
|
|
package layouts
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
|
|
|
|
|
|
|
var _ tomo.Layout = ContractVertical
|
|
|
|
|
|
|
|
// Row arranges boxes in a row. Boxes that share an index with a true value will
|
|
|
|
// expand, and others will contract.
|
|
|
|
type Row []bool
|
|
|
|
|
|
|
|
// Column arranges boxes in a column. Boxes that share an index with a true
|
|
|
|
// value will expand, and others will contract.
|
|
|
|
type Column []bool
|
|
|
|
|
|
|
|
func (column Column) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
|
|
|
|
dot := image.Point { }
|
|
|
|
for _, box := range boxes {
|
|
|
|
minimum := box.MinimumSize()
|
|
|
|
dot.Y += minimum.Y
|
|
|
|
if dot.X < minimum.X {
|
|
|
|
dot.X = minimum.X
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dot.Y += hints.Gap.Y * (len(boxes) - 1)
|
|
|
|
return dot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (row Row) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
|
|
|
|
dot := image.Point { }
|
|
|
|
for _, box := range boxes {
|
|
|
|
minimum := box.MinimumSize()
|
|
|
|
dot.X += minimum.X
|
|
|
|
if dot.Y < minimum.Y {
|
|
|
|
dot.Y = minimum.Y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dot.X += hints.Gap.X * (len(boxes) - 1)
|
|
|
|
return dot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (column Column) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
|
|
|
expands := func (index int) bool {
|
2024-06-12 01:32:32 -06:00
|
|
|
if index >= len(column) { return false }
|
2024-06-12 01:15:38 -06:00
|
|
|
return column[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine expanding box size
|
2024-06-12 01:32:32 -06:00
|
|
|
expandingSize := 0.0
|
2024-06-12 01:15:38 -06:00
|
|
|
if !hints.OverflowY {
|
|
|
|
gaps := len(boxes) - 1
|
|
|
|
freeSpace := float64(hints.Bounds.Dy() - hints.Gap.Y * gaps)
|
|
|
|
nExpanding := 0; for index, box := range boxes {
|
|
|
|
if expands(index) {
|
|
|
|
nExpanding ++
|
|
|
|
} else {
|
2024-06-15 05:58:51 -06:00
|
|
|
freeSpace -= float64(box.MinimumSize().Y)
|
2024-06-12 01:15:38 -06:00
|
|
|
}
|
|
|
|
}
|
2024-06-15 05:54:49 -06:00
|
|
|
expandingSize = freeSpace / float64(nExpanding)
|
2024-06-12 01:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// determine width
|
|
|
|
width := 0
|
|
|
|
if hints.OverflowX {
|
|
|
|
for _, box := range boxes {
|
|
|
|
minimum := box.MinimumSize()
|
|
|
|
if width < minimum.X { width = minimum.X }
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
width = hints.Bounds.Dx()
|
|
|
|
}
|
|
|
|
|
|
|
|
// arrange
|
|
|
|
dot := hints.Bounds.Min
|
|
|
|
for index, box := range boxes {
|
|
|
|
if index > 0 { dot.Y += hints.Gap.Y }
|
|
|
|
|
|
|
|
// determine height
|
|
|
|
height := box.MinimumSize().Y
|
|
|
|
if hints.OverflowY {
|
|
|
|
if box, ok := box.(tomo.ContentBox); ok {
|
|
|
|
height = box.RecommendedHeight(width)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if expands(index) {
|
2024-06-12 01:32:32 -06:00
|
|
|
height = int(expandingSize)
|
2024-06-12 01:15:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set bounds
|
|
|
|
box.SetBounds(image.Rectangle {
|
|
|
|
Min: dot,
|
|
|
|
Max: dot.Add(image.Pt(width, height)),
|
|
|
|
})
|
|
|
|
dot.Y += height
|
|
|
|
}
|
|
|
|
|
|
|
|
height := dot.Y - hints.Bounds.Min.Y
|
|
|
|
offset := 0
|
|
|
|
|
|
|
|
switch hints.AlignY {
|
|
|
|
case tomo.AlignMiddle:
|
|
|
|
offset = (hints.Bounds.Dy() - height) / 2
|
|
|
|
case tomo.AlignEnd:
|
|
|
|
offset = hints.Bounds.Dy() - height
|
|
|
|
}
|
|
|
|
for _, box := range boxes {
|
|
|
|
box.SetBounds(box.Bounds().Add(image.Pt(0, offset)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (row Row) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
|
|
|
expands := func (index int) bool {
|
2024-06-12 01:32:32 -06:00
|
|
|
if index >= len(row) { return false }
|
2024-06-12 01:15:38 -06:00
|
|
|
return row[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine expanding box size
|
2024-06-12 01:32:32 -06:00
|
|
|
expandingSize := 0.0
|
2024-06-12 01:15:38 -06:00
|
|
|
if !hints.OverflowY {
|
|
|
|
gaps := len(boxes) - 1
|
|
|
|
freeSpace := float64(hints.Bounds.Dx() - hints.Gap.X * gaps)
|
|
|
|
nExpanding := 0; for index, box := range boxes {
|
|
|
|
if expands(index) {
|
|
|
|
nExpanding ++
|
|
|
|
} else {
|
2024-06-15 05:58:51 -06:00
|
|
|
freeSpace -= float64(box.MinimumSize().X)
|
2024-06-12 01:15:38 -06:00
|
|
|
}
|
|
|
|
}
|
2024-06-12 01:32:32 -06:00
|
|
|
expandingSize = freeSpace / float64(nExpanding)
|
2024-06-12 01:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// determine height
|
|
|
|
height := 0
|
|
|
|
if hints.OverflowY {
|
|
|
|
for _, box := range boxes {
|
|
|
|
minimum := box.MinimumSize()
|
|
|
|
if height < minimum.Y { height = minimum.Y }
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
height = hints.Bounds.Dy()
|
|
|
|
}
|
|
|
|
|
|
|
|
// arrange
|
|
|
|
dot := hints.Bounds.Min
|
|
|
|
for index, box := range boxes {
|
|
|
|
if index > 0 { dot.X += hints.Gap.X }
|
|
|
|
|
|
|
|
// determine width
|
|
|
|
width := box.MinimumSize().X
|
|
|
|
if hints.OverflowY {
|
|
|
|
if box, ok := box.(tomo.ContentBox); ok {
|
|
|
|
width = box.RecommendedHeight(height)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if expands(index) {
|
2024-06-12 01:32:32 -06:00
|
|
|
width = int(expandingSize)
|
2024-06-12 01:15:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set bounds
|
|
|
|
box.SetBounds(image.Rectangle {
|
|
|
|
Min: dot,
|
|
|
|
Max: dot.Add(image.Pt(width, height)),
|
|
|
|
})
|
|
|
|
dot.X += width
|
|
|
|
}
|
|
|
|
|
|
|
|
width := dot.X - hints.Bounds.Min.X
|
|
|
|
offset := 0
|
|
|
|
|
|
|
|
switch hints.AlignX {
|
|
|
|
case tomo.AlignMiddle:
|
|
|
|
offset = (hints.Bounds.Dx() - width) / 2
|
|
|
|
case tomo.AlignEnd:
|
|
|
|
offset = hints.Bounds.Dx() - width
|
|
|
|
}
|
|
|
|
for _, box := range boxes {
|
|
|
|
box.SetBounds(box.Bounds().Add(image.Pt(offset, 0)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (column Column) RecommendedHeight (hints tomo.LayoutHints, boxes []tomo.Box, width int) int {
|
2024-06-12 01:35:38 -06:00
|
|
|
height := 0
|
|
|
|
for _, box := range boxes {
|
|
|
|
if box, ok := box.(tomo.ContentBox); ok {
|
|
|
|
height += box.RecommendedHeight(width)
|
|
|
|
} else {
|
|
|
|
height += box.MinimumSize().Y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
height += hints.Gap.Y * (len(boxes) - 1)
|
|
|
|
return height
|
2024-06-12 01:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (row Row) RecommendedHeight (hints tomo.LayoutHints, boxes []tomo.Box, width int) int {
|
2024-06-12 01:35:38 -06:00
|
|
|
height := 0
|
|
|
|
for _, box := range boxes {
|
|
|
|
minimum := box.MinimumSize()
|
|
|
|
boxHeight := 0
|
|
|
|
if box, ok := box.(tomo.ContentBox); ok {
|
|
|
|
boxHeight = box.RecommendedHeight(minimum.X)
|
|
|
|
} else {
|
|
|
|
boxHeight = minimum.Y
|
|
|
|
}
|
|
|
|
if boxHeight > height { height = boxHeight }
|
|
|
|
}
|
|
|
|
return height
|
2024-06-12 01:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (column Column) RecommendedWidth (hints tomo.LayoutHints, boxes []tomo.Box, height int) int {
|
2024-06-12 01:35:38 -06:00
|
|
|
width := 0
|
|
|
|
for _, box := range boxes {
|
|
|
|
minimum := box.MinimumSize()
|
|
|
|
boxWidth := 0
|
|
|
|
if box, ok := box.(tomo.ContentBox); ok {
|
|
|
|
boxWidth = box.RecommendedHeight(minimum.Y)
|
|
|
|
} else {
|
|
|
|
boxWidth = minimum.X
|
|
|
|
}
|
|
|
|
if boxWidth > width { width = boxWidth }
|
|
|
|
}
|
|
|
|
return width
|
2024-06-12 01:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (row Row) RecommendedWidth (hints tomo.LayoutHints, boxes []tomo.Box, height int) int {
|
2024-06-12 01:35:38 -06:00
|
|
|
width := 0
|
|
|
|
for _, box := range boxes {
|
|
|
|
if box, ok := box.(tomo.ContentBox); ok {
|
|
|
|
width += box.RecommendedWidth(height)
|
|
|
|
} else {
|
|
|
|
width += box.MinimumSize().X
|
|
|
|
}
|
|
|
|
}
|
|
|
|
width += hints.Gap.X * (len(boxes) - 1)
|
|
|
|
return width
|
2024-06-12 01:15:38 -06:00
|
|
|
}
|