2023-01-10 23:56:05 -07:00
|
|
|
package layouts
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
|
|
|
|
2023-01-15 22:09:01 -07:00
|
|
|
// 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
|
|
|
|
// regardless of whether it is expanding or not. The remaining elements are
|
|
|
|
// arranged at the bottom in a row called the control row, which is aligned to
|
|
|
|
// the right, the last element being the rightmost one.
|
2023-01-10 23:56:05 -07:00
|
|
|
type Dialog struct {
|
|
|
|
// If Gap is true, a gap will be placed between each element.
|
|
|
|
Gap bool
|
|
|
|
|
|
|
|
// If Pad is true, there will be padding running along the inside of the
|
|
|
|
// layout's border.
|
|
|
|
Pad bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arrange arranges a list of entries into a dialog.
|
|
|
|
func (layout Dialog) Arrange (entries []tomo.LayoutEntry, width, height int) {
|
|
|
|
if layout.Pad {
|
2023-01-30 16:22:19 -07:00
|
|
|
width -= theme.Margin() * 2
|
|
|
|
height -= theme.Margin() * 2
|
2023-01-10 23:56:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
controlRowWidth, controlRowHeight := 0, 0
|
|
|
|
if len(entries) > 1 {
|
|
|
|
controlRowWidth,
|
2023-01-17 13:55:16 -07:00
|
|
|
controlRowHeight = layout.minimumSizeOfControlRow(entries[1:])
|
2023-01-10 23:56:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) > 0 {
|
2023-01-31 12:48:28 -07:00
|
|
|
entries[0].Bounds.Min = image.Point { }
|
2023-01-10 23:56:05 -07:00
|
|
|
if layout.Pad {
|
2023-01-31 12:48:28 -07:00
|
|
|
entries[0].Bounds.Min.X += theme.Margin()
|
|
|
|
entries[0].Bounds.Min.Y += theme.Margin()
|
2023-01-10 23:56:05 -07:00
|
|
|
}
|
|
|
|
mainHeight := height - controlRowHeight
|
|
|
|
if layout.Gap {
|
2023-01-30 16:22:19 -07:00
|
|
|
mainHeight -= theme.Margin()
|
2023-01-10 23:56:05 -07:00
|
|
|
}
|
2023-01-31 12:48:28 -07:00
|
|
|
mainBounds := entries[0].Bounds
|
|
|
|
if mainBounds.Dy() != mainHeight || mainBounds.Dx() != width {
|
|
|
|
entries[0].Bounds.Max =
|
|
|
|
mainBounds.Min.Add(image.Pt(width, mainHeight))
|
2023-01-10 23:56:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) > 1 {
|
|
|
|
freeSpace := width
|
|
|
|
expandingElements := 0
|
|
|
|
|
|
|
|
// count the number of expanding elements and the amount of free
|
|
|
|
// space for them to collectively occupy
|
|
|
|
for index, entry := range entries[1:] {
|
|
|
|
if entry.Expand {
|
|
|
|
expandingElements ++
|
|
|
|
} else {
|
|
|
|
entryMinWidth, _ := entry.MinimumSize()
|
|
|
|
freeSpace -= entryMinWidth
|
|
|
|
}
|
|
|
|
if index > 0 && layout.Gap {
|
2023-01-30 16:22:19 -07:00
|
|
|
freeSpace -= theme.Margin()
|
2023-01-10 23:56:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
expandingElementWidth := 0
|
|
|
|
if expandingElements > 0 {
|
|
|
|
expandingElementWidth = freeSpace / expandingElements
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine starting position and dimensions for control row
|
|
|
|
x, y := 0, height - controlRowHeight
|
|
|
|
if expandingElements == 0 {
|
|
|
|
x = width - controlRowWidth
|
|
|
|
}
|
|
|
|
if layout.Pad {
|
2023-01-30 16:22:19 -07:00
|
|
|
x += theme.Margin()
|
|
|
|
y += theme.Margin()
|
2023-01-10 23:56:05 -07:00
|
|
|
}
|
|
|
|
height -= controlRowHeight
|
|
|
|
|
|
|
|
// set the size and position of each element in the control row
|
|
|
|
for index, entry := range entries[1:] {
|
2023-01-30 16:22:19 -07:00
|
|
|
if index > 0 && layout.Gap { x += theme.Margin() }
|
2023-01-10 23:56:05 -07:00
|
|
|
|
2023-01-31 12:48:28 -07:00
|
|
|
entries[index + 1].Bounds.Min = image.Pt(x, y)
|
2023-01-10 23:56:05 -07:00
|
|
|
entryWidth := 0
|
|
|
|
if entry.Expand {
|
|
|
|
entryWidth = expandingElementWidth
|
|
|
|
} else {
|
|
|
|
entryWidth, _ = entry.MinimumSize()
|
|
|
|
}
|
|
|
|
x += entryWidth
|
2023-01-31 12:48:28 -07:00
|
|
|
entryBounds := entry.Bounds
|
2023-01-10 23:56:05 -07:00
|
|
|
if entryBounds.Dy() != controlRowHeight ||
|
|
|
|
entryBounds.Dx() != entryWidth {
|
2023-01-31 16:04:12 -07:00
|
|
|
entries[index].Bounds.Max = entryBounds.Min.Add (
|
2023-01-31 12:48:28 -07:00
|
|
|
image.Pt(entryWidth, controlRowHeight))
|
2023-01-10 23:56:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MinimumSize returns the minimum width and height that will be needed to
|
|
|
|
// arrange the given list of entries.
|
2023-01-15 22:09:01 -07:00
|
|
|
func (layout Dialog) MinimumSize (
|
|
|
|
entries []tomo.LayoutEntry,
|
|
|
|
) (
|
|
|
|
width, height int,
|
|
|
|
) {
|
2023-01-10 23:56:05 -07:00
|
|
|
if len(entries) > 0 {
|
|
|
|
mainChildHeight := 0
|
|
|
|
width, mainChildHeight = entries[0].MinimumSize()
|
|
|
|
height += mainChildHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) > 1 {
|
2023-01-30 16:22:19 -07:00
|
|
|
if layout.Gap { height += theme.Margin() }
|
2023-01-10 23:56:05 -07:00
|
|
|
additionalWidth,
|
2023-01-17 13:55:16 -07:00
|
|
|
additionalHeight := layout.minimumSizeOfControlRow(entries[1:])
|
2023-01-10 23:56:05 -07:00
|
|
|
height += additionalHeight
|
|
|
|
if additionalWidth > width {
|
|
|
|
width = additionalWidth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if layout.Pad {
|
2023-01-30 16:22:19 -07:00
|
|
|
width += theme.Margin() * 2
|
|
|
|
height += theme.Margin() * 2
|
2023-01-10 23:56:05 -07:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-19 14:54:49 -07:00
|
|
|
// 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 (
|
2023-01-17 13:55:16 -07:00
|
|
|
entries []tomo.LayoutEntry,
|
|
|
|
width int,
|
|
|
|
) (
|
|
|
|
height int,
|
|
|
|
) {
|
|
|
|
if layout.Pad {
|
2023-01-30 16:22:19 -07:00
|
|
|
width -= theme.Margin() * 2
|
2023-01-17 13:55:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) > 0 {
|
|
|
|
mainChildHeight := 0
|
|
|
|
if child, flexible := entries[0].Element.(tomo.Flexible); flexible {
|
2023-01-19 14:54:49 -07:00
|
|
|
mainChildHeight = child.FlexibleHeightFor(width)
|
2023-01-17 13:55:16 -07:00
|
|
|
} else {
|
|
|
|
_, mainChildHeight = entries[0].MinimumSize()
|
|
|
|
}
|
|
|
|
height += mainChildHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) > 1 {
|
2023-01-30 16:22:19 -07:00
|
|
|
if layout.Gap { height += theme.Margin() }
|
2023-01-17 13:55:16 -07:00
|
|
|
_, additionalHeight := layout.minimumSizeOfControlRow(entries[1:])
|
|
|
|
height += additionalHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
if layout.Pad {
|
2023-01-30 16:22:19 -07:00
|
|
|
height += theme.Margin() * 2
|
2023-01-17 13:55:16 -07:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: possibly flatten this method to account for flexible elements within
|
|
|
|
// the control row.
|
|
|
|
func (layout Dialog) minimumSizeOfControlRow (
|
2023-01-10 23:56:05 -07:00
|
|
|
entries []tomo.LayoutEntry,
|
|
|
|
) (
|
|
|
|
width, height int,
|
|
|
|
) {
|
|
|
|
for index, entry := range entries {
|
|
|
|
entryWidth, entryHeight := entry.MinimumSize()
|
|
|
|
if entryHeight > height {
|
|
|
|
height = entryHeight
|
|
|
|
}
|
|
|
|
width += entryWidth
|
|
|
|
if layout.Gap && index > 0 {
|
2023-01-30 16:22:19 -07:00
|
|
|
width += theme.Margin()
|
2023-01-10 23:56:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|