typescriptvisual-studio-codevscode-extensionsvscode-api

How to get the current selected view everytime when view is changed from vscode API


I've added a new view in vscode while making an extension.

I want to run a function everytime when this view is opened.

Is there any vscode method to run a function everytime this view is opened or when user come on this view.

enter image description here


Solution

  • If your view is a TreeView, you can listen to its visibility:

    const treeViewVisibilityListener = this.tabView.onDidChangeVisibility(async event => {
        console.log("here");
    });
    context.subscriptions.push(treeViewVisibilityListener);
    

    event will have the values {visible: true} or {visible: false} depending on whether it was opened or not. It also works for when the view is hidden or unhidden from the Activity Bar or Panel or Secondary Bar - depending on where the user moves it.

    If your view is a Webview you can use the same .onDidChangeVisibility listener.

    These do not fire if the View clicked on/revealed is not yours.