diff --git a/ui/app/internal/input/key.go b/ui/app/internal/input/key.go index 4a63ce06..bbcd7fc2 100644 --- a/ui/app/internal/input/key.go +++ b/ui/app/internal/input/key.go @@ -9,11 +9,13 @@ import ( "gioui.org/ui/key" ) +type TextInputState uint8 + type keyQueue struct { focus input.Key handlers map[input.Key]*keyHandler reader ui.OpsReader - state key.TextInputState + state TextInputState } type keyHandler struct { @@ -29,9 +31,15 @@ const ( priNewFocus ) +const ( + TextInputKeep TextInputState = iota + TextInputClose + TextInputOpen +) + // InputState returns the last text input state as // determined in Frame. -func (q *keyQueue) InputState() key.TextInputState { +func (q *keyQueue) InputState() TextInputState { return q.state } @@ -53,7 +61,6 @@ func (q *keyQueue) Frame(root *ui.Ops, events *handlerEvents) { } } } - changed := focus != nil && focus != q.focus if focus != q.focus { if q.focus != nil { events.Add(q.focus, key.FocusEvent{Focus: false}) @@ -67,13 +74,11 @@ func (q *keyQueue) Frame(root *ui.Ops, events *handlerEvents) { } switch { case pri == priNewFocus: - q.state = key.TextInputOpen + q.state = TextInputOpen case hide: - q.state = key.TextInputClose - case changed: - q.state = key.TextInputFocus + q.state = TextInputClose default: - q.state = key.TextInputKeep + q.state = TextInputKeep } } diff --git a/ui/app/internal/input/router.go b/ui/app/internal/input/router.go index 28434a1e..56ea881b 100644 --- a/ui/app/internal/input/router.go +++ b/ui/app/internal/input/router.go @@ -65,7 +65,7 @@ func (q *Router) Add(e input.Event) bool { return q.handlers.Updated() } -func (q *Router) InputState() key.TextInputState { +func (q *Router) TextInputState() TextInputState { return q.kqueue.InputState() } diff --git a/ui/app/os_android.go b/ui/app/os_android.go index 0742c965..256f48c4 100644 --- a/ui/app/os_android.go +++ b/ui/app/os_android.go @@ -393,20 +393,17 @@ func onTouchEvent(env *C.JNIEnv, class C.jclass, handle C.jlong, action, pointer }) } -func (w *window) setTextInput(s key.TextInputState) { +func (w *window) showTextInput(show bool) { if w.view == 0 { return } - switch s { - case key.TextInputOpen: - runInJVM(func(env *C.JNIEnv) { + runInJVM(func(env *C.JNIEnv) { + if show { C.gio_jni_CallVoidMethod(env, w.view, w.mshowTextInput) - }) - case key.TextInputClose: - runInJVM(func(env *C.JNIEnv) { + } else { C.gio_jni_CallVoidMethod(env, w.view, w.mhideTextInput) - }) - } + } + }) } func Main() { diff --git a/ui/app/os_ios.go b/ui/app/os_ios.go index 9ef63132..a5088a46 100644 --- a/ui/app/os_ios.go +++ b/ui/app/os_ios.go @@ -235,14 +235,13 @@ func (w *window) isVisible() bool { return w.visible.Load().(bool) } -func (w *window) setTextInput(s key.TextInputState) { +func (w *window) showTextInput(show bool) { if w.view == 0 { return } - switch s { - case key.TextInputOpen: + if show { C.gio_showTextInput(w.view) - case key.TextInputClose: + } else { C.gio_hideTextInput(w.view) } } diff --git a/ui/app/os_js.go b/ui/app/os_js.go index b622bdfd..c5e4c28e 100644 --- a/ui/app/os_js.go +++ b/ui/app/os_js.go @@ -317,11 +317,10 @@ func (w *window) setAnimating(anim bool) { w.animating = anim } -func (w *window) setTextInput(s key.TextInputState) { - switch s { - case key.TextInputOpen: +func (w *window) showTextInput(show bool) { + if show { w.focus() - case key.TextInputClose: + } else { w.blur() } } diff --git a/ui/app/os_macos.go b/ui/app/os_macos.go index 703bb425..97666e8d 100644 --- a/ui/app/os_macos.go +++ b/ui/app/os_macos.go @@ -77,7 +77,7 @@ func (w *window) contextView() C.CFTypeRef { return w.view } -func (w *window) setTextInput(s key.TextInputState) {} +func (w *window) showTextInput(show bool) {} func (w *window) setAnimating(anim bool) { var animb C.BOOL diff --git a/ui/app/os_wayland.go b/ui/app/os_wayland.go index a2ba800f..d28d9b37 100644 --- a/ui/app/os_wayland.go +++ b/ui/app/os_wayland.go @@ -1087,7 +1087,7 @@ func (w *window) nativeWindow(visID int) (unsafe.Pointer, int, int) { return unsafe.Pointer(w.surf), width * scale, height * scale } -func (w *window) setTextInput(s key.TextInputState) {} +func (w *window) showTextInput(show bool) {} // detectFontScale reports current font scale, or 1.0 // if it fails. diff --git a/ui/app/os_windows.go b/ui/app/os_windows.go index d599b9af..3e63564c 100644 --- a/ui/app/os_windows.go +++ b/ui/app/os_windows.go @@ -441,7 +441,7 @@ func (w *window) destroy() { } } -func (w *window) setTextInput(s key.TextInputState) {} +func (w *window) showTextInput(show bool) {} func (w *window) display() uintptr { return uintptr(w.hdc) diff --git a/ui/app/window.go b/ui/app/window.go index 04341597..18678760 100644 --- a/ui/app/window.go +++ b/ui/app/window.go @@ -12,7 +12,6 @@ import ( "gioui.org/ui/app/internal/gpu" iinput "gioui.org/ui/app/internal/input" "gioui.org/ui/input" - "gioui.org/ui/key" "gioui.org/ui/system" ) @@ -23,11 +22,10 @@ type WindowOptions struct { } type Window struct { - driver *window - lastFrame time.Time - drawStart time.Time - gpu *gpu.GPU - inputState key.TextInputState + driver *window + lastFrame time.Time + drawStart time.Time + gpu *gpu.GPU out chan Event in chan Event @@ -63,8 +61,8 @@ var _ interface { // setAnimating sets the animation flag. When the window is animating, // DrawEvents are delivered as fast as the display can handle them. setAnimating(anim bool) - // setTextInput updates the virtual keyboard state. - setTextInput(s key.TextInputState) + // showTextInput updates the virtual keyboard state. + showTextInput(show bool) } = (*window)(nil) // Pre-allocate the ack event to avoid garbage. @@ -103,13 +101,6 @@ func (w *Window) Events() <-chan Event { return w.out } -func (w *Window) setTextInput(s key.TextInputState) { - if s != w.inputState && (s == key.TextInputClose || s == key.TextInputOpen) { - w.driver.setTextInput(s) - } - w.inputState = s -} - func (w *Window) Queue() *Queue { return &w.queue } @@ -127,7 +118,12 @@ func (w *Window) draw(size image.Point, frame *ui.Ops) { w.gpu.Draw(w.queue.q.Profiling(), size, frame) w.queue.q.Frame(frame) now := time.Now() - w.setTextInput(w.queue.q.InputState()) + switch w.queue.q.TextInputState() { + case iinput.TextInputOpen: + w.driver.showTextInput(true) + case iinput.TextInputClose: + w.driver.showTextInput(false) + } frameDur := now.Sub(w.lastFrame) frameDur = frameDur.Truncate(100 * time.Microsecond) w.lastFrame = now diff --git a/ui/key/key.go b/ui/key/key.go index 43484933..40259af1 100644 --- a/ui/key/key.go +++ b/ui/key/key.go @@ -30,20 +30,11 @@ type EditEvent struct { type Modifiers uint32 -type TextInputState uint8 - const ( ModCommand Modifiers = 1 << iota ModShift ) -const ( - TextInputKeep TextInputState = iota - TextInputFocus - TextInputClose - TextInputOpen -) - const ( NameLeftArrow = '←' NameRightArrow = '→'