Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create when condition for pin/unpin editor #49

Open
Dimfred opened this issue Jul 21, 2021 · 4 comments
Open

Unable to create when condition for pin/unpin editor #49

Dimfred opened this issue Jul 21, 2021 · 4 comments
Labels
upstream Issues related to upstream dependencies

Comments

@Dimfred
Copy link

Dimfred commented Jul 21, 2021

Hey I am trying to create the pin / unpin logic, like with the toggle project tree example in the docs, but I can't make it work. Do you have any suggestions?

{
    "key": "p",
    "name": "Pin / Unpin editor",
    "type": "conditional",
    "bindings": [
        {
            "key": "",
            "name": "default",
            "type": "command",
            "command": "workbench.action.pinEditor",
        },
        {
            "key": "when:activeEditorIsPinned",
            "name": "Unpin Editor",
            "type": "command",
            "command": "workbench.action.unpinEditor",
        },
    ]
}
@stevenguh
Copy link
Member

stevenguh commented Jul 22, 2021

You will need to add the following to you shortcut (keybindings.json).

{
	"key": "p",
	"command": "whichkey.triggerKey",
	"args": {
		"key": "p",
		"when": "activeEditorIsPinned"
	},
	"when": "whichkeyVisible && activeEditorIsPinned"
}

We can also choose to bundle this as default so we don't have two separate key for pin and unpin.


Just tested this and apparently, the activeEditorIsPinned context is unset once the focus exited the editor. So which-key can't the shortcut isn't being triggered (hence, not able to pass the when condition to which-key) when the menu is in focus.

Unfortunately, this is limit of the hack we have to capture context. Either vscode keep activeEditorIsPinned set or provide a getContext API; otherwise, this is something that we can't do atm.

@Dimfred
Copy link
Author

Dimfred commented Jul 22, 2021

Ah that is very sad. But thank you for the investigation, hope there something will happen soon, would be great to have that.

EDIT:
So I had some time and tinkered a bit around and got a hack for that. It basically uses xdotool to send a key, and that key represents the two toggle states.

keybindings.json:

{
    "key": "f5",
    "command": "workbench.action.pinEditor",
    "when": "!activeEditorIsPinned",
},
{
    "key": "f5",
    "command": "workbench.action.unpinEditor",
    "when": "activeEditorIsPinned",
},

settings.json:

{
    "key": "p",
    "name": "Pin/Unpin editor",
    "type": "command",
    "command": "workbench.action.terminal.sendSequence",
    // without cleaning
    "args": { "text": "i;xdotool key F5\n"},
    // with cleaning
    "args": { "text": "i;xdotool key F5;xdotool key Escape;clear\n"},
},

the ;i is only to leave the normal mode of my zsh. I also bound i() { return 0 } just to not get an error in zshrc.
I would also like the command to be executed in the background without showing it in the terminal history, but not sure whether this is possible.

@stevenguh
Copy link
Member

I am glad you are able to find a workaround.

I looked at the vscode api it doesn't seem like they provide anything to extension to know if the editor is pinned. To properly support this, we likely need to do a feature request to vscode for either

  • Have programmatic access to the editor pinned state
  • Add toggle command to toggle between pin and unpin

@Dimfred
Copy link
Author

Dimfred commented Aug 2, 2021

Yeah I think the second option is something they will do. If I have time I probably willl open an issue and request a unified command.

@stevenguh stevenguh added the upstream Issues related to upstream dependencies label Aug 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream Issues related to upstream dependencies
Projects
None yet
Development

No branches or pull requests

2 participants