Repository navigation
Description
- VSCode Version: Code - Insiders 1.17.0-insider (f7962f0, 2017-09-20T05:24:33.599Z)
- OS Version: Windows_NT x64 6.1.7601
- Extensions: none
Key bindings for commands that toggle sidebar or panel visibility ignore 'when' clause contexts, making it impossible to limit them to certain contexts.
Use case: some shortcut schemes (e.g. Intellij IDEA) have a shortcut to hide current active sidebar or panel, and a VScode extension that provides such keybindings wants to recreate this functionality through a shortcut that toggles panels and the sidebar only when they are active, essentially turning a toggle into a closer. But since 'when' clause contexts for these actions always evaluate to true, the shortcuts become globally active and act as toggles. And while the last three shortcuts from that snippet are replaceable with a global shortcut for workbench.action.closePanel, there is no corresponding action to close (not toggle) the sidebar.
Steps to reproduce:
- Pick an unused shortcut, e.g.
ctrl+l, and a context that would be inactive during the next steps, e.g.parameterHintsVisible. - Add any one of the following settings to
keybindings.json:
{ "key": "ctrl+l", "command": "workbench.action.toggleSidebarVisibility",
"when": "parameterHintsVisible" }{ "key": "ctrl+l", "command": "workbench.action.togglePanel",
"when": "parameterHintsVisible" }{ "key": "ctrl+l", "command": "workbench.actions.view.problems",
"when": "parameterHintsVisible" }{ "key": "ctrl+l", "command": "workbench.debug.action.toggleRepl",
"when": "parameterHintsVisible" }{ "key": "ctrl+l", "command": "workbench.action.terminal.toggleTerminal",
"when": "parameterHintsVisible" }- Set focus to the current text editor.
- Press
ctrl+l.
Expected result: nothing should happen, the 'when' clause evaluates to false so the shortcut should be disabled.
Actual result: the shortcut works, the relevant panel/sidebar visibility is toggled.
Activity
usernamehw commentedon Nov 10, 2017
Sandeep Somavarapu (@sandy081) What is this issue:
bug?as-designed?feature-request? Is there any milestone?[-]'When' clause contexts are ignored for shortcuts that toggle panels or sidebar[/-][+]parameterHintsVisible-clause is ignored for shortcuts that toggle panels or sidebar[/+]jrieken commentedon Nov 10, 2017
Dmitry Bogorad (@monk-time) The
parameterHintsVisible-clause is set within an editor and its value represents if the signature/parameter help widget is visible or not. That usually happens when typing code, e.g function invocations. So, my question is why you depend on that clause?usernamehw commentedon Nov 10, 2017
I am not the issue author, but previous title was more precise: it's not limited to
parameterHintsVisiblecontext --- these commands ignore ALL when contexts (at least the ones that I tried).This works:
{ "key": "ctrl+shift+8", "command": "editor.action.insertSnippet", "when": "editorLangId == 'javascript'" }This doesn't:
{ "key": "ctrl+shift+9", "command": "workbench.action.togglePanel", "when": "editorLangId == 'javascript'" }Is this a known issue? All
workbench*commands exceptworkbench.action.editor.changeLanguageModeignore when context.monk-time commentedon Nov 10, 2017
Johannes Rieken (@jrieken) The poster above is right, and I admit I could've made it clearer: when I was testing this, all clauses that I've tried were ignored. As I wrote in Step 1,
parameterHintsVisibleis just an example of a clause that is most likely to be false, it could be any other clause.I'll change the title back if you don't mind.
[-]parameterHintsVisible-clause is ignored for shortcuts that toggle panels or sidebar[/-][+] 'When' clauses are ignored for shortcuts that toggle panels or sidebar[/+]usernamehw commentedon Nov 10, 2017
Is this issue related #26318?
49 remaining items
sbatten commentedon Nov 1, 2018
sorry, my fixes actually bring back the behavior described here. the behavior of keybindings working out of context needs to be fixed in the keybindingservice because currently
lookupKeybindingreturns the latest keybinding but ignores context. this should be changed imo to use an optionally supplied context.thoughts alexandrudima
alexdima commentedon Nov 2, 2018
SteVen Batten (@sbatten) As we are all aware from multiple discussions on this topic, contexts are different depending on where the focus sits. I can implement a new method in
IKeybindingServiceto look up keybindings based on context, but then won't you have 100% the same problem as the one described in #55453 , which cannot be solved due to performance concerns -- specifically #55453 (comment)sbatten commentedon Nov 2, 2018
alexandrudima this iteration, I have made some improvements that greatly reduce the amount of updates the menu based on context. My current view is that the global application menu reflects the global context so enablement/disablement/keybindings must be based on context keys in the global scope. Thoughts?
alexdima commentedon Nov 6, 2018
SteVen Batten (@sbatten)
That is also incorrect, but in a different, more subtle way, and arguably would make things a lot worse. Before jumping to implement something worse, let's look at the problem.
Take, for example, the Selection menu:
Now let's look at a default keybinding from there:
{ "key": "shift+alt+up", "command": "editor.action.copyLinesUpAction", "when": "editorTextFocus && !editorReadonly" },editorTextFocusis undefined in the global context, so thewhenclause would not hold. So, the action would get no keybinding rendered next to it if we were to lookup keybindings using the global context.Also, I think this bug affects only the native menu, because in the native menu case the menu itself (Electron via C++ code) listens to those keybindings and executes the menu item directly, outside of our logic. For custom menus, this is no longer a problem.
I think there are two possible correct fixes:
jrieken commentedon Nov 6, 2018
We might be able to know that. I have tested mac and clicking vs keyboard shortcuts can be differentiated by the event - it's only partial but when pressing
cmd+zthemetaKeymodifier is set, when clicking all are false.sbatten commentedon Nov 6, 2018
alexandrudima
This was more of a statement of how I think things should be not necessarily how they are, but I agree this should be fleshed out. With this view actually in place, it limits the menu updates to relevant updates only for enablement/disablement/keybindings thanks to the support in Johannes Rieken (@jrieken)'s menu model.
For keybindings, we could fix this issue by trying to update the menu on every focus change, but that seems excessive. We also would still run into this issue with enablement/disablement, however. Keybindings get resolved for the menu each time it is updated. Enablement/Disablement does not. The context is set once when the menus are created in the model.
This makes me ask the question why the context service doesn't support evaluating against the current context based on focus if that's what matters instead of being required to track focus around. If I could set a flag on the context service to do this before handing it to the menu model and the keybinding service could use that when evaluating keybinds, it could work.
Yes, I am coupling two issues but they are both related to resolving commands against a focused context.
Fixes #35225: Detect menu items invoked via keybinding and dispatch k…