mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
936c266b03
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>
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package widget_test
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
|
|
"gioui.org/f32"
|
|
"gioui.org/io/pointer"
|
|
"gioui.org/io/router"
|
|
"gioui.org/layout"
|
|
"gioui.org/op"
|
|
"gioui.org/widget"
|
|
)
|
|
|
|
func ExampleClickable_passthrough() {
|
|
// When laying out clickable widgets on top of each other,
|
|
// pointer events can be passed down for the underlying
|
|
// widgets to pick them up.
|
|
var button1, button2 widget.Clickable
|
|
var r router.Router
|
|
gtx := layout.Context{
|
|
Ops: new(op.Ops),
|
|
Constraints: layout.Exact(image.Pt(100, 100)),
|
|
Queue: &r,
|
|
}
|
|
|
|
// widget lays out two buttons on top of each other.
|
|
widget := func() {
|
|
button1.Layout(gtx)
|
|
area := pointer.Rect(image.Rectangle{Max: gtx.Constraints.Max})
|
|
// button2 completely covers button1, but pass-through allows pointer
|
|
// events to pass through to button1.
|
|
area.PassThrough = true
|
|
defer area.Push(gtx.Ops).Pop()
|
|
button2.Layout(gtx)
|
|
}
|
|
|
|
// The first layout and call to Frame declare the Clickable handlers
|
|
// to the input router, so the following pointer events are propagated.
|
|
widget()
|
|
r.Frame(gtx.Ops)
|
|
// Simulate one click on the buttons by sending a Press and Release event.
|
|
r.Queue(
|
|
pointer.Event{
|
|
Source: pointer.Mouse,
|
|
Buttons: pointer.ButtonPrimary,
|
|
Type: pointer.Press,
|
|
Position: f32.Pt(50, 50),
|
|
},
|
|
pointer.Event{
|
|
Source: pointer.Mouse,
|
|
Buttons: pointer.ButtonPrimary,
|
|
Type: pointer.Release,
|
|
Position: f32.Pt(50, 50),
|
|
},
|
|
)
|
|
// The second layout ensures that the click event is registered by the buttons.
|
|
widget()
|
|
|
|
if button1.Clicked() {
|
|
fmt.Println("button1 clicked!")
|
|
}
|
|
if button2.Clicked() {
|
|
fmt.Println("button2 clicked!")
|
|
}
|
|
|
|
// Output:
|
|
// button1 clicked!
|
|
// button2 clicked!
|
|
}
|