[gnome-shell/wip/carlosg/osk-updates: 6/11] ibusManager: Add OSK completion mode




commit 1592c98907180f93d8859feea427d0caf3bd3090
Author: Carlos Garnacho <carlosg gnome org>
Date:   Fri Apr 22 13:28:02 2022 +0200

    ibusManager: Add OSK completion mode
    
    This mode changes the current IBus engine to ibus-typing-booster
    under the rug (i.e. no changes in keyboard status menu) for any
    XKB engine selected.
    
    In order to make it useful for the currently selected language,
    the typing-booster dictionary is changed to the current XKB
    layout language. And since the OSK has its own emoji panel,
    typing-boosters own emoji completion is disabled.
    
    These changes only apply as long as the OSK panel is shown,
    reverting to the original engine and typing-booster configuration
    after it is hidden. This in theory also caters for users that
    do have ibus-typing-booster enabled as an input source.
    
    The final effect is text prediction for the language that is
    being typed, according to the OSK layout, given that
    ibus-typing-booster and the relevant hunspell dictionaries are
    used.

 js/misc/ibusManager.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
 js/misc/inputMethod.js |  7 +++++++
 js/ui/keyboard.js      | 10 ++++++++++
 3 files changed, 64 insertions(+), 1 deletion(-)
---
diff --git a/js/misc/ibusManager.js b/js/misc/ibusManager.js
index 55c0d308b4..f996f66376 100644
--- a/js/misc/ibusManager.js
+++ b/js/misc/ibusManager.js
@@ -22,6 +22,11 @@ _checkIBusVersion(1, 5, 2);
 let _ibusManager = null;
 const IBUS_SYSTEMD_SERVICE = 'org.freedesktop.IBus.session.GNOME.service';
 
+const TYPING_BOOSTER_ENGINE = 'typing-booster';
+const IBUS_TYPING_BOOSTER_SCHEMA = 'org.freedesktop.ibus.engine.typing-booster';
+const KEY_EMOJIPREDICTIONS = 'emojipredictions';
+const KEY_DICTIONARY = 'dictionary';
+
 function _checkIBusVersion(requiredMajor, requiredMinor, requiredMicro) {
     if ((IBus.MAJOR_VERSION > requiredMajor) ||
         (IBus.MAJOR_VERSION == requiredMajor && IBus.MINOR_VERSION > requiredMinor) ||
@@ -285,9 +290,12 @@ var IBusManager = class {
     }
 
     preloadEngines(ids) {
-        if (!this._ibus || ids.length == 0)
+        if (!this._ibus || !this._ready)
             return;
 
+        if (!ids.includes(TYPING_BOOSTER_ENGINE))
+            ids.push(TYPING_BOOSTER_ENGINE);
+
         if (this._preloadEnginesId != 0) {
             GLib.source_remove(this._preloadEnginesId);
             this._preloadEnginesId = 0;
@@ -307,5 +315,43 @@ var IBusManager = class {
                     return GLib.SOURCE_REMOVE;
                 });
     }
+
+    toggleOskCompletion(enabled) {
+        /* Needs typing-booster available */
+        if (!this._engines.has(TYPING_BOOSTER_ENGINE))
+            return false;
+        /* Can do only on xkb engines */
+        if (enabled && !this._currentEngineName.startsWith('xkb:'))
+            return false;
+        /* Avoid modifications if typing booster is already active */
+        if (enabled && this._currentEngineName === TYPING_BOOSTER_ENGINE)
+            return true;
+
+        if (this._oskCompletion === enabled)
+            return true;
+
+        this._oskCompletion = enabled;
+        let settings =
+            new Gio.Settings({ schema_id: IBUS_TYPING_BOOSTER_SCHEMA });
+
+        if (enabled) {
+            this._preOskState = {
+                'engine': this._currentEngineName,
+                'emoji': settings.get_value(KEY_EMOJIPREDICTIONS),
+                'langs': settings.get_value(KEY_DICTIONARY),
+            };
+            settings.set_boolean(KEY_EMOJIPREDICTIONS, false);
+            settings.set_string(
+                KEY_DICTIONARY, GLib.get_language_names().join(','));
+            this.setEngine(TYPING_BOOSTER_ENGINE);
+        } else if (this._preOskState) {
+            const { engine, emoji, langs } = this._preOskState;
+            this._preOskState = null;
+            this.setEngine(engine);
+            settings.set_value(KEY_EMOJIPREDICTIONS, emoji);
+            settings.set_value(KEY_DICTIONARY, langs);
+        }
+        return true;
+    }
 };
 Signals.addSignalMethods(IBusManager.prototype);
diff --git a/js/misc/inputMethod.js b/js/misc/inputMethod.js
index f22d7ad0c6..f207f289de 100644
--- a/js/misc/inputMethod.js
+++ b/js/misc/inputMethod.js
@@ -299,4 +299,11 @@ var InputMethod = GObject.registerClass({
     hasPreedit() {
         return this._preeditStr !== '';
     }
+
+    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);
+    }
 });
diff --git a/js/ui/keyboard.js b/js/ui/keyboard.js
index 3a33d2189e..59d4793c71 100644
--- a/js/ui/keyboard.js
+++ b/js/ui/keyboard.js
@@ -1494,7 +1494,13 @@ var Keyboard = GObject.registerClass({
                 if (key.action === 'modifier')
                     return;
 
+                if (keyval && this._oskCompletionEnabled) {
+                    Main.inputMethod.handleVirtualKey(keyval);
+                    return;
+                }
+
                 if (str === '' || !Main.inputMethod.currentFocus ||
+                    (keyval && this._oskCompletionEnabled) ||
                     this._modifiers.length > 0 ||
                     !this._keyboardController.commitString(str, true)) {
                     if (keyval != 0) {
@@ -1855,6 +1861,8 @@ var Keyboard = GObject.registerClass({
             return;
         }
 
+        this._oskCompletionEnabled =
+            IBusManager.getIBusManager().toggleOskCompletion(true);
         this._clearKeyboardRestTimer();
 
         if (immediate) {
@@ -1889,6 +1897,8 @@ var Keyboard = GObject.registerClass({
         if (!this._keyboardVisible)
             return;
 
+        IBusManager.getIBusManager().toggleOskCompletion(false);
+        this._oskCompletionEnabled = false;
         this._clearKeyboardRestTimer();
 
         if (immediate) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]