Home > front end >  How to get the current selected view everytime when view is changed from vscode API
How to get the current selected view everytime when view is changed from vscode API

Time:01-02

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

CodePudding user response:

I think you're looking for onDidChangeActiveTextEditor

Code Example

const vscode = require('vscode');

function runFunction() {
  // Your code here
}

vscode.window.onDidChangeActiveTextEditor(event => {
  if (event.document.uri.scheme === 'your-view-scheme') {
    runFunction();
  }
});

CodePudding user response:

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.

  • Related