From e9e6e4fbe71bd13dafccdb98093bbddd6e2bcf87 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 2 Mar 2023 17:58:42 -0500 Subject: [PATCH] Added padding/margin distinction to layouts --- layouts/basic/dialog.go | 20 ++++++++++++-------- layouts/basic/horizontal.go | 17 ++++++++++------- layouts/basic/vertical.go | 13 ++++++++----- layouts/layout.go | 17 +++++++++++++++-- 4 files changed, 45 insertions(+), 22 deletions(-) diff --git a/layouts/basic/dialog.go b/layouts/basic/dialog.go index ba75df7..f7fdc4f 100644 --- a/layouts/basic/dialog.go +++ b/layouts/basic/dialog.go @@ -10,7 +10,7 @@ import "git.tebibyte.media/sashakoshka/tomo/elements" // arranged at the bottom in a row called the control row, which is aligned to // the right, the last element being the rightmost one. type Dialog struct { - // If Gap is true, a gap will be placed between each element. + // If Mergin is true, a margin will be placed between each element. Gap bool // If Pad is true, there will be padding running along the inside of the @@ -22,15 +22,16 @@ type Dialog struct { func (layout Dialog) Arrange ( entries []layouts.LayoutEntry, margin int, + padding int, bounds image.Rectangle, ) { - if layout.Pad { bounds = bounds.Inset(margin) } + if layout.Pad { bounds = bounds.Inset(padding) } controlRowWidth, controlRowHeight := 0, 0 if len(entries) > 1 { controlRowWidth, controlRowHeight = layout.minimumSizeOfControlRow ( - entries[1:], margin) + entries[1:], margin, padding) } if len(entries) > 0 { @@ -102,6 +103,7 @@ func (layout Dialog) Arrange ( func (layout Dialog) MinimumSize ( entries []layouts.LayoutEntry, margin int, + padding int, ) ( width, height int, ) { @@ -115,7 +117,7 @@ func (layout Dialog) MinimumSize ( if layout.Gap { height += margin } additionalWidth, additionalHeight := layout.minimumSizeOfControlRow ( - entries[1:], margin) + entries[1:], margin, padding) height += additionalHeight if additionalWidth > width { width = additionalWidth @@ -123,8 +125,8 @@ func (layout Dialog) MinimumSize ( } if layout.Pad { - width += margin * 2 - height += margin * 2 + width += padding * 2 + height += padding * 2 } return } @@ -134,6 +136,7 @@ func (layout Dialog) MinimumSize ( func (layout Dialog) FlexibleHeightFor ( entries []layouts.LayoutEntry, margin int, + padding int, width int, ) ( height int, @@ -155,12 +158,12 @@ func (layout Dialog) FlexibleHeightFor ( if len(entries) > 1 { if layout.Gap { height += margin } _, additionalHeight := layout.minimumSizeOfControlRow ( - entries[1:], margin) + entries[1:], margin, padding) height += additionalHeight } if layout.Pad { - height += margin * 2 + height += padding * 2 } return } @@ -170,6 +173,7 @@ func (layout Dialog) FlexibleHeightFor ( func (layout Dialog) minimumSizeOfControlRow ( entries []layouts.LayoutEntry, margin int, + padding int, ) ( width, height int, ) { diff --git a/layouts/basic/horizontal.go b/layouts/basic/horizontal.go index 220dcb8..d37e27d 100644 --- a/layouts/basic/horizontal.go +++ b/layouts/basic/horizontal.go @@ -20,13 +20,14 @@ type Horizontal struct { func (layout Horizontal) Arrange ( entries []layouts.LayoutEntry, margin int, + padding int, bounds image.Rectangle, ) { - if layout.Pad { bounds = bounds.Inset(margin) } + if layout.Pad { bounds = bounds.Inset(padding) } // get width of expanding elements expandingElementWidth := layout.expandingElementWidth ( - entries, margin, bounds.Dx()) + entries, margin, padding, bounds.Dx()) // set the size and position of each element dot := bounds.Min @@ -78,20 +79,21 @@ func (layout Horizontal) MinimumSize ( func (layout Horizontal) FlexibleHeightFor ( entries []layouts.LayoutEntry, margin int, + padding int, width int, ) ( height int, ) { - if layout.Pad { width -= margin * 2 } + if layout.Pad { width -= padding * 2 } // get width of expanding elements expandingElementWidth := layout.expandingElementWidth ( - entries, margin, width) + entries, margin, padding, width) x, y := 0, 0 if layout.Pad { - x += margin - y += margin + x += padding + y += padding } // set the size and position of each element @@ -110,7 +112,7 @@ func (layout Horizontal) FlexibleHeightFor ( } if layout.Pad { - height += margin * 2 + height += padding * 2 } return } @@ -118,6 +120,7 @@ func (layout Horizontal) FlexibleHeightFor ( func (layout Horizontal) expandingElementWidth ( entries []layouts.LayoutEntry, margin int, + padding int, freeSpace int, ) ( width int, diff --git a/layouts/basic/vertical.go b/layouts/basic/vertical.go index 2002db4..dc43da8 100644 --- a/layouts/basic/vertical.go +++ b/layouts/basic/vertical.go @@ -20,9 +20,10 @@ type Vertical struct { func (layout Vertical) Arrange ( entries []layouts.LayoutEntry, margin int, + padding int, bounds image.Rectangle, ) { - if layout.Pad { bounds = bounds.Inset(margin) } + if layout.Pad { bounds = bounds.Inset(padding) } // count the number of expanding elements and the amount of free space // for them to collectively occupy, while gathering minimum heights. @@ -78,6 +79,7 @@ func (layout Vertical) Arrange ( func (layout Vertical) MinimumSize ( entries []layouts.LayoutEntry, margin int, + padding int, ) ( width, height int, ) { @@ -93,8 +95,8 @@ func (layout Vertical) MinimumSize ( } if layout.Pad { - width += margin * 2 - height += margin * 2 + width += padding * 2 + height += padding * 2 } return } @@ -104,13 +106,14 @@ func (layout Vertical) MinimumSize ( func (layout Vertical) FlexibleHeightFor ( entries []layouts.LayoutEntry, margin int, + padding int, width int, ) ( height int, ) { if layout.Pad { - width -= margin * 2 - height += margin * 2 + width -= padding * 2 + height += padding * 2 } for index, entry := range entries { diff --git a/layouts/layout.go b/layouts/layout.go index 9921a88..e847e27 100644 --- a/layouts/layout.go +++ b/layouts/layout.go @@ -11,6 +11,8 @@ type LayoutEntry struct { Expand bool } +// TODO: have layouts take in artist.Inset for margin and padding + // Layout is capable of arranging elements within a container. It is also able // to determine the minimum amount of room it needs to do so. type Layout interface { @@ -18,17 +20,28 @@ type Layout interface { // and changes the position of the entiries in the slice so that they // are properly laid out. The given width and height should not be less // than what is returned by MinimumSize. - Arrange (entries []LayoutEntry, bounds image.Rectangle) + Arrange ( + entries []LayoutEntry, + margin, padding int, + bounds image.Rectangle, + ) // MinimumSize returns the minimum width and height that the layout // needs to properly arrange the given slice of layout entries. - MinimumSize (entries []LayoutEntry) (width, height int) + MinimumSize ( + entries []LayoutEntry, + margin, padding int, + ) ( + width, height int, + ) // FlexibleHeightFor Returns the minimum height the layout needs to lay // out the specified elements at the given width, taking into account // flexible elements. FlexibleHeightFor ( entries []LayoutEntry, + margin int, + padding int, squeeze int, ) ( height int,