简体   繁体   中英

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).

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)
        }
       }
      )

It is currently not possible.

There was a related issue raised on github for vscode: https://github.com/Microsoft/vscode/issues/2732

The reasoning given in the response for not allowing information messages to be closed is that "their intent is that a user must react on them" .

Using the status bar for messages which need to be updated / dismissed is suggested there as the recommended approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM