keybindings.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-FileCopyrightText: GSConnect Developers https://github.com/GSConnect
  2. //
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. import * as Main from 'resource:///org/gnome/shell/ui/main.js';
  5. import Meta from 'gi://Meta';
  6. import Shell from 'gi://Shell';
  7. /**
  8. * Keybindings.Manager is a simple convenience class for managing keyboard
  9. * shortcuts in GNOME Shell. You bind a shortcut using add(), which on success
  10. * will return a non-zero action id that can later be used with remove() to
  11. * unbind the shortcut.
  12. *
  13. * Accelerators are accepted in the form returned by Gtk.accelerator_name() and
  14. * callbacks are invoked directly, so should be complete closures.
  15. *
  16. * References:
  17. * https://developer.gnome.org/gtk3/stable/gtk3-Keyboard-Accelerators.html
  18. * https://developer.gnome.org/meta/stable/MetaDisplay.html
  19. * https://developer.gnome.org/meta/stable/meta-MetaKeybinding.html
  20. * https://gitlab.gnome.org/GNOME/gnome-shell/blob/master/js/ui/windowManager.js#L1093-1112
  21. */
  22. export class Manager {
  23. constructor() {
  24. this._keybindings = new Map();
  25. this._acceleratorActivatedId = global.display.connect(
  26. 'accelerator-activated',
  27. this._onAcceleratorActivated.bind(this)
  28. );
  29. }
  30. _onAcceleratorActivated(display, action, inputDevice, timestamp) {
  31. try {
  32. const binding = this._keybindings.get(action);
  33. if (binding !== undefined)
  34. binding.callback();
  35. } catch (e) {
  36. logError(e);
  37. }
  38. }
  39. /**
  40. * Add a keybinding with callback
  41. *
  42. * @param {string} accelerator - An accelerator in the form '<Control>q'
  43. * @param {Function} callback - A callback for the accelerator
  44. * @return {number} A non-zero action id on success, or 0 on failure
  45. */
  46. add(accelerator, callback) {
  47. try {
  48. const action = global.display.grab_accelerator(accelerator, 0);
  49. if (action === Meta.KeyBindingAction.NONE)
  50. throw new Error(`Failed to add keybinding: '${accelerator}'`);
  51. const name = Meta.external_binding_name_for_action(action);
  52. Main.wm.allowKeybinding(name, Shell.ActionMode.ALL);
  53. this._keybindings.set(action, {name: name, callback: callback});
  54. return action;
  55. } catch (e) {
  56. logError(e);
  57. }
  58. }
  59. /**
  60. * Remove a keybinding
  61. *
  62. * @param {number} action - A non-zero action id returned by add()
  63. */
  64. remove(action) {
  65. try {
  66. const binding = this._keybindings.get(action);
  67. global.display.ungrab_accelerator(action);
  68. Main.wm.allowKeybinding(binding.name, Shell.ActionMode.NONE);
  69. this._keybindings.delete(action);
  70. } catch (e) {
  71. logError(new Error(`Failed to remove keybinding: ${e.message}`));
  72. }
  73. }
  74. /**
  75. * Remove all keybindings
  76. */
  77. removeAll() {
  78. for (const action of this._keybindings.keys())
  79. this.remove(action);
  80. }
  81. /**
  82. * Destroy the keybinding manager and remove all keybindings
  83. */
  84. destroy() {
  85. global.display.disconnect(this._acceleratorActivatedId);
  86. this.removeAll();
  87. }
  88. }