forked from joejulian/gio
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>
324 lines
7.4 KiB
Go
324 lines
7.4 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package pointer
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"fmt"
|
|
"image"
|
|
"strings"
|
|
"time"
|
|
|
|
"gioui.org/f32"
|
|
"gioui.org/internal/opconst"
|
|
"gioui.org/io/event"
|
|
"gioui.org/io/key"
|
|
"gioui.org/op"
|
|
)
|
|
|
|
// Event is a pointer event.
|
|
type Event struct {
|
|
Type Type
|
|
Source Source
|
|
// PointerID is the id for the pointer and can be used
|
|
// to track a particular pointer from Press to
|
|
// Release or Cancel.
|
|
PointerID ID
|
|
// Priority is the priority of the receiving handler
|
|
// for this event.
|
|
Priority Priority
|
|
// Time is when the event was received. The
|
|
// timestamp is relative to an undefined base.
|
|
Time time.Duration
|
|
// Buttons are the set of pressed mouse buttons for this event.
|
|
Buttons Buttons
|
|
// Position is the position of the event, relative to
|
|
// the current transformation, as set by op.TransformOp.
|
|
Position f32.Point
|
|
// Scroll is the scroll amount, if any.
|
|
Scroll f32.Point
|
|
// Modifiers is the set of active modifiers when
|
|
// the mouse button was pressed.
|
|
Modifiers key.Modifiers
|
|
}
|
|
|
|
// AreaOp updates the hit area to the intersection of the current
|
|
// hit area and the area. The area is transformed before applying
|
|
// it.
|
|
type AreaOp struct {
|
|
// PassThrough areas and their children don't block events to siblings
|
|
// them.
|
|
PassThrough bool
|
|
|
|
kind areaKind
|
|
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
|
|
}
|
|
|
|
// InputOp declares an input handler ready for pointer
|
|
// events.
|
|
type InputOp struct {
|
|
Tag event.Tag
|
|
// Grab, if set, request that the handler get
|
|
// Grabbed priority.
|
|
Grab bool
|
|
// Types is a bitwise-or of event types to receive.
|
|
Types Type
|
|
// ScrollBounds describe the maximum scrollable distances in both
|
|
// axes. Specifically, any Event e delivered to Tag will satisfy
|
|
//
|
|
// ScrollBounds.Min.X <= e.Scroll.X <= ScrollBounds.Max.X (horizontal axis)
|
|
// ScrollBounds.Min.Y <= e.Scroll.Y <= ScrollBounds.Max.Y (vertical axis)
|
|
ScrollBounds image.Rectangle
|
|
}
|
|
|
|
type ID uint16
|
|
|
|
// Type of an Event.
|
|
type Type uint8
|
|
|
|
// Priority of an Event.
|
|
type Priority uint8
|
|
|
|
// Source of an Event.
|
|
type Source uint8
|
|
|
|
// Buttons is a set of mouse buttons
|
|
type Buttons uint8
|
|
|
|
// CursorName is the name of a cursor.
|
|
type CursorName string
|
|
|
|
// Must match app/internal/input.areaKind
|
|
type areaKind uint8
|
|
|
|
const (
|
|
// CursorDefault is the default cursor.
|
|
CursorDefault CursorName = ""
|
|
// CursorText is the cursor for text.
|
|
CursorText CursorName = "text"
|
|
// CursorPointer is the cursor for a link.
|
|
CursorPointer CursorName = "pointer"
|
|
// CursorCrossHair is the cursor for precise location.
|
|
CursorCrossHair CursorName = "crosshair"
|
|
// CursorColResize is the cursor for vertical resize.
|
|
CursorColResize CursorName = "col-resize"
|
|
// CursorRowResize is the cursor for horizontal resize.
|
|
CursorRowResize CursorName = "row-resize"
|
|
// CursorGrab is the cursor for moving object in any direction.
|
|
CursorGrab CursorName = "grab"
|
|
// CursorNone hides the cursor. To show it again, use any other cursor.
|
|
CursorNone CursorName = "none"
|
|
)
|
|
|
|
const (
|
|
// A Cancel event is generated when the current gesture is
|
|
// interrupted by other handlers or the system.
|
|
Cancel Type = (1 << iota) >> 1
|
|
// Press of a pointer.
|
|
Press
|
|
// Release of a pointer.
|
|
Release
|
|
// Move of a pointer.
|
|
Move
|
|
// Drag of a pointer.
|
|
Drag
|
|
// Pointer enters an area watching for pointer input
|
|
Enter
|
|
// Pointer leaves an area watching for pointer input
|
|
Leave
|
|
// Scroll of a pointer.
|
|
Scroll
|
|
)
|
|
|
|
const (
|
|
// Mouse generated event.
|
|
Mouse Source = iota
|
|
// Touch generated event.
|
|
Touch
|
|
)
|
|
|
|
const (
|
|
// Shared priority is for handlers that
|
|
// are part of a matching set larger than 1.
|
|
Shared Priority = iota
|
|
// Foremost priority is like Shared, but the
|
|
// handler is the foremost of the matching set.
|
|
Foremost
|
|
// Grabbed is used for matching sets of size 1.
|
|
Grabbed
|
|
)
|
|
|
|
const (
|
|
// ButtonPrimary is the primary button, usually the left button for a
|
|
// right-handed user.
|
|
ButtonPrimary Buttons = 1 << iota
|
|
// ButtonSecondary is the secondary button, usually the right button for a
|
|
// right-handed user.
|
|
ButtonSecondary
|
|
// ButtonTertiary is the tertiary button, usually the middle button.
|
|
ButtonTertiary
|
|
)
|
|
|
|
const (
|
|
areaRect areaKind = iota
|
|
areaEllipse
|
|
)
|
|
|
|
// Rect constructs a rectangular hit area.
|
|
func Rect(size image.Rectangle) AreaOp {
|
|
return AreaOp{
|
|
kind: areaRect,
|
|
rect: size,
|
|
}
|
|
}
|
|
|
|
// Ellipse constructs an ellipsoid hit area.
|
|
func Ellipse(size image.Rectangle) AreaOp {
|
|
return AreaOp{
|
|
kind: areaEllipse,
|
|
rect: size,
|
|
}
|
|
}
|
|
|
|
// 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(o.kind)
|
|
if o.PassThrough {
|
|
data[2] = 1
|
|
}
|
|
bo := binary.LittleEndian
|
|
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) {
|
|
data := o.Write1(opconst.TypeCursorLen, op.Name)
|
|
data[0] = byte(opconst.TypeCursor)
|
|
}
|
|
|
|
// Add panics if the scroll range does not contain zero.
|
|
func (op InputOp) Add(o *op.Ops) {
|
|
if op.Tag == nil {
|
|
panic("Tag must be non-nil")
|
|
}
|
|
if b := op.ScrollBounds; b.Min.X > 0 || b.Max.X < 0 || b.Min.Y > 0 || b.Max.Y < 0 {
|
|
panic(fmt.Errorf("invalid scroll range value %v", b))
|
|
}
|
|
data := o.Write1(opconst.TypePointerInputLen, op.Tag)
|
|
data[0] = byte(opconst.TypePointerInput)
|
|
if op.Grab {
|
|
data[1] = 1
|
|
}
|
|
data[2] = byte(op.Types)
|
|
bo := binary.LittleEndian
|
|
bo.PutUint32(data[3:], uint32(op.ScrollBounds.Min.X))
|
|
bo.PutUint32(data[7:], uint32(op.ScrollBounds.Min.Y))
|
|
bo.PutUint32(data[11:], uint32(op.ScrollBounds.Max.X))
|
|
bo.PutUint32(data[15:], uint32(op.ScrollBounds.Max.Y))
|
|
}
|
|
|
|
func (t Type) String() string {
|
|
switch t {
|
|
case Press:
|
|
return "Press"
|
|
case Release:
|
|
return "Release"
|
|
case Cancel:
|
|
return "Cancel"
|
|
case Move:
|
|
return "Move"
|
|
case Drag:
|
|
return "Drag"
|
|
case Enter:
|
|
return "Enter"
|
|
case Leave:
|
|
return "Leave"
|
|
case Scroll:
|
|
return "Scroll"
|
|
default:
|
|
panic("unknown Type")
|
|
}
|
|
}
|
|
|
|
func (p Priority) String() string {
|
|
switch p {
|
|
case Shared:
|
|
return "Shared"
|
|
case Foremost:
|
|
return "Foremost"
|
|
case Grabbed:
|
|
return "Grabbed"
|
|
default:
|
|
panic("unknown priority")
|
|
}
|
|
}
|
|
|
|
func (s Source) String() string {
|
|
switch s {
|
|
case Mouse:
|
|
return "Mouse"
|
|
case Touch:
|
|
return "Touch"
|
|
default:
|
|
panic("unknown source")
|
|
}
|
|
}
|
|
|
|
// Contain reports whether the set b contains
|
|
// all of the buttons.
|
|
func (b Buttons) Contain(buttons Buttons) bool {
|
|
return b&buttons == buttons
|
|
}
|
|
|
|
func (b Buttons) String() string {
|
|
var strs []string
|
|
if b.Contain(ButtonPrimary) {
|
|
strs = append(strs, "ButtonPrimary")
|
|
}
|
|
if b.Contain(ButtonSecondary) {
|
|
strs = append(strs, "ButtonSecondary")
|
|
}
|
|
if b.Contain(ButtonTertiary) {
|
|
strs = append(strs, "ButtonTertiary")
|
|
}
|
|
return strings.Join(strs, "|")
|
|
}
|
|
|
|
func (c CursorName) String() string {
|
|
if c == CursorDefault {
|
|
return "default"
|
|
}
|
|
return string(c)
|
|
}
|
|
|
|
func (Event) ImplementsEvent() {}
|