Disable and enable our keybindings more robustly

This commit is contained in:
Ole Jørgen Brønner
2018-07-06 11:26:54 +02:00
parent 71b6ee8f56
commit e1bc0853a2
3 changed files with 47 additions and 23 deletions

View File

@@ -211,10 +211,6 @@ function enable() {
setKeybinding('switch-to-workspace-last', // <Super>End
Keybindings.byMutterName('switch-last').handler);
Keybindings.actions.forEach(a => {
setKeybinding(a.name, a.handler);
});
// Only enable modules after disable have been run
if (enabled) {
log('enable called without calling disable');
@@ -252,10 +248,6 @@ function disable() {
Meta.keybindings_set_custom_handler('switch-to-workspace-down', null);
Meta.keybindings_set_custom_handler('switch-to-workspace-up', null);
Keybindings.actions.forEach(a => {
setKeybinding(a.name, () => {});
});
if (!enabled)
return;
log(`disable ${SESSIONID}`);

View File

@@ -59,27 +59,17 @@ function registerAction(actionName, handler, options) {
activeInScratch,
} = options;
let actionId, mutterName, keyHandler;
let mutterName, keyHandler;
if (settings) {
Utils.assert(actionName, "Schema action must have a name");
mutterName = actionName;
keyHandler = opensNavigator ? Navigator.preview_navigate : handler;
actionId = Main.wm.addKeybinding(
actionName,
settings, mutterFlags || Meta.KeyBindingFlags.NONE,
Shell.ActionMode.NORMAL,
keyHandler);
// If the id is NONE the action is already registered
if (actionId === Meta.KeyBindingAction.NONE)
return null;
} else {
// actionId, mutterName and keyHandler will be set if/when the action is bound
}
let action = {
id: actionId, // mutter action
id: Meta.KeyBindingAction.NONE,
name: actionName,
mutterName: mutterName,
keyHandler: keyHandler,
@@ -87,15 +77,17 @@ function registerAction(actionName, handler, options) {
options: options,
};
enableAction(action); // sets `action.id`
actions.push(action);
if (actionName)
nameMap[actionName] = action;
if (actionId)
actionIdMap[actionId] = action;
if (action.id)
actionIdMap[action.id] = action;
return action;
}
/**
* Bind a key to an action (possibly creating a new action)
*/
@@ -233,14 +225,50 @@ function handleAccelerator(display, actionId, deviceId, timestamp) {
}
}
function disableAction(action) {
if (action.options.settings) {
Main.wm.removeKeybinding(action.mutterName);
action.id = Meta.KeyBindingAction.NONE;
} else {
// Should only be called in disable/enable - schemaless actions are
// disabled/enabled by other means
}
}
function enableAction(action) {
if (action.id !== Meta.KeyBindingAction.NONE)
return; // Already enabled (happens on enable right after init)
if (action.options.settings) {
let actionId = Main.wm.addKeybinding(
action.mutterName,
action.options.settings,
action.options.mutterFlags || Meta.KeyBindingFlags.NONE,
Shell.ActionMode.NORMAL,
action.keyHandler);
if (actionId !== Meta.KeyBindingAction.NONE)
action.id = actionId;
else
Utils.warn("Could not enable action", action.name);
} else {
// Should only be called in disable/enable - schemaless actions are
// disabled/enabled by other means
}
}
function enable() {
signals.connect(
global.display,
'accelerator-activated',
Utils.dynamic_function_ref(handleAccelerator.name, Me)
);
actions.forEach(enableAction);
}
function disable() {
signals.destroy();
actions.forEach(disableAction);
}

View File

@@ -13,6 +13,10 @@ function debug() {
print(Array.prototype.join.call(arguments, " | "));
}
function warn(...args) {
print("WARNING:", ...args);
}
function assert(condition, message, ...dump) {
if (!condition) {
throw new Error(message + "\n", dump);