-
Notifications
You must be signed in to change notification settings - Fork 52
/
keybindings.js
111 lines (98 loc) · 3.34 KB
/
keybindings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
/* global global */
/* BEGIN NON-G45 */
const Main = imports.ui.main;
const Meta = imports.gi.Meta;
const Shell = imports.gi.Shell;
/* END NON-G45 */
/* BEGIN G45 */
// import * as Main from 'resource:///org/gnome/shell/ui/main.js';
// import Meta from 'gi://Meta';
// import Shell from 'gi://Shell';
/* END G45 */
/**
* Keybindings.Manager is a simple convenience class for managing keyboard
* shortcuts in GNOME Shell. You bind a shortcut using add(), which on success
* will return a non-zero action id that can later be used with remove() to
* unbind the shortcut.
*
* Accelerators are accepted in the form returned by Gtk.accelerator_name() and
* callbacks are invoked directly, so should be complete closures.
*
* References:
* https://developer.gnome.org/gtk3/stable/gtk3-Keyboard-Accelerators.html
* https://developer.gnome.org/meta/stable/MetaDisplay.html
* https://developer.gnome.org/meta/stable/meta-MetaKeybinding.html
* https://gitlab.gnome.org/GNOME/gnome-shell/blob/master/js/ui/windowManager.js#L1093-1112
*/
/* BEGIN NON-G45 */
var KeyBindingsManager = class KeyBindingsManager {
/* END NON-G45 */
/* BEGIN G45 */
// export default class KeyBindingsManager {
/* END G45 */
constructor() {
this._keybindings = new Map();
this._acceleratorActivatedId = global.display.connect(
'accelerator-activated',
this._onAcceleratorActivated.bind(this)
);
}
_onAcceleratorActivated(display, action, _deviceId, _timestamp) {
try {
let binding = this._keybindings.get(action);
if (binding !== undefined)
binding.callback();
} catch (e) {
logError(e);
}
}
/**
* Add a keybinding with callback
*
* @param {string} accelerator - An accelerator in the form '<Control>q'
* @param {Function} callback - A callback for the accelerator
* @returns {number} - A non-zero action id on success, or 0 on failure
*/
add(accelerator, callback) {
let action = Meta.KeyBindingAction.NONE;
action = global.display.grab_accelerator(accelerator, 0);
if (action !== Meta.KeyBindingAction.NONE) {
let name = Meta.external_binding_name_for_action(action);
Main.wm.allowKeybinding(name, Shell.ActionMode.ALL);
this._keybindings.set(action, {name, callback});
} else {
logError(new Error(`Failed to add keybinding: '${accelerator}'`));
}
return action;
}
/**
* Remove a keybinding
*
* @param {number} action - A non-zero action id returned by add()
*/
remove(action) {
try {
let binding = this._keybindings.get(action);
global.display.ungrab_accelerator(action);
Main.wm.allowKeybinding(binding.name, Shell.ActionMode.NONE);
this._keybindings.delete(action);
} catch (e) {
logError(new Error(`Failed to remove keybinding: ${e.message}`));
}
}
/**
* Remove all keybindings
*/
removeAll() {
for (let action of this._keybindings.keys())
this.remove(action);
}
/**
* Destroy the keybinding manager and remove all keybindings
*/
destroy() {
global.display.disconnect(this._acceleratorActivatedId);
this.removeAll();
}
}