Added padding/margin distinction to layouts

This commit is contained in:
Sasha Koshka 2023-03-02 17:58:42 -05:00
parent 285cb4810f
commit e9e6e4fbe7
4 changed files with 45 additions and 22 deletions

View File

@ -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 // arranged at the bottom in a row called the control row, which is aligned to
// the right, the last element being the rightmost one. // the right, the last element being the rightmost one.
type Dialog struct { 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 Gap bool
// If Pad is true, there will be padding running along the inside of the // 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 ( func (layout Dialog) Arrange (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin int,
padding int,
bounds image.Rectangle, bounds image.Rectangle,
) { ) {
if layout.Pad { bounds = bounds.Inset(margin) } if layout.Pad { bounds = bounds.Inset(padding) }
controlRowWidth, controlRowHeight := 0, 0 controlRowWidth, controlRowHeight := 0, 0
if len(entries) > 1 { if len(entries) > 1 {
controlRowWidth, controlRowWidth,
controlRowHeight = layout.minimumSizeOfControlRow ( controlRowHeight = layout.minimumSizeOfControlRow (
entries[1:], margin) entries[1:], margin, padding)
} }
if len(entries) > 0 { if len(entries) > 0 {
@ -102,6 +103,7 @@ func (layout Dialog) Arrange (
func (layout Dialog) MinimumSize ( func (layout Dialog) MinimumSize (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin int,
padding int,
) ( ) (
width, height int, width, height int,
) { ) {
@ -115,7 +117,7 @@ func (layout Dialog) MinimumSize (
if layout.Gap { height += margin } if layout.Gap { height += margin }
additionalWidth, additionalWidth,
additionalHeight := layout.minimumSizeOfControlRow ( additionalHeight := layout.minimumSizeOfControlRow (
entries[1:], margin) entries[1:], margin, padding)
height += additionalHeight height += additionalHeight
if additionalWidth > width { if additionalWidth > width {
width = additionalWidth width = additionalWidth
@ -123,8 +125,8 @@ func (layout Dialog) MinimumSize (
} }
if layout.Pad { if layout.Pad {
width += margin * 2 width += padding * 2
height += margin * 2 height += padding * 2
} }
return return
} }
@ -134,6 +136,7 @@ func (layout Dialog) MinimumSize (
func (layout Dialog) FlexibleHeightFor ( func (layout Dialog) FlexibleHeightFor (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin int,
padding int,
width int, width int,
) ( ) (
height int, height int,
@ -155,12 +158,12 @@ func (layout Dialog) FlexibleHeightFor (
if len(entries) > 1 { if len(entries) > 1 {
if layout.Gap { height += margin } if layout.Gap { height += margin }
_, additionalHeight := layout.minimumSizeOfControlRow ( _, additionalHeight := layout.minimumSizeOfControlRow (
entries[1:], margin) entries[1:], margin, padding)
height += additionalHeight height += additionalHeight
} }
if layout.Pad { if layout.Pad {
height += margin * 2 height += padding * 2
} }
return return
} }
@ -170,6 +173,7 @@ func (layout Dialog) FlexibleHeightFor (
func (layout Dialog) minimumSizeOfControlRow ( func (layout Dialog) minimumSizeOfControlRow (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin int,
padding int,
) ( ) (
width, height int, width, height int,
) { ) {

View File

@ -20,13 +20,14 @@ type Horizontal struct {
func (layout Horizontal) Arrange ( func (layout Horizontal) Arrange (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin int,
padding int,
bounds image.Rectangle, bounds image.Rectangle,
) { ) {
if layout.Pad { bounds = bounds.Inset(margin) } if layout.Pad { bounds = bounds.Inset(padding) }
// get width of expanding elements // get width of expanding elements
expandingElementWidth := layout.expandingElementWidth ( expandingElementWidth := layout.expandingElementWidth (
entries, margin, bounds.Dx()) entries, margin, padding, bounds.Dx())
// set the size and position of each element // set the size and position of each element
dot := bounds.Min dot := bounds.Min
@ -78,20 +79,21 @@ func (layout Horizontal) MinimumSize (
func (layout Horizontal) FlexibleHeightFor ( func (layout Horizontal) FlexibleHeightFor (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin int,
padding int,
width int, width int,
) ( ) (
height int, height int,
) { ) {
if layout.Pad { width -= margin * 2 } if layout.Pad { width -= padding * 2 }
// get width of expanding elements // get width of expanding elements
expandingElementWidth := layout.expandingElementWidth ( expandingElementWidth := layout.expandingElementWidth (
entries, margin, width) entries, margin, padding, width)
x, y := 0, 0 x, y := 0, 0
if layout.Pad { if layout.Pad {
x += margin x += padding
y += margin y += padding
} }
// set the size and position of each element // set the size and position of each element
@ -110,7 +112,7 @@ func (layout Horizontal) FlexibleHeightFor (
} }
if layout.Pad { if layout.Pad {
height += margin * 2 height += padding * 2
} }
return return
} }
@ -118,6 +120,7 @@ func (layout Horizontal) FlexibleHeightFor (
func (layout Horizontal) expandingElementWidth ( func (layout Horizontal) expandingElementWidth (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin int,
padding int,
freeSpace int, freeSpace int,
) ( ) (
width int, width int,

View File

@ -20,9 +20,10 @@ type Vertical struct {
func (layout Vertical) Arrange ( func (layout Vertical) Arrange (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin int,
padding int,
bounds image.Rectangle, 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 // 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.
@ -78,6 +79,7 @@ func (layout Vertical) Arrange (
func (layout Vertical) MinimumSize ( func (layout Vertical) MinimumSize (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin int,
padding int,
) ( ) (
width, height int, width, height int,
) { ) {
@ -93,8 +95,8 @@ func (layout Vertical) MinimumSize (
} }
if layout.Pad { if layout.Pad {
width += margin * 2 width += padding * 2
height += margin * 2 height += padding * 2
} }
return return
} }
@ -104,13 +106,14 @@ func (layout Vertical) MinimumSize (
func (layout Vertical) FlexibleHeightFor ( func (layout Vertical) FlexibleHeightFor (
entries []layouts.LayoutEntry, entries []layouts.LayoutEntry,
margin int, margin int,
padding int,
width int, width int,
) ( ) (
height int, height int,
) { ) {
if layout.Pad { if layout.Pad {
width -= margin * 2 width -= padding * 2
height += margin * 2 height += padding * 2
} }
for index, entry := range entries { for index, entry := range entries {

View File

@ -11,6 +11,8 @@ type LayoutEntry struct {
Expand bool 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 // 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. // to determine the minimum amount of room it needs to do so.
type Layout interface { type Layout interface {
@ -18,17 +20,28 @@ type Layout interface {
// and changes the position of the entiries in the slice so that they // 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 // are properly laid out. The given width and height should not be less
// than what is returned by MinimumSize. // 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 // MinimumSize returns the minimum width and height that the layout
// needs to properly arrange the given slice of layout entries. // 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 // FlexibleHeightFor Returns the minimum height the layout needs to lay
// out the specified elements at the given width, taking into account // out the specified elements at the given width, taking into account
// flexible elements. // flexible elements.
FlexibleHeightFor ( FlexibleHeightFor (
entries []LayoutEntry, entries []LayoutEntry,
margin int,
padding int,
squeeze int, squeeze int,
) ( ) (
height int, height int,