mirror of
https://github.com/FranP-code/wintile.git
synced 2025-10-13 00:33:46 +00:00
Revised hotkey bindings to remove script requirement.
This commit is contained in:
14
README.md
14
README.md
@@ -3,17 +3,3 @@ WinTile: Windows 10 window tiling for GNOME
|
|||||||
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>`.
|
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>`.
|
||||||
|
|
||||||
<img src='demo.gif'>
|
<img src='demo.gif'>
|
||||||
|
|
||||||
Selecting the Super key
|
|
||||||
-----------------------
|
|
||||||
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.
|
|
||||||
|
|
||||||
To use `<Super>`+`<Arrows>` for this extension:
|
|
||||||
```
|
|
||||||
$ bash ~/.local/share/gnome-shell/extensions/wintile@nowsci.com/setHotKey.sh Super
|
|
||||||
```
|
|
||||||
|
|
||||||
To reset `<Super>`+`<Arrows>` to default for GNOME:
|
|
||||||
```
|
|
||||||
$ bash ~/.local/share/gnome-shell/extensions/wintile@nowsci.com/setHotKey.sh ControlShiftSuper
|
|
||||||
```
|
|
||||||
|
|||||||
122
extension.js
122
extension.js
@@ -1,8 +1,7 @@
|
|||||||
const Lang = imports.lang
|
|
||||||
const Meta = imports.gi.Meta
|
const Meta = imports.gi.Meta
|
||||||
const Shell = imports.gi.Shell
|
|
||||||
const Main = imports.ui.main
|
const Main = imports.ui.main
|
||||||
const Mainloop = imports.mainloop;
|
const Mainloop = imports.mainloop;
|
||||||
|
const Gio = imports.gi.Gio;
|
||||||
|
|
||||||
let _close = 50;
|
let _close = 50;
|
||||||
var debug = false;
|
var debug = false;
|
||||||
@@ -11,57 +10,21 @@ var _log = function(){}
|
|||||||
if (debug)
|
if (debug)
|
||||||
_log = log.bind(window.console);
|
_log = log.bind(window.console);
|
||||||
|
|
||||||
|
const Config = imports.misc.config;
|
||||||
|
window.gsconnect = {
|
||||||
|
extdatadir: imports.misc.extensionUtils.getCurrentExtension().path,
|
||||||
|
shell_version: parseInt(Config.PACKAGE_VERSION.split('.')[1], 10)
|
||||||
|
};
|
||||||
|
imports.searchPath.unshift(gsconnect.extdatadir);
|
||||||
|
|
||||||
const KeyManager = new Lang.Class({
|
const KeyBindings = imports.keybindings
|
||||||
Name: 'MyKeyManager',
|
let keyManager = new KeyBindings.Manager();
|
||||||
|
var oldbindings = {
|
||||||
_init: function() {
|
unmaximize: [],
|
||||||
this.grabbers = new Map()
|
maximize: [],
|
||||||
|
toggle_tiled_left: [],
|
||||||
global.display.connect(
|
toggle_tiled_right: []
|
||||||
'accelerator-activated',
|
}
|
||||||
Lang.bind(this, function(display, action, deviceId, timestamp){
|
|
||||||
_log('Accelerator Activated: [display={}, action={}, deviceId={}, timestamp={}]',
|
|
||||||
display, action, deviceId, timestamp)
|
|
||||||
this._onAccelerator(action)
|
|
||||||
}))
|
|
||||||
},
|
|
||||||
|
|
||||||
listenFor: function(accelerator, callback){
|
|
||||||
_log('Trying to listen for hot key [accelerator={}]', accelerator)
|
|
||||||
let action = global.display.grab_accelerator(accelerator)
|
|
||||||
|
|
||||||
if(action == Meta.KeyBindingAction.NONE) {
|
|
||||||
_log('Unable to grab accelerator [binding={}]', accelerator)
|
|
||||||
} else {
|
|
||||||
_log('Grabbed accelerator [action={}]', action)
|
|
||||||
let name = Meta.external_binding_name_for_action(action)
|
|
||||||
_log('Received binding name for action [name={}, action={}]',
|
|
||||||
name, action)
|
|
||||||
|
|
||||||
_log('Requesting WM to allow binding [name={}]', name)
|
|
||||||
Main.wm.allowKeybinding(name, Shell.ActionMode.ALL)
|
|
||||||
|
|
||||||
this.grabbers.set(action, {
|
|
||||||
name: name,
|
|
||||||
accelerator: accelerator,
|
|
||||||
callback: callback,
|
|
||||||
action: action
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
},
|
|
||||||
|
|
||||||
_onAccelerator: function(action) {
|
|
||||||
let grabber = this.grabbers.get(action)
|
|
||||||
|
|
||||||
if(grabber) {
|
|
||||||
this.grabbers.get(action).callback()
|
|
||||||
} else {
|
|
||||||
_log('No listeners [action={}]', action)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
function isClose(a, b) {
|
function isClose(a, b) {
|
||||||
if (a <= b && a > b - _close)
|
if (a <= b && a > b - _close)
|
||||||
@@ -304,17 +267,48 @@ function requestMove(direction) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var enable = function() {
|
function changeBinding(settings, key, oldBinding, newBinding) {
|
||||||
let modifier = "<ctrl><super><shift>";
|
var binding = oldbindings[key.replace(/-/g, '_')];
|
||||||
let modifier2 = "<super>";
|
var _newbindings = [];
|
||||||
let keyManager = new KeyManager()
|
for (var i = 0; i < binding.length; i++) {
|
||||||
keyManager.listenFor(modifier+"left", function() { requestMove("left") })
|
let currentbinding = binding[i];
|
||||||
keyManager.listenFor(modifier+"right", function() { requestMove("right") })
|
if (currentbinding == oldBinding)
|
||||||
keyManager.listenFor(modifier+"up", function() { requestMove("up") })
|
currentbinding = newBinding;
|
||||||
keyManager.listenFor(modifier+"down", function() { requestMove("down") })
|
_newbindings.push(currentbinding)
|
||||||
keyManager.listenFor(modifier2+"left", function() { requestMove("left") })
|
}
|
||||||
keyManager.listenFor(modifier2+"right", function() { requestMove("right") })
|
settings.set_strv(key, _newbindings);
|
||||||
keyManager.listenFor(modifier2+"up", function() { requestMove("up") })
|
|
||||||
keyManager.listenFor(modifier2+"down", function() { requestMove("down") })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resetBinding(settings, key) {
|
||||||
|
var binding = oldbindings[key.replace(/-/g, '_')];
|
||||||
|
settings.set_strv(key, binding);
|
||||||
|
}
|
||||||
|
|
||||||
|
var enable = function() {
|
||||||
|
let desktopSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.wm.keybindings' });
|
||||||
|
let mutterSettings = new Gio.Settings({ schema_id: 'org.gnome.mutter.keybindings' });
|
||||||
|
oldbindings['unmaximize'] = desktopSettings.get_strv('unmaximize');
|
||||||
|
oldbindings['maximize'] = desktopSettings.get_strv('maximize');
|
||||||
|
oldbindings['toggle_tiled_left'] = mutterSettings.get_strv('toggle-tiled-left');
|
||||||
|
oldbindings['toggle_tiled_right'] = mutterSettings.get_strv('toggle-tiled-right');
|
||||||
|
changeBinding(desktopSettings, 'unmaximize', '<Super>Down', '<Control><Shift><Super>Down');
|
||||||
|
changeBinding(desktopSettings, 'maximize', '<Super>Up', '<Control><Shift><Super>Up');
|
||||||
|
changeBinding(mutterSettings, 'toggle-tiled-left', '<Super>Left', '<Control><Shift><Super>Left');
|
||||||
|
changeBinding(mutterSettings, 'toggle-tiled-right', '<Super>Right', '<Control><Shift><Super>Right');
|
||||||
|
Mainloop.timeout_add(3000, function() {
|
||||||
|
keyManager.add("<Super>left", function() { requestMove("left") })
|
||||||
|
keyManager.add("<Super>right", function() { requestMove("right") })
|
||||||
|
keyManager.add("<Super>up", function() { requestMove("up") })
|
||||||
|
keyManager.add("<Super>down", function() { requestMove("down") })
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var disable = function() {
|
||||||
|
keyManager.removeAll();
|
||||||
|
let desktopSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.wm.keybindings' });
|
||||||
|
let mutterSettings = new Gio.Settings({ schema_id: 'org.gnome.mutter.keybindings' });
|
||||||
|
resetBinding(desktopSettings, 'unmaximize');
|
||||||
|
resetBinding(desktopSettings, 'maximize');
|
||||||
|
resetBinding(mutterSettings, 'toggle-tiled-left');
|
||||||
|
resetBinding(mutterSettings, 'toggle-tiled-right');
|
||||||
|
}
|
||||||
|
|||||||
110
keybindings.js
Normal file
110
keybindings.js
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const Config = imports.misc.config;
|
||||||
|
const Main = imports.ui.main;
|
||||||
|
const Meta = imports.gi.Meta;
|
||||||
|
const Shell = imports.gi.Shell;
|
||||||
|
|
||||||
|
const SHELL_VERSION_MINOR = parseInt(Config.PACKAGE_VERSION.split('.')[1]);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
var Manager = class Manager {
|
||||||
|
|
||||||
|
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
|
||||||
|
* @return {Number} - A non-zero action id on success, or 0 on failure
|
||||||
|
*/
|
||||||
|
add(accelerator, callback) {
|
||||||
|
let action = Meta.KeyBindingAction.NONE;
|
||||||
|
|
||||||
|
// A flags argument was added somewhere between 3.30-3.32
|
||||||
|
if (SHELL_VERSION_MINOR > 30) {
|
||||||
|
action = global.display.grab_accelerator(accelerator, 0);
|
||||||
|
} else {
|
||||||
|
action = global.display.grab_accelerator(accelerator);
|
||||||
|
}
|
||||||
|
|
||||||
|
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: name, callback: callback});
|
||||||
|
} else {
|
||||||
|
logError(new Error(`Failed to add keybinding: '${accelerator}'`));
|
||||||
|
}
|
||||||
|
|
||||||
|
return action;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a keybinding
|
||||||
|
*
|
||||||
|
* @param {Number} accelerator - 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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
25
setHotKey.sh
25
setHotKey.sh
@@ -1,25 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
if [ -z "$1" ]; then
|
|
||||||
echo "Usage: setHotKey.sh <Super|ControlShiftSuper>"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$1" = "ControlShiftSuper" ]; then
|
|
||||||
gsettings set org.gnome.desktop.wm.keybindings unmaximize "['<Super>Down', '<Alt>F5']" >/dev/null 2>&1
|
|
||||||
gsettings set org.gnome.desktop.wm.keybindings maximize "['<Super>Up']" >/dev/null 2>&1
|
|
||||||
gsettings set org.cinnamon.desktop.keybindings.wm push-tile-left "['<Super>Left']" >/dev/null 2>&1
|
|
||||||
gsettings set org.gnome.mutter.keybindings toggle-tiled-left "['<Super>Left']" >/dev/null 2>&1
|
|
||||||
gsettings set org.cinnamon.desktop.keybindings.wm push-tile-right "['<Super>Right']" >/dev/null 2>&1
|
|
||||||
gsettings set org.gnome.mutter.keybindings toggle-tiled-right "['<Super>Right']" >/dev/null 2>&1
|
|
||||||
echo -e "\nNow run Alt-F2 then R to restart GNOME.\n"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$1" = "Super" ]; then
|
|
||||||
gsettings set org.gnome.desktop.wm.keybindings unmaximize "['<Control><Shift><Super>Down', '<Alt>F5']" >/dev/null 2>&1
|
|
||||||
gsettings set org.gnome.desktop.wm.keybindings maximize "['<Control><Shift><Super>Up']" >/dev/null 2>&1
|
|
||||||
gsettings set org.cinnamon.desktop.keybindings.wm push-tile-left "['<Control><Shift><Super>Left']" >/dev/null 2>&1
|
|
||||||
gsettings set org.gnome.mutter.keybindings toggle-tiled-left "['<Control><Shift><Super>Left']" >/dev/null 2>&1
|
|
||||||
gsettings set org.cinnamon.desktop.keybindings.wm push-tile-right "['<Control><Shift><Super>Right']" >/dev/null 2>&1
|
|
||||||
gsettings set org.gnome.mutter.keybindings toggle-tiled-right "['<Control><Shift><Super>Right']" >/dev/null 2>&1
|
|
||||||
echo -e "\nNow run Alt-F2 then R to restart GNOME.\n"
|
|
||||||
fi
|
|
||||||
Reference in New Issue
Block a user