This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/layouts/basic/dialog.go

153 lines
3.8 KiB
Go
Raw Normal View History

2023-02-02 06:47:31 +00:00
package basicLayouts
2023-01-11 06:56:05 +00:00
import "image"
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-02-02 06:47:31 +00:00
import "git.tebibyte.media/sashakoshka/tomo/layouts"
2023-01-11 06:56:05 +00: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-11 06:56:05 +00:00
type Dialog struct {
// If Mergin is true, a margin will be placed between each element.
2023-01-11 06:56:05 +00:00
Gap bool
// If Pad is true, there will be padding running along the inside of the
// layout's border.
Pad bool
}
2023-01-31 23:39:17 +00:00
// Arrange arranges a list of entries into a dialog.
2023-02-02 06:47:31 +00:00
func (layout Dialog) Arrange (
entries []layouts.LayoutEntry,
margin image.Point,
padding artist.Inset,
2023-02-02 06:47:31 +00:00
bounds image.Rectangle,
) {
if layout.Pad { bounds = padding.Apply(bounds) }
2023-01-31 23:39:17 +00:00
2023-01-11 06:56:05 +00:00
controlRowWidth, controlRowHeight := 0, 0
if len(entries) > 1 {
controlRowWidth,
2023-02-02 06:47:31 +00:00
controlRowHeight = layout.minimumSizeOfControlRow (
entries[1:], margin, padding)
2023-01-11 06:56:05 +00:00
}
if len(entries) > 0 {
2023-01-31 23:57:29 +00:00
main := entries[0]
main.Bounds.Min = bounds.Min
2023-01-31 23:39:17 +00:00
mainHeight := bounds.Dy() - controlRowHeight
2023-01-11 06:56:05 +00:00
if layout.Gap {
mainHeight -= margin.Y
2023-01-11 06:56:05 +00:00
}
2023-01-31 23:57:29 +00:00
main.Bounds.Max = main.Bounds.Min.Add(image.Pt(bounds.Dx(), mainHeight))
entries[0] = main
2023-01-11 06:56:05 +00:00
}
if len(entries) > 1 {
2023-01-31 23:39:17 +00:00
freeSpace := bounds.Dx()
2023-01-11 06:56:05 +00:00
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 {
freeSpace -= margin.X
2023-01-11 06:56:05 +00:00
}
}
expandingElementWidth := 0
if expandingElements > 0 {
expandingElementWidth = freeSpace / expandingElements
}
// determine starting position and dimensions for control row
2023-01-31 23:57:29 +00:00
dot := image.Pt(bounds.Min.X, bounds.Max.Y - controlRowHeight)
2023-01-11 06:56:05 +00:00
if expandingElements == 0 {
2023-01-31 23:57:29 +00:00
dot.X = bounds.Max.X - controlRowWidth
2023-01-11 06:56:05 +00:00
}
// 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 += margin.X }
2023-01-11 06:56:05 +00:00
2023-01-31 23:57:29 +00:00
entry.Bounds.Min = dot
2023-01-11 06:56:05 +00:00
entryWidth := 0
if entry.Expand {
entryWidth = expandingElementWidth
} else {
entryWidth, _ = entry.MinimumSize()
}
2023-01-31 23:57:29 +00:00
dot.X += entryWidth
entryBounds := entry.Bounds
2023-01-11 06:56:05 +00:00
if entryBounds.Dy() != controlRowHeight ||
entryBounds.Dx() != entryWidth {
2023-01-31 23:57:29 +00:00
entry.Bounds.Max = entryBounds.Min.Add (
image.Pt(entryWidth, controlRowHeight))
2023-01-11 06:56:05 +00:00
}
2023-01-31 23:57:29 +00:00
entries[index + 1] = entry
2023-01-11 06:56:05 +00:00
}
}
}
// MinimumSize returns the minimum width and height that will be needed to
// arrange the given list of entries.
func (layout Dialog) MinimumSize (
2023-02-02 06:47:31 +00:00
entries []layouts.LayoutEntry,
margin image.Point,
padding artist.Inset,
) (
width, height int,
) {
2023-01-11 06:56:05 +00:00
if len(entries) > 0 {
mainChildHeight := 0
width, mainChildHeight = entries[0].MinimumSize()
height += mainChildHeight
}
if len(entries) > 1 {
if layout.Gap { height += margin.X }
2023-01-11 06:56:05 +00:00
additionalWidth,
2023-02-02 06:47:31 +00:00
additionalHeight := layout.minimumSizeOfControlRow (
entries[1:], margin, padding)
2023-01-11 06:56:05 +00:00
height += additionalHeight
if additionalWidth > width {
width = additionalWidth
}
}
if layout.Pad {
width += padding.Horizontal()
height += padding.Vertical()
2023-01-11 06:56:05 +00:00
}
return
}
2023-01-17 20:55:16 +00:00
func (layout Dialog) minimumSizeOfControlRow (
2023-02-02 06:47:31 +00:00
entries []layouts.LayoutEntry,
margin image.Point,
padding artist.Inset,
2023-01-11 06:56:05 +00:00
) (
width, height int,
) {
for index, entry := range entries {
entryWidth, entryHeight := entry.MinimumSize()
if entryHeight > height {
height = entryHeight
}
width += entryWidth
if layout.Gap && index > 0 {
width += margin.X
2023-01-11 06:56:05 +00:00
}
}
return
}