io/key: [API] replace key.InputOp with a filter

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-10-20 09:08:39 -05:00
parent 73c3849da4
commit 27ef6dd7a2
21 changed files with 294 additions and 383 deletions
+35 -7
View File
@@ -39,10 +39,11 @@ type keyHandler struct {
visible bool
new bool
focusable bool
active bool
hint key.InputHint
order int
dirOrder int
filter key.Set
filters []key.Filter
trans f32.Affine2D
}
@@ -116,6 +117,7 @@ func (q *keyQueue) Frame(events *handlerEvents) {
h.new = false
h.visible = false
h.focusable = false
h.active = false
}
q.updateFocusLayout()
}
@@ -255,7 +257,25 @@ func (q *keyQueue) AreaFor(t event.Tag) int {
}
func (q *keyQueue) Accepts(t event.Tag, e key.Event) bool {
return q.handlers[t].filter.Contains(e.Name, e.Modifiers)
for _, f := range q.handlers[t].filters {
if keyFilterMatch(f, e) {
return true
}
}
return false
}
func keyFilterMatch(f key.Filter, e key.Event) bool {
if f.Name != e.Name {
return false
}
if e.Modifiers&f.Required != f.Required {
return false
}
if e.Modifiers&^(f.Required|f.Optional) != 0 {
return false
}
return true
}
func (q *keyQueue) Focus(focus event.Tag, events *handlerEvents) {
@@ -288,6 +308,15 @@ func (q *keyQueue) softKeyboard(show bool) {
}
}
func (q *keyQueue) filter(tag event.Tag, f key.Filter) {
h := q.handlerFor(tag)
if !h.active {
h.active = true
h.filters = h.filters[:0]
}
h.filters = append(h.filters, f)
}
func (q *keyQueue) focusable(tag event.Tag) {
h := q.handlerFor(tag)
h.focusable = true
@@ -305,14 +334,13 @@ func (q *keyQueue) handlerFor(tag event.Tag) *keyHandler {
return h
}
func (q *keyQueue) inputOp(op key.InputOp, t f32.Affine2D, area int, bounds image.Rectangle) {
h := q.handlerFor(op.Tag)
func (q *keyQueue) inputOp(tag event.Tag, t f32.Affine2D, area int, bounds image.Rectangle) {
h := q.handlerFor(tag)
if h.order == -1 {
h.order = len(q.order)
q.order = append(q.order, op.Tag)
q.dirOrder = append(q.dirOrder, dirFocusEntry{tag: op.Tag, area: area, bounds: bounds})
q.order = append(q.order, tag)
q.dirOrder = append(q.dirOrder, dirFocusEntry{tag: tag, area: area, bounds: bounds})
}
h.filter = op.Keys
h.visible = true
h.trans = t
}
+63 -50
View File
@@ -15,10 +15,10 @@ import (
"gioui.org/op/clip"
)
func TestKeyWakeup(t *testing.T) {
func TestInputWakeup(t *testing.T) {
handler := new(int)
var ops op.Ops
key.InputOp{Tag: handler}.Add(&ops)
event.InputOp(&ops, handler)
var r Router
// Test that merely adding a handler doesn't trigger redraw.
@@ -39,12 +39,12 @@ func TestKeyMultiples(t *testing.T) {
r := new(Router)
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
r.Source().Queue(key.FocusCmd{Tag: &handlers[2]})
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
// The last one must be focused:
key.InputOp{Tag: &handlers[2]}.Add(ops)
event.InputOp(ops, &handlers[2])
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
@@ -64,14 +64,14 @@ func TestKeyStacked(t *testing.T) {
ops := new(op.Ops)
r := new(Router)
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
r.Source().Queue(key.FocusCmd{})
r.Source().Queue(key.SoftKeyboardCmd{Show: false})
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
r.Source().Queue(key.FocusCmd{Tag: &handlers[1]})
key.InputOp{Tag: &handlers[2]}.Add(ops)
event.InputOp(ops, &handlers[2])
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
key.InputOp{Tag: &handlers[3]}.Add(ops)
event.InputOp(ops, &handlers[3])
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
@@ -107,35 +107,39 @@ func TestKeyRemoveFocus(t *testing.T) {
r := new(Router)
// New InputOp with Focus and Keyboard:
key.InputOp{Tag: &handlers[0], Keys: "Short-Tab"}.Add(ops)
event.InputOp(ops, &handlers[0])
r.Source().Queue(key.FocusCmd{Tag: &handlers[0]})
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
// New InputOp without any focus:
key.InputOp{Tag: &handlers[1], Keys: "Short-Tab"}.Add(ops)
event.InputOp(ops, &handlers[1])
filters := []event.Filter{
key.FocusFilter{},
key.Filter{Name: key.NameTab, Required: key.ModShortcut},
}
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
r.Events(&handlers[i], filters...)
}
r.Frame(ops)
// Add some key events:
event := event.Event(key.Event{Name: key.NameTab, Modifiers: key.ModShortcut, State: key.Press})
r.Queue(event)
evt := event.Event(key.Event{Name: key.NameTab, Modifiers: key.ModShortcut, State: key.Press})
r.Queue(evt)
assertKeyEvent(t, r.Events(&handlers[0], key.FocusFilter{}), true, event)
assertKeyEvent(t, r.Events(&handlers[1], key.FocusFilter{}), false)
assertKeyEvent(t, r.Events(&handlers[0], filters...), true, evt)
assertKeyEvent(t, r.Events(&handlers[1], filters...), false)
assertFocus(t, r, &handlers[0])
assertKeyboard(t, r, TextInputOpen)
ops.Reset()
// Will get the focus removed:
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
// Unchanged:
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
// Remove focus by focusing on a tag that don't exist.
r.Source().Queue(key.FocusCmd{Tag: new(int)})
@@ -148,9 +152,8 @@ func TestKeyRemoveFocus(t *testing.T) {
ops.Reset()
key.InputOp{Tag: &handlers[0]}.Add(ops)
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[0])
event.InputOp(ops, &handlers[1])
r.Frame(ops)
@@ -164,11 +167,11 @@ func TestKeyRemoveFocus(t *testing.T) {
// Set focus to InputOp which already
// exists in the previous frame:
r.Source().Queue(key.FocusCmd{Tag: &handlers[0]})
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
// Remove focus.
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
r.Source().Queue(key.FocusCmd{})
r.Frame(ops)
@@ -185,11 +188,11 @@ func TestKeyFocusedInvisible(t *testing.T) {
// Set new InputOp with focus:
r.Source().Queue(key.FocusCmd{Tag: &handlers[0]})
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
// Set new InputOp without focus:
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
@@ -209,7 +212,7 @@ func TestKeyFocusedInvisible(t *testing.T) {
//
// Unchanged:
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
r.Frame(ops)
@@ -221,7 +224,7 @@ func TestKeyFocusedInvisible(t *testing.T) {
r.Frame(ops)
// Unchanged
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
r.Frame(ops)
@@ -229,10 +232,10 @@ func TestKeyFocusedInvisible(t *testing.T) {
// Respawn the first element:
// It must receive one `Event{Focus: false}`.
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
// Unchanged
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
@@ -263,7 +266,7 @@ func TestDirectionalFocus(t *testing.T) {
for i, bounds := range handlers {
cl := clip.Rect(bounds).Push(ops)
key.InputOp{Tag: &handlers[i]}.Add(ops)
event.InputOp(ops, &handlers[i])
cl.Pop()
r.Events(&handlers[i], key.FocusFilter{})
}
@@ -307,7 +310,6 @@ func TestFocusScroll(t *testing.T) {
r.Events(h, filters...)
parent := clip.Rect(image.Rect(1, 1, 14, 39)).Push(ops)
cl := clip.Rect(image.Rect(10, -20, 20, 30)).Push(ops)
key.InputOp{Tag: h}.Add(ops)
event.InputOp(ops, h)
// Test that h is scrolled even if behind another handler.
event.InputOp(ops, new(int))
@@ -334,7 +336,6 @@ func TestFocusClick(t *testing.T) {
}
assertEventPointerTypeSequence(t, r.Events(h, filters...), pointer.Cancel)
cl := clip.Rect(image.Rect(0, 0, 10, 10)).Push(ops)
key.InputOp{Tag: h}.Add(ops)
event.InputOp(ops, h)
cl.Pop()
r.Frame(ops)
@@ -359,21 +360,31 @@ func TestKeyRouting(t *testing.T) {
rect := clip.Rect{Max: image.Pt(10, 10)}
macro := op.Record(macroOps)
key.InputOp{Tag: &handlers[0], Keys: "A"}.Add(ops)
event.InputOp(ops, &handlers[0])
cl1 := rect.Push(ops)
key.InputOp{Tag: &handlers[1], Keys: "B"}.Add(ops)
key.InputOp{Tag: &handlers[2], Keys: "A"}.Add(ops)
event.InputOp(ops, &handlers[1])
event.InputOp(ops, &handlers[2])
cl1.Pop()
cl2 := rect.Push(ops)
key.InputOp{Tag: &handlers[3]}.Add(ops)
key.InputOp{Tag: &handlers[4], Keys: "A"}.Add(ops)
event.InputOp(ops, &handlers[3])
event.InputOp(ops, &handlers[4])
cl2.Pop()
call := macro.Stop()
call.Add(ops)
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
fa := []event.Filter{
key.FocusFilter{},
key.Filter{Name: "A"},
}
fb := []event.Filter{
key.FocusFilter{},
key.Filter{Name: "B"},
}
r.Events(&handlers[0], fa...)
r.Events(&handlers[1], fb...)
r.Events(&handlers[2], fa...)
r.Events(&handlers[3], key.FocusFilter{})
r.Events(&handlers[4], fa...)
r.Frame(ops)
@@ -382,17 +393,19 @@ func TestKeyRouting(t *testing.T) {
// With no focus, the events should traverse the final branch of the hit tree
// searching for handlers.
assertKeyEvent(t, r.Events(&handlers[4], key.FocusFilter{}), false, A)
assertKeyEvent(t, r.Events(&handlers[4], fa...), false, A)
assertKeyEvent(t, r.Events(&handlers[3], key.FocusFilter{}), false)
assertKeyEvent(t, r.Events(&handlers[2], key.FocusFilter{}), false)
assertKeyEvent(t, r.Events(&handlers[1], key.FocusFilter{}), false, B)
assertKeyEvent(t, r.Events(&handlers[0], key.FocusFilter{}), false)
assertKeyEvent(t, r.Events(&handlers[2], fa...), false)
assertKeyEvent(t, r.Events(&handlers[1], fb...), false, B)
assertKeyEvent(t, r.Events(&handlers[0], fa...), false)
r2 := new(Router)
for i := range handlers {
r2.Events(&handlers[i], key.FocusFilter{})
}
r2.Events(&handlers[0], fa...)
r2.Events(&handlers[1], fb...)
r2.Events(&handlers[2], fa...)
r2.Events(&handlers[3], key.FocusFilter{})
r2.Events(&handlers[4], fa...)
r2.Source().Queue(key.FocusCmd{Tag: &handlers[3]})
r2.Frame(ops)
@@ -401,11 +414,11 @@ func TestKeyRouting(t *testing.T) {
// With focus, the events should traverse the branch of the hit tree
// containing the focused element.
assertKeyEvent(t, r2.Events(&handlers[4], key.FocusFilter{}), false)
assertKeyEvent(t, r2.Events(&handlers[4], fa...), false)
assertKeyEvent(t, r2.Events(&handlers[3], key.FocusFilter{}), true)
assertKeyEvent(t, r2.Events(&handlers[2], key.FocusFilter{}), false)
assertKeyEvent(t, r2.Events(&handlers[1], key.FocusFilter{}), false)
assertKeyEvent(t, r2.Events(&handlers[0], key.FocusFilter{}), false, A)
assertKeyEvent(t, r2.Events(&handlers[2], fa...), false)
assertKeyEvent(t, r2.Events(&handlers[1], fb...), false)
assertKeyEvent(t, r2.Events(&handlers[0], fa...), false, A)
}
func assertKeyEvent(t *testing.T, events []event.Event, expectedFocus bool, expectedInputs ...event.Event) {
-11
View File
@@ -10,7 +10,6 @@ import (
f32internal "gioui.org/internal/f32"
"gioui.org/internal/ops"
"gioui.org/io/event"
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/io/semantic"
"gioui.org/io/system"
@@ -43,7 +42,6 @@ type hitNode struct {
// For handler nodes.
tag event.Tag
ktag event.Tag
pass bool
}
@@ -262,15 +260,6 @@ func (q *pointerQueue) handlerFor(tag event.Tag, events *handlerEvents) *pointer
return h
}
func (c *pointerCollector) keyInputOp(op key.InputOp) {
areaID := c.currentArea()
c.addHitNode(hitNode{
area: areaID,
ktag: op.Tag,
pass: true,
})
}
func (c *pointerCollector) actionInputOp(act system.Action) {
areaID := c.currentArea()
area := &c.q.areas[areaID]
+1 -1
View File
@@ -1008,7 +1008,7 @@ func TestDeferredInputOp(t *testing.T) {
var r Router
m := op.Record(&ops)
key.InputOp{Tag: new(int)}.Add(&ops)
event.InputOp(&ops, new(int))
call := m.Stop()
op.Defer(&ops, call)
+17 -36
View File
@@ -131,6 +131,8 @@ func (s Source) Events(k event.Tag, filters ...event.Filter) []event.Event {
func (q *Router) Events(k event.Tag, filters ...event.Filter) []event.Event {
for _, f := range filters {
switch f := f.(type) {
case key.Filter:
q.key.queue.filter(k, f)
case key.FocusFilter:
q.key.queue.focusable(k)
case pointer.Filter:
@@ -167,25 +169,6 @@ func (q *Router) Frame(frame *op.Ops) {
}
}
// Queue key events to the topmost handler.
func (q *Router) QueueTopmost(events ...key.Event) bool {
var topmost event.Tag
pq := &q.pointer.queue
for _, h := range pq.hitTree {
if h.ktag != nil {
topmost = h.ktag
break
}
}
if topmost == nil {
return false
}
for _, e := range events {
q.handlers.Add(topmost, e)
}
return q.handlers.HadEvents()
}
// Queue events and report whether at least one handler had an event queued.
func (q *Router) Queue(events ...event.Event) bool {
for _, e := range events {
@@ -273,7 +256,7 @@ func (q *Router) queueKeyEvent(e key.Event) {
if focused {
// If there is a focused tag, traverse its ancestry through the
// hit tree to search for handlers.
for ; pq.hitTree[idx].ktag != f; idx-- {
for ; pq.hitTree[idx].tag != f; idx-- {
}
}
for idx != -1 {
@@ -283,11 +266,11 @@ func (q *Router) queueKeyEvent(e key.Event) {
} else {
idx--
}
if n.ktag == nil {
if n.tag == nil {
continue
}
if kq.Accepts(n.ktag, e) {
q.handlers.Add(n.ktag, e)
if kq.Accepts(n.tag, e) {
q.handlers.Add(n.tag, e)
break
}
}
@@ -479,6 +462,9 @@ func (q *Router) collect() {
case ops.TypeInput:
tag := encOp.Refs[0].(event.Tag)
pc.inputOp(tag, &q.handlers)
a := pc.currentArea()
b := pc.currentAreaBounds()
kq.inputOp(tag, t, a, b)
// Pointer ops.
case ops.TypePass:
@@ -491,17 +477,6 @@ func (q *Router) collect() {
case ops.TypeActionInput:
act := system.Action(encOp.Data[1])
pc.actionInputOp(act)
case ops.TypeKeyInput:
filter := key.Set(*encOp.Refs[1].(*string))
op := key.InputOp{
Tag: encOp.Refs[0].(event.Tag),
Keys: filter,
}
a := pc.currentArea()
b := pc.currentAreaBounds()
pc.keyInputOp(op)
kq.inputOp(op, t, a, b)
case ops.TypeKeyInputHint:
op := key.InputHintOp{
Tag: encOp.Refs[0].(event.Tag),
@@ -583,6 +558,14 @@ func (h *handlerEvents) Events(k event.Tag, filters ...event.Filter) []event.Eve
func filtersMatches(filters []event.Filter, e event.Event) bool {
switch e := e.(type) {
case key.Event:
for _, f := range filters {
if f, ok := f.(key.Filter); ok {
if keyFilterMatch(f, e) {
return true
}
}
}
case key.FocusEvent, key.SnippetEvent, key.EditEvent, key.SelectionEvent:
for _, f := range filters {
if _, ok := f.(key.FocusFilter); ok {
@@ -614,8 +597,6 @@ func filtersMatches(filters []event.Filter, e event.Event) bool {
return true
}
}
default:
return true
}
return false
}
+59 -165
View File
@@ -18,16 +18,15 @@ import (
"gioui.org/op"
)
// InputOp declares a handler ready for key events.
// Key events are in general only delivered to the
// focused key handler.
type InputOp struct {
Tag event.Tag
// Keys is the set of keys Tag can handle. That is, Tag will only
// receive an Event if its key and modifiers are accepted by Keys.Contains.
// As a special case, the topmost (first added) InputOp handler receives all
// unhandled events.
Keys Set
// Filter matches [Event]s.
type Filter struct {
// Required is the set of modifiers that must be included in events matched.
Required Modifiers
// Optional is the set of modifiers that may be included in events matched.
Optional Modifiers
// Name of the key to be matched. As a special case, the empty
// Name matches every key not matched by any other filter.
Name Name
}
// InputHintOp describes the type of text expected by a tag.
@@ -54,22 +53,6 @@ type SnippetCmd struct {
Snippet
}
// Set is an expression that describes a set of key combinations, in the form
// "<modifiers>-<keyset>|...". Modifiers are separated by dashes, optional
// modifiers are enclosed by parentheses. A key set is either a literal key
// name or a list of key names separated by commas and enclosed in brackets.
//
// The "Short" modifier matches the shortcut modifier (ModShortcut) and
// "ShortAlt" matches the alternative modifier (ModShortcutAlt).
//
// Examples:
//
// - A|B matches the A and B keys
// - [A,B] also matches the A and B keys
// - Shift-A matches A key if shift is pressed, and no other modifier.
// - Shift-(Ctrl)-A matches A if shift is pressed, and optionally ctrl.
type Set string
// Range represents a range of text, such as an editor's selection.
// Start and End are in runes.
type Range struct {
@@ -110,11 +93,8 @@ type FocusEvent struct {
// An Event is generated when a key is pressed. For text input
// use EditEvent.
type Event struct {
// Name of the key. For letters, the upper case form is used, via
// unicode.ToUpper. The shift modifier is taken into account, all other
// modifiers are ignored. For example, the "shift-1" and "ctrl-shift-1"
// combinations both give the Name "!" with the US keyboard layout.
Name string
// Name of the key.
Name Name
// Modifiers is the set of active modifiers when the key was pressed.
Modifiers Modifiers
// State is the state of the key when the event was fired.
@@ -184,41 +164,49 @@ const (
ModSuper
)
// Name is the identifier for a keyboard key.
//
// For letters, the upper case form is used, via unicode.ToUpper.
// The shift modifier is taken into account, all other
// modifiers are ignored. For example, the "shift-1" and "ctrl-shift-1"
// combinations both give the Name "!" with the US keyboard layout.
type Name string
const (
// Names for special keys.
NameLeftArrow = "←"
NameRightArrow = "→"
NameUpArrow = "↑"
NameDownArrow = "↓"
NameReturn = "⏎"
NameEnter = "⌤"
NameEscape = "⎋"
NameHome = "⇱"
NameEnd = "⇲"
NameDeleteBackward = "⌫"
NameDeleteForward = "⌦"
NamePageUp = "⇞"
NamePageDown = "⇟"
NameTab = "Tab"
NameSpace = "Space"
NameCtrl = "Ctrl"
NameShift = "Shift"
NameAlt = "Alt"
NameSuper = "Super"
NameCommand = "⌘"
NameF1 = "F1"
NameF2 = "F2"
NameF3 = "F3"
NameF4 = "F4"
NameF5 = "F5"
NameF6 = "F6"
NameF7 = "F7"
NameF8 = "F8"
NameF9 = "F9"
NameF10 = "F10"
NameF11 = "F11"
NameF12 = "F12"
NameBack = "Back"
NameLeftArrow Name = "←"
NameRightArrow Name = "→"
NameUpArrow Name = "↑"
NameDownArrow Name = "↓"
NameReturn Name = "⏎"
NameEnter Name = "⌤"
NameEscape Name = "⎋"
NameHome Name = "⇱"
NameEnd Name = "⇲"
NameDeleteBackward Name = "⌫"
NameDeleteForward Name = "⌦"
NamePageUp Name = "⇞"
NamePageDown Name = "⇟"
NameTab Name = "Tab"
NameSpace Name = "Space"
NameCtrl Name = "Ctrl"
NameShift Name = "Shift"
NameAlt Name = "Alt"
NameSuper Name = "Super"
NameCommand Name = "⌘"
NameF1 Name = "F1"
NameF2 Name = "F2"
NameF3 Name = "F3"
NameF4 Name = "F4"
NameF5 Name = "F5"
NameF6 Name = "F6"
NameF7 Name = "F7"
NameF8 Name = "F8"
NameF9 Name = "F9"
NameF10 Name = "F10"
NameF11 Name = "F11"
NameF12 Name = "F12"
NameBack Name = "Back"
)
type FocusDirection int
@@ -241,105 +229,10 @@ func (m Modifiers) Contain(m2 Modifiers) bool {
// FocusCmd requests to set or clear the keyboard focus.
type FocusCmd struct {
// Tag is the new focus. The focus is cleared if Tag is nil, or if Tag
// has no InputOp in the same frame.
// has no [event.Op] references.
Tag event.Tag
}
func (k Set) Contains(name string, mods Modifiers) bool {
ks := string(k)
for len(ks) > 0 {
// Cut next key expression.
chord, rest, _ := cut(ks, "|")
ks = rest
// Separate key set and modifier set.
var modSet, keySet string
sep := strings.LastIndex(chord, "-")
if sep != -1 {
modSet, keySet = chord[:sep], chord[sep+1:]
} else {
modSet, keySet = "", chord
}
if !keySetContains(keySet, name) {
continue
}
if modSetContains(modSet, mods) {
return true
}
}
return false
}
func keySetContains(keySet, name string) bool {
// Check for single key match.
if keySet == name {
return true
}
// Check for set match.
if len(keySet) < 2 || keySet[0] != '[' || keySet[len(keySet)-1] != ']' {
return false
}
keySet = keySet[1 : len(keySet)-1]
for len(keySet) > 0 {
key, rest, _ := cut(keySet, ",")
keySet = rest
if key == name {
return true
}
}
return false
}
func modSetContains(modSet string, mods Modifiers) bool {
var smods Modifiers
for len(modSet) > 0 {
mod, rest, _ := cut(modSet, "-")
modSet = rest
if len(mod) >= 2 && mod[0] == '(' && mod[len(mod)-1] == ')' {
mods &^= modFor(mod[1 : len(mod)-1])
} else {
smods |= modFor(mod)
}
}
return mods == smods
}
// cut is a copy of the standard library strings.Cut.
// TODO: remove when Go 1.18 is our minimum.
func cut(s, sep string) (before, after string, found bool) {
if i := strings.Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
}
func modFor(name string) Modifiers {
switch name {
case NameCtrl:
return ModCtrl
case NameShift:
return ModShift
case NameAlt:
return ModAlt
case NameSuper:
return ModSuper
case NameCommand:
return ModCommand
case "Short":
return ModShortcut
case "ShortAlt":
return ModShortcutAlt
}
return 0
}
func (h InputOp) Add(o *op.Ops) {
if h.Tag == nil {
panic("Tag must be non-nil")
}
data := ops.Write2String(&o.Internal, ops.TypeKeyInputLen, h.Tag, string(h.Keys))
data[0] = byte(ops.TypeKeyInput)
}
func (h InputHintOp) Add(o *op.Ops) {
if h.Tag == nil {
panic("Tag must be non-nil")
@@ -360,24 +253,25 @@ func (SoftKeyboardCmd) ImplementsCommand() {}
func (SelectionCmd) ImplementsCommand() {}
func (SnippetCmd) ImplementsCommand() {}
func (Filter) ImplementsFilter() {}
func (FocusFilter) ImplementsFilter() {}
func (m Modifiers) String() string {
var strs []string
if m.Contain(ModCtrl) {
strs = append(strs, NameCtrl)
strs = append(strs, string(NameCtrl))
}
if m.Contain(ModCommand) {
strs = append(strs, NameCommand)
strs = append(strs, string(NameCommand))
}
if m.Contain(ModShift) {
strs = append(strs, NameShift)
strs = append(strs, string(NameShift))
}
if m.Contain(ModAlt) {
strs = append(strs, NameAlt)
strs = append(strs, string(NameAlt))
}
if m.Contain(ModSuper) {
strs = append(strs, NameSuper)
strs = append(strs, string(NameSuper))
}
return strings.Join(strs, "-")
}
-35
View File
@@ -1,35 +0,0 @@
// SPDX-License-Identifier: Unlicense OR MIT
package key
import (
"testing"
)
func TestKeySet(t *testing.T) {
const allMods = ModAlt | ModShift | ModSuper | ModCtrl | ModCommand
tests := []struct {
Set Set
Matches []Event
Mismatches []Event
}{
{"A", []Event{{Name: "A"}}, []Event{{Name: "A", Modifiers: ModShift}}},
{"[A,B,C]", []Event{{Name: "A"}, {Name: "B"}}, []Event{}},
{"Short-A", []Event{{Name: "A", Modifiers: ModShortcut}}, []Event{{Name: "A", Modifiers: ModShift}}},
{"(Ctrl)-A", []Event{{Name: "A", Modifiers: ModCtrl}, {Name: "A"}}, []Event{{Name: "A", Modifiers: ModShift}}},
{"Shift-[A,B,C]", []Event{{Name: "A", Modifiers: ModShift}}, []Event{{Name: "B", Modifiers: ModShift | ModCtrl}}},
{Set(allMods.String() + "-A"), []Event{{Name: "A", Modifiers: allMods}}, []Event{}},
}
for _, tst := range tests {
for _, e := range tst.Matches {
if !tst.Set.Contains(e.Name, e.Modifiers) {
t.Errorf("key set %q didn't contain %+v", tst.Set, e)
}
}
for _, e := range tst.Mismatches {
if tst.Set.Contains(e.Name, e.Modifiers) {
t.Errorf("key set %q contains %+v", tst.Set, e)
}
}
}
}