mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 01:15:35 +00:00
ui: remove ui.Ops parameters from layouts and path builder structs
Layouts and path builders are transient and need an ops list for operation. However, instead of passing the ops list to every method, pass the list in an init method and store it for subsequent methods. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+17
-15
@@ -15,6 +15,7 @@ type Flex struct {
|
||||
CrossAxisAlignment CrossAxisAlignment
|
||||
MainAxisSize MainAxisSize
|
||||
|
||||
ops *ui.Ops
|
||||
constrained bool
|
||||
cs Constraints
|
||||
begun bool
|
||||
@@ -57,10 +58,11 @@ const (
|
||||
Stretch
|
||||
)
|
||||
|
||||
func (f *Flex) Init(cs Constraints) {
|
||||
func (f *Flex) Init(ops *ui.Ops, cs Constraints) {
|
||||
if f.constrained {
|
||||
panic("Constrain must be called exactly once")
|
||||
}
|
||||
f.ops = ops
|
||||
f.constrained = true
|
||||
f.cs = cs
|
||||
f.taken = 0
|
||||
@@ -68,7 +70,7 @@ func (f *Flex) Init(cs Constraints) {
|
||||
f.maxBaseline = 0
|
||||
}
|
||||
|
||||
func (f *Flex) begin(ops *ui.Ops) {
|
||||
func (f *Flex) begin() {
|
||||
if !f.constrained {
|
||||
panic("must Constrain before adding a child")
|
||||
}
|
||||
@@ -76,12 +78,12 @@ func (f *Flex) begin(ops *ui.Ops) {
|
||||
panic("must End before adding a child")
|
||||
}
|
||||
f.begun = true
|
||||
ops.Begin()
|
||||
ui.OpLayer{}.Add(ops)
|
||||
f.ops.Begin()
|
||||
ui.OpLayer{}.Add(f.ops)
|
||||
}
|
||||
|
||||
func (f *Flex) Rigid(ops *ui.Ops) Constraints {
|
||||
f.begin(ops)
|
||||
func (f *Flex) Rigid() Constraints {
|
||||
f.begin()
|
||||
mainc := axisMainConstraint(f.Axis, f.cs)
|
||||
mainMax := mainc.Max
|
||||
if mainc.Max != ui.Inf {
|
||||
@@ -90,8 +92,8 @@ func (f *Flex) Rigid(ops *ui.Ops) Constraints {
|
||||
return axisConstraints(f.Axis, Constraint{Max: mainMax}, f.crossConstraintChild(f.cs))
|
||||
}
|
||||
|
||||
func (f *Flex) Flexible(ops *ui.Ops, flex float32, mode FlexMode) Constraints {
|
||||
f.begin(ops)
|
||||
func (f *Flex) Flexible(flex float32, mode FlexMode) Constraints {
|
||||
f.begin()
|
||||
mainc := axisMainConstraint(f.Axis, f.cs)
|
||||
var flexSize int
|
||||
if mainc.Max != ui.Inf && mainc.Max > f.taken {
|
||||
@@ -104,12 +106,12 @@ func (f *Flex) Flexible(ops *ui.Ops, flex float32, mode FlexMode) Constraints {
|
||||
return axisConstraints(f.Axis, submainc, f.crossConstraintChild(f.cs))
|
||||
}
|
||||
|
||||
func (f *Flex) End(ops *ui.Ops, dims Dimens) FlexChild {
|
||||
func (f *Flex) End(dims Dimens) FlexChild {
|
||||
if !f.begun {
|
||||
panic("End called without an active child")
|
||||
}
|
||||
f.begun = false
|
||||
block := ops.End()
|
||||
block := f.ops.End()
|
||||
f.taken += axisMain(f.Axis, dims.Size)
|
||||
if c := axisCross(f.Axis, dims.Size); c > f.maxCross {
|
||||
f.maxCross = c
|
||||
@@ -120,7 +122,7 @@ func (f *Flex) End(ops *ui.Ops, dims Dimens) FlexChild {
|
||||
return FlexChild{block, dims}
|
||||
}
|
||||
|
||||
func (f *Flex) Layout(ops *ui.Ops, children ...FlexChild) Dimens {
|
||||
func (f *Flex) Layout(children ...FlexChild) Dimens {
|
||||
mainc := axisMainConstraint(f.Axis, f.cs)
|
||||
crossSize := axisCrossConstraint(f.Axis, f.cs).Constrain(f.maxCross)
|
||||
var space int
|
||||
@@ -157,12 +159,12 @@ func (f *Flex) Layout(ops *ui.Ops, children ...FlexChild) Dimens {
|
||||
cross = f.maxBaseline - b
|
||||
}
|
||||
}
|
||||
ui.OpPush{}.Add(ops)
|
||||
ui.OpPush{}.Add(f.ops)
|
||||
ui.OpTransform{
|
||||
Transform: ui.Offset(toPointF(axisPoint(f.Axis, mainSize, cross))),
|
||||
}.Add(ops)
|
||||
child.block.Add(ops)
|
||||
ui.OpPop{}.Add(ops)
|
||||
}.Add(f.ops)
|
||||
child.block.Add(f.ops)
|
||||
ui.OpPop{}.Add(f.ops)
|
||||
mainSize += axisMain(f.Axis, dims.Size)
|
||||
switch f.MainAxisAlignment {
|
||||
case SpaceEvenly:
|
||||
|
||||
+9
-3
@@ -70,10 +70,12 @@ func ExactConstraints(size image.Point) Constraints {
|
||||
type Insets struct {
|
||||
Top, Right, Bottom, Left float32
|
||||
|
||||
cs Constraints
|
||||
ops *ui.Ops
|
||||
cs Constraints
|
||||
}
|
||||
|
||||
func (in *Insets) Begin(ops *ui.Ops, cs Constraints) Constraints {
|
||||
in.ops = ops
|
||||
in.cs = cs
|
||||
mcs := cs
|
||||
t, r, b, l := int(math.Round(float64(in.Top))), int(math.Round(float64(in.Right))), int(math.Round(float64(in.Bottom))), int(math.Round(float64(in.Left)))
|
||||
@@ -102,7 +104,8 @@ func (in *Insets) Begin(ops *ui.Ops, cs Constraints) Constraints {
|
||||
return mcs
|
||||
}
|
||||
|
||||
func (in *Insets) End(ops *ui.Ops, dims Dimens) Dimens {
|
||||
func (in *Insets) End(dims Dimens) Dimens {
|
||||
ops := in.ops
|
||||
ui.OpPop{}.Add(ops)
|
||||
t, r, b, l := int(math.Round(float64(in.Top))), int(math.Round(float64(in.Right))), int(math.Round(float64(in.Bottom))), int(math.Round(float64(in.Left)))
|
||||
return Dimens{
|
||||
@@ -144,17 +147,20 @@ func (s Sized) Constrain(cs Constraints) Constraints {
|
||||
}
|
||||
|
||||
type Align struct {
|
||||
ops *ui.Ops
|
||||
Alignment Direction
|
||||
cs Constraints
|
||||
}
|
||||
|
||||
func (a *Align) Begin(ops *ui.Ops, cs Constraints) Constraints {
|
||||
a.ops = ops
|
||||
a.cs = cs
|
||||
ops.Begin()
|
||||
return cs.Loose()
|
||||
}
|
||||
|
||||
func (a *Align) End(ops *ui.Ops, dims Dimens) Dimens {
|
||||
func (a *Align) End(dims Dimens) Dimens {
|
||||
ops := a.ops
|
||||
block := ops.End()
|
||||
sz := dims.Size
|
||||
if a.cs.Width.Max != ui.Inf {
|
||||
|
||||
+15
-16
@@ -24,6 +24,7 @@ type List struct {
|
||||
// The distance scrolled since last call to Init.
|
||||
Distance int
|
||||
|
||||
ops *ui.Ops
|
||||
scroll gesture.Scroll
|
||||
scrollDir int
|
||||
|
||||
@@ -46,7 +47,8 @@ const (
|
||||
iterateBackward
|
||||
)
|
||||
|
||||
func (l *List) Init(cs Constraints, len int) {
|
||||
func (l *List) Init(ops *ui.Ops, cs Constraints, len int) {
|
||||
l.ops = ops
|
||||
l.dir = iterateNone
|
||||
l.maxSize = 0
|
||||
l.children = l.children[:0]
|
||||
@@ -55,6 +57,7 @@ func (l *List) Init(cs Constraints, len int) {
|
||||
if l.first > len {
|
||||
l.first = len
|
||||
}
|
||||
ops.Begin()
|
||||
}
|
||||
|
||||
func (l *List) Dragging() bool {
|
||||
@@ -69,19 +72,16 @@ func (l *List) Update(c *ui.Config, q pointer.Events) {
|
||||
l.offset += d
|
||||
}
|
||||
|
||||
func (l *List) Next(ops *ui.Ops) (int, Constraints, bool) {
|
||||
func (l *List) Next() (int, Constraints, bool) {
|
||||
if l.dir != iterateNone {
|
||||
panic("a previous Next was not finished with Elem")
|
||||
}
|
||||
i, ok := l.next()
|
||||
var cs Constraints
|
||||
if ok {
|
||||
if len(l.children) == 0 {
|
||||
ops.Begin()
|
||||
}
|
||||
cs = axisConstraints(l.Axis, Constraint{Max: ui.Inf}, l.crossConstraintChild(l.cs))
|
||||
ops.Begin()
|
||||
ui.OpLayer{}.Add(ops)
|
||||
l.ops.Begin()
|
||||
ui.OpLayer{}.Add(l.ops)
|
||||
}
|
||||
return i, cs, ok
|
||||
}
|
||||
@@ -110,8 +110,8 @@ func (l *List) next() (int, bool) {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (l *List) End(ops *ui.Ops, dims Dimens) {
|
||||
block := ops.End()
|
||||
func (l *List) End(dims Dimens) {
|
||||
block := l.ops.End()
|
||||
child := scrollChild{dims.Size, block}
|
||||
switch l.dir {
|
||||
case iterateForward:
|
||||
@@ -130,7 +130,7 @@ func (l *List) End(ops *ui.Ops, dims Dimens) {
|
||||
l.dir = iterateNone
|
||||
}
|
||||
|
||||
func (l *List) Layout(ops *ui.Ops) Dimens {
|
||||
func (l *List) Layout() Dimens {
|
||||
mainc := axisMainConstraint(l.Axis, l.cs)
|
||||
for len(l.children) > 0 {
|
||||
sz := l.children[0].size
|
||||
@@ -155,6 +155,7 @@ func (l *List) Layout(ops *ui.Ops) Dimens {
|
||||
break
|
||||
}
|
||||
}
|
||||
ops := l.ops
|
||||
pos := -l.offset
|
||||
for _, child := range l.children {
|
||||
sz := child.size
|
||||
@@ -192,12 +193,10 @@ func (l *List) Layout(ops *ui.Ops) Dimens {
|
||||
l.scroll.Stop()
|
||||
}
|
||||
dims := axisPoint(l.Axis, mainc.Constrain(pos), maxCross)
|
||||
if len(l.children) > 0 {
|
||||
block := ops.End()
|
||||
pointer.AreaRect(dims).Add(ops)
|
||||
l.scroll.Add(ops)
|
||||
block.Add(ops)
|
||||
}
|
||||
block := ops.End()
|
||||
pointer.AreaRect(dims).Add(ops)
|
||||
l.scroll.Add(ops)
|
||||
block.Add(ops)
|
||||
return Dimens{Size: dims}
|
||||
}
|
||||
|
||||
|
||||
+17
-15
@@ -11,6 +11,7 @@ import (
|
||||
type Stack struct {
|
||||
Alignment Direction
|
||||
|
||||
ops *ui.Ops
|
||||
constrained bool
|
||||
cs Constraints
|
||||
begun bool
|
||||
@@ -36,14 +37,15 @@ const (
|
||||
W
|
||||
)
|
||||
|
||||
func (s *Stack) Init(cs Constraints) {
|
||||
func (s *Stack) Init(ops *ui.Ops, cs Constraints) {
|
||||
s.ops = ops
|
||||
s.cs = cs
|
||||
s.constrained = true
|
||||
s.maxSZ = image.Point{}
|
||||
s.baseline = 0
|
||||
}
|
||||
|
||||
func (s *Stack) begin(ops *ui.Ops) {
|
||||
func (s *Stack) begin() {
|
||||
if !s.constrained {
|
||||
panic("must Constrain before adding a child")
|
||||
}
|
||||
@@ -51,25 +53,25 @@ func (s *Stack) begin(ops *ui.Ops) {
|
||||
panic("must End before adding a child")
|
||||
}
|
||||
s.begun = true
|
||||
ops.Begin()
|
||||
ui.OpLayer{}.Add(ops)
|
||||
s.ops.Begin()
|
||||
ui.OpLayer{}.Add(s.ops)
|
||||
}
|
||||
|
||||
func (s *Stack) Rigid(ops *ui.Ops) Constraints {
|
||||
s.begin(ops)
|
||||
func (s *Stack) Rigid() Constraints {
|
||||
s.begin()
|
||||
return s.cs
|
||||
}
|
||||
|
||||
func (s *Stack) Expand(ops *ui.Ops) Constraints {
|
||||
s.begin(ops)
|
||||
func (s *Stack) Expand() Constraints {
|
||||
s.begin()
|
||||
return Constraints{
|
||||
Width: Constraint{Min: s.maxSZ.X, Max: s.maxSZ.X},
|
||||
Height: Constraint{Min: s.maxSZ.Y, Max: s.maxSZ.Y},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stack) End(ops *ui.Ops, dims Dimens) StackChild {
|
||||
b := ops.End()
|
||||
func (s *Stack) End(dims Dimens) StackChild {
|
||||
b := s.ops.End()
|
||||
s.begun = false
|
||||
if w := dims.Size.X; w > s.maxSZ.X {
|
||||
s.maxSZ.X = w
|
||||
@@ -85,7 +87,7 @@ func (s *Stack) End(ops *ui.Ops, dims Dimens) StackChild {
|
||||
return StackChild{b, dims}
|
||||
}
|
||||
|
||||
func (s *Stack) Layout(ops *ui.Ops, children ...StackChild) Dimens {
|
||||
func (s *Stack) Layout(children ...StackChild) Dimens {
|
||||
for _, ch := range children {
|
||||
sz := ch.dims.Size
|
||||
var p image.Point
|
||||
@@ -101,10 +103,10 @@ func (s *Stack) Layout(ops *ui.Ops, children ...StackChild) Dimens {
|
||||
case SW, S, SE:
|
||||
p.Y = s.maxSZ.Y - sz.Y
|
||||
}
|
||||
ui.OpPush{}.Add(ops)
|
||||
ui.OpTransform{Transform: ui.Offset(toPointF(p))}.Add(ops)
|
||||
ch.block.Add(ops)
|
||||
ui.OpPop{}.Add(ops)
|
||||
ui.OpPush{}.Add(s.ops)
|
||||
ui.OpTransform{Transform: ui.Offset(toPointF(p))}.Add(s.ops)
|
||||
ch.block.Add(s.ops)
|
||||
ui.OpPop{}.Add(s.ops)
|
||||
}
|
||||
b := s.baseline
|
||||
if b == 0 {
|
||||
|
||||
Reference in New Issue
Block a user