From ae8e91093b6457408ad8ea09177e0afb8ae91982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20J=C3=B8rgen=20Br=C3=B8nner?= Date: Mon, 1 Jul 2019 23:40:55 +0200 Subject: [PATCH] Show a warning notification when failing to bind a key (user.js) --- examples/keybindings.js | 2 +- keybindings.js | 43 ++++++++++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/examples/keybindings.js b/examples/keybindings.js index 18f291c8..6e5b0aef 100644 --- a/examples/keybindings.js +++ b/examples/keybindings.js @@ -109,7 +109,7 @@ function cycleMonitor(binding = "d") { function showNavigator(binding = "j") { - Keybindings.bindkey("j", "show-minimap", () => null, { opensMinimap: true }) + Keybindings.bindkey(binding, "show-minimap", () => null, { opensMinimap: true }) } diff --git a/keybindings.js b/keybindings.js index 6fcbd8f2..0a9d6b78 100644 --- a/keybindings.js +++ b/keybindings.js @@ -290,7 +290,33 @@ function bindkey(keystr, actionName=null, handler=null, options={}) { action.keystr = keystr; action.keycombo = keycombo; - enableAction(action); + if (enableAction(action) === Meta.KeyBindingAction.NONE) { + // Keybinding failed: try to supply a useful error message + let message; + let boundAction = keycomboMap[keycombo]; + if (boundAction) { + message = `${keystr} already bound to paperwm action: ${boundAction.name}`; + } else { + let boundId = getBoundActionId(keystr); + if (boundId !== Meta.KeyBindingAction.NONE) { + let builtInAction = + Object.entries(Meta.KeyBindingAction).find(([name, id]) => id === boundId); + if (builtInAction) { + message = `${keystr} already bound to built-in action: ${builtInAction[0]}`; + } else { + message = `${keystr} already bound to unknown action with id: ${boundId}`; + } + } + } + + if (!message) { + message = "Usually caused by the binding already being taken, but could not identify which action"; + } + + Extension.imports.extension.errorNotification( + "PaperWM (user.js): Could not enable keybinding", + `Tried to bind ${keystr} to ${actionName}\n${message}`); + } return action.id; } @@ -382,7 +408,7 @@ function disableAction(action) { function enableAction(action) { if (action.id !== Meta.KeyBindingAction.NONE) - return; // Already enabled (happens on enable right after init) + return action.id; // Already enabled (happens on enable right after init) if (action.options.settings) { let actionId = Main.wm.addKeybinding( @@ -395,13 +421,14 @@ function enableAction(action) { if (actionId !== Meta.KeyBindingAction.NONE) { action.id = actionId; actionIdMap[actionId] = action; - } else + } else { Utils.warn("Could not enable action", action.name); + } } else { if (keycomboMap[action.keycombo]) { - Utils.assert("Other action bound to", action.keystr, keycomboMap[action.keycombo].name) - return; + Utils.warn("Other action bound to", action.keystr, keycomboMap[action.keycombo].name) + return Meta.KeyBindingAction.NONE; } let actionId; @@ -413,8 +440,8 @@ function enableAction(action) { } if (actionId === Meta.KeyBindingAction.NONE) { - log("Failed to grab. Binding probably already taken"); - return; + Utils.warn("Failed to grab. Binding probably already taken"); + return Meta.KeyBindingAction.NONE; } let mutterName = Meta.external_binding_name_for_action(actionId); @@ -433,6 +460,8 @@ function enableAction(action) { } Main.wm.allowKeybinding(action.mutterName, Shell.ActionMode.ALL); + + return action.id; } }