Skip to content

'When' clauses are ignored for all commands in global menu #35225

Closed

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:

  1. Pick an unused shortcut, e.g. ctrl+l, and a context that would be inactive during the next steps, e.g. parameterHintsVisible.
  2. 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" }
  1. Set focus to the current text editor.
  2. 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

  1. usernamehw commented on Nov 10, 2017

    @usernamehw
    Contributor

    Sandeep Somavarapu (@sandy081) What is this issue: bug? as-designed? feature-request? Is there any milestone?

  2. changed the title [-]'When' clause contexts are ignored for shortcuts that toggle panels or sidebar[/-] [+]parameterHintsVisible-clause is ignored for shortcuts that toggle panels or sidebar[/+] on Nov 10, 2017
  3. added
    under-discussion
    on Nov 10, 2017
  4. jrieken commented on Nov 10, 2017

    @jrieken
    Contributor

    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?

  5. usernamehw commented on Nov 10, 2017

    @usernamehw
    Contributor

    I am not the issue author, but previous title was more precise: it's not limited to parameterHintsVisible context --- 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 except workbench.action.editor.changeLanguageMode ignore when context.

  6. monk-time commented on Nov 10, 2017

    @monk-time
    Author

    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, parameterHintsVisible is 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.

  7. changed the title [-]parameterHintsVisible-clause is ignored for shortcuts that toggle panels or sidebar[/-] [+] 'When' clauses are ignored for shortcuts that toggle panels or sidebar[/+] on Nov 10, 2017
  8. usernamehw commented on Nov 10, 2017

    @usernamehw
    Contributor

    Is this issue related #26318?

  9. 49 remaining items

  10. sbatten commented on Nov 1, 2018

    @sbatten
    Member

    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 lookupKeybinding returns the latest keybinding but ignores context. this should be changed imo to use an optionally supplied context.

    thoughts alexandrudima

  11. alexdima commented on Nov 2, 2018

    @alexdima
    Member

    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 IKeybindingService to 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)

  12. sbatten commented on Nov 2, 2018

    @sbatten
    Member

    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?

  13. alexdima commented on Nov 6, 2018

    @alexdima
    Member

    SteVen Batten (@sbatten)

    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?

    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" },

    editorTextFocus is undefined in the global context, so the when clause 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:

    1. lookup keybindings based on focused context and constantly update the menus.
    2. enhance Electron to provide information if a native menu item is invoked because the user clicked on it, or used arrow keys + enter, or invoked the keyboard shortcut. If we were to differentiate between these cases, then we could interpret ourselves the keyboard shortcut.
  14. jrieken commented on Nov 6, 2018

    @jrieken
    Contributor

    enhance Electron to provide information if a native menu item is invoked because the user clicked on it, or used arrow keys + enter, or invoked the keyboard shortcut.

    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+z the metaKey modifier is set, when clicking all are false.

    screenshot 2018-11-06 at 15 25 14

  15. sbatten commented on Nov 6, 2018

    @sbatten
    Member

    alexandrudima

    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.

    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.

    lookup keybindings based on focused context and constantly update the menus.

    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.

  16. added a commit that references this issue on Dec 3, 2018
    a0b7054
  17. locked and limited conversation to collaborators on Feb 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugkeybindingsverified

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions