Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically close vscode.window's showInformationMessage box

I just began learning about vscode extensions and I'm wondering if there is a simple way to programmatically close the information message box that is generated via vscode.window.showInformationMessage(). If you want to reproduce, I started with the WordCount demo here, and after copy/pasting the body of extension.ts, as outlined in the tutorial, I made some modifications to activate() like so...

export function activate(context: ExtensionContext) {
    let wordCounter = new WordCounter();
    let wcTimeout = null;
    let setWCTimeout = function() {
        clearWCTimeout();
        wcTimeout = setTimeout(clearWCTimeout, 1000);
    };
    let clearWCTimeout = function() {
        if (wcTimeout !== null) {
            clearTimeout(wcTimeout);
            wcTimeout = null;
            // clear/hide the message box here, but how?
        }
    };

    let disposable = commands.registerCommand('extension.sayHello', () => {
        wordCounter.updateWordCount();
        setWCTimeout();
        vscode.window
            .showInformationMessage(wordCounter._getWordCount(window.activeTextEditor.document).toString())
            .then(clearWCTimeout);
    });

    // Add to a list of disposables which are disposed when this extension is deactivated.
    context.subscriptions.push(wordCounter);
    context.subscriptions.push(wcTimeout);
    context.subscriptions.push(disposable);
}   

What I've tried, or considered:

  • calling vscode.window.showInformationMessage() with both null and empty string - does nothing, anything besides null|empty string results in a new information message box appearing
  • disposing of the command - didn't really have any reason to think this would work, and the command has to be re-registered before it will work again of course
  • tried to access the DOM to simply find the "Close" button and trigger a click on it - but no luck finding any kind of entry point for manipulating the DOM

Side note: I am interested in best practices within this paradigm. For instance, is there a popular node lib that wraps js timers that I might want to consider using? However, that is not my primary concern with this post. If you're going to comment on the delay mechanism (setTimeout()/clearTimeout()), please make it constructive in terms of what is best practice within this environment/paradigm (beyond "that's ugly", or "that's not how [you would personally] do it).

like image 754
Eric Lease Avatar asked Jan 20 '16 07:01

Eric Lease


1 Answers

Although it seems that the message does not have an explicit close API (https://github.com/Microsoft/vscode/issues/2732), my workaround is to use a progress to simulate an automatically-closed notification:

      vscode.window.withProgress(
        {
          location: vscode.ProgressLocation.Notification,
          title: 'Finding ...',
          cancellable: false,
        },
        async (progress, token) => {
         for (let i = 0; i < 10; i++) {
          setTimeout(() => {
            progress.report({ increment: i*10, message: title })
          }, 10000)
        }
       }
      )

like image 167
K. Symbol Avatar answered Oct 18 '22 16:10

K. Symbol