mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
all: rename io/event.Key to Tag
Key had an unfortunate association with keyboard input.
This is an API change. The following rewrites were run to fixup
Gio code:
$ gofmt -r 'pointer.InputOp{Key:a} -> pointer.InputOp{Tag:a}' -w .
$ gofmt -r 'pointer.InputOp{Key:a, Grab:b} -> pointer.InputOp{Tag:a, Grab:b}' -w .
$ gofmt -r 'key.InputOp{Key:a} -> key.InputOp{Tag:a}' -w .
$ gofmt -r 'key.InputOp{Key:a, Focus:b} -> key.InputOp{Tag:a, Focus:b}' -w .
$ gofmt -r 'event.Key -> event.Tag' -w .
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+1
-1
@@ -390,7 +390,7 @@ func (w *Window) run(opts *window.Options) {
|
||||
}
|
||||
}
|
||||
|
||||
func (q *queue) Events(k event.Key) []event.Event {
|
||||
func (q *queue) Events(k event.Tag) []event.Event {
|
||||
return q.q.Events(k)
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -112,7 +112,7 @@ var touchSlop = unit.Dp(3)
|
||||
|
||||
// Add the handler to the operation list to receive click events.
|
||||
func (c *Click) Add(ops *op.Ops) {
|
||||
op := pointer.InputOp{Key: c}
|
||||
op := pointer.InputOp{Tag: c}
|
||||
op.Add(ops)
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ func (c *Click) Events(q event.Queue) []ClickEvent {
|
||||
|
||||
// Add the handler to the operation list to receive scroll events.
|
||||
func (s *Scroll) Add(ops *op.Ops) {
|
||||
oph := pointer.InputOp{Key: s, Grab: s.grab}
|
||||
oph := pointer.InputOp{Tag: s, Grab: s.grab}
|
||||
oph.Add(ops)
|
||||
if s.flinger.Active() {
|
||||
op.InvalidateOp{}.Add(ops)
|
||||
|
||||
+7
-7
@@ -25,7 +25,7 @@ The following example declares a handler ready for key input:
|
||||
|
||||
ops := new(op.Ops)
|
||||
var h *Handler = ...
|
||||
key.InputOp{Key: h}.Add(ops)
|
||||
key.InputOp{Tag: h}.Add(ops)
|
||||
|
||||
*/
|
||||
package event
|
||||
@@ -33,14 +33,14 @@ package event
|
||||
// Queue maps an event handler key to the events
|
||||
// available to the handler.
|
||||
type Queue interface {
|
||||
// Events returns the available events for a
|
||||
// Key.
|
||||
Events(k Key) []Event
|
||||
// Events returns the available events for an
|
||||
// event handler tag.
|
||||
Events(t Tag) []Event
|
||||
}
|
||||
|
||||
// Key is the stable identifier for an event handler.
|
||||
// For a handler h, the key is typically &h.
|
||||
type Key interface{}
|
||||
// Tag is the stable identifier for an event handler.
|
||||
// For a handler h, the tag is typically &h.
|
||||
type Tag interface{}
|
||||
|
||||
// Event is the marker interface for events.
|
||||
type Event interface {
|
||||
|
||||
+2
-2
@@ -22,7 +22,7 @@ import (
|
||||
// focused key handler. Set the Focus flag to request
|
||||
// the focus.
|
||||
type InputOp struct {
|
||||
Key event.Key
|
||||
Tag event.Tag
|
||||
Focus bool
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ func (m Modifiers) Contain(m2 Modifiers) bool {
|
||||
}
|
||||
|
||||
func (h InputOp) Add(o *op.Ops) {
|
||||
data := o.Write(opconst.TypeKeyInputLen, h.Key)
|
||||
data := o.Write(opconst.TypeKeyInputLen, h.Tag)
|
||||
data[0] = byte(opconst.TypeKeyInput)
|
||||
if h.Focus {
|
||||
data[1] = 1
|
||||
|
||||
@@ -57,7 +57,7 @@ type AreaOp struct {
|
||||
// InputOp declares an input handler ready for pointer
|
||||
// events.
|
||||
type InputOp struct {
|
||||
Key event.Key
|
||||
Tag event.Tag
|
||||
// Grab, if set, request that the handler get
|
||||
// Grabbed priority.
|
||||
Grab bool
|
||||
@@ -155,7 +155,7 @@ func (op AreaOp) Add(o *op.Ops) {
|
||||
}
|
||||
|
||||
func (h InputOp) Add(o *op.Ops) {
|
||||
data := o.Write(opconst.TypePointerInputLen, h.Key)
|
||||
data := o.Write(opconst.TypePointerInputLen, h.Tag)
|
||||
data[0] = byte(opconst.TypePointerInput)
|
||||
if h.Grab {
|
||||
data[1] = 1
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
// Op registers a handler for receiving
|
||||
// Events.
|
||||
type Op struct {
|
||||
Key event.Key
|
||||
Tag event.Tag
|
||||
}
|
||||
|
||||
// Event contains profile data from a single
|
||||
@@ -24,7 +24,7 @@ type Event struct {
|
||||
}
|
||||
|
||||
func (p Op) Add(o *op.Ops) {
|
||||
data := o.Write(opconst.TypeProfileLen, p.Key)
|
||||
data := o.Write(opconst.TypeProfileLen, p.Tag)
|
||||
data[0] = byte(opconst.TypeProfile)
|
||||
}
|
||||
|
||||
|
||||
+11
-11
@@ -13,8 +13,8 @@ import (
|
||||
type TextInputState uint8
|
||||
|
||||
type keyQueue struct {
|
||||
focus event.Key
|
||||
handlers map[event.Key]*keyHandler
|
||||
focus event.Tag
|
||||
handlers map[event.Tag]*keyHandler
|
||||
reader ops.Reader
|
||||
state TextInputState
|
||||
}
|
||||
@@ -46,7 +46,7 @@ func (q *keyQueue) InputState() TextInputState {
|
||||
|
||||
func (q *keyQueue) Frame(root *op.Ops, events *handlerEvents) {
|
||||
if q.handlers == nil {
|
||||
q.handlers = make(map[event.Key]*keyHandler)
|
||||
q.handlers = make(map[event.Tag]*keyHandler)
|
||||
}
|
||||
for _, h := range q.handlers {
|
||||
h.active = false
|
||||
@@ -89,8 +89,8 @@ func (q *keyQueue) Push(e event.Event, events *handlerEvents) {
|
||||
}
|
||||
}
|
||||
|
||||
func (q *keyQueue) resolveFocus(events *handlerEvents) (event.Key, listenerPriority, bool) {
|
||||
var k event.Key
|
||||
func (q *keyQueue) resolveFocus(events *handlerEvents) (event.Tag, listenerPriority, bool) {
|
||||
var k event.Tag
|
||||
var pri listenerPriority
|
||||
var hide bool
|
||||
loop:
|
||||
@@ -102,21 +102,21 @@ loop:
|
||||
switch {
|
||||
case op.Focus:
|
||||
newPri = priNewFocus
|
||||
case op.Key == q.focus:
|
||||
case op.Tag == q.focus:
|
||||
newPri = priCurrentFocus
|
||||
default:
|
||||
newPri = priDefault
|
||||
}
|
||||
// Switch focus if higher priority or if focus requested.
|
||||
if newPri.replaces(pri) {
|
||||
k, pri = op.Key, newPri
|
||||
k, pri = op.Tag, newPri
|
||||
}
|
||||
h, ok := q.handlers[op.Key]
|
||||
h, ok := q.handlers[op.Tag]
|
||||
if !ok {
|
||||
h = new(keyHandler)
|
||||
q.handlers[op.Key] = h
|
||||
q.handlers[op.Tag] = h
|
||||
// Reset the handler on (each) first appearance.
|
||||
events.Set(op.Key, []event.Event{key.FocusEvent{Focus: false}})
|
||||
events.Set(op.Tag, []event.Event{key.FocusEvent{Focus: false}})
|
||||
}
|
||||
h.active = true
|
||||
case opconst.TypeHideInput:
|
||||
@@ -144,7 +144,7 @@ func decodeKeyInputOp(d []byte, refs []interface{}) key.InputOp {
|
||||
panic("invalid op")
|
||||
}
|
||||
return key.InputOp{
|
||||
Tag: refs[0].(event.Tag),
|
||||
Focus: d[1] != 0,
|
||||
Key: refs[0].(event.Key),
|
||||
}
|
||||
}
|
||||
|
||||
+14
-14
@@ -17,14 +17,14 @@ import (
|
||||
type pointerQueue struct {
|
||||
hitTree []hitNode
|
||||
areas []areaNode
|
||||
handlers map[event.Key]*pointerHandler
|
||||
handlers map[event.Tag]*pointerHandler
|
||||
pointers []pointerInfo
|
||||
reader ops.Reader
|
||||
scratch []event.Key
|
||||
scratch []event.Tag
|
||||
|
||||
// prev and curr are two additional scratch slices that track active
|
||||
// pointer event handlers from the previous and current frame
|
||||
prev, curr []event.Key
|
||||
prev, curr []event.Tag
|
||||
}
|
||||
|
||||
type hitNode struct {
|
||||
@@ -34,13 +34,13 @@ type hitNode struct {
|
||||
pass bool
|
||||
|
||||
// For handler nodes.
|
||||
key event.Key
|
||||
key event.Tag
|
||||
}
|
||||
|
||||
type pointerInfo struct {
|
||||
id pointer.ID
|
||||
pressed bool
|
||||
handlers []event.Key
|
||||
handlers []event.Tag
|
||||
}
|
||||
|
||||
type pointerHandler struct {
|
||||
@@ -98,14 +98,14 @@ func (q *pointerQueue) collectHandlers(r *ops.Reader, events *handlerEvents, t o
|
||||
next: node,
|
||||
area: area,
|
||||
pass: pass,
|
||||
key: op.Key,
|
||||
key: op.Tag,
|
||||
})
|
||||
node = len(q.hitTree) - 1
|
||||
h, ok := q.handlers[op.Key]
|
||||
h, ok := q.handlers[op.Tag]
|
||||
if !ok {
|
||||
h = new(pointerHandler)
|
||||
q.handlers[op.Key] = h
|
||||
events.Set(op.Key, []event.Event{pointer.Event{Type: pointer.Cancel}})
|
||||
q.handlers[op.Tag] = h
|
||||
events.Set(op.Tag, []event.Event{pointer.Event{Type: pointer.Cancel}})
|
||||
}
|
||||
h.active = true
|
||||
h.area = area
|
||||
@@ -115,7 +115,7 @@ func (q *pointerQueue) collectHandlers(r *ops.Reader, events *handlerEvents, t o
|
||||
}
|
||||
}
|
||||
|
||||
func (q *pointerQueue) opHit(handlers *[]event.Key, pos f32.Point) {
|
||||
func (q *pointerQueue) opHit(handlers *[]event.Tag, pos f32.Point) {
|
||||
// Track whether we're passing through hits.
|
||||
pass := true
|
||||
idx := len(q.hitTree) - 1
|
||||
@@ -160,7 +160,7 @@ func (a *areaNode) hit(p f32.Point) bool {
|
||||
|
||||
func (q *pointerQueue) init() {
|
||||
if q.handlers == nil {
|
||||
q.handlers = make(map[event.Key]*pointerHandler)
|
||||
q.handlers = make(map[event.Tag]*pointerHandler)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ func (q *pointerQueue) Frame(root *op.Ops, events *handlerEvents) {
|
||||
}
|
||||
}
|
||||
|
||||
func (q *pointerQueue) dropHandler(k event.Key, events *handlerEvents) {
|
||||
func (q *pointerQueue) dropHandler(k event.Tag, events *handlerEvents) {
|
||||
events.Add(k, pointer.Event{Type: pointer.Cancel})
|
||||
q.handlers[k].wantsGrab = false
|
||||
for i := range q.pointers {
|
||||
@@ -278,7 +278,7 @@ func (q *pointerQueue) Push(e pointer.Event, events *handlerEvents) {
|
||||
// evTemplate but with the type specified by evType.
|
||||
//
|
||||
// This is useful for delivering pointer.Enter and pointer.Leave events.
|
||||
func (q *pointerQueue) deliverEventsToMissingHandlers(a, b []event.Key, evType pointer.Type, evTemplate pointer.Event, events *handlerEvents) {
|
||||
func (q *pointerQueue) deliverEventsToMissingHandlers(a, b []event.Tag, evType pointer.Type, evTemplate pointer.Event, events *handlerEvents) {
|
||||
for _, newH := range b {
|
||||
found := false
|
||||
for _, oldH := range a {
|
||||
@@ -350,8 +350,8 @@ func decodePointerInputOp(d []byte, refs []interface{}) pointer.InputOp {
|
||||
panic("invalid op")
|
||||
}
|
||||
return pointer.InputOp{
|
||||
Tag: refs[0].(event.Tag),
|
||||
Grab: d[1] != 0,
|
||||
Key: refs[0].(event.Key),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-10
@@ -22,7 +22,7 @@ func TestPointerDrag(t *testing.T) {
|
||||
Y: 100,
|
||||
},
|
||||
}).Add(&ops)
|
||||
pointer.InputOp{Key: handler}.Add(&ops)
|
||||
pointer.InputOp{Tag: handler}.Add(&ops)
|
||||
|
||||
var r Router
|
||||
r.Frame(&ops)
|
||||
@@ -62,7 +62,7 @@ func TestPointerMove(t *testing.T) {
|
||||
Y: 100,
|
||||
},
|
||||
}).Add(&ops)
|
||||
pointer.InputOp{Key: handler1}.Add(&ops)
|
||||
pointer.InputOp{Tag: handler1}.Add(&ops)
|
||||
// Handler 2 area: (50, 50) - (100, 100) (areas intersect).
|
||||
pointer.Rect(image.Rectangle{
|
||||
Min: image.Point{
|
||||
@@ -74,7 +74,7 @@ func TestPointerMove(t *testing.T) {
|
||||
Y: 200,
|
||||
},
|
||||
}).Add(&ops)
|
||||
pointer.InputOp{Key: handler2}.Add(&ops)
|
||||
pointer.InputOp{Tag: handler2}.Add(&ops)
|
||||
|
||||
var r Router
|
||||
r.Frame(&ops)
|
||||
@@ -146,7 +146,7 @@ func TestPointerEnterLeave(t *testing.T) {
|
||||
Y: 100,
|
||||
},
|
||||
}).Add(&ops)
|
||||
pointer.InputOp{Key: handler1}.Add(&ops)
|
||||
pointer.InputOp{Tag: handler1}.Add(&ops)
|
||||
stack.Pop()
|
||||
|
||||
// Handler 2 area: (50, 50) - (100, 100) (areas intersect).
|
||||
@@ -161,7 +161,7 @@ func TestPointerEnterLeave(t *testing.T) {
|
||||
Y: 200,
|
||||
},
|
||||
}).Add(&ops)
|
||||
pointer.InputOp{Key: handler2}.Add(&ops)
|
||||
pointer.InputOp{Tag: handler2}.Add(&ops)
|
||||
stack.Pop()
|
||||
|
||||
var r Router
|
||||
@@ -264,7 +264,7 @@ func TestPointerEnterLeaveNested(t *testing.T) {
|
||||
Y: 100,
|
||||
},
|
||||
}).Add(&ops)
|
||||
pointer.InputOp{Key: handler1}.Add(&ops)
|
||||
pointer.InputOp{Tag: handler1}.Add(&ops)
|
||||
|
||||
// Handler 2 area: (25, 25) - (75, 75) (nested within first).
|
||||
pointer.Rect(image.Rectangle{
|
||||
@@ -277,7 +277,7 @@ func TestPointerEnterLeaveNested(t *testing.T) {
|
||||
Y: 75,
|
||||
},
|
||||
}).Add(&ops)
|
||||
pointer.InputOp{Key: handler2}.Add(&ops)
|
||||
pointer.InputOp{Tag: handler2}.Add(&ops)
|
||||
|
||||
var r Router
|
||||
r.Frame(&ops)
|
||||
@@ -376,7 +376,7 @@ func TestPointerActiveInputDisappears(t *testing.T) {
|
||||
Y: 100,
|
||||
},
|
||||
}).Add(ops)
|
||||
pointer.InputOp{Key: handler1}.Add(ops)
|
||||
pointer.InputOp{Tag: handler1}.Add(ops)
|
||||
stack.Pop()
|
||||
}
|
||||
|
||||
@@ -458,7 +458,7 @@ func BenchmarkRouterAdd(b *testing.B) {
|
||||
for i := startingHandlerCount; i < maxHandlerCount; i *= 3 {
|
||||
handlerCount := i
|
||||
b.Run(fmt.Sprintf("%d-handlers", i), func(b *testing.B) {
|
||||
handlers := make([]event.Key, handlerCount)
|
||||
handlers := make([]event.Tag, handlerCount)
|
||||
for i := 0; i < handlerCount; i++ {
|
||||
h := new(int)
|
||||
*h = i
|
||||
@@ -473,7 +473,7 @@ func BenchmarkRouterAdd(b *testing.B) {
|
||||
Y: 100,
|
||||
},
|
||||
}).Add(&ops)
|
||||
pointer.InputOp{Key: handlers[i]}.Add(&ops)
|
||||
pointer.InputOp{Tag: handlers[i]}.Add(&ops)
|
||||
}
|
||||
var r Router
|
||||
r.Frame(&ops)
|
||||
|
||||
+10
-10
@@ -39,17 +39,17 @@ type Router struct {
|
||||
|
||||
// ProfileOp summary.
|
||||
profiling bool
|
||||
profHandlers map[event.Key]struct{}
|
||||
profHandlers map[event.Tag]struct{}
|
||||
profile profile.Event
|
||||
}
|
||||
|
||||
type handlerEvents struct {
|
||||
handlers map[event.Key][]event.Event
|
||||
handlers map[event.Tag][]event.Event
|
||||
hadEvents bool
|
||||
}
|
||||
|
||||
// Events returns the available events for the handler key.
|
||||
func (q *Router) Events(k event.Key) []event.Event {
|
||||
func (q *Router) Events(k event.Tag) []event.Event {
|
||||
events := q.handlers.Events(k)
|
||||
if _, isprof := q.profHandlers[k]; isprof {
|
||||
delete(q.profHandlers, k)
|
||||
@@ -111,10 +111,10 @@ func (q *Router) collect() {
|
||||
case opconst.TypeProfile:
|
||||
op := decodeProfileOp(encOp.Data, encOp.Refs)
|
||||
if q.profHandlers == nil {
|
||||
q.profHandlers = make(map[event.Key]struct{})
|
||||
q.profHandlers = make(map[event.Tag]struct{})
|
||||
}
|
||||
q.profiling = true
|
||||
q.profHandlers[op.Key] = struct{}{}
|
||||
q.profHandlers[op.Tag] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -133,17 +133,17 @@ func (q *Router) WakeupTime() (time.Time, bool) {
|
||||
|
||||
func (h *handlerEvents) init() {
|
||||
if h.handlers == nil {
|
||||
h.handlers = make(map[event.Key][]event.Event)
|
||||
h.handlers = make(map[event.Tag][]event.Event)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handlerEvents) Set(k event.Key, evts []event.Event) {
|
||||
func (h *handlerEvents) Set(k event.Tag, evts []event.Event) {
|
||||
h.init()
|
||||
h.handlers[k] = evts
|
||||
h.hadEvents = true
|
||||
}
|
||||
|
||||
func (h *handlerEvents) Add(k event.Key, e event.Event) {
|
||||
func (h *handlerEvents) Add(k event.Tag, e event.Event) {
|
||||
h.init()
|
||||
h.handlers[k] = append(h.handlers[k], e)
|
||||
h.hadEvents = true
|
||||
@@ -155,7 +155,7 @@ func (h *handlerEvents) HadEvents() bool {
|
||||
return u
|
||||
}
|
||||
|
||||
func (h *handlerEvents) Events(k event.Key) []event.Event {
|
||||
func (h *handlerEvents) Events(k event.Tag) []event.Event {
|
||||
if events, ok := h.handlers[k]; ok {
|
||||
h.handlers[k] = h.handlers[k][:0]
|
||||
// Schedule another frame if we delivered events to the user
|
||||
@@ -184,7 +184,7 @@ func decodeProfileOp(d []byte, refs []interface{}) profile.Op {
|
||||
panic("invalid op")
|
||||
}
|
||||
return profile.Op{
|
||||
Key: refs[0].(event.Key),
|
||||
Tag: refs[0].(event.Tag),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -74,7 +74,7 @@ func (c *Context) Px(v unit.Value) int {
|
||||
|
||||
// Events returns the events available for the key. If no
|
||||
// queue is configured, Events returns nil.
|
||||
func (c *Context) Events(k event.Key) []event.Event {
|
||||
func (c *Context) Events(k event.Tag) []event.Event {
|
||||
if c.queue == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
+1
-1
@@ -296,7 +296,7 @@ func (e *Editor) layout(gtx *layout.Context) {
|
||||
e.shapes = append(e.shapes, line{off, path})
|
||||
}
|
||||
|
||||
key.InputOp{Key: &e.eventKey, Focus: e.requestFocus}.Add(gtx.Ops)
|
||||
key.InputOp{Tag: &e.eventKey, Focus: e.requestFocus}.Add(gtx.Ops)
|
||||
e.requestFocus = false
|
||||
pointerPadding := gtx.Px(unit.Dp(4))
|
||||
r := image.Rectangle{Max: e.viewSize}
|
||||
|
||||
Reference in New Issue
Block a user