Updated layouts to match

This commit is contained in:
Sasha Koshka 2023-02-02 01:47:31 -05:00
parent 04d2ea4767
commit da6fe2c845
3 changed files with 86 additions and 62 deletions

View File

@ -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

View File

@ -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
}
}

View File

@ -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