[gnome-shell/wip/carlosg/osk-updates: 59/79] keyboard: Add/handle "modifier" action keys




commit c4dba0fd367659079a8c72f19778833db66fd2df
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Apr 16 13:06:09 2022 +0200

    keyboard: Add/handle "modifier" action keys
    
    These keys are handled so that the related modifier keyval (e.g. left
    control) is toggled on, and flushed on the next non-modifier key
    press submission.

 js/ui/keyboard.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 64 insertions(+), 8 deletions(-)
---
diff --git a/js/ui/keyboard.js b/js/ui/keyboard.js
index e4e0389cea..a06a75b9c4 100644
--- a/js/ui/keyboard.js
+++ b/js/ui/keyboard.js
@@ -493,15 +493,15 @@ var Key = GObject.registerClass({
     }
 
     setLatched(latched) {
-        if (!this._icon)
-            return;
-
-        if (latched) {
+        if (latched)
             this.keyButton.add_style_pseudo_class('latched');
-            this._icon.icon_name = 'keyboard-caps-lock-symbolic';
-        } else {
+        else
             this.keyButton.remove_style_pseudo_class('latched');
-            this._icon.icon_name = 'keyboard-shift-symbolic';
+
+        /* Shift keys have icons */
+        if (this._icon) {
+            this._icon.icon_name = latched
+                ? 'keyboard-caps-lock-symbolic' : 'keyboard-shift-symbolic';
         }
     }
 });
@@ -1293,6 +1293,8 @@ var Keyboard = GObject.registerClass({
         this._focusWindowStartY = null;
 
         this._latched = false; // current level is latched
+        this._modifiers = [];
+        this._modifierKeys = {};
 
         this._suggestions = null;
         this._emojiKeyVisible = Meta.is_wayland_compositor();
@@ -1502,11 +1504,22 @@ var Keyboard = GObject.registerClass({
                 button.setWidth(key.width);
 
             button.connect('commit', (actor, keyval, str) => {
+                /* These are emitted later on */
+                if (key.action === 'modifier')
+                    return;
+
                 if (str === '' || !Main.inputMethod.currentFocus ||
+                    this._modifiers.length > 0 ||
                     !this._keyboardController.commitString(str, true)) {
                     if (keyval != 0) {
+                        this._forwardModifiers(this._modifiers, true);
                         this._keyboardController.keyvalPress(keyval);
-                        this._keyboardController.keyvalRelease(keyval);
+                        GLib.timeout_add(GLib.PRIORITY_DEFAULT, 50, () => {
+                            this._keyboardController.keyvalRelease(keyval);
+                            this._forwardModifiers(this._modifiers, false);
+                            this._disableAllModifiers();
+                            return GLib.SOURCE_REMOVE;
+                        });
                     }
                 }
 
@@ -1520,6 +1533,8 @@ var Keyboard = GObject.registerClass({
                     this._popupLanguageMenu(button);
                 } else if (key.action === 'emoji') {
                     this._toggleEmoji();
+                } else if (key.action === 'modifier') {
+                    this._toggleModifier(key.keyval);
                 } else if (!this._longPressed && key.action === 'levelSwitch') {
                     this._setActiveLayer(key.level);
                     this._setLatched(
@@ -1542,6 +1557,12 @@ var Keyboard = GObject.registerClass({
                 }
             }
 
+            if (key.action === 'modifier') {
+                let modifierKeys = this._modifierKeys[key.keyval] || [];
+                modifierKeys.push(button);
+                this._modifierKeys[key.keyval] = modifierKeys;
+            }
+
             if (key.action || key.keyval)
                 button.keyButton.add_style_class_name('default-key');
 
@@ -1554,6 +1575,39 @@ var Keyboard = GObject.registerClass({
         this._setCurrentLevelLatched(this._currentPage, this._latched);
     }
 
+    _setModifierEnabled(keyval, enabled) {
+        const idx = this._modifiers.indexOf(keyval);
+        if (enabled &&  idx === -1)
+            this._modifiers.push(keyval);
+        else if (!enabled)
+            this._modifiers.splice(idx, 1);
+
+        for (const key of this._modifierKeys[keyval])
+            key.setLatched(enabled);
+    }
+
+    _toggleModifier(keyval) {
+        const isActive = this._modifiers.indexOf(keyval) !== -1;
+        this._setModifierEnabled(keyval, !isActive);
+    }
+
+    _forwardModifiers(modifiers, press) {
+        for (const keyval of modifiers) {
+            if (press)
+                this._keyboardController.keyvalPress(keyval);
+            else
+                this._keyboardController.keyvalRelease(keyval);
+        }
+    }
+
+    _disableAllModifiers() {
+        // The modifier list is modified inside the loop
+        while (this._modifiers.length > 0) {
+            const keyval = this._modifiers[0];
+            this._setModifierEnabled(keyval, false);
+        }
+    }
+
     _popupLanguageMenu(keyActor) {
         if (this._languagePopup)
             this._languagePopup.destroy();
@@ -1699,6 +1753,7 @@ var Keyboard = GObject.registerClass({
             delete this._currentPage._destroyID;
         }
 
+        this._disableAllModifiers();
         this._currentPage = currentPage;
         this._currentPage._destroyID = this._currentPage.connect('destroy', () => {
             this._currentPage = null;
@@ -1779,6 +1834,7 @@ var Keyboard = GObject.registerClass({
 
         this._animateHide();
         this.setCursorLocation(null);
+        this._disableAllModifiers();
     }
 
     _animateShow() {


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