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

154 Views Asked by At

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

1

There are 1 best solutions below

0
On BEST ANSWER

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.