mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
960f3068a1
A previous change merged PassOp with AreaOp under the assumption that
the pass mode would be set on a particular area. That assumption turns
out not to hold, so this change brings back PassOp as an independent
stack operation.
This is an API change: replace AreaOp{Pass: true} with a separate
pointer.PassOp operation.
Fixes gio#288
Signed-off-by: Elias Naur <mail@eliasnaur.com>
71 lines
1.6 KiB
Go
71 lines
1.6 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)
|
|
// button2 completely covers button1, but pass-through allows pointer
|
|
// events to pass through to button1.
|
|
defer pointer.PassOp{}.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!
|
|
}
|