Adding Keyboard Shortcut Functionality to the Windows Quick List Applet in Linux Mint Cinnamon
Tags: linux, cinnamon, applet, javascript
Published: 2024-05-02 09:34:02

## Adding Keybinding Functionality to the Windows Quick List Applet in Cinnamon


If you're using the Windows Quick List applet in Cinnamon and want to enhance its functionality by adding a customizable keybinding, follow these steps to modify the `applet.js` and `settings-schema.json` files.


## Modifying `applet.js`

Step 1 - Import the `Settings` modules at the beginning of the file:
```
  const Settings = imports.ui.settings;
```

Step 2 - In the `constructor` method of the `CinnamonWindowsQuickListApplet` class, add the following lines after the existing code:

```
this.settings = new Settings.AppletSettings(this, metadata.uuid, instance_id);
this.settings.bindProperty(Settings.BindingDirection.IN,
                           "keyOpen",
                           "keybinding",
                           this.on_keybinding_changed,
                           null);
this.actor.connect('key-press-event',
                   Lang.bind(this,
                             this._onSourceKeyPress));

this.on_keybinding_changed();
```

Step 3 - Add the `_onSourceKeyPress`, `on_keybinding_changed`, and `on_hotkey_triggered` methods to the `CinnamonWindowsQuickListApplet` class:

```
_onSourceKeyPress(actor, event) {
        let symbol = event.get_key_symbol();

        if (symbol == Clutter.KEY_space || symbol == Clutter.KEY_Return) {
            this.menu.toggle();
            return true;
        } else if (symbol == Clutter.KEY_Escape && this.menu.isOpen) {
            this.menu.close();
            return true;
        } else if (symbol == Clutter.KEY_Down) {
            if (!this.menu.isOpen)
                this.menu.toggle();
            this.menu.actor.navigate_focus(this.actor, Gtk.DirectionType.DOWN, false);
            return true;
        } else
            return false;
    }

    on_keybinding_changed() {
        Main.keybindingManager.addHotKey("must-be-unique-id",
                                         this.keybinding,
                                         Lang.bind(this,
                                                   
    this.on_hotkey_triggered));
    }

    on_hotkey_triggered() {
        if (this.menu.isOpen) {
            this.menu.close(false);
        } else {
            this.updateMenu();
            this.menu.open(true);
        }
    }
```

These changes will make the Windows Quick List applet accessible via the keybinding defined in the applet settings. The `_onSourceKeyPress` method handles the key press events when the applet actor is focused, allowing you to toggle the menu with the Space or Return key, close the menu with the Escape key, and navigate the menu with the Down arrow key.
The `on_keybinding_changed` method is called when the keybinding setting changes and adds the hotkey using `Main.keybindingManager.addHotKey`. The `on_hotkey_triggered` method is called when the hotkey is triggered, and it toggles the menu open or closed.



## Modifying `settings-schema.json`
To make the hotkey customizable, crate a new file `settings-schema.json` in the same path and add the corresponding setting definition to the applet's settings schema file:
```
{
  "section1": {
    "type": "section",
    "description": "Behavior"
  },
  "keyOpen": {
    "type": "keybinding",
    "description": "Show List",
    "default": "<Super>a",
    "tooltip": "Shortcut to open/close the window list"
  }
}
```

This adds a new setting named `keyOpen` under the "Behavior" section. The setting is of type "keybinding" and allows users to customize the shortcut for opening and closing the window list. The default keybinding is set to `<Super>a`, but users can change it according to their preference.

With these modifications to `applet.js` and `settings-schema.json`, your Windows Quick List applet will now have a customizable keybinding that users can configure through the applet settings.

If you are too lazy to tweak the source code, clone [this repo](https://github.com/shawonsaha/windows-quick-list-with-keyboard-shortcut) and replace files inside `/usr/share/cinnamon/applets/windows-quick-list@cinnamon.org`

Remember to restart Cinnamon or reload the applet after making these changes for them to take effect.

Happy customizing!