diff options
Diffstat (limited to 'vendor/gioui.org/io/pointer/pointer.go')
-rw-r--r-- | vendor/gioui.org/io/pointer/pointer.go | 184 |
1 files changed, 125 insertions, 59 deletions
diff --git a/vendor/gioui.org/io/pointer/pointer.go b/vendor/gioui.org/io/pointer/pointer.go index 3344d6b..97987f7 100644 --- a/vendor/gioui.org/io/pointer/pointer.go +++ b/vendor/gioui.org/io/pointer/pointer.go @@ -4,12 +4,13 @@ package pointer import ( "encoding/binary" + "fmt" "image" "strings" "time" "gioui.org/f32" - "gioui.org/internal/opconst" + "gioui.org/internal/ops" "gioui.org/io/event" "gioui.org/io/key" "gioui.org/op" @@ -41,12 +42,21 @@ type Event struct { 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 { - kind areaKind - rect image.Rectangle +// PassOp sets the pass-through mode. InputOps added while the pass-through +// mode is set don't block events to siblings. +type PassOp struct { +} + +// PassStack represents a PassOp on the pass stack. +type PassStack struct { + ops *ops.Ops + id ops.StackID + macroID int +} + +// CursorNameOp sets the cursor for the current area. +type CursorNameOp struct { + Name CursorName } // InputOp declares an input handler ready for pointer @@ -58,17 +68,18 @@ type InputOp struct { Grab bool // Types is a bitwise-or of event types to receive. Types Type -} - -// PassOp sets the pass-through mode. -type PassOp struct { - Pass bool + // 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 +type Type uint // Priority of an Event. type Priority uint8 @@ -79,8 +90,27 @@ type Source uint8 // Buttons is a set of mouse buttons type Buttons uint8 -// Must match app/internal/input.areaKind -type areaKind uint8 +// CursorName is the name of a cursor. +type CursorName string + +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 @@ -121,61 +151,90 @@ const ( ) const ( - ButtonLeft Buttons = 1 << iota - ButtonRight - ButtonMiddle -) - -const ( - areaRect areaKind = iota - areaEllipse + // 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 ) -// Rect constructs a rectangular hit area. -func Rect(size image.Rectangle) AreaOp { - return AreaOp{ - kind: areaRect, - rect: size, +// frect converts a rectangle to a f32.Rectangle. +func frect(r image.Rectangle) f32.Rectangle { + return f32.Rectangle{ + Min: fpt(r.Min), Max: fpt(r.Max), } } -// Ellipse constructs an ellipsoid hit area. -func Ellipse(size image.Rectangle) AreaOp { - return AreaOp{ - kind: areaEllipse, - rect: size, +// fpt converts an point to a f32.Point. +func fpt(p image.Point) f32.Point { + return f32.Point{ + X: float32(p.X), Y: float32(p.Y), } } -func (op AreaOp) Add(o *op.Ops) { - data := o.Write(opconst.TypeAreaLen) - data[0] = byte(opconst.TypeArea) - data[1] = byte(op.kind) - bo := binary.LittleEndian - bo.PutUint32(data[2:], uint32(op.rect.Min.X)) - bo.PutUint32(data[6:], uint32(op.rect.Min.Y)) - bo.PutUint32(data[10:], uint32(op.rect.Max.X)) - bo.PutUint32(data[14:], uint32(op.rect.Max.Y)) +// Push the current pass mode to the pass stack and set the pass mode. +func (p PassOp) Push(o *op.Ops) PassStack { + id, mid := ops.PushOp(&o.Internal, ops.PassStack) + data := ops.Write(&o.Internal, ops.TypePassLen) + data[0] = byte(ops.TypePass) + return PassStack{ops: &o.Internal, id: id, macroID: mid} } -func (h InputOp) Add(o *op.Ops) { - data := o.Write(opconst.TypePointerInputLen, h.Tag) - data[0] = byte(opconst.TypePointerInput) - if h.Grab { - data[1] = 1 - } - data[2] = byte(h.Types) +func (p PassStack) Pop() { + ops.PopOp(p.ops, ops.PassStack, p.id, p.macroID) + data := ops.Write(p.ops, ops.TypePopPassLen) + data[0] = byte(ops.TypePopPass) +} + +func (op CursorNameOp) Add(o *op.Ops) { + data := ops.Write1(&o.Internal, ops.TypeCursorLen, op.Name) + data[0] = byte(ops.TypeCursor) } -func (op PassOp) Add(o *op.Ops) { - data := o.Write(opconst.TypePassLen) - data[0] = byte(opconst.TypePass) - if op.Pass { +// 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)) + } + if op.Types>>16 > 0 { + panic(fmt.Errorf("value in Types overflows uint16")) + } + data := ops.Write1(&o.Internal, ops.TypePointerInputLen, op.Tag) + data[0] = byte(ops.TypePointerInput) + if op.Grab { data[1] = 1 } + bo := binary.LittleEndian + bo.PutUint16(data[2:], uint16(op.Types)) + bo.PutUint32(data[4:], uint32(op.ScrollBounds.Min.X)) + bo.PutUint32(data[8:], uint32(op.ScrollBounds.Min.Y)) + bo.PutUint32(data[12:], uint32(op.ScrollBounds.Max.X)) + bo.PutUint32(data[16:], uint32(op.ScrollBounds.Max.Y)) } func (t Type) String() string { + if t == Cancel { + return "Cancel" + } + var buf strings.Builder + for tt := Type(1); tt > 0; tt <<= 1 { + if t&tt > 0 { + if buf.Len() > 0 { + buf.WriteByte('|') + } + buf.WriteString((t & tt).string()) + } + } + return buf.String() +} + +func (t Type) string() string { switch t { case Press: return "Press" @@ -230,16 +289,23 @@ func (b Buttons) Contain(buttons Buttons) bool { func (b Buttons) String() string { var strs []string - if b.Contain(ButtonLeft) { - strs = append(strs, "ButtonLeft") + if b.Contain(ButtonPrimary) { + strs = append(strs, "ButtonPrimary") } - if b.Contain(ButtonRight) { - strs = append(strs, "ButtonRight") + if b.Contain(ButtonSecondary) { + strs = append(strs, "ButtonSecondary") } - if b.Contain(ButtonMiddle) { - strs = append(strs, "ButtonMiddle") + 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() {} |