user1438833
user1438833

Reputation: 61

VSCode extension: How to programmatically show view container in panel

I am writing a VSCode extension which has a view container with a WebviewView added to it.

"contributes": {
    "commands": [
      {
        "command": "showView",
        "title": "Show view"
      }
    ],
    "viewsContainers": {
      "panel": [
        {
          "id": "mycontainer",
          "title": "My Container",
          "icon": "media/app.svg"
        }
      ]
    },
    "views": {
      "mycontainer": [
        {
          "type": "webview",
          "id": "myview",
          "name": "MyView"
        }
      ]
    }
  },

In showView command implementation. I want to programmatically make the view myview to display in VSCode UI. How to do that?

Upvotes: 6

Views: 3099

Answers (2)

deammer
deammer

Reputation: 329

You can focus on a view in the sidebar by using the view's id and executing the focus command:

vscode.commands.executeCommand("myview.focus")

Upvotes: 14

DAC
DAC

Reputation: 366

VSCode exposes a focus command on every view registered.

Your example view has ID myview so you want to call the myview.focus command.

You can verify this by opening File->Prefs->Keyboard Shortcuts and searching for myview.

To test the command, try adding the following in your extension activate function. It will create a clickable button on the bottom statusbar that will focus your sidebar view.

activate = function (workspace) {
    const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 10);
    statusBarItem.command = 'myview.focus';
    statusBarItem.text = 'MYVIEW';
    statusBarItem.show();
    workspace.subscriptions.push(statusBarItem);
}

Upvotes: 4

Related Questions