From 524a5401266f8b30c2210cf147530cbb815b71ed Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Tue, 11 Oct 2022 18:23:19 +0200 Subject: [PATCH 1/2] keyboard: Refactor code Move inline anonymous function to be its own. This method will become asynchronous in following commits. --- js/ui/keyboard.js | 54 +++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/js/ui/keyboard.js b/js/ui/keyboard.js index 895691c34e..28369316e1 100644 --- a/js/ui/keyboard.js +++ b/js/ui/keyboard.js @@ -1510,31 +1510,8 @@ var Keyboard = GObject.registerClass({ button.setWidth(key.width); if (key.action !== 'modifier') { - button.connect('commit', (actor, keyval, str) => { - if (this._modifiers.size === 0 && str !== '' && - keyval && this._oskCompletionEnabled) { - Main.inputMethod.handleVirtualKey(keyval); - return; - } - - if (str === '' || !Main.inputMethod.currentFocus || - (keyval && this._oskCompletionEnabled) || - this._modifiers.size > 0 || - !this._keyboardController.commitString(str, true)) { - if (keyval !== 0) { - this._forwardModifiers(this._modifiers, Clutter.EventType.KEY_PRESS); - this._keyboardController.keyvalPress(keyval); - GLib.timeout_add(GLib.PRIORITY_DEFAULT, KEY_RELEASE_TIMEOUT, () => { - this._keyboardController.keyvalRelease(keyval); - this._forwardModifiers(this._modifiers, Clutter.EventType.KEY_RELEASE); - this._disableAllModifiers(); - return GLib.SOURCE_REMOVE; - }); - } - } - - if (!this._latched) - this._setActiveLayer(0); + button.connect('commit', (_actor, keyval, str) => { + this._commitAction(keyval, str); }); } @@ -1592,6 +1569,33 @@ var Keyboard = GObject.registerClass({ } } + _commitAction(keyval, str) { + if (this._modifiers.size === 0 && str !== '' && + keyval && this._oskCompletionEnabled) { + Main.inputMethod.handleVirtualKey(keyval); + return; + } + + if (str === '' || !Main.inputMethod.currentFocus || + (keyval && this._oskCompletionEnabled) || + this._modifiers.size > 0 || + !this._keyboardController.commitString(str, true)) { + if (keyval !== 0) { + this._forwardModifiers(this._modifiers, Clutter.EventType.KEY_PRESS); + this._keyboardController.keyvalPress(keyval); + GLib.timeout_add(GLib.PRIORITY_DEFAULT, KEY_RELEASE_TIMEOUT, () => { + this._keyboardController.keyvalRelease(keyval); + this._forwardModifiers(this._modifiers, Clutter.EventType.KEY_RELEASE); + this._disableAllModifiers(); + return GLib.SOURCE_REMOVE; + }); + } + } + + if (!this._latched) + this._setActiveLayer(0); + } + _previousWordPosition(text, cursor) { /* Skip word prior to cursor */ let pos = Math.max(0, text.slice(0, cursor).search(/\s+\S+\s*$/)); -- GitLab From 6e997c993e8c392b88cb97644ad604be6dd8028a Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Tue, 11 Oct 2022 18:24:15 +0200 Subject: [PATCH 2/2] inputMethod: Check return value when letting IM handle virtual keys When propagating keys from the OSK, we usually feed these directly to the IBusInputContext and let the IM handle the effects of this virtual key event (which may also include feeding a key event back to us). But these functions may also return a FALSE value if the key was "let through" by the IM, which means the ball is in our yard again, and we are responsible of letting this event get to its destination. If that happens, just fall through, so the string is committed to the client as an UTF-8 string, or propagated through keyboard events. Closes: https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/5930 --- js/misc/inputMethod.js | 19 ++++++++++++++----- js/ui/keyboard.js | 6 +++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/js/misc/inputMethod.js b/js/misc/inputMethod.js index 7bf6646d80..906fe1fc93 100644 --- a/js/misc/inputMethod.js +++ b/js/misc/inputMethod.js @@ -7,6 +7,8 @@ const Main = imports.ui.main; Gio._promisify(IBus.Bus.prototype, 'create_input_context_async', 'create_input_context_async_finish'); +Gio._promisify(IBus.InputContext.prototype, + 'process_key_event_async', 'process_key_event_async_finish'); var HIDE_PANEL_TIME = 50; @@ -329,10 +331,17 @@ var InputMethod = GObject.registerClass({ return this._preeditVisible && this._preeditStr !== '' && this._preeditStr !== null; } - handleVirtualKey(keyval) { - this._context.process_key_event_async( - keyval, 0, 0, -1, null, null); - this._context.process_key_event_async( - keyval, 0, IBus.ModifierType.RELEASE_MASK, -1, null, null); + async handleVirtualKey(keyval) { + try { + if (!await this._context.process_key_event_async( + keyval, 0, 0, -1, null)) + return false; + + await this._context.process_key_event_async( + keyval, 0, IBus.ModifierType.RELEASE_MASK, -1, null); + return true; + } catch (e) { + return false; + } } }); diff --git a/js/ui/keyboard.js b/js/ui/keyboard.js index 28369316e1..3241571de7 100644 --- a/js/ui/keyboard.js +++ b/js/ui/keyboard.js @@ -1569,11 +1569,11 @@ var Keyboard = GObject.registerClass({ } } - _commitAction(keyval, str) { + async _commitAction(keyval, str) { if (this._modifiers.size === 0 && str !== '' && keyval && this._oskCompletionEnabled) { - Main.inputMethod.handleVirtualKey(keyval); - return; + if (await Main.inputMethod.handleVirtualKey(keyval)) + return; } if (str === '' || !Main.inputMethod.currentFocus || -- GitLab