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/horizontal.go

146 lines
3.4 KiB
Go
Raw Normal View History

2023-02-02 06:47:31 +00:00
package basicLayouts
2023-01-11 04:20:42 +00:00
import "image"
2023-02-02 06:47:31 +00:00
import "git.tebibyte.media/sashakoshka/tomo/layouts"
import "git.tebibyte.media/sashakoshka/tomo/elements"
2023-01-11 04:20:42 +00:00
// 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
// list will positioned on the right. All elements have the same height.
type Horizontal 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 horizontally.
2023-02-02 06:47:31 +00:00
func (layout Horizontal) Arrange (
entries []layouts.LayoutEntry,
margin int,
bounds image.Rectangle,
) {
if layout.Pad { bounds = bounds.Inset(margin) }
2023-01-31 23:06:55 +00:00
// get width of expanding elements
2023-02-02 06:47:31 +00:00
expandingElementWidth := layout.expandingElementWidth (
entries, margin, bounds.Dx())
2023-01-11 04:20:42 +00:00
// set the size and position of each element
2023-01-31 23:39:17 +00:00
dot := bounds.Min
2023-01-11 04:20:42 +00:00
for index, entry := range entries {
2023-02-02 06:47:31 +00:00
if index > 0 && layout.Gap { dot.X += margin }
2023-01-11 04:20:42 +00:00
2023-01-31 23:06:55 +00:00
entry.Bounds.Min = dot
2023-01-11 04:20:42 +00:00
entryWidth := 0
if entry.Expand {
entryWidth = expandingElementWidth
} else {
entryWidth, _ = entry.MinimumSize()
}
2023-01-31 23:06:55 +00:00
dot.X += entryWidth
2023-01-31 23:57:29 +00:00
entry.Bounds.Max = entry.Bounds.Min.Add(image.Pt(entryWidth, bounds.Dy()))
2023-01-31 23:06:55 +00:00
entries[index] = entry
2023-01-11 04:20:42 +00:00
}
}
// MinimumSize returns the minimum width and height that will be needed to
// arrange the given list of entries.
func (layout Horizontal) MinimumSize (
2023-02-02 06:47:31 +00:00
entries []layouts.LayoutEntry,
margin int,
) (
width, height int,
) {
2023-01-11 04:20:42 +00:00
for index, entry := range entries {
entryWidth, entryHeight := entry.MinimumSize()
if entryHeight > height {
height = entryHeight
}
width += entryWidth
if layout.Gap && index > 0 {
2023-02-02 06:47:31 +00:00
width += margin
2023-01-11 04:20:42 +00:00
}
}
if layout.Pad {
2023-02-02 06:47:31 +00:00
width += margin * 2
height += margin * 2
2023-01-11 04:20:42 +00:00
}
return
}
2023-01-19 21:54:49 +00: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 Horizontal) FlexibleHeightFor (
2023-02-02 06:47:31 +00:00
entries []layouts.LayoutEntry,
margin int,
width int,
) (
height int,
) {
2023-02-02 06:47:31 +00:00
if layout.Pad { width -= margin * 2 }
// get width of expanding elements
2023-02-02 06:47:31 +00:00
expandingElementWidth := layout.expandingElementWidth (
entries, margin, width)
x, y := 0, 0
if layout.Pad {
2023-02-02 06:47:31 +00:00
x += margin
y += margin
}
// set the size and position of each element
for index, entry := range entries {
entryWidth, entryHeight := entry.MinimumSize()
if entry.Expand {
entryWidth = expandingElementWidth
}
2023-02-02 06:47:31 +00:00
if child, flexible := entry.Element.(elements.Flexible); flexible {
2023-01-19 21:54:49 +00:00
entryHeight = child.FlexibleHeightFor(entryWidth)
}
if entryHeight > height { height = entryHeight }
x += entryWidth
2023-02-02 06:47:31 +00:00
if index > 0 && layout.Gap { x += margin }
}
if layout.Pad {
2023-02-02 06:47:31 +00:00
height += margin * 2
}
return
}
func (layout Horizontal) expandingElementWidth (
2023-02-02 06:47:31 +00:00
entries []layouts.LayoutEntry,
margin int,
freeSpace int,
) (
width int,
) {
expandingElements := 0
// count the number of expanding elements and the amount of free space
// for them to collectively occupy
for index, entry := range entries {
if entry.Expand {
expandingElements ++
} else {
entryMinWidth, _ := entry.MinimumSize()
freeSpace -= entryMinWidth
}
if index > 0 && layout.Gap {
2023-02-02 06:47:31 +00:00
freeSpace -= margin
}
}
if expandingElements > 0 {
width = freeSpace / expandingElements
}
return
}