Layouts now take in proper margin and padding values

This commit is contained in:
Sasha Koshka 2023-03-04 01:42:14 -05:00
parent 90ce0d7281
commit 5fc5af92df
5 changed files with 69 additions and 65 deletions

View File

@ -246,10 +246,9 @@ func (element *Container) SetConfig (new config.Config) {
func (element *Container) FlexibleHeightFor (width int) (height int) { func (element *Container) FlexibleHeightFor (width int) (height int) {
margin := element.theme.Margin(theme.PatternBackground) margin := element.theme.Margin(theme.PatternBackground)
padding := element.theme.Padding(theme.PatternBackground) padding := element.theme.Padding(theme.PatternBackground)
// TODO: have layouts take in entire margins/padding
return element.layout.FlexibleHeightFor ( return element.layout.FlexibleHeightFor (
element.children, element.children,
margin.X, padding.Horizontal(), width) margin, padding, width)
} }
func (element *Container) OnFlexibleHeightChange (callback func ()) { func (element *Container) OnFlexibleHeightChange (callback func ()) {
@ -323,13 +322,12 @@ func (element *Container) unfocusAllChildren() {
func (element *Container) updateMinimumSize () { func (element *Container) updateMinimumSize () {
margin := element.theme.Margin(theme.PatternBackground) margin := element.theme.Margin(theme.PatternBackground)
padding := element.theme.Padding(theme.PatternBackground) padding := element.theme.Padding(theme.PatternBackground)
// TODO: have layouts take in entire margins/padding
width, height := element.layout.MinimumSize ( width, height := element.layout.MinimumSize (
element.children, margin.X, padding.Horizontal()) element.children, margin, padding)
if element.flexible { if element.flexible {
height = element.layout.FlexibleHeightFor ( height = element.layout.FlexibleHeightFor (
element.children, margin.X, element.children, margin,
padding.Horizontal(), width) padding, width)
} }
element.core.SetMinimumSize(width, height) element.core.SetMinimumSize(width, height)
} }
@ -337,8 +335,7 @@ func (element *Container) updateMinimumSize () {
func (element *Container) doLayout () { func (element *Container) doLayout () {
margin := element.theme.Margin(theme.PatternBackground) margin := element.theme.Margin(theme.PatternBackground)
padding := element.theme.Padding(theme.PatternBackground) padding := element.theme.Padding(theme.PatternBackground)
// TODO: have layouts take in entire margins/padding
element.layout.Arrange ( element.layout.Arrange (
element.children, margin.X, element.children, margin,
padding.Horizontal(), element.Bounds()) padding, element.Bounds())
} }

View File

@ -1,6 +1,7 @@
package basicLayouts package basicLayouts
import "image" import "image"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/layouts" import "git.tebibyte.media/sashakoshka/tomo/layouts"
import "git.tebibyte.media/sashakoshka/tomo/elements" import "git.tebibyte.media/sashakoshka/tomo/elements"
@ -21,11 +22,11 @@ type Dialog struct {
// Arrange arranges a list of entries into a dialog. // Arrange arranges a list of entries into a dialog.
func (layout Dialog) Arrange ( func (layout Dialog) Arrange (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin image.Point,
padding int, padding artist.Inset,
bounds image.Rectangle, bounds image.Rectangle,
) { ) {
if layout.Pad { bounds = bounds.Inset(padding) } if layout.Pad { bounds = padding.Apply(bounds) }
controlRowWidth, controlRowHeight := 0, 0 controlRowWidth, controlRowHeight := 0, 0
if len(entries) > 1 { if len(entries) > 1 {
@ -39,7 +40,7 @@ func (layout Dialog) Arrange (
main.Bounds.Min = bounds.Min main.Bounds.Min = bounds.Min
mainHeight := bounds.Dy() - controlRowHeight mainHeight := bounds.Dy() - controlRowHeight
if layout.Gap { if layout.Gap {
mainHeight -= margin mainHeight -= margin.Y
} }
main.Bounds.Max = main.Bounds.Min.Add(image.Pt(bounds.Dx(), mainHeight)) main.Bounds.Max = main.Bounds.Min.Add(image.Pt(bounds.Dx(), mainHeight))
entries[0] = main entries[0] = main
@ -59,7 +60,7 @@ func (layout Dialog) Arrange (
freeSpace -= entryMinWidth freeSpace -= entryMinWidth
} }
if index > 0 && layout.Gap { if index > 0 && layout.Gap {
freeSpace -= margin freeSpace -= margin.X
} }
} }
expandingElementWidth := 0 expandingElementWidth := 0
@ -75,7 +76,7 @@ func (layout Dialog) Arrange (
// set the size and position of each element in the control row // set the size and position of each element in the control row
for index, entry := range entries[1:] { for index, entry := range entries[1:] {
if index > 0 && layout.Gap { dot.X += margin } if index > 0 && layout.Gap { dot.X += margin.X }
entry.Bounds.Min = dot entry.Bounds.Min = dot
entryWidth := 0 entryWidth := 0
@ -102,8 +103,8 @@ func (layout Dialog) Arrange (
// arrange the given list of entries. // arrange the given list of entries.
func (layout Dialog) MinimumSize ( func (layout Dialog) MinimumSize (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin image.Point,
padding int, padding artist.Inset,
) ( ) (
width, height int, width, height int,
) { ) {
@ -114,7 +115,7 @@ func (layout Dialog) MinimumSize (
} }
if len(entries) > 1 { if len(entries) > 1 {
if layout.Gap { height += margin } if layout.Gap { height += margin.X }
additionalWidth, additionalWidth,
additionalHeight := layout.minimumSizeOfControlRow ( additionalHeight := layout.minimumSizeOfControlRow (
entries[1:], margin, padding) entries[1:], margin, padding)
@ -125,8 +126,8 @@ func (layout Dialog) MinimumSize (
} }
if layout.Pad { if layout.Pad {
width += padding * 2 width += padding.Horizontal()
height += padding * 2 height += padding.Vertical()
} }
return return
} }
@ -135,14 +136,14 @@ func (layout Dialog) MinimumSize (
// specified elements at the given width, taking into account flexible elements. // specified elements at the given width, taking into account flexible elements.
func (layout Dialog) FlexibleHeightFor ( func (layout Dialog) FlexibleHeightFor (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin image.Point,
padding int, padding artist.Inset,
width int, width int,
) ( ) (
height int, height int,
) { ) {
if layout.Pad { if layout.Pad {
width -= margin * 2 width -= padding.Horizontal()
} }
if len(entries) > 0 { if len(entries) > 0 {
@ -156,14 +157,14 @@ func (layout Dialog) FlexibleHeightFor (
} }
if len(entries) > 1 { if len(entries) > 1 {
if layout.Gap { height += margin } if layout.Gap { height += margin.Y }
_, additionalHeight := layout.minimumSizeOfControlRow ( _, additionalHeight := layout.minimumSizeOfControlRow (
entries[1:], margin, padding) entries[1:], margin, padding)
height += additionalHeight height += additionalHeight
} }
if layout.Pad { if layout.Pad {
height += padding * 2 height += padding.Vertical()
} }
return return
} }
@ -172,8 +173,8 @@ func (layout Dialog) FlexibleHeightFor (
// the control row. // the control row.
func (layout Dialog) minimumSizeOfControlRow ( func (layout Dialog) minimumSizeOfControlRow (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin image.Point,
padding int, padding artist.Inset,
) ( ) (
width, height int, width, height int,
) { ) {
@ -184,7 +185,7 @@ func (layout Dialog) minimumSizeOfControlRow (
} }
width += entryWidth width += entryWidth
if layout.Gap && index > 0 { if layout.Gap && index > 0 {
width += margin width += margin.X
} }
} }
return return

View File

@ -1,6 +1,7 @@
package basicLayouts package basicLayouts
import "image" import "image"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/layouts" import "git.tebibyte.media/sashakoshka/tomo/layouts"
import "git.tebibyte.media/sashakoshka/tomo/elements" import "git.tebibyte.media/sashakoshka/tomo/elements"
@ -19,11 +20,11 @@ type Horizontal struct {
// Arrange arranges a list of entries horizontally. // Arrange arranges a list of entries horizontally.
func (layout Horizontal) Arrange ( func (layout Horizontal) Arrange (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin image.Point,
padding int, padding artist.Inset,
bounds image.Rectangle, bounds image.Rectangle,
) { ) {
if layout.Pad { bounds = bounds.Inset(padding) } if layout.Pad { bounds = padding.Apply(bounds) }
// get width of expanding elements // get width of expanding elements
expandingElementWidth := layout.expandingElementWidth ( expandingElementWidth := layout.expandingElementWidth (
@ -32,7 +33,7 @@ func (layout Horizontal) Arrange (
// set the size and position of each element // set the size and position of each element
dot := bounds.Min dot := bounds.Min
for index, entry := range entries { for index, entry := range entries {
if index > 0 && layout.Gap { dot.X += margin } if index > 0 && layout.Gap { dot.X += margin.X }
entry.Bounds.Min = dot entry.Bounds.Min = dot
entryWidth := 0 entryWidth := 0
@ -52,7 +53,8 @@ func (layout Horizontal) Arrange (
// arrange the given list of entries. // arrange the given list of entries.
func (layout Horizontal) MinimumSize ( func (layout Horizontal) MinimumSize (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin image.Point,
padding artist.Inset,
) ( ) (
width, height int, width, height int,
) { ) {
@ -63,13 +65,13 @@ func (layout Horizontal) MinimumSize (
} }
width += entryWidth width += entryWidth
if layout.Gap && index > 0 { if layout.Gap && index > 0 {
width += margin width += margin.X
} }
} }
if layout.Pad { if layout.Pad {
width += margin * 2 width += padding.Horizontal()
height += margin * 2 height += padding.Vertical()
} }
return return
} }
@ -78,13 +80,13 @@ func (layout Horizontal) MinimumSize (
// specified elements at the given width, taking into account flexible elements. // specified elements at the given width, taking into account flexible elements.
func (layout Horizontal) FlexibleHeightFor ( func (layout Horizontal) FlexibleHeightFor (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin image.Point,
padding int, padding artist.Inset,
width int, width int,
) ( ) (
height int, height int,
) { ) {
if layout.Pad { width -= padding * 2 } if layout.Pad { width -= padding.Horizontal() }
// get width of expanding elements // get width of expanding elements
expandingElementWidth := layout.expandingElementWidth ( expandingElementWidth := layout.expandingElementWidth (
@ -92,8 +94,8 @@ func (layout Horizontal) FlexibleHeightFor (
x, y := 0, 0 x, y := 0, 0
if layout.Pad { if layout.Pad {
x += padding x += padding.Horizontal()
y += padding y += padding.Vertical()
} }
// set the size and position of each element // set the size and position of each element
@ -108,19 +110,19 @@ func (layout Horizontal) FlexibleHeightFor (
if entryHeight > height { height = entryHeight } if entryHeight > height { height = entryHeight }
x += entryWidth x += entryWidth
if index > 0 && layout.Gap { x += margin } if index > 0 && layout.Gap { x += margin.X }
} }
if layout.Pad { if layout.Pad {
height += padding * 2 height += padding.Vertical()
} }
return return
} }
func (layout Horizontal) expandingElementWidth ( func (layout Horizontal) expandingElementWidth (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin image.Point,
padding int, padding artist.Inset,
freeSpace int, freeSpace int,
) ( ) (
width int, width int,
@ -137,7 +139,7 @@ func (layout Horizontal) expandingElementWidth (
freeSpace -= entryMinWidth freeSpace -= entryMinWidth
} }
if index > 0 && layout.Gap { if index > 0 && layout.Gap {
freeSpace -= margin freeSpace -= margin.X
} }
} }

View File

@ -1,6 +1,7 @@
package basicLayouts package basicLayouts
import "image" import "image"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/layouts" import "git.tebibyte.media/sashakoshka/tomo/layouts"
import "git.tebibyte.media/sashakoshka/tomo/elements" import "git.tebibyte.media/sashakoshka/tomo/elements"
@ -19,11 +20,11 @@ type Vertical struct {
// Arrange arranges a list of entries vertically. // Arrange arranges a list of entries vertically.
func (layout Vertical) Arrange ( func (layout Vertical) Arrange (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin image.Point,
padding int, padding artist.Inset,
bounds image.Rectangle, bounds image.Rectangle,
) { ) {
if layout.Pad { bounds = bounds.Inset(padding) } if layout.Pad { bounds = padding.Apply(bounds) }
// count the number of expanding elements and the amount of free space // count the number of expanding elements and the amount of free space
// for them to collectively occupy, while gathering minimum heights. // for them to collectively occupy, while gathering minimum heights.
@ -46,7 +47,7 @@ func (layout Vertical) Arrange (
freeSpace -= entryMinHeight freeSpace -= entryMinHeight
} }
if index > 0 && layout.Gap { if index > 0 && layout.Gap {
freeSpace -= margin freeSpace -= margin.Y
} }
} }
@ -58,7 +59,7 @@ func (layout Vertical) Arrange (
// set the size and position of each element // set the size and position of each element
dot := bounds.Min dot := bounds.Min
for index, entry := range entries { for index, entry := range entries {
if index > 0 && layout.Gap { dot.Y += margin } if index > 0 && layout.Gap { dot.Y += margin.Y }
entry.Bounds.Min = dot entry.Bounds.Min = dot
entryHeight := 0 entryHeight := 0
@ -78,8 +79,8 @@ func (layout Vertical) Arrange (
// arrange the given list of entries. // arrange the given list of entries.
func (layout Vertical) MinimumSize ( func (layout Vertical) MinimumSize (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin image.Point,
padding int, padding artist.Inset,
) ( ) (
width, height int, width, height int,
) { ) {
@ -90,13 +91,13 @@ func (layout Vertical) MinimumSize (
} }
height += entryHeight height += entryHeight
if layout.Gap && index > 0 { if layout.Gap && index > 0 {
height += margin height += margin.Y
} }
} }
if layout.Pad { if layout.Pad {
width += padding * 2 width += padding.Horizontal()
height += padding * 2 height += padding.Vertical()
} }
return return
} }
@ -105,15 +106,15 @@ func (layout Vertical) MinimumSize (
// specified elements at the given width, taking into account flexible elements. // specified elements at the given width, taking into account flexible elements.
func (layout Vertical) FlexibleHeightFor ( func (layout Vertical) FlexibleHeightFor (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin image.Point,
padding int, padding artist.Inset,
width int, width int,
) ( ) (
height int, height int,
) { ) {
if layout.Pad { if layout.Pad {
width -= padding * 2 width -= padding.Horizontal()
height += padding * 2 height += padding.Vertical()
} }
for index, entry := range entries { for index, entry := range entries {
@ -126,7 +127,7 @@ func (layout Vertical) FlexibleHeightFor (
} }
if layout.Gap && index > 0 { if layout.Gap && index > 0 {
height += margin height += margin.Y
} }
} }
return return

View File

@ -1,6 +1,7 @@
package layouts package layouts
import "image" import "image"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/elements" import "git.tebibyte.media/sashakoshka/tomo/elements"
// LayoutEntry associates an element with layout and positioning information so // LayoutEntry associates an element with layout and positioning information so
@ -24,7 +25,8 @@ type Layout interface {
// than what is returned by MinimumSize. // than what is returned by MinimumSize.
Arrange ( Arrange (
entries []LayoutEntry, entries []LayoutEntry,
margin, padding int, margin image.Point,
padding artist.Inset,
bounds image.Rectangle, bounds image.Rectangle,
) )
@ -32,7 +34,8 @@ type Layout interface {
// needs to properly arrange the given slice of layout entries. // needs to properly arrange the given slice of layout entries.
MinimumSize ( MinimumSize (
entries []LayoutEntry, entries []LayoutEntry,
margin, padding int, margin image.Point,
padding artist.Inset,
) ( ) (
width, height int, width, height int,
) )
@ -42,8 +45,8 @@ type Layout interface {
// flexible elements. // flexible elements.
FlexibleHeightFor ( FlexibleHeightFor (
entries []LayoutEntry, entries []LayoutEntry,
margin int, margin image.Point,
padding int, padding artist.Inset,
squeeze int, squeeze int,
) ( ) (
height int, height int,