Skip to content

Commit 4488d9e

Browse files
committed
Revised hotkey bindings to remove script requirement.
1 parent 4758f67 commit 4488d9e

File tree

4 files changed

+167
-102
lines changed

4 files changed

+167
-102
lines changed

README.md

-14
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,3 @@ WinTile: Windows 10 window tiling for GNOME
33
WinTile is a hotkey driven window tiling system for GNOME that imitates the standard `Win-Arrow` keys of Windows 10, allowing you to maximize, maximize to sides, or 1/4 sized to corner a window using just `<Super>`+`<Arrows>`.
44

55
<img src='demo.gif'>
6-
7-
Selecting the Super key
8-
-----------------------
9-
By default, this extension uses `<Super><Control><Shift>`+`<Arrows>` to move windows. This is because `<Super>`+`<Arrows>` is reserved by GNOME in the keyboard shortcut settings. The below script will toggle these default key bindings so you can use `<Super>`+`<Arrows>` for this extension.
10-
11-
To use `<Super>`+`<Arrows>` for this extension:
12-
```
13-
$ bash ~/.local/share/gnome-shell/extensions/[email protected]/setHotKey.sh Super
14-
```
15-
16-
To reset `<Super>`+`<Arrows>` to default for GNOME:
17-
```
18-
$ bash ~/.local/share/gnome-shell/extensions/[email protected]/setHotKey.sh ControlShiftSuper
19-
```

extension.js

+57-63
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const Lang = imports.lang
21
const Meta = imports.gi.Meta
3-
const Shell = imports.gi.Shell
42
const Main = imports.ui.main
53
const Mainloop = imports.mainloop;
4+
const Gio = imports.gi.Gio;
65

76
let _close = 50;
87
var debug = false;
@@ -11,57 +10,21 @@ var _log = function(){}
1110
if (debug)
1211
_log = log.bind(window.console);
1312

13+
const Config = imports.misc.config;
14+
window.gsconnect = {
15+
extdatadir: imports.misc.extensionUtils.getCurrentExtension().path,
16+
shell_version: parseInt(Config.PACKAGE_VERSION.split('.')[1], 10)
17+
};
18+
imports.searchPath.unshift(gsconnect.extdatadir);
1419

15-
const KeyManager = new Lang.Class({
16-
Name: 'MyKeyManager',
17-
18-
_init: function() {
19-
this.grabbers = new Map()
20-
21-
global.display.connect(
22-
'accelerator-activated',
23-
Lang.bind(this, function(display, action, deviceId, timestamp){
24-
_log('Accelerator Activated: [display={}, action={}, deviceId={}, timestamp={}]',
25-
display, action, deviceId, timestamp)
26-
this._onAccelerator(action)
27-
}))
28-
},
29-
30-
listenFor: function(accelerator, callback){
31-
_log('Trying to listen for hot key [accelerator={}]', accelerator)
32-
let action = global.display.grab_accelerator(accelerator)
33-
34-
if(action == Meta.KeyBindingAction.NONE) {
35-
_log('Unable to grab accelerator [binding={}]', accelerator)
36-
} else {
37-
_log('Grabbed accelerator [action={}]', action)
38-
let name = Meta.external_binding_name_for_action(action)
39-
_log('Received binding name for action [name={}, action={}]',
40-
name, action)
41-
42-
_log('Requesting WM to allow binding [name={}]', name)
43-
Main.wm.allowKeybinding(name, Shell.ActionMode.ALL)
44-
45-
this.grabbers.set(action, {
46-
name: name,
47-
accelerator: accelerator,
48-
callback: callback,
49-
action: action
50-
})
51-
}
52-
53-
},
54-
55-
_onAccelerator: function(action) {
56-
let grabber = this.grabbers.get(action)
57-
58-
if(grabber) {
59-
this.grabbers.get(action).callback()
60-
} else {
61-
_log('No listeners [action={}]', action)
62-
}
63-
}
64-
})
20+
const KeyBindings = imports.keybindings
21+
let keyManager = new KeyBindings.Manager();
22+
var oldbindings = {
23+
unmaximize: [],
24+
maximize: [],
25+
toggle_tiled_left: [],
26+
toggle_tiled_right: []
27+
}
6528

6629
function isClose(a, b) {
6730
if (a <= b && a > b - _close)
@@ -304,17 +267,48 @@ function requestMove(direction) {
304267
});
305268
}
306269

270+
function changeBinding(settings, key, oldBinding, newBinding) {
271+
var binding = oldbindings[key.replace(/-/g, '_')];
272+
var _newbindings = [];
273+
for (var i = 0; i < binding.length; i++) {
274+
let currentbinding = binding[i];
275+
if (currentbinding == oldBinding)
276+
currentbinding = newBinding;
277+
_newbindings.push(currentbinding)
278+
}
279+
settings.set_strv(key, _newbindings);
280+
}
281+
282+
function resetBinding(settings, key) {
283+
var binding = oldbindings[key.replace(/-/g, '_')];
284+
settings.set_strv(key, binding);
285+
}
286+
307287
var enable = function() {
308-
let modifier = "<ctrl><super><shift>";
309-
let modifier2 = "<super>";
310-
let keyManager = new KeyManager()
311-
keyManager.listenFor(modifier+"left", function() { requestMove("left") })
312-
keyManager.listenFor(modifier+"right", function() { requestMove("right") })
313-
keyManager.listenFor(modifier+"up", function() { requestMove("up") })
314-
keyManager.listenFor(modifier+"down", function() { requestMove("down") })
315-
keyManager.listenFor(modifier2+"left", function() { requestMove("left") })
316-
keyManager.listenFor(modifier2+"right", function() { requestMove("right") })
317-
keyManager.listenFor(modifier2+"up", function() { requestMove("up") })
318-
keyManager.listenFor(modifier2+"down", function() { requestMove("down") })
288+
let desktopSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.wm.keybindings' });
289+
let mutterSettings = new Gio.Settings({ schema_id: 'org.gnome.mutter.keybindings' });
290+
oldbindings['unmaximize'] = desktopSettings.get_strv('unmaximize');
291+
oldbindings['maximize'] = desktopSettings.get_strv('maximize');
292+
oldbindings['toggle_tiled_left'] = mutterSettings.get_strv('toggle-tiled-left');
293+
oldbindings['toggle_tiled_right'] = mutterSettings.get_strv('toggle-tiled-right');
294+
changeBinding(desktopSettings, 'unmaximize', '<Super>Down', '<Control><Shift><Super>Down');
295+
changeBinding(desktopSettings, 'maximize', '<Super>Up', '<Control><Shift><Super>Up');
296+
changeBinding(mutterSettings, 'toggle-tiled-left', '<Super>Left', '<Control><Shift><Super>Left');
297+
changeBinding(mutterSettings, 'toggle-tiled-right', '<Super>Right', '<Control><Shift><Super>Right');
298+
Mainloop.timeout_add(3000, function() {
299+
keyManager.add("<Super>left", function() { requestMove("left") })
300+
keyManager.add("<Super>right", function() { requestMove("right") })
301+
keyManager.add("<Super>up", function() { requestMove("up") })
302+
keyManager.add("<Super>down", function() { requestMove("down") })
303+
});
319304
}
320305

306+
var disable = function() {
307+
keyManager.removeAll();
308+
let desktopSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.wm.keybindings' });
309+
let mutterSettings = new Gio.Settings({ schema_id: 'org.gnome.mutter.keybindings' });
310+
resetBinding(desktopSettings, 'unmaximize');
311+
resetBinding(desktopSettings, 'maximize');
312+
resetBinding(mutterSettings, 'toggle-tiled-left');
313+
resetBinding(mutterSettings, 'toggle-tiled-right');
314+
}

keybindings.js

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

setHotKey.sh

-25
This file was deleted.

0 commit comments

Comments
 (0)