mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-03 08:25:34 +00:00
all: [API] split operation stack into per-state stacks
The op.Save and Load methods exist to support the need for
transformation, clip, pointer area state to behave as stacks. For
example, layout needs to apply an offset to its children but not
subsequent operations.
Before this change, op.Save and Load were used to save and restore the
state:
ops := new(op.Ops)
// Save state.
state := op.Save(ops)
// Apply offset.
op.Offset(...).Add(ops)
// Draw with offset applied.
draw(ops)
// Restore state.
state.Load()
A drawback with the op.Save mechanism is that there is no direct
connection between the state change and the saving and loading of state.
This causes confusion as to when a Save/Load is needed and who is
responsible for performing them, which leads to subtle bugs and over-use
of Save/Loads.
This change gets rid of the general state stack and replaces it with
per-state stacks. There is now a stack for transformation, clip, pointer
areas, and they can only be restored by the code pushing state to them.
The example above now becomes:
ops := new(op.Ops)
// Push offset to the transformation stack.
stack := op.Offset(...).Push(ops)
// Draw with offset applied.
draw(ops)
// Restore state.
stack.Pop()
For convenience, transformation also be Add'ed if the stack operation is
not required.
Simple state such as the current material no longer has a way to be
restored; it is assumed the client of a PaintOp adds their desired
material operation before it.
API change: replace op.Save/Load with explicit Push/Pop scopes for
op.TransformOps, pointer.AreaOps, clip.Ops.
To ease porting, this change retains a version of op.Save/Load that
saves and restores the transformation and clip stacks. It also retains
an Add method for clip.Op.
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+7
-9
@@ -41,25 +41,23 @@ operations is the intersection of the areas.
|
||||
|
||||
Matching events
|
||||
|
||||
StackOp operations and input handlers form an implicit tree.
|
||||
Each stack operation is a node, and each input handler is associated
|
||||
with the most recent node.
|
||||
Areas form an implicit tree, with input handlers as leaves. The children of
|
||||
an area is every area and handler added between its Push and corresponding Pop.
|
||||
|
||||
For example:
|
||||
|
||||
ops := new(op.Ops)
|
||||
var stack op.StackOp
|
||||
var h1, h2 *Handler
|
||||
|
||||
state := op.Save(ops)
|
||||
area := pointer.Rect(...).Push(ops)
|
||||
pointer.InputOp{Tag: h1}.Add(Ops)
|
||||
state.Load()
|
||||
area.Pop()
|
||||
|
||||
state = op.Save(ops)
|
||||
area := pointer.Rect(...).Push(ops)
|
||||
pointer.InputOp{Tag: h2}.Add(ops)
|
||||
state.Load()
|
||||
area.Pop()
|
||||
|
||||
implies a tree of two inner nodes, each with one pointer handler.
|
||||
implies a tree of two inner nodes, each with one pointer handler attached.
|
||||
|
||||
When determining which handlers match an Event, only handlers whose
|
||||
areas contain the event position are considered. The matching
|
||||
|
||||
+29
-8
@@ -54,6 +54,13 @@ type AreaOp struct {
|
||||
rect image.Rectangle
|
||||
}
|
||||
|
||||
// AreaStack represents an AreaOp on the stack of areas.
|
||||
type AreaStack struct {
|
||||
ops *op.Ops
|
||||
id op.StackID
|
||||
macroID int
|
||||
}
|
||||
|
||||
// CursorNameOp sets the cursor for the current area.
|
||||
type CursorNameOp struct {
|
||||
Name CursorName
|
||||
@@ -185,18 +192,32 @@ func Ellipse(size image.Rectangle) AreaOp {
|
||||
}
|
||||
}
|
||||
|
||||
func (op AreaOp) Add(o *op.Ops) {
|
||||
data := o.Write(opconst.TypeAreaLen)
|
||||
// Push the current area to the stack and intersects the current area with the
|
||||
// area represented by o.
|
||||
func (o AreaOp) Push(ops *op.Ops) AreaStack {
|
||||
id, macroID := ops.PushOp(op.AreaStack)
|
||||
o.add(ops, true)
|
||||
return AreaStack{ops: ops, id: id, macroID: macroID}
|
||||
}
|
||||
|
||||
func (o AreaOp) add(ops *op.Ops, push bool) {
|
||||
data := ops.Write(opconst.TypeAreaLen)
|
||||
data[0] = byte(opconst.TypeArea)
|
||||
data[1] = byte(op.kind)
|
||||
if op.PassThrough {
|
||||
data[1] = byte(o.kind)
|
||||
if o.PassThrough {
|
||||
data[2] = 1
|
||||
}
|
||||
bo := binary.LittleEndian
|
||||
bo.PutUint32(data[3:], uint32(op.rect.Min.X))
|
||||
bo.PutUint32(data[7:], uint32(op.rect.Min.Y))
|
||||
bo.PutUint32(data[11:], uint32(op.rect.Max.X))
|
||||
bo.PutUint32(data[15:], uint32(op.rect.Max.Y))
|
||||
bo.PutUint32(data[3:], uint32(o.rect.Min.X))
|
||||
bo.PutUint32(data[7:], uint32(o.rect.Min.Y))
|
||||
bo.PutUint32(data[11:], uint32(o.rect.Max.X))
|
||||
bo.PutUint32(data[15:], uint32(o.rect.Max.Y))
|
||||
}
|
||||
|
||||
func (o AreaStack) Pop() {
|
||||
o.ops.PopOp(op.AreaStack, o.id, o.macroID)
|
||||
data := o.ops.Write(opconst.TypePopAreaLen)
|
||||
data[0] = byte(opconst.TypePopArea)
|
||||
}
|
||||
|
||||
func (op CursorNameOp) Add(o *op.Ops) {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
package pointer
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gioui.org/op"
|
||||
)
|
||||
|
||||
func TestTransformChecks(t *testing.T) {
|
||||
defer func() {
|
||||
if err := recover(); err == nil {
|
||||
t.Error("cross-macro Pop didn't panic")
|
||||
}
|
||||
}()
|
||||
var ops op.Ops
|
||||
area := AreaOp{}.Push(&ops)
|
||||
op.Record(&ops)
|
||||
area.Pop()
|
||||
}
|
||||
Reference in New Issue
Block a user