From da6fe2c845da95ee38b0139ffae3365896daaddc Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 2 Feb 2023 01:47:31 -0500 Subject: [PATCH] Updated layouts to match --- layouts/{ => basic}/dialog.go | 54 ++++++++++++++++++------------- layouts/{ => basic}/horizontal.go | 54 ++++++++++++++++++------------- layouts/{ => basic}/vertical.go | 40 +++++++++++++---------- 3 files changed, 86 insertions(+), 62 deletions(-) rename layouts/{ => basic}/dialog.go (78%) rename layouts/{ => basic}/horizontal.go (72%) rename layouts/{ => basic}/vertical.go (78%) diff --git a/layouts/dialog.go b/layouts/basic/dialog.go similarity index 78% rename from layouts/dialog.go rename to layouts/basic/dialog.go index 836d4b3..ba75df7 100644 --- a/layouts/dialog.go +++ b/layouts/basic/dialog.go @@ -1,8 +1,8 @@ -package layouts +package basicLayouts import "image" -import "git.tebibyte.media/sashakoshka/tomo" -import "git.tebibyte.media/sashakoshka/tomo/theme" +import "git.tebibyte.media/sashakoshka/tomo/layouts" +import "git.tebibyte.media/sashakoshka/tomo/elements" // Dialog arranges elements in the form of a dialog box. The first element is // positioned above as the main focus of the dialog, and is set to expand @@ -19,13 +19,18 @@ type Dialog struct { } // Arrange arranges a list of entries into a dialog. -func (layout Dialog) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle) { - if layout.Pad { bounds = bounds.Inset(theme.Margin()) } +func (layout Dialog) Arrange ( + entries []layouts.LayoutEntry, + margin int, + bounds image.Rectangle, +) { + if layout.Pad { bounds = bounds.Inset(margin) } controlRowWidth, controlRowHeight := 0, 0 if len(entries) > 1 { controlRowWidth, - controlRowHeight = layout.minimumSizeOfControlRow(entries[1:]) + controlRowHeight = layout.minimumSizeOfControlRow ( + entries[1:], margin) } if len(entries) > 0 { @@ -33,7 +38,7 @@ func (layout Dialog) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle main.Bounds.Min = bounds.Min mainHeight := bounds.Dy() - controlRowHeight if layout.Gap { - mainHeight -= theme.Margin() + mainHeight -= margin } main.Bounds.Max = main.Bounds.Min.Add(image.Pt(bounds.Dx(), mainHeight)) entries[0] = main @@ -53,7 +58,7 @@ func (layout Dialog) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle freeSpace -= entryMinWidth } if index > 0 && layout.Gap { - freeSpace -= theme.Margin() + freeSpace -= margin } } expandingElementWidth := 0 @@ -69,7 +74,7 @@ func (layout Dialog) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle // set the size and position of each element in the control row for index, entry := range entries[1:] { - if index > 0 && layout.Gap { dot.X += theme.Margin() } + if index > 0 && layout.Gap { dot.X += margin } entry.Bounds.Min = dot entryWidth := 0 @@ -95,7 +100,8 @@ func (layout Dialog) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle // MinimumSize returns the minimum width and height that will be needed to // arrange the given list of entries. func (layout Dialog) MinimumSize ( - entries []tomo.LayoutEntry, + entries []layouts.LayoutEntry, + margin int, ) ( width, height int, ) { @@ -106,9 +112,10 @@ func (layout Dialog) MinimumSize ( } if len(entries) > 1 { - if layout.Gap { height += theme.Margin() } + if layout.Gap { height += margin } additionalWidth, - additionalHeight := layout.minimumSizeOfControlRow(entries[1:]) + additionalHeight := layout.minimumSizeOfControlRow ( + entries[1:], margin) height += additionalHeight if additionalWidth > width { width = additionalWidth @@ -116,8 +123,8 @@ func (layout Dialog) MinimumSize ( } if layout.Pad { - width += theme.Margin() * 2 - height += theme.Margin() * 2 + width += margin * 2 + height += margin * 2 } return } @@ -125,18 +132,19 @@ func (layout Dialog) MinimumSize ( // FlexibleHeightFor Returns the minimum height the layout needs to lay out the // specified elements at the given width, taking into account flexible elements. func (layout Dialog) FlexibleHeightFor ( - entries []tomo.LayoutEntry, + entries []layouts.LayoutEntry, + margin int, width int, ) ( height int, ) { if layout.Pad { - width -= theme.Margin() * 2 + width -= margin * 2 } if len(entries) > 0 { mainChildHeight := 0 - if child, flexible := entries[0].Element.(tomo.Flexible); flexible { + if child, flexible := entries[0].Element.(elements.Flexible); flexible { mainChildHeight = child.FlexibleHeightFor(width) } else { _, mainChildHeight = entries[0].MinimumSize() @@ -145,13 +153,14 @@ func (layout Dialog) FlexibleHeightFor ( } if len(entries) > 1 { - if layout.Gap { height += theme.Margin() } - _, additionalHeight := layout.minimumSizeOfControlRow(entries[1:]) + if layout.Gap { height += margin } + _, additionalHeight := layout.minimumSizeOfControlRow ( + entries[1:], margin) height += additionalHeight } if layout.Pad { - height += theme.Margin() * 2 + height += margin * 2 } return } @@ -159,7 +168,8 @@ func (layout Dialog) FlexibleHeightFor ( // TODO: possibly flatten this method to account for flexible elements within // the control row. func (layout Dialog) minimumSizeOfControlRow ( - entries []tomo.LayoutEntry, + entries []layouts.LayoutEntry, + margin int, ) ( width, height int, ) { @@ -170,7 +180,7 @@ func (layout Dialog) minimumSizeOfControlRow ( } width += entryWidth if layout.Gap && index > 0 { - width += theme.Margin() + width += margin } } return diff --git a/layouts/horizontal.go b/layouts/basic/horizontal.go similarity index 72% rename from layouts/horizontal.go rename to layouts/basic/horizontal.go index 95c414b..220dcb8 100644 --- a/layouts/horizontal.go +++ b/layouts/basic/horizontal.go @@ -1,8 +1,8 @@ -package layouts +package basicLayouts import "image" -import "git.tebibyte.media/sashakoshka/tomo" -import "git.tebibyte.media/sashakoshka/tomo/theme" +import "git.tebibyte.media/sashakoshka/tomo/layouts" +import "git.tebibyte.media/sashakoshka/tomo/elements" // Horizontal arranges elements horizontally. Elements at the start of the entry // list will be positioned on the left, and elements at the end of the entry @@ -17,16 +17,21 @@ type Horizontal struct { } // Arrange arranges a list of entries horizontally. -func (layout Horizontal) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle) { - if layout.Pad { bounds = bounds.Inset(theme.Margin()) } +func (layout Horizontal) Arrange ( + entries []layouts.LayoutEntry, + margin int, + bounds image.Rectangle, +) { + if layout.Pad { bounds = bounds.Inset(margin) } // get width of expanding elements - expandingElementWidth := layout.expandingElementWidth(entries, bounds.Dx()) + expandingElementWidth := layout.expandingElementWidth ( + entries, margin, bounds.Dx()) // set the size and position of each element dot := bounds.Min for index, entry := range entries { - if index > 0 && layout.Gap { dot.X += theme.Margin() } + if index > 0 && layout.Gap { dot.X += margin } entry.Bounds.Min = dot entryWidth := 0 @@ -45,7 +50,8 @@ func (layout Horizontal) Arrange (entries []tomo.LayoutEntry, bounds image.Recta // MinimumSize returns the minimum width and height that will be needed to // arrange the given list of entries. func (layout Horizontal) MinimumSize ( - entries []tomo.LayoutEntry, + entries []layouts.LayoutEntry, + margin int, ) ( width, height int, ) { @@ -56,13 +62,13 @@ func (layout Horizontal) MinimumSize ( } width += entryWidth if layout.Gap && index > 0 { - width += theme.Margin() + width += margin } } if layout.Pad { - width += theme.Margin() * 2 - height += theme.Margin() * 2 + width += margin * 2 + height += margin * 2 } return } @@ -70,21 +76,22 @@ func (layout Horizontal) MinimumSize ( // FlexibleHeightFor Returns the minimum height the layout needs to lay out the // specified elements at the given width, taking into account flexible elements. func (layout Horizontal) FlexibleHeightFor ( - entries []tomo.LayoutEntry, + entries []layouts.LayoutEntry, + margin int, width int, ) ( height int, ) { - if layout.Pad { - width -= theme.Margin() * 2 - } + if layout.Pad { width -= margin * 2 } + // get width of expanding elements - expandingElementWidth := layout.expandingElementWidth(entries, width) + expandingElementWidth := layout.expandingElementWidth ( + entries, margin, width) x, y := 0, 0 if layout.Pad { - x += theme.Margin() - y += theme.Margin() + x += margin + y += margin } // set the size and position of each element @@ -93,23 +100,24 @@ func (layout Horizontal) FlexibleHeightFor ( if entry.Expand { entryWidth = expandingElementWidth } - if child, flexible := entry.Element.(tomo.Flexible); flexible { + if child, flexible := entry.Element.(elements.Flexible); flexible { entryHeight = child.FlexibleHeightFor(entryWidth) } if entryHeight > height { height = entryHeight } x += entryWidth - if index > 0 && layout.Gap { x += theme.Margin() } + if index > 0 && layout.Gap { x += margin } } if layout.Pad { - height += theme.Margin() * 2 + height += margin * 2 } return } func (layout Horizontal) expandingElementWidth ( - entries []tomo.LayoutEntry, + entries []layouts.LayoutEntry, + margin int, freeSpace int, ) ( width int, @@ -126,7 +134,7 @@ func (layout Horizontal) expandingElementWidth ( freeSpace -= entryMinWidth } if index > 0 && layout.Gap { - freeSpace -= theme.Margin() + freeSpace -= margin } } diff --git a/layouts/vertical.go b/layouts/basic/vertical.go similarity index 78% rename from layouts/vertical.go rename to layouts/basic/vertical.go index 9455bc0..2002db4 100644 --- a/layouts/vertical.go +++ b/layouts/basic/vertical.go @@ -1,8 +1,8 @@ -package layouts +package basicLayouts import "image" -import "git.tebibyte.media/sashakoshka/tomo" -import "git.tebibyte.media/sashakoshka/tomo/theme" +import "git.tebibyte.media/sashakoshka/tomo/layouts" +import "git.tebibyte.media/sashakoshka/tomo/elements" // Vertical arranges elements vertically. Elements at the start of the entry // list will be positioned at the top, and elements at the end of the entry list @@ -17,8 +17,12 @@ type Vertical struct { } // Arrange arranges a list of entries vertically. -func (layout Vertical) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle) { - if layout.Pad { bounds = bounds.Inset(theme.Margin()) } +func (layout Vertical) Arrange ( + entries []layouts.LayoutEntry, + margin int, + bounds image.Rectangle, +) { + if layout.Pad { bounds = bounds.Inset(margin) } // count the number of expanding elements and the amount of free space // for them to collectively occupy, while gathering minimum heights. @@ -28,7 +32,7 @@ func (layout Vertical) Arrange (entries []tomo.LayoutEntry, bounds image.Rectang for index, entry := range entries { var entryMinHeight int - if child, flexible := entry.Element.(tomo.Flexible); flexible { + if child, flexible := entry.Element.(elements.Flexible); flexible { entryMinHeight = child.FlexibleHeightFor(bounds.Dx()) } else { _, entryMinHeight = entry.MinimumSize() @@ -41,7 +45,7 @@ func (layout Vertical) Arrange (entries []tomo.LayoutEntry, bounds image.Rectang freeSpace -= entryMinHeight } if index > 0 && layout.Gap { - freeSpace -= theme.Margin() + freeSpace -= margin } } @@ -53,7 +57,7 @@ func (layout Vertical) Arrange (entries []tomo.LayoutEntry, bounds image.Rectang // set the size and position of each element dot := bounds.Min for index, entry := range entries { - if index > 0 && layout.Gap { dot.Y += theme.Margin() } + if index > 0 && layout.Gap { dot.Y += margin } entry.Bounds.Min = dot entryHeight := 0 @@ -72,7 +76,8 @@ func (layout Vertical) Arrange (entries []tomo.LayoutEntry, bounds image.Rectang // MinimumSize returns the minimum width and height that will be needed to // arrange the given list of entries. func (layout Vertical) MinimumSize ( - entries []tomo.LayoutEntry, + entries []layouts.LayoutEntry, + margin int, ) ( width, height int, ) { @@ -83,13 +88,13 @@ func (layout Vertical) MinimumSize ( } height += entryHeight if layout.Gap && index > 0 { - height += theme.Margin() + height += margin } } if layout.Pad { - width += theme.Margin() * 2 - height += theme.Margin() * 2 + width += margin * 2 + height += margin * 2 } return } @@ -97,18 +102,19 @@ func (layout Vertical) MinimumSize ( // FlexibleHeightFor Returns the minimum height the layout needs to lay out the // specified elements at the given width, taking into account flexible elements. func (layout Vertical) FlexibleHeightFor ( - entries []tomo.LayoutEntry, + entries []layouts.LayoutEntry, + margin int, width int, ) ( height int, ) { if layout.Pad { - width -= theme.Margin() * 2 - height += theme.Margin() * 2 + width -= margin * 2 + height += margin * 2 } for index, entry := range entries { - child, flexible := entry.Element.(tomo.Flexible) + child, flexible := entry.Element.(elements.Flexible) if flexible { height += child.FlexibleHeightFor(width) } else { @@ -117,7 +123,7 @@ func (layout Vertical) FlexibleHeightFor ( } if layout.Gap && index > 0 { - height += theme.Margin() + height += margin } } return