mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 01:45:36 +00:00
io/key: [API] replace key.InputOp with a filter
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -238,17 +238,17 @@ func (x *Context) UpdateMask(depressed, latched, locked, depressedGroup, latched
|
|||||||
C.xkb_layout_index_t(depressedGroup), C.xkb_layout_index_t(latchedGroup), C.xkb_layout_index_t(lockedGroup))
|
C.xkb_layout_index_t(depressedGroup), C.xkb_layout_index_t(latchedGroup), C.xkb_layout_index_t(lockedGroup))
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertKeysym(s C.xkb_keysym_t) (string, bool) {
|
func convertKeysym(s C.xkb_keysym_t) (key.Name, bool) {
|
||||||
if 'a' <= s && s <= 'z' {
|
if 'a' <= s && s <= 'z' {
|
||||||
return string(rune(s - 'a' + 'A')), true
|
return key.Name(rune(s - 'a' + 'A')), true
|
||||||
}
|
}
|
||||||
if C.XKB_KEY_KP_0 <= s && s <= C.XKB_KEY_KP_9 {
|
if C.XKB_KEY_KP_0 <= s && s <= C.XKB_KEY_KP_9 {
|
||||||
return string(rune(s - C.XKB_KEY_KP_0 + '0')), true
|
return key.Name(rune(s - C.XKB_KEY_KP_0 + '0')), true
|
||||||
}
|
}
|
||||||
if ' ' < s && s <= '~' {
|
if ' ' < s && s <= '~' {
|
||||||
return string(rune(s)), true
|
return key.Name(rune(s)), true
|
||||||
}
|
}
|
||||||
var n string
|
var n key.Name
|
||||||
switch s {
|
switch s {
|
||||||
case C.XKB_KEY_Escape:
|
case C.XKB_KEY_Escape:
|
||||||
n = key.NameEscape
|
n = key.NameEscape
|
||||||
|
|||||||
+2
-2
@@ -899,8 +899,8 @@ func runInJVM(jvm *C.JavaVM, f func(env *C.JNIEnv)) {
|
|||||||
f(env)
|
f(env)
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertKeyCode(code C.jint) (string, bool) {
|
func convertKeyCode(code C.jint) (key.Name, bool) {
|
||||||
var n string
|
var n key.Name
|
||||||
switch code {
|
switch code {
|
||||||
case C.AKEYCODE_FORWARD_DEL:
|
case C.AKEYCODE_FORWARD_DEL:
|
||||||
n = key.NameDeleteForward
|
n = key.NameDeleteForward
|
||||||
|
|||||||
+1
-1
@@ -310,7 +310,7 @@ func (w *window) SetCursor(cursor pointer.Cursor) {
|
|||||||
w.cursor = windowSetCursor(w.cursor, cursor)
|
w.cursor = windowSetCursor(w.cursor, cursor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *window) onKeyCommand(name string) {
|
func (w *window) onKeyCommand(name key.Name) {
|
||||||
w.w.Event(key.Event{
|
w.w.Event(key.Event{
|
||||||
Name: name,
|
Name: name,
|
||||||
})
|
})
|
||||||
|
|||||||
+3
-3
@@ -752,8 +752,8 @@ func osMain() {
|
|||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
func translateKey(k string) (string, bool) {
|
func translateKey(k string) (key.Name, bool) {
|
||||||
var n string
|
var n key.Name
|
||||||
|
|
||||||
switch k {
|
switch k {
|
||||||
case "ArrowUp":
|
case "ArrowUp":
|
||||||
@@ -820,7 +820,7 @@ func translateKey(k string) (string, bool) {
|
|||||||
r, s := utf8.DecodeRuneInString(k)
|
r, s := utf8.DecodeRuneInString(k)
|
||||||
// If there is exactly one printable character, return that.
|
// If there is exactly one printable character, return that.
|
||||||
if s == len(k) && unicode.IsPrint(r) {
|
if s == len(k) && unicode.IsPrint(r) {
|
||||||
return strings.ToUpper(k), true
|
return key.Name(strings.ToUpper(k)), true
|
||||||
}
|
}
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -920,8 +920,8 @@ func osMain() {
|
|||||||
C.gio_main()
|
C.gio_main()
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertKey(k rune) (string, bool) {
|
func convertKey(k rune) (key.Name, bool) {
|
||||||
var n string
|
var n key.Name
|
||||||
switch k {
|
switch k {
|
||||||
case 0x1b:
|
case 0x1b:
|
||||||
n = key.NameEscape
|
n = key.NameEscape
|
||||||
@@ -982,7 +982,7 @@ func convertKey(k rune) (string, bool) {
|
|||||||
if !unicode.IsPrint(k) {
|
if !unicode.IsPrint(k) {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
n = string(k)
|
n = key.Name(k)
|
||||||
}
|
}
|
||||||
return n, true
|
return n, true
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -870,11 +870,11 @@ func (w *window) raise() {
|
|||||||
windows.SWP_NOMOVE|windows.SWP_NOSIZE|windows.SWP_SHOWWINDOW)
|
windows.SWP_NOMOVE|windows.SWP_NOSIZE|windows.SWP_SHOWWINDOW)
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertKeyCode(code uintptr) (string, bool) {
|
func convertKeyCode(code uintptr) (key.Name, bool) {
|
||||||
if '0' <= code && code <= '9' || 'A' <= code && code <= 'Z' {
|
if '0' <= code && code <= '9' || 'A' <= code && code <= 'Z' {
|
||||||
return string(rune(code)), true
|
return key.Name(rune(code)), true
|
||||||
}
|
}
|
||||||
var r string
|
var r key.Name
|
||||||
|
|
||||||
switch code {
|
switch code {
|
||||||
case windows.VK_ESCAPE:
|
case windows.VK_ESCAPE:
|
||||||
|
|||||||
@@ -903,11 +903,6 @@ func (w *Window) processEvent(d driver, e event.Event) bool {
|
|||||||
handled = false
|
handled = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// As a special case, the top-most input handler receives all unhandled
|
|
||||||
// events.
|
|
||||||
if !handled {
|
|
||||||
handled = w.queue.QueueTopmost(e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
w.updateCursor(d)
|
w.updateCursor(d)
|
||||||
if handled {
|
if handled {
|
||||||
|
|||||||
@@ -63,7 +63,6 @@ const (
|
|||||||
TypePass
|
TypePass
|
||||||
TypePopPass
|
TypePopPass
|
||||||
TypeInput
|
TypeInput
|
||||||
TypeKeyInput
|
|
||||||
TypeKeyInputHint
|
TypeKeyInputHint
|
||||||
TypeSave
|
TypeSave
|
||||||
TypeLoad
|
TypeLoad
|
||||||
@@ -140,7 +139,6 @@ const (
|
|||||||
TypePassLen = 1
|
TypePassLen = 1
|
||||||
TypePopPassLen = 1
|
TypePopPassLen = 1
|
||||||
TypeInputLen = 1
|
TypeInputLen = 1
|
||||||
TypeKeyInputLen = 1
|
|
||||||
TypeKeyInputHintLen = 1 + 1
|
TypeKeyInputHintLen = 1 + 1
|
||||||
TypeSaveLen = 1 + 4
|
TypeSaveLen = 1 + 4
|
||||||
TypeLoadLen = 1 + 4
|
TypeLoadLen = 1 + 4
|
||||||
@@ -415,7 +413,6 @@ var opProps = [0x100]opProp{
|
|||||||
TypePass: {Size: TypePassLen, NumRefs: 0},
|
TypePass: {Size: TypePassLen, NumRefs: 0},
|
||||||
TypePopPass: {Size: TypePopPassLen, NumRefs: 0},
|
TypePopPass: {Size: TypePopPassLen, NumRefs: 0},
|
||||||
TypeInput: {Size: TypeInputLen, NumRefs: 1},
|
TypeInput: {Size: TypeInputLen, NumRefs: 1},
|
||||||
TypeKeyInput: {Size: TypeKeyInputLen, NumRefs: 2},
|
|
||||||
TypeKeyInputHint: {Size: TypeKeyInputHintLen, NumRefs: 1},
|
TypeKeyInputHint: {Size: TypeKeyInputHintLen, NumRefs: 1},
|
||||||
TypeSave: {Size: TypeSaveLen, NumRefs: 0},
|
TypeSave: {Size: TypeSaveLen, NumRefs: 0},
|
||||||
TypeLoad: {Size: TypeLoadLen, NumRefs: 0},
|
TypeLoad: {Size: TypeLoadLen, NumRefs: 0},
|
||||||
@@ -478,8 +475,6 @@ func (t OpType) String() string {
|
|||||||
return "PopPass"
|
return "PopPass"
|
||||||
case TypeInput:
|
case TypeInput:
|
||||||
return "Input"
|
return "Input"
|
||||||
case TypeKeyInput:
|
|
||||||
return "KeyInput"
|
|
||||||
case TypeKeyInputHint:
|
case TypeKeyInputHint:
|
||||||
return "KeyInputHint"
|
return "KeyInputHint"
|
||||||
case TypeSave:
|
case TypeSave:
|
||||||
|
|||||||
+35
-7
@@ -39,10 +39,11 @@ type keyHandler struct {
|
|||||||
visible bool
|
visible bool
|
||||||
new bool
|
new bool
|
||||||
focusable bool
|
focusable bool
|
||||||
|
active bool
|
||||||
hint key.InputHint
|
hint key.InputHint
|
||||||
order int
|
order int
|
||||||
dirOrder int
|
dirOrder int
|
||||||
filter key.Set
|
filters []key.Filter
|
||||||
trans f32.Affine2D
|
trans f32.Affine2D
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,6 +117,7 @@ func (q *keyQueue) Frame(events *handlerEvents) {
|
|||||||
h.new = false
|
h.new = false
|
||||||
h.visible = false
|
h.visible = false
|
||||||
h.focusable = false
|
h.focusable = false
|
||||||
|
h.active = false
|
||||||
}
|
}
|
||||||
q.updateFocusLayout()
|
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 {
|
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) {
|
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) {
|
func (q *keyQueue) focusable(tag event.Tag) {
|
||||||
h := q.handlerFor(tag)
|
h := q.handlerFor(tag)
|
||||||
h.focusable = true
|
h.focusable = true
|
||||||
@@ -305,14 +334,13 @@ func (q *keyQueue) handlerFor(tag event.Tag) *keyHandler {
|
|||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *keyQueue) inputOp(op key.InputOp, t f32.Affine2D, area int, bounds image.Rectangle) {
|
func (q *keyQueue) inputOp(tag event.Tag, t f32.Affine2D, area int, bounds image.Rectangle) {
|
||||||
h := q.handlerFor(op.Tag)
|
h := q.handlerFor(tag)
|
||||||
if h.order == -1 {
|
if h.order == -1 {
|
||||||
h.order = len(q.order)
|
h.order = len(q.order)
|
||||||
q.order = append(q.order, op.Tag)
|
q.order = append(q.order, tag)
|
||||||
q.dirOrder = append(q.dirOrder, dirFocusEntry{tag: op.Tag, area: area, bounds: bounds})
|
q.dirOrder = append(q.dirOrder, dirFocusEntry{tag: tag, area: area, bounds: bounds})
|
||||||
}
|
}
|
||||||
h.filter = op.Keys
|
|
||||||
h.visible = true
|
h.visible = true
|
||||||
h.trans = t
|
h.trans = t
|
||||||
}
|
}
|
||||||
|
|||||||
+63
-50
@@ -15,10 +15,10 @@ import (
|
|||||||
"gioui.org/op/clip"
|
"gioui.org/op/clip"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestKeyWakeup(t *testing.T) {
|
func TestInputWakeup(t *testing.T) {
|
||||||
handler := new(int)
|
handler := new(int)
|
||||||
var ops op.Ops
|
var ops op.Ops
|
||||||
key.InputOp{Tag: handler}.Add(&ops)
|
event.InputOp(&ops, handler)
|
||||||
|
|
||||||
var r Router
|
var r Router
|
||||||
// Test that merely adding a handler doesn't trigger redraw.
|
// Test that merely adding a handler doesn't trigger redraw.
|
||||||
@@ -39,12 +39,12 @@ func TestKeyMultiples(t *testing.T) {
|
|||||||
r := new(Router)
|
r := new(Router)
|
||||||
|
|
||||||
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
|
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]})
|
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:
|
// The last one must be focused:
|
||||||
key.InputOp{Tag: &handlers[2]}.Add(ops)
|
event.InputOp(ops, &handlers[2])
|
||||||
|
|
||||||
for i := range handlers {
|
for i := range handlers {
|
||||||
r.Events(&handlers[i], key.FocusFilter{})
|
r.Events(&handlers[i], key.FocusFilter{})
|
||||||
@@ -64,14 +64,14 @@ func TestKeyStacked(t *testing.T) {
|
|||||||
ops := new(op.Ops)
|
ops := new(op.Ops)
|
||||||
r := new(Router)
|
r := new(Router)
|
||||||
|
|
||||||
key.InputOp{Tag: &handlers[0]}.Add(ops)
|
event.InputOp(ops, &handlers[0])
|
||||||
r.Source().Queue(key.FocusCmd{})
|
r.Source().Queue(key.FocusCmd{})
|
||||||
r.Source().Queue(key.SoftKeyboardCmd{Show: false})
|
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]})
|
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})
|
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
|
||||||
key.InputOp{Tag: &handlers[3]}.Add(ops)
|
event.InputOp(ops, &handlers[3])
|
||||||
|
|
||||||
for i := range handlers {
|
for i := range handlers {
|
||||||
r.Events(&handlers[i], key.FocusFilter{})
|
r.Events(&handlers[i], key.FocusFilter{})
|
||||||
@@ -107,35 +107,39 @@ func TestKeyRemoveFocus(t *testing.T) {
|
|||||||
r := new(Router)
|
r := new(Router)
|
||||||
|
|
||||||
// New InputOp with Focus and Keyboard:
|
// 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.FocusCmd{Tag: &handlers[0]})
|
||||||
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
|
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
|
||||||
|
|
||||||
// New InputOp without any focus:
|
// 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 {
|
for i := range handlers {
|
||||||
r.Events(&handlers[i], key.FocusFilter{})
|
r.Events(&handlers[i], filters...)
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Frame(ops)
|
r.Frame(ops)
|
||||||
|
|
||||||
// Add some key events:
|
// Add some key events:
|
||||||
event := event.Event(key.Event{Name: key.NameTab, Modifiers: key.ModShortcut, State: key.Press})
|
evt := event.Event(key.Event{Name: key.NameTab, Modifiers: key.ModShortcut, State: key.Press})
|
||||||
r.Queue(event)
|
r.Queue(evt)
|
||||||
|
|
||||||
assertKeyEvent(t, r.Events(&handlers[0], key.FocusFilter{}), true, event)
|
assertKeyEvent(t, r.Events(&handlers[0], filters...), true, evt)
|
||||||
assertKeyEvent(t, r.Events(&handlers[1], key.FocusFilter{}), false)
|
assertKeyEvent(t, r.Events(&handlers[1], filters...), false)
|
||||||
assertFocus(t, r, &handlers[0])
|
assertFocus(t, r, &handlers[0])
|
||||||
assertKeyboard(t, r, TextInputOpen)
|
assertKeyboard(t, r, TextInputOpen)
|
||||||
|
|
||||||
ops.Reset()
|
ops.Reset()
|
||||||
|
|
||||||
// Will get the focus removed:
|
// Will get the focus removed:
|
||||||
key.InputOp{Tag: &handlers[0]}.Add(ops)
|
event.InputOp(ops, &handlers[0])
|
||||||
|
|
||||||
// Unchanged:
|
// Unchanged:
|
||||||
key.InputOp{Tag: &handlers[1]}.Add(ops)
|
event.InputOp(ops, &handlers[1])
|
||||||
|
|
||||||
// Remove focus by focusing on a tag that don't exist.
|
// Remove focus by focusing on a tag that don't exist.
|
||||||
r.Source().Queue(key.FocusCmd{Tag: new(int)})
|
r.Source().Queue(key.FocusCmd{Tag: new(int)})
|
||||||
@@ -148,9 +152,8 @@ func TestKeyRemoveFocus(t *testing.T) {
|
|||||||
|
|
||||||
ops.Reset()
|
ops.Reset()
|
||||||
|
|
||||||
key.InputOp{Tag: &handlers[0]}.Add(ops)
|
event.InputOp(ops, &handlers[0])
|
||||||
|
event.InputOp(ops, &handlers[1])
|
||||||
key.InputOp{Tag: &handlers[1]}.Add(ops)
|
|
||||||
|
|
||||||
r.Frame(ops)
|
r.Frame(ops)
|
||||||
|
|
||||||
@@ -164,11 +167,11 @@ func TestKeyRemoveFocus(t *testing.T) {
|
|||||||
// Set focus to InputOp which already
|
// Set focus to InputOp which already
|
||||||
// exists in the previous frame:
|
// exists in the previous frame:
|
||||||
r.Source().Queue(key.FocusCmd{Tag: &handlers[0]})
|
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})
|
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
|
||||||
|
|
||||||
// Remove focus.
|
// Remove focus.
|
||||||
key.InputOp{Tag: &handlers[1]}.Add(ops)
|
event.InputOp(ops, &handlers[1])
|
||||||
r.Source().Queue(key.FocusCmd{})
|
r.Source().Queue(key.FocusCmd{})
|
||||||
|
|
||||||
r.Frame(ops)
|
r.Frame(ops)
|
||||||
@@ -185,11 +188,11 @@ func TestKeyFocusedInvisible(t *testing.T) {
|
|||||||
|
|
||||||
// Set new InputOp with focus:
|
// Set new InputOp with focus:
|
||||||
r.Source().Queue(key.FocusCmd{Tag: &handlers[0]})
|
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})
|
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
|
||||||
|
|
||||||
// Set new InputOp without focus:
|
// Set new InputOp without focus:
|
||||||
key.InputOp{Tag: &handlers[1]}.Add(ops)
|
event.InputOp(ops, &handlers[1])
|
||||||
|
|
||||||
for i := range handlers {
|
for i := range handlers {
|
||||||
r.Events(&handlers[i], key.FocusFilter{})
|
r.Events(&handlers[i], key.FocusFilter{})
|
||||||
@@ -209,7 +212,7 @@ func TestKeyFocusedInvisible(t *testing.T) {
|
|||||||
//
|
//
|
||||||
|
|
||||||
// Unchanged:
|
// Unchanged:
|
||||||
key.InputOp{Tag: &handlers[1]}.Add(ops)
|
event.InputOp(ops, &handlers[1])
|
||||||
|
|
||||||
r.Frame(ops)
|
r.Frame(ops)
|
||||||
|
|
||||||
@@ -221,7 +224,7 @@ func TestKeyFocusedInvisible(t *testing.T) {
|
|||||||
r.Frame(ops)
|
r.Frame(ops)
|
||||||
|
|
||||||
// Unchanged
|
// Unchanged
|
||||||
key.InputOp{Tag: &handlers[1]}.Add(ops)
|
event.InputOp(ops, &handlers[1])
|
||||||
|
|
||||||
r.Frame(ops)
|
r.Frame(ops)
|
||||||
|
|
||||||
@@ -229,10 +232,10 @@ func TestKeyFocusedInvisible(t *testing.T) {
|
|||||||
|
|
||||||
// Respawn the first element:
|
// Respawn the first element:
|
||||||
// It must receive one `Event{Focus: false}`.
|
// It must receive one `Event{Focus: false}`.
|
||||||
key.InputOp{Tag: &handlers[0]}.Add(ops)
|
event.InputOp(ops, &handlers[0])
|
||||||
|
|
||||||
// Unchanged
|
// Unchanged
|
||||||
key.InputOp{Tag: &handlers[1]}.Add(ops)
|
event.InputOp(ops, &handlers[1])
|
||||||
|
|
||||||
for i := range handlers {
|
for i := range handlers {
|
||||||
r.Events(&handlers[i], key.FocusFilter{})
|
r.Events(&handlers[i], key.FocusFilter{})
|
||||||
@@ -263,7 +266,7 @@ func TestDirectionalFocus(t *testing.T) {
|
|||||||
|
|
||||||
for i, bounds := range handlers {
|
for i, bounds := range handlers {
|
||||||
cl := clip.Rect(bounds).Push(ops)
|
cl := clip.Rect(bounds).Push(ops)
|
||||||
key.InputOp{Tag: &handlers[i]}.Add(ops)
|
event.InputOp(ops, &handlers[i])
|
||||||
cl.Pop()
|
cl.Pop()
|
||||||
r.Events(&handlers[i], key.FocusFilter{})
|
r.Events(&handlers[i], key.FocusFilter{})
|
||||||
}
|
}
|
||||||
@@ -307,7 +310,6 @@ func TestFocusScroll(t *testing.T) {
|
|||||||
r.Events(h, filters...)
|
r.Events(h, filters...)
|
||||||
parent := clip.Rect(image.Rect(1, 1, 14, 39)).Push(ops)
|
parent := clip.Rect(image.Rect(1, 1, 14, 39)).Push(ops)
|
||||||
cl := clip.Rect(image.Rect(10, -20, 20, 30)).Push(ops)
|
cl := clip.Rect(image.Rect(10, -20, 20, 30)).Push(ops)
|
||||||
key.InputOp{Tag: h}.Add(ops)
|
|
||||||
event.InputOp(ops, h)
|
event.InputOp(ops, h)
|
||||||
// Test that h is scrolled even if behind another handler.
|
// Test that h is scrolled even if behind another handler.
|
||||||
event.InputOp(ops, new(int))
|
event.InputOp(ops, new(int))
|
||||||
@@ -334,7 +336,6 @@ func TestFocusClick(t *testing.T) {
|
|||||||
}
|
}
|
||||||
assertEventPointerTypeSequence(t, r.Events(h, filters...), pointer.Cancel)
|
assertEventPointerTypeSequence(t, r.Events(h, filters...), pointer.Cancel)
|
||||||
cl := clip.Rect(image.Rect(0, 0, 10, 10)).Push(ops)
|
cl := clip.Rect(image.Rect(0, 0, 10, 10)).Push(ops)
|
||||||
key.InputOp{Tag: h}.Add(ops)
|
|
||||||
event.InputOp(ops, h)
|
event.InputOp(ops, h)
|
||||||
cl.Pop()
|
cl.Pop()
|
||||||
r.Frame(ops)
|
r.Frame(ops)
|
||||||
@@ -359,21 +360,31 @@ func TestKeyRouting(t *testing.T) {
|
|||||||
rect := clip.Rect{Max: image.Pt(10, 10)}
|
rect := clip.Rect{Max: image.Pt(10, 10)}
|
||||||
|
|
||||||
macro := op.Record(macroOps)
|
macro := op.Record(macroOps)
|
||||||
key.InputOp{Tag: &handlers[0], Keys: "A"}.Add(ops)
|
event.InputOp(ops, &handlers[0])
|
||||||
cl1 := rect.Push(ops)
|
cl1 := rect.Push(ops)
|
||||||
key.InputOp{Tag: &handlers[1], Keys: "B"}.Add(ops)
|
event.InputOp(ops, &handlers[1])
|
||||||
key.InputOp{Tag: &handlers[2], Keys: "A"}.Add(ops)
|
event.InputOp(ops, &handlers[2])
|
||||||
cl1.Pop()
|
cl1.Pop()
|
||||||
cl2 := rect.Push(ops)
|
cl2 := rect.Push(ops)
|
||||||
key.InputOp{Tag: &handlers[3]}.Add(ops)
|
event.InputOp(ops, &handlers[3])
|
||||||
key.InputOp{Tag: &handlers[4], Keys: "A"}.Add(ops)
|
event.InputOp(ops, &handlers[4])
|
||||||
cl2.Pop()
|
cl2.Pop()
|
||||||
call := macro.Stop()
|
call := macro.Stop()
|
||||||
call.Add(ops)
|
call.Add(ops)
|
||||||
|
|
||||||
for i := range handlers {
|
fa := []event.Filter{
|
||||||
r.Events(&handlers[i], key.FocusFilter{})
|
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)
|
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
|
// With no focus, the events should traverse the final branch of the hit tree
|
||||||
// searching for handlers.
|
// 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[3], key.FocusFilter{}), false)
|
||||||
assertKeyEvent(t, r.Events(&handlers[2], key.FocusFilter{}), false)
|
assertKeyEvent(t, r.Events(&handlers[2], fa...), false)
|
||||||
assertKeyEvent(t, r.Events(&handlers[1], key.FocusFilter{}), false, B)
|
assertKeyEvent(t, r.Events(&handlers[1], fb...), false, B)
|
||||||
assertKeyEvent(t, r.Events(&handlers[0], key.FocusFilter{}), false)
|
assertKeyEvent(t, r.Events(&handlers[0], fa...), false)
|
||||||
|
|
||||||
r2 := new(Router)
|
r2 := new(Router)
|
||||||
|
|
||||||
for i := range handlers {
|
r2.Events(&handlers[0], fa...)
|
||||||
r2.Events(&handlers[i], key.FocusFilter{})
|
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.Source().Queue(key.FocusCmd{Tag: &handlers[3]})
|
||||||
r2.Frame(ops)
|
r2.Frame(ops)
|
||||||
@@ -401,11 +414,11 @@ func TestKeyRouting(t *testing.T) {
|
|||||||
|
|
||||||
// With focus, the events should traverse the branch of the hit tree
|
// With focus, the events should traverse the branch of the hit tree
|
||||||
// containing the focused element.
|
// 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[3], key.FocusFilter{}), true)
|
||||||
assertKeyEvent(t, r2.Events(&handlers[2], key.FocusFilter{}), false)
|
assertKeyEvent(t, r2.Events(&handlers[2], fa...), false)
|
||||||
assertKeyEvent(t, r2.Events(&handlers[1], key.FocusFilter{}), false)
|
assertKeyEvent(t, r2.Events(&handlers[1], fb...), false)
|
||||||
assertKeyEvent(t, r2.Events(&handlers[0], key.FocusFilter{}), false, A)
|
assertKeyEvent(t, r2.Events(&handlers[0], fa...), false, A)
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertKeyEvent(t *testing.T, events []event.Event, expectedFocus bool, expectedInputs ...event.Event) {
|
func assertKeyEvent(t *testing.T, events []event.Event, expectedFocus bool, expectedInputs ...event.Event) {
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
f32internal "gioui.org/internal/f32"
|
f32internal "gioui.org/internal/f32"
|
||||||
"gioui.org/internal/ops"
|
"gioui.org/internal/ops"
|
||||||
"gioui.org/io/event"
|
"gioui.org/io/event"
|
||||||
"gioui.org/io/key"
|
|
||||||
"gioui.org/io/pointer"
|
"gioui.org/io/pointer"
|
||||||
"gioui.org/io/semantic"
|
"gioui.org/io/semantic"
|
||||||
"gioui.org/io/system"
|
"gioui.org/io/system"
|
||||||
@@ -43,7 +42,6 @@ type hitNode struct {
|
|||||||
|
|
||||||
// For handler nodes.
|
// For handler nodes.
|
||||||
tag event.Tag
|
tag event.Tag
|
||||||
ktag event.Tag
|
|
||||||
pass bool
|
pass bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,15 +260,6 @@ func (q *pointerQueue) handlerFor(tag event.Tag, events *handlerEvents) *pointer
|
|||||||
return h
|
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) {
|
func (c *pointerCollector) actionInputOp(act system.Action) {
|
||||||
areaID := c.currentArea()
|
areaID := c.currentArea()
|
||||||
area := &c.q.areas[areaID]
|
area := &c.q.areas[areaID]
|
||||||
|
|||||||
@@ -1008,7 +1008,7 @@ func TestDeferredInputOp(t *testing.T) {
|
|||||||
|
|
||||||
var r Router
|
var r Router
|
||||||
m := op.Record(&ops)
|
m := op.Record(&ops)
|
||||||
key.InputOp{Tag: new(int)}.Add(&ops)
|
event.InputOp(&ops, new(int))
|
||||||
call := m.Stop()
|
call := m.Stop()
|
||||||
|
|
||||||
op.Defer(&ops, call)
|
op.Defer(&ops, call)
|
||||||
|
|||||||
+17
-36
@@ -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 {
|
func (q *Router) Events(k event.Tag, filters ...event.Filter) []event.Event {
|
||||||
for _, f := range filters {
|
for _, f := range filters {
|
||||||
switch f := f.(type) {
|
switch f := f.(type) {
|
||||||
|
case key.Filter:
|
||||||
|
q.key.queue.filter(k, f)
|
||||||
case key.FocusFilter:
|
case key.FocusFilter:
|
||||||
q.key.queue.focusable(k)
|
q.key.queue.focusable(k)
|
||||||
case pointer.Filter:
|
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.
|
// Queue events and report whether at least one handler had an event queued.
|
||||||
func (q *Router) Queue(events ...event.Event) bool {
|
func (q *Router) Queue(events ...event.Event) bool {
|
||||||
for _, e := range events {
|
for _, e := range events {
|
||||||
@@ -273,7 +256,7 @@ func (q *Router) queueKeyEvent(e key.Event) {
|
|||||||
if focused {
|
if focused {
|
||||||
// If there is a focused tag, traverse its ancestry through the
|
// If there is a focused tag, traverse its ancestry through the
|
||||||
// hit tree to search for handlers.
|
// hit tree to search for handlers.
|
||||||
for ; pq.hitTree[idx].ktag != f; idx-- {
|
for ; pq.hitTree[idx].tag != f; idx-- {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for idx != -1 {
|
for idx != -1 {
|
||||||
@@ -283,11 +266,11 @@ func (q *Router) queueKeyEvent(e key.Event) {
|
|||||||
} else {
|
} else {
|
||||||
idx--
|
idx--
|
||||||
}
|
}
|
||||||
if n.ktag == nil {
|
if n.tag == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if kq.Accepts(n.ktag, e) {
|
if kq.Accepts(n.tag, e) {
|
||||||
q.handlers.Add(n.ktag, e)
|
q.handlers.Add(n.tag, e)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -479,6 +462,9 @@ func (q *Router) collect() {
|
|||||||
case ops.TypeInput:
|
case ops.TypeInput:
|
||||||
tag := encOp.Refs[0].(event.Tag)
|
tag := encOp.Refs[0].(event.Tag)
|
||||||
pc.inputOp(tag, &q.handlers)
|
pc.inputOp(tag, &q.handlers)
|
||||||
|
a := pc.currentArea()
|
||||||
|
b := pc.currentAreaBounds()
|
||||||
|
kq.inputOp(tag, t, a, b)
|
||||||
|
|
||||||
// Pointer ops.
|
// Pointer ops.
|
||||||
case ops.TypePass:
|
case ops.TypePass:
|
||||||
@@ -491,17 +477,6 @@ func (q *Router) collect() {
|
|||||||
case ops.TypeActionInput:
|
case ops.TypeActionInput:
|
||||||
act := system.Action(encOp.Data[1])
|
act := system.Action(encOp.Data[1])
|
||||||
pc.actionInputOp(act)
|
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:
|
case ops.TypeKeyInputHint:
|
||||||
op := key.InputHintOp{
|
op := key.InputHintOp{
|
||||||
Tag: encOp.Refs[0].(event.Tag),
|
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 {
|
func filtersMatches(filters []event.Filter, e event.Event) bool {
|
||||||
switch e := e.(type) {
|
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:
|
case key.FocusEvent, key.SnippetEvent, key.EditEvent, key.SelectionEvent:
|
||||||
for _, f := range filters {
|
for _, f := range filters {
|
||||||
if _, ok := f.(key.FocusFilter); ok {
|
if _, ok := f.(key.FocusFilter); ok {
|
||||||
@@ -614,8 +597,6 @@ func filtersMatches(filters []event.Filter, e event.Event) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
+59
-165
@@ -18,16 +18,15 @@ import (
|
|||||||
"gioui.org/op"
|
"gioui.org/op"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InputOp declares a handler ready for key events.
|
// Filter matches [Event]s.
|
||||||
// Key events are in general only delivered to the
|
type Filter struct {
|
||||||
// focused key handler.
|
// Required is the set of modifiers that must be included in events matched.
|
||||||
type InputOp struct {
|
Required Modifiers
|
||||||
Tag event.Tag
|
// Optional is the set of modifiers that may be included in events matched.
|
||||||
// Keys is the set of keys Tag can handle. That is, Tag will only
|
Optional Modifiers
|
||||||
// receive an Event if its key and modifiers are accepted by Keys.Contains.
|
// Name of the key to be matched. As a special case, the empty
|
||||||
// As a special case, the topmost (first added) InputOp handler receives all
|
// Name matches every key not matched by any other filter.
|
||||||
// unhandled events.
|
Name Name
|
||||||
Keys Set
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// InputHintOp describes the type of text expected by a tag.
|
// InputHintOp describes the type of text expected by a tag.
|
||||||
@@ -54,22 +53,6 @@ type SnippetCmd struct {
|
|||||||
Snippet
|
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.
|
// Range represents a range of text, such as an editor's selection.
|
||||||
// Start and End are in runes.
|
// Start and End are in runes.
|
||||||
type Range struct {
|
type Range struct {
|
||||||
@@ -110,11 +93,8 @@ type FocusEvent struct {
|
|||||||
// An Event is generated when a key is pressed. For text input
|
// An Event is generated when a key is pressed. For text input
|
||||||
// use EditEvent.
|
// use EditEvent.
|
||||||
type Event struct {
|
type Event struct {
|
||||||
// Name of the key. For letters, the upper case form is used, via
|
// Name of the key.
|
||||||
// unicode.ToUpper. The shift modifier is taken into account, all other
|
Name Name
|
||||||
// modifiers are ignored. For example, the "shift-1" and "ctrl-shift-1"
|
|
||||||
// combinations both give the Name "!" with the US keyboard layout.
|
|
||||||
Name string
|
|
||||||
// Modifiers is the set of active modifiers when the key was pressed.
|
// Modifiers is the set of active modifiers when the key was pressed.
|
||||||
Modifiers Modifiers
|
Modifiers Modifiers
|
||||||
// State is the state of the key when the event was fired.
|
// State is the state of the key when the event was fired.
|
||||||
@@ -184,41 +164,49 @@ const (
|
|||||||
ModSuper
|
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 (
|
const (
|
||||||
// Names for special keys.
|
// Names for special keys.
|
||||||
NameLeftArrow = "←"
|
NameLeftArrow Name = "←"
|
||||||
NameRightArrow = "→"
|
NameRightArrow Name = "→"
|
||||||
NameUpArrow = "↑"
|
NameUpArrow Name = "↑"
|
||||||
NameDownArrow = "↓"
|
NameDownArrow Name = "↓"
|
||||||
NameReturn = "⏎"
|
NameReturn Name = "⏎"
|
||||||
NameEnter = "⌤"
|
NameEnter Name = "⌤"
|
||||||
NameEscape = "⎋"
|
NameEscape Name = "⎋"
|
||||||
NameHome = "⇱"
|
NameHome Name = "⇱"
|
||||||
NameEnd = "⇲"
|
NameEnd Name = "⇲"
|
||||||
NameDeleteBackward = "⌫"
|
NameDeleteBackward Name = "⌫"
|
||||||
NameDeleteForward = "⌦"
|
NameDeleteForward Name = "⌦"
|
||||||
NamePageUp = "⇞"
|
NamePageUp Name = "⇞"
|
||||||
NamePageDown = "⇟"
|
NamePageDown Name = "⇟"
|
||||||
NameTab = "Tab"
|
NameTab Name = "Tab"
|
||||||
NameSpace = "Space"
|
NameSpace Name = "Space"
|
||||||
NameCtrl = "Ctrl"
|
NameCtrl Name = "Ctrl"
|
||||||
NameShift = "Shift"
|
NameShift Name = "Shift"
|
||||||
NameAlt = "Alt"
|
NameAlt Name = "Alt"
|
||||||
NameSuper = "Super"
|
NameSuper Name = "Super"
|
||||||
NameCommand = "⌘"
|
NameCommand Name = "⌘"
|
||||||
NameF1 = "F1"
|
NameF1 Name = "F1"
|
||||||
NameF2 = "F2"
|
NameF2 Name = "F2"
|
||||||
NameF3 = "F3"
|
NameF3 Name = "F3"
|
||||||
NameF4 = "F4"
|
NameF4 Name = "F4"
|
||||||
NameF5 = "F5"
|
NameF5 Name = "F5"
|
||||||
NameF6 = "F6"
|
NameF6 Name = "F6"
|
||||||
NameF7 = "F7"
|
NameF7 Name = "F7"
|
||||||
NameF8 = "F8"
|
NameF8 Name = "F8"
|
||||||
NameF9 = "F9"
|
NameF9 Name = "F9"
|
||||||
NameF10 = "F10"
|
NameF10 Name = "F10"
|
||||||
NameF11 = "F11"
|
NameF11 Name = "F11"
|
||||||
NameF12 = "F12"
|
NameF12 Name = "F12"
|
||||||
NameBack = "Back"
|
NameBack Name = "Back"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FocusDirection int
|
type FocusDirection int
|
||||||
@@ -241,105 +229,10 @@ func (m Modifiers) Contain(m2 Modifiers) bool {
|
|||||||
// FocusCmd requests to set or clear the keyboard focus.
|
// FocusCmd requests to set or clear the keyboard focus.
|
||||||
type FocusCmd struct {
|
type FocusCmd struct {
|
||||||
// Tag is the new focus. The focus is cleared if Tag is nil, or if Tag
|
// 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
|
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) {
|
func (h InputHintOp) Add(o *op.Ops) {
|
||||||
if h.Tag == nil {
|
if h.Tag == nil {
|
||||||
panic("Tag must be non-nil")
|
panic("Tag must be non-nil")
|
||||||
@@ -360,24 +253,25 @@ func (SoftKeyboardCmd) ImplementsCommand() {}
|
|||||||
func (SelectionCmd) ImplementsCommand() {}
|
func (SelectionCmd) ImplementsCommand() {}
|
||||||
func (SnippetCmd) ImplementsCommand() {}
|
func (SnippetCmd) ImplementsCommand() {}
|
||||||
|
|
||||||
|
func (Filter) ImplementsFilter() {}
|
||||||
func (FocusFilter) ImplementsFilter() {}
|
func (FocusFilter) ImplementsFilter() {}
|
||||||
|
|
||||||
func (m Modifiers) String() string {
|
func (m Modifiers) String() string {
|
||||||
var strs []string
|
var strs []string
|
||||||
if m.Contain(ModCtrl) {
|
if m.Contain(ModCtrl) {
|
||||||
strs = append(strs, NameCtrl)
|
strs = append(strs, string(NameCtrl))
|
||||||
}
|
}
|
||||||
if m.Contain(ModCommand) {
|
if m.Contain(ModCommand) {
|
||||||
strs = append(strs, NameCommand)
|
strs = append(strs, string(NameCommand))
|
||||||
}
|
}
|
||||||
if m.Contain(ModShift) {
|
if m.Contain(ModShift) {
|
||||||
strs = append(strs, NameShift)
|
strs = append(strs, string(NameShift))
|
||||||
}
|
}
|
||||||
if m.Contain(ModAlt) {
|
if m.Contain(ModAlt) {
|
||||||
strs = append(strs, NameAlt)
|
strs = append(strs, string(NameAlt))
|
||||||
}
|
}
|
||||||
if m.Contain(ModSuper) {
|
if m.Contain(ModSuper) {
|
||||||
strs = append(strs, NameSuper)
|
strs = append(strs, string(NameSuper))
|
||||||
}
|
}
|
||||||
return strings.Join(strs, "-")
|
return strings.Join(strs, "-")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+10
-9
@@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gioui.org/gesture"
|
"gioui.org/gesture"
|
||||||
|
"gioui.org/io/event"
|
||||||
"gioui.org/io/key"
|
"gioui.org/io/key"
|
||||||
"gioui.org/io/pointer"
|
"gioui.org/io/pointer"
|
||||||
"gioui.org/io/semantic"
|
"gioui.org/io/semantic"
|
||||||
@@ -25,7 +26,7 @@ type Clickable struct {
|
|||||||
keyTag struct{}
|
keyTag struct{}
|
||||||
requestClicks int
|
requestClicks int
|
||||||
focused bool
|
focused bool
|
||||||
pressedKey string
|
pressedKey key.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// Click represents a click.
|
// Click represents a click.
|
||||||
@@ -102,13 +103,7 @@ func (b *Clickable) Layout(gtx layout.Context, w layout.Widget) layout.Dimension
|
|||||||
defer clip.Rect(image.Rectangle{Max: dims.Size}).Push(gtx.Ops).Pop()
|
defer clip.Rect(image.Rectangle{Max: dims.Size}).Push(gtx.Ops).Pop()
|
||||||
semantic.EnabledOp(gtx.Enabled()).Add(gtx.Ops)
|
semantic.EnabledOp(gtx.Enabled()).Add(gtx.Ops)
|
||||||
b.click.Add(gtx.Ops)
|
b.click.Add(gtx.Ops)
|
||||||
if gtx.Enabled() {
|
event.InputOp(gtx.Ops, &b.keyTag)
|
||||||
keys := key.Set("⏎|Space")
|
|
||||||
if !b.focused {
|
|
||||||
keys = ""
|
|
||||||
}
|
|
||||||
key.InputOp{Tag: &b.keyTag, Keys: keys}.Add(gtx.Ops)
|
|
||||||
}
|
|
||||||
c.Add(gtx.Ops)
|
c.Add(gtx.Ops)
|
||||||
return dims
|
return dims
|
||||||
}
|
}
|
||||||
@@ -162,7 +157,13 @@ func (b *Clickable) Update(gtx layout.Context) []Click {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, e := range gtx.Events(&b.keyTag, key.FocusFilter{}) {
|
filters := []event.Filter{
|
||||||
|
key.FocusFilter{},
|
||||||
|
}
|
||||||
|
if b.focused {
|
||||||
|
filters = append(filters, key.Filter{Name: key.NameReturn}, key.Filter{Name: key.NameSpace})
|
||||||
|
}
|
||||||
|
for _, e := range gtx.Events(&b.keyTag, filters...) {
|
||||||
switch e := e.(type) {
|
switch e := e.(type) {
|
||||||
case key.FocusEvent:
|
case key.FocusEvent:
|
||||||
b.focused = e.Focus
|
b.focused = e.Focus
|
||||||
|
|||||||
+50
-28
@@ -330,9 +330,57 @@ func (e *Editor) processKey(gtx layout.Context) {
|
|||||||
if e.text.Changed() {
|
if e.text.Changed() {
|
||||||
e.events = append(e.events, ChangeEvent{})
|
e.events = append(e.events, ChangeEvent{})
|
||||||
}
|
}
|
||||||
|
filters := []event.Filter{key.FocusFilter{}, transfer.TargetFilter{Type: "application/text"}}
|
||||||
|
if e.focused {
|
||||||
|
filters = append(filters,
|
||||||
|
key.Filter{Name: key.NameEnter, Optional: key.ModShift},
|
||||||
|
key.Filter{Name: key.NameReturn, Optional: key.ModShift},
|
||||||
|
|
||||||
|
key.Filter{Name: "Z", Required: key.ModShortcut, Optional: key.ModShift},
|
||||||
|
key.Filter{Name: "C", Required: key.ModShortcut},
|
||||||
|
key.Filter{Name: "V", Required: key.ModShortcut},
|
||||||
|
key.Filter{Name: "X", Required: key.ModShortcut},
|
||||||
|
key.Filter{Name: "A", Required: key.ModShortcut},
|
||||||
|
|
||||||
|
key.Filter{Name: key.NameDeleteBackward, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
key.Filter{Name: key.NameDeleteForward, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
|
||||||
|
key.Filter{Name: key.NameHome, Optional: key.ModShift},
|
||||||
|
key.Filter{Name: key.NameEnd, Optional: key.ModShift},
|
||||||
|
key.Filter{Name: key.NamePageDown, Optional: key.ModShift},
|
||||||
|
key.Filter{Name: key.NamePageUp, Optional: key.ModShift},
|
||||||
|
)
|
||||||
|
caret, _ := e.text.Selection()
|
||||||
|
if caret > 0 {
|
||||||
|
if gtx.Locale.Direction.Progression() == system.FromOrigin {
|
||||||
|
filters = append(filters,
|
||||||
|
key.Filter{Name: key.NameLeftArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
key.Filter{Name: key.NameUpArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
filters = append(filters,
|
||||||
|
key.Filter{Name: key.NameRightArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
key.Filter{Name: key.NameDownArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if caret < e.text.Len() {
|
||||||
|
if gtx.Locale.Direction.Progression() == system.FromOrigin {
|
||||||
|
filters = append(filters,
|
||||||
|
key.Filter{Name: key.NameRightArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
key.Filter{Name: key.NameDownArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
filters = append(filters,
|
||||||
|
key.Filter{Name: key.NameLeftArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
key.Filter{Name: key.NameUpArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// adjust keeps track of runes dropped because of MaxLen.
|
// adjust keeps track of runes dropped because of MaxLen.
|
||||||
var adjust int
|
var adjust int
|
||||||
for _, ke := range gtx.Events(&e.eventKey, transfer.TargetFilter{Type: "application/text"}, key.FocusFilter{}) {
|
for _, ke := range gtx.Events(&e.eventKey, filters...) {
|
||||||
e.blinkStart = gtx.Now
|
e.blinkStart = gtx.Now
|
||||||
switch ke := ke.(type) {
|
switch ke := ke.(type) {
|
||||||
case key.FocusEvent:
|
case key.FocusEvent:
|
||||||
@@ -625,33 +673,7 @@ func (e *Editor) layout(gtx layout.Context, textMaterial, selectMaterial op.Call
|
|||||||
|
|
||||||
defer clip.Rect(image.Rectangle{Max: visibleDims.Size}).Push(gtx.Ops).Pop()
|
defer clip.Rect(image.Rectangle{Max: visibleDims.Size}).Push(gtx.Ops).Pop()
|
||||||
pointer.CursorText.Add(gtx.Ops)
|
pointer.CursorText.Add(gtx.Ops)
|
||||||
var keys key.Set
|
event.InputOp(gtx.Ops, &e.eventKey)
|
||||||
if e.focused {
|
|
||||||
const keyFilterNoLeftUp = "(ShortAlt)-(Shift)-[→,↓]|(Shift)-[⏎,⌤]|(ShortAlt)-(Shift)-[⌫,⌦]|(Shift)-[⇞,⇟,⇱,⇲]|Short-[C,V,X,A]|Short-(Shift)-Z"
|
|
||||||
const keyFilterNoRightDown = "(ShortAlt)-(Shift)-[←,↑]|(Shift)-[⏎,⌤]|(ShortAlt)-(Shift)-[⌫,⌦]|(Shift)-[⇞,⇟,⇱,⇲]|Short-[C,V,X,A]|Short-(Shift)-Z"
|
|
||||||
const keyFilterNoArrows = "(Shift)-[⏎,⌤]|(ShortAlt)-(Shift)-[⌫,⌦]|(Shift)-[⇞,⇟,⇱,⇲]|Short-[C,V,X,A]|Short-(Shift)-Z"
|
|
||||||
const keyFilterAllArrows = "(ShortAlt)-(Shift)-[←,→,↑,↓]|(Shift)-[⏎,⌤]|(ShortAlt)-(Shift)-[⌫,⌦]|(Shift)-[⇞,⇟,⇱,⇲]|Short-[C,V,X,A]|Short-(Shift)-Z"
|
|
||||||
caret, _ := e.text.Selection()
|
|
||||||
switch {
|
|
||||||
case caret == 0 && caret == e.text.Len():
|
|
||||||
keys = keyFilterNoArrows
|
|
||||||
case caret == 0:
|
|
||||||
if gtx.Locale.Direction.Progression() == system.FromOrigin {
|
|
||||||
keys = keyFilterNoLeftUp
|
|
||||||
} else {
|
|
||||||
keys = keyFilterNoRightDown
|
|
||||||
}
|
|
||||||
case caret == e.text.Len():
|
|
||||||
if gtx.Locale.Direction.Progression() == system.FromOrigin {
|
|
||||||
keys = keyFilterNoRightDown
|
|
||||||
} else {
|
|
||||||
keys = keyFilterNoLeftUp
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
keys = keyFilterAllArrows
|
|
||||||
}
|
|
||||||
}
|
|
||||||
key.InputOp{Tag: &e.eventKey, Keys: keys}.Add(gtx.Ops)
|
|
||||||
key.InputHintOp{Tag: &e.eventKey, Hint: e.InputHint}.Add(gtx.Ops)
|
key.InputHintOp{Tag: &e.eventKey, Hint: e.InputHint}.Add(gtx.Ops)
|
||||||
|
|
||||||
e.scroller.Add(gtx.Ops)
|
e.scroller.Add(gtx.Ops)
|
||||||
|
|||||||
@@ -124,6 +124,9 @@ func TestEditorReadOnly(t *testing.T) {
|
|||||||
gtx.Ops.Reset()
|
gtx.Ops.Reset()
|
||||||
layoutEditor()
|
layoutEditor()
|
||||||
r.Frame(gtx.Ops)
|
r.Frame(gtx.Ops)
|
||||||
|
gtx.Ops.Reset()
|
||||||
|
layoutEditor()
|
||||||
|
r.Frame(gtx.Ops)
|
||||||
|
|
||||||
// Select everything.
|
// Select everything.
|
||||||
gtx.Ops.Reset()
|
gtx.Ops.Reset()
|
||||||
@@ -1005,8 +1008,11 @@ func TestSelectMove(t *testing.T) {
|
|||||||
gtx.Ops.Reset()
|
gtx.Ops.Reset()
|
||||||
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
|
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
|
||||||
r.Frame(gtx.Ops)
|
r.Frame(gtx.Ops)
|
||||||
|
gtx.Ops.Reset()
|
||||||
|
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
|
||||||
|
r.Frame(gtx.Ops)
|
||||||
|
|
||||||
for _, keyName := range []string{key.NameLeftArrow, key.NameRightArrow, key.NameUpArrow, key.NameDownArrow} {
|
for _, keyName := range []key.Name{key.NameLeftArrow, key.NameRightArrow, key.NameUpArrow, key.NameDownArrow} {
|
||||||
// Select 345
|
// Select 345
|
||||||
e.SetCaret(3, 6)
|
e.SetCaret(3, 6)
|
||||||
if expected, got := "345", e.SelectedText(); expected != got {
|
if expected, got := "345", e.SelectedText(); expected != got {
|
||||||
|
|||||||
+10
-5
@@ -4,6 +4,7 @@ package widget
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"gioui.org/gesture"
|
"gioui.org/gesture"
|
||||||
|
"gioui.org/io/event"
|
||||||
"gioui.org/io/key"
|
"gioui.org/io/key"
|
||||||
"gioui.org/io/pointer"
|
"gioui.org/io/pointer"
|
||||||
"gioui.org/io/semantic"
|
"gioui.org/io/semantic"
|
||||||
@@ -59,7 +60,13 @@ func (e *Enum) Update(gtx layout.Context) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, ev := range gtx.Events(&state.tag, key.FocusFilter{}) {
|
filters := []event.Filter{
|
||||||
|
key.FocusFilter{},
|
||||||
|
}
|
||||||
|
if e.focused && e.focus == state.key {
|
||||||
|
filters = append(filters, key.Filter{Name: key.NameReturn}, key.Filter{Name: key.NameSpace})
|
||||||
|
}
|
||||||
|
for _, ev := range gtx.Events(&state.tag, filters...) {
|
||||||
switch ev := ev.(type) {
|
switch ev := ev.(type) {
|
||||||
case key.FocusEvent:
|
case key.FocusEvent:
|
||||||
if ev.Focus {
|
if ev.Focus {
|
||||||
@@ -69,7 +76,7 @@ func (e *Enum) Update(gtx layout.Context) bool {
|
|||||||
e.focused = false
|
e.focused = false
|
||||||
}
|
}
|
||||||
case key.Event:
|
case key.Event:
|
||||||
if !e.focused || ev.State != key.Release {
|
if ev.State != key.Release {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if ev.Name != key.NameReturn && ev.Name != key.NameSpace {
|
if ev.Name != key.NameReturn && ev.Name != key.NameSpace {
|
||||||
@@ -117,9 +124,7 @@ func (e *Enum) Layout(gtx layout.Context, k string, content layout.Widget) layou
|
|||||||
}
|
}
|
||||||
clk := &state.click
|
clk := &state.click
|
||||||
clk.Add(gtx.Ops)
|
clk.Add(gtx.Ops)
|
||||||
if gtx.Enabled() {
|
event.InputOp(gtx.Ops, &state.tag)
|
||||||
key.InputOp{Tag: &state.tag, Keys: "⏎|Space"}.Add(gtx.Ops)
|
|
||||||
}
|
|
||||||
semantic.SelectedOp(k == e.Value).Add(gtx.Ops)
|
semantic.SelectedOp(k == e.Value).Add(gtx.Ops)
|
||||||
semantic.EnabledOp(gtx.Enabled()).Add(gtx.Ops)
|
semantic.EnabledOp(gtx.Enabled()).Add(gtx.Ops)
|
||||||
c.Add(gtx.Ops)
|
c.Add(gtx.Ops)
|
||||||
|
|||||||
+22
-7
@@ -204,12 +204,7 @@ func (l *Selectable) Layout(gtx layout.Context, lt *text.Shaper, font font.Font,
|
|||||||
dims := l.text.Dimensions()
|
dims := l.text.Dimensions()
|
||||||
defer clip.Rect(image.Rectangle{Max: dims.Size}).Push(gtx.Ops).Pop()
|
defer clip.Rect(image.Rectangle{Max: dims.Size}).Push(gtx.Ops).Pop()
|
||||||
pointer.CursorText.Add(gtx.Ops)
|
pointer.CursorText.Add(gtx.Ops)
|
||||||
var keys key.Set
|
event.InputOp(gtx.Ops, l)
|
||||||
if l.focused {
|
|
||||||
const keyFilter = "(ShortAlt)-(Shift)-[←,→,↑,↓]|(Shift)-[⇞,⇟,⇱,⇲]|Short-[C,X,A]"
|
|
||||||
keys = keyFilter
|
|
||||||
}
|
|
||||||
key.InputOp{Tag: l, Keys: keys}.Add(gtx.Ops)
|
|
||||||
|
|
||||||
l.clicker.Add(gtx.Ops)
|
l.clicker.Add(gtx.Ops)
|
||||||
l.dragger.Add(gtx.Ops)
|
l.dragger.Add(gtx.Ops)
|
||||||
@@ -304,7 +299,27 @@ func (e *Selectable) clickDragEvents(gtx layout.Context) []event.Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Selectable) processKey(gtx layout.Context) {
|
func (e *Selectable) processKey(gtx layout.Context) {
|
||||||
for _, ke := range gtx.Events(e, key.FocusFilter{}) {
|
filters := []event.Filter{
|
||||||
|
key.FocusFilter{},
|
||||||
|
}
|
||||||
|
if e.focused {
|
||||||
|
filters = append(filters,
|
||||||
|
key.Filter{Name: key.NameLeftArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
key.Filter{Name: key.NameRightArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
key.Filter{Name: key.NameUpArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
key.Filter{Name: key.NameDownArrow, Optional: key.ModShortcutAlt | key.ModShift},
|
||||||
|
|
||||||
|
key.Filter{Name: key.NamePageUp, Optional: key.ModShift},
|
||||||
|
key.Filter{Name: key.NamePageDown, Optional: key.ModShift},
|
||||||
|
key.Filter{Name: key.NameEnd, Optional: key.ModShift},
|
||||||
|
key.Filter{Name: key.NameHome, Optional: key.ModShift},
|
||||||
|
|
||||||
|
key.Filter{Name: "C", Required: key.ModShortcut},
|
||||||
|
key.Filter{Name: "X", Required: key.ModShortcut},
|
||||||
|
key.Filter{Name: "A", Required: key.ModShortcut},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
for _, ke := range gtx.Events(e, filters...) {
|
||||||
switch ke := ke.(type) {
|
switch ke := ke.(type) {
|
||||||
case key.FocusEvent:
|
case key.FocusEvent:
|
||||||
e.focused = ke.Focus
|
e.focused = ke.Focus
|
||||||
|
|||||||
@@ -57,8 +57,10 @@ func TestSelectableMove(t *testing.T) {
|
|||||||
s.SetCaret(3, 6)
|
s.SetCaret(3, 6)
|
||||||
s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{})
|
s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{})
|
||||||
r.Frame(gtx.Ops)
|
r.Frame(gtx.Ops)
|
||||||
|
s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{})
|
||||||
|
r.Frame(gtx.Ops)
|
||||||
|
|
||||||
for _, keyName := range []string{key.NameLeftArrow, key.NameRightArrow, key.NameUpArrow, key.NameDownArrow} {
|
for _, keyName := range []key.Name{key.NameLeftArrow, key.NameRightArrow, key.NameUpArrow, key.NameDownArrow} {
|
||||||
// Select 345
|
// Select 345
|
||||||
s.SetCaret(3, 6)
|
s.SetCaret(3, 6)
|
||||||
if start, end := s.Selection(); start != 3 || end != 6 {
|
if start, end := s.Selection(); start != 3 || end != 6 {
|
||||||
|
|||||||
Reference in New Issue
Block a user