2019-03-05 20:52:18 +00:00
|
|
|
package ui
|
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
import (
|
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
)
|
2019-03-05 20:52:18 +00:00
|
|
|
|
|
|
|
type StackPanel struct {
|
|
|
|
ContainerBase
|
|
|
|
Orientation Orientation
|
|
|
|
}
|
|
|
|
|
2019-04-10 19:23:56 +00:00
|
|
|
func BuildStackPanel(o Orientation, fn func(*StackPanel)) *StackPanel {
|
|
|
|
var p = &StackPanel{Orientation: o}
|
|
|
|
if fn != nil {
|
|
|
|
fn(p)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-08-19 10:27:34 +00:00
|
|
|
func (p *StackPanel) preArrange(ctx Context, bounds geom.RectangleF32, clip bool) []geom.RectangleF32 {
|
2019-03-05 20:52:18 +00:00
|
|
|
bounds = p.Orientation.FlipRect(bounds)
|
|
|
|
var length float32
|
|
|
|
var stretch int
|
|
|
|
var desired = make([]geom.PointF32, len(p.Children))
|
|
|
|
for i, child := range p.Children {
|
2021-08-19 10:27:34 +00:00
|
|
|
var size = p.Orientation.FlipPt(child.DesiredSize(ctx, p.Orientation.FlipPt(bounds.Size())))
|
2019-03-05 20:52:18 +00:00
|
|
|
if geom.IsNaN32(size.Y) {
|
|
|
|
stretch++
|
|
|
|
} else {
|
|
|
|
length += size.Y
|
|
|
|
}
|
|
|
|
desired[i] = size
|
|
|
|
}
|
|
|
|
var remainder = bounds.Dy() - length
|
|
|
|
var childOffset float32
|
2021-08-19 10:27:34 +00:00
|
|
|
childBounds := make([]geom.RectangleF32, 0, len(p.Children))
|
|
|
|
for _, size := range desired {
|
2019-03-05 20:52:18 +00:00
|
|
|
var height = size.Y
|
|
|
|
if geom.IsNaN32(size.Y) {
|
|
|
|
if remainder < 0 {
|
|
|
|
height = 0
|
|
|
|
} else {
|
|
|
|
height = remainder / float32(stretch)
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 10:27:34 +00:00
|
|
|
minY := bounds.Min.Y + childOffset
|
|
|
|
maxY := minY + height
|
|
|
|
if clip {
|
|
|
|
if minY > bounds.Max.Y {
|
|
|
|
minY = bounds.Max.Y
|
|
|
|
}
|
|
|
|
if maxY > bounds.Max.Y {
|
|
|
|
maxY = bounds.Max.Y
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 15:55:43 +00:00
|
|
|
var child = geom.RectF32(bounds.Min.X, minY, bounds.Max.X, maxY)
|
2021-08-19 10:27:34 +00:00
|
|
|
childBounds = append(childBounds, p.Orientation.FlipRect(child))
|
2019-03-05 20:52:18 +00:00
|
|
|
childOffset += height
|
|
|
|
}
|
2021-08-19 10:27:34 +00:00
|
|
|
return childBounds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *StackPanel) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32, parent Control) {
|
|
|
|
childBounds := p.preArrange(ctx, bounds, true)
|
|
|
|
for i := range p.Children {
|
|
|
|
p.Children[i].Arrange(ctx, childBounds[i], offset, p)
|
|
|
|
}
|
|
|
|
p.ControlBase.Arrange(ctx, bounds, offset, parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *StackPanel) BoundsUnclipped(ctx Context, path ControlPath) geom.RectangleF32 {
|
|
|
|
if len(path) == 0 {
|
|
|
|
return p.bounds
|
|
|
|
}
|
|
|
|
next := path[0]
|
|
|
|
childBounds := p.preArrange(ctx, p.bounds, false)
|
|
|
|
for i, child := range p.Children {
|
|
|
|
if child != next && next.Self() != child.Self() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return childBounds[i]
|
|
|
|
}
|
|
|
|
panic("child not found in path")
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 13:37:53 +00:00
|
|
|
func (p *StackPanel) DesiredSize(ctx Context, size geom.PointF32) geom.PointF32 {
|
2019-03-05 20:52:18 +00:00
|
|
|
var length float32
|
|
|
|
var width float32
|
|
|
|
for _, child := range p.Children {
|
2020-05-16 13:37:53 +00:00
|
|
|
var size = child.DesiredSize(ctx, size)
|
2019-03-05 20:52:18 +00:00
|
|
|
var l, w = p.Orientation.LengthParallel(size), p.Orientation.LengthPerpendicular(size)
|
|
|
|
if geom.IsNaN32(l) {
|
|
|
|
length = l
|
|
|
|
} else {
|
|
|
|
if !geom.IsNaN32(length) {
|
|
|
|
length += l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if geom.IsNaN32(w) {
|
|
|
|
width = w
|
|
|
|
} else {
|
|
|
|
if !geom.IsNaN32(width) {
|
|
|
|
width = geom.Max32(width, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p.Orientation.Pt(length, width)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *StackPanel) Render(ctx Context) {
|
|
|
|
p.RenderBackground(ctx)
|
|
|
|
var bounds = p.Bounds()
|
|
|
|
for _, child := range p.Children {
|
|
|
|
var childB = child.Bounds()
|
2019-07-04 20:06:48 +00:00
|
|
|
if childB.Min.X >= bounds.Max.X || childB.Min.Y >= bounds.Max.Y || childB.Max.X < bounds.Min.X || childB.Max.Y < bounds.Min.Y || childB.Max.X < 0 || childB.Max.Y < 0 {
|
2019-03-05 20:52:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
child.Render(ctx)
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 10:27:34 +00:00
|
|
|
|
|
|
|
func (p *StackPanel) ScrollIntoView(ctx Context, path ControlPath) {
|
|
|
|
if p.parent == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.parent.ScrollIntoView(ctx, path.Prepend(p))
|
|
|
|
}
|