Combine Row/Column layouts into Contract layout

This commit is contained in:
Sasha Koshka 2024-05-17 03:56:49 -04:00
parent 68d49e1b02
commit 1cb9e8091e
2 changed files with 86 additions and 83 deletions

View File

@ -119,9 +119,5 @@ func (flow Flow) deltaMinor (rectangle image.Rectangle) int {
}
}
func (flow Flow) fallback () tomo.Layout {
if flow.v() {
return Column { }
} else {
return Row { }
}
return Contract(flow)
}

View File

@ -3,51 +3,18 @@ package layouts
import "image"
import "git.tebibyte.media/tomo/tomo"
type Row struct { }
// Contract is a layout that arranges boxes in a simple row or column according
// to their minimum sizes.
type Contract bool
func (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
}
// ContractVertical is a vertical contracted layout.
const ContractVertical Contract = true
func (Row) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
// TODO respect alignment value
dot := hints.Bounds.Min
for index, box := range boxes {
if index > 0 { dot.X += hints.Gap.X }
minimum := box.MinimumSize()
box.SetBounds(image.Rectangle {
Min: dot,
Max: dot.Add(image.Pt(minimum.X, hints.Bounds.Dy())),
})
dot.X += minimum.X
}
// ContractHorizontal is a horizontal contracted layout.
const ContractHorizontal Contract = false
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)))
}
}
type Column struct { }
func (Column) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
func (contract Contract) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
if contract.v() {
dot := image.Point { }
for _, box := range boxes {
minimum := box.MinimumSize()
@ -58,10 +25,22 @@ func (Column) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point
}
dot.Y += hints.Gap.Y * (len(boxes) - 1)
return dot
} else {
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) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
// TODO respect alignment value
func (contract Contract) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
if contract.v() {
dot := hints.Bounds.Min
for index, box := range boxes {
if index > 0 { dot.Y += hints.Gap.Y }
@ -85,4 +64,32 @@ func (Column) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
for _, box := range boxes {
box.SetBounds(box.Bounds().Add(image.Pt(0, offset)))
}
} else {
dot := hints.Bounds.Min
for index, box := range boxes {
if index > 0 { dot.X += hints.Gap.X }
minimum := box.MinimumSize()
box.SetBounds(image.Rectangle {
Min: dot,
Max: dot.Add(image.Pt(minimum.X, hints.Bounds.Dy())),
})
dot.X += minimum.X
}
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 (contract Contract) v () bool { return contract == ContractVertical }
func (contract Contract) h () bool { return contract == ContractHorizontal }