From 21431975de8abedf38b4d82092bd51c4c0e8b5a9 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 28 Feb 2022 14:24:17 +0100 Subject: [PATCH] app,io/router: map Androids' DPAD_CENTER to a click Mapping it to key.NameReturn confuses widgets such as Editor that treats clicks separate from return key presses. Signed-off-by: Elias Naur --- app/os_android.go | 4 +++- app/window.go | 6 ++++++ io/router/key.go | 5 +++++ io/router/router.go | 17 +++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/os_android.go b/app/os_android.go index c2c5a94f..f3576651 100644 --- a/app/os_android.go +++ b/app/os_android.go @@ -903,7 +903,7 @@ func convertKeyCode(code C.jint) (string, bool) { n = key.NameDeleteBackward case C.AKEYCODE_NUMPAD_ENTER: n = key.NameEnter - case C.AKEYCODE_ENTER, C.AKEYCODE_DPAD_CENTER: + case C.AKEYCODE_ENTER: n = key.NameReturn case C.AKEYCODE_CTRL_LEFT, C.AKEYCODE_CTRL_RIGHT: n = key.NameCtrl @@ -932,6 +932,8 @@ func Java_org_gioui_GioView_onKeyEvent(env *C.JNIEnv, class C.jclass, handle C.j w.callbacks.MoveFocus(router.FocusLeft) case C.AKEYCODE_DPAD_RIGHT: w.callbacks.MoveFocus(router.FocusRight) + case C.AKEYCODE_DPAD_CENTER: + w.callbacks.ClickFocus() } } if n, ok := convertKeyCode(keyCode); ok { diff --git a/app/window.go b/app/window.go index 4908882f..1c9da580 100644 --- a/app/window.go +++ b/app/window.go @@ -510,6 +510,12 @@ func (c *callbacks) MoveFocus(dir router.FocusDirection) { c.w.updateAnimation(c.d) } +func (c *callbacks) ClickFocus() { + c.w.queue.q.ClickFocus() + c.w.setNextFrame(time.Time{}) + c.w.updateAnimation(c.d) +} + func (e *editorState) Replace(r key.Range, text string) { if r.Start > r.End { r.Start, r.End = r.End, r.Start diff --git a/io/router/key.go b/io/router/key.go index 713d95d7..8266c1c1 100644 --- a/io/router/key.go +++ b/io/router/key.go @@ -253,6 +253,11 @@ func (q *keyQueue) Push(e event.Event, events *handlerEvents) { } } +func (q *keyQueue) BoundsFor(t event.Tag) f32.Rectangle { + order := q.handlers[t].dirOrder + return q.dirOrder[order].bounds +} + func (q *keyQueue) setFocus(focus event.Tag, events *handlerEvents) { if focus != nil { if _, exists := q.handlers[focus]; !exists { diff --git a/io/router/router.go b/io/router/router.go index 08a1c2a9..9e4bbbe1 100644 --- a/io/router/router.go +++ b/io/router/router.go @@ -152,6 +152,23 @@ func (q *Router) MoveFocus(dir FocusDirection) { q.key.queue.MoveFocus(dir, &q.handlers) } +func (q *Router) ClickFocus() { + focus := q.key.queue.focus + if focus == nil { + return + } + bounds := q.key.queue.BoundsFor(focus) + center := bounds.Max.Add(bounds.Min).Mul(.5) + e := pointer.Event{ + Position: center, + Source: pointer.Touch, + } + e.Type = pointer.Press + q.pointer.queue.Push(e, &q.handlers) + e.Type = pointer.Release + q.pointer.queue.Push(e, &q.handlers) +} + // TextInputState returns the input state from the most recent // call to Frame. func (q *Router) TextInputState() TextInputState {