如何以编程方式关闭 vscode.window 的 showInformationMessage 框 [英] How to programmatically close vscode.window's showInformationMessage box

查看:43
本文介绍了如何以编程方式关闭 vscode.window 的 showInformationMessage 框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 vscode 扩展,我想知道是否有一种简单的方法可以以编程方式关闭通过 vscode.window.showInformationMessage() 生成的信息消息框.如果你想重现,我从 WordCount demo here 开始,然后在复制/粘贴 extension.ts 的正文后,如教程中所述,我对 activate() 进行了一些修改,如下所示...

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

我尝试过或考虑过的:

  • 使用 null 和空字符串调用 vscode.window.showInformationMessage() - 什么都不做,除了 null| 空字符串导致出现一个新的信息消息框
  • 处理命令 - 真的没有任何理由认为这会起作用,当然必须重新注册命令才能再次起作用
  • 试图访问 DOM 以简单地找到关闭"按钮并触发点击它 - 但没有找到任何类型的操作 DOM 的入口点
  • 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

旁注:我对这种范式中的最佳实践感兴趣.例如,是否有一个流行的 node lib 包装了我可能想要考虑使用的 js 计时器?然而,这不是我对这篇文章的主要关注.如果您要对延迟机制 (setTimeout()/clearTimeout()) 发表评论,请在此环境/范式中的最佳实践方面使其具有建设性(超越这很丑陋"),或这不是[你个人]会这样做的方式).

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

推荐答案

目前无法实现.

在 github 上针对 vscode 提出了一个相关问题:https://github.com/Microsoft/vscode/issues/2732

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.

这篇关于如何以编程方式关闭 vscode.window 的 showInformationMessage 框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆