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




commit 48b3f8b28cbb855ab0a8874466c6b855a67e0757
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 48d8640d58..b43d7113aa 100644
--- a/js/ui/keyboard.js
+++ b/js/ui/keyboard.js
@@ -488,15 +488,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';
         }
     }
 });
@@ -1283,6 +1283,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();
@@ -1492,11 +1494,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;
+                        });
                     }
                 }
 
@@ -1510,6 +1523,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(
@@ -1532,6 +1547,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');
 
@@ -1544,6 +1565,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();
@@ -1689,6 +1743,7 @@ var Keyboard = GObject.registerClass({
             delete this._currentPage._destroyID;
         }
 
+        this._disableAllModifiers();
         this._currentPage = currentPage;
         this._currentPage._destroyID = this._currentPage.connect('destroy', () => {
             this._currentPage = null;
@@ -1769,6 +1824,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]