microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
160.51k stars 28.1k forks source link

Add a webview/title contribution point #174905

Open DanTup opened 1 year ago

DanTup commented 1 year ago

I'd like to add a button to the title bar for a webview editor:

Screenshot 2023-02-21 at 10 59 46

There's a webview/context contribution point, but not webview/title. I tried using editor/title and setting when: webviewId == dartDevTools however that doesn't appear either.

For tools that can run embedded or externally in a browser (which is probably common for tools being embedded as WebViews), having an option to add a button like "pop out in browser" would be useful while VS Code doesn't have multi-window support.

dlschmidt commented 1 year ago

I have also just encountered this missing contribution point.

eamodio commented 8 months ago

You can use editor/title and the activeWebviewPanelId context key. I also add resourceScheme == webview-panel to be extra safe too

eamodio commented 8 months ago

Related: https://github.com/microsoft/vscode/issues/195960

DanTup commented 7 months ago

@eamodio how can you relate this back to the webview it was for? It seems like the first argument passed to the command is just a URI (like webview-panel:webview-panel/webview-4ce7551a-1749-423a-9e91-408a4856efd4) but I have no idea how to determine which webview panel this was for (specifically, I can't seem to access anything to match up against when I called createWebviewPanel).

eamodio commented 7 months ago

@DanTup for editor/title use activeWebviewPanelId which is set to the viewType you pass into window.createWebviewPanel, but if you want to add items to the tab context menu editor/title/context it only works if the tab is the active tab, which is why I opened: https://github.com/microsoft/vscode/issues/195960

Here is an example from GitLens: https://github.com/gitkraken/vscode-gitlens/blob/05ad596a99e3552d87b3d8eef47182dec2532472/package.json#L11017-L11026

DanTup commented 7 months ago

@eamodio what I meant was, when a user clicks the button and it runs my command - how can I tell from within the command which webview the button was clicked for? (eg. I may spawn multiple webviews dynamically at runtime, so I can't use unique webViewIds and command IDs in package.json).

The first argument to the command is a URI, but it doesn't contain enough information for me to relate it back to my call to createWebviewPanel. My understanding is that commands in editor/title should be specific to the editor, but that's not possible if we can't determine which editor they were clicked in.

(Looking at the GitLens example, it seems like those commands are probably "global" and not specific to the editor they're attached to?)

eamodio commented 7 months ago

Ah, yeah, that isn't ideal. I have those editor/title commands registered "centrally" and then just delegate the desired action to the active webview of the correct type.