Theia Message Service
消息服务允许您向用户显示消息、交互式对话和进度信息。 您可以注入 MessageService 并在其上调用 info、warn 或 error 来报告您的消息(请参见下面的代码示例):
@inject(MessageService) private readonly messageService: MessageService this.messageService.info('Hello World!')
默认情况下,Theia 会在右下角以 toast 通知的形式显示消息。 您可以在下面看到不同消息类型(信息、警告和错误)的屏幕截图。 请注意,您可以通过提供自定义 MessageClient 来实现显示消息的不同行为。
Info

Warn

Error

默认情况下,通知将一直显示,直到用户关闭它们。 您可以选择定义一个超时时间,之后消息将自动关闭:
this.messageService.info('Say Hello with timeout',{timeout: 3000})
或者,您还可以添加用户可以执行的操作。 如果用户执行一个动作,消息服务调用将解析为已移交的动作字符串。
在下面的代码示例中,我们提供了“Say Hello again!” 和“Cancel”这两个动作。 点击Say Hello again将弹出另一条消息,“取消”将不做任何操作。
@inject(MessageService) private readonly messageService: MessageService this.messageService .error("Hello World!", "Say Hello again!", "Cancel") .then((action) => { if (action === "Say Hello again!") this.messageService.info("Hello again!"); })
相应的 toast 通知将如下所示:

当用户选择“Say Hello again”时,将显示另一个 toast 通知:

进度条
消息服务还允许您报告正在进行操作的进度。 您可以增量更新进度条和消息,同时 toast 通知保持可见,直到操作完成。 下面的示例打开一个进度条,并在完成之前将状态更新 3 次。 更多详细信息请参见 MessageService 的 TypeDoc。
this.messageService .showProgress({ text: `Doing something`, }) .then((progress) => { // Do something progress.report({ message: "First step completed", work: { done: 10, total: 100 }, }); // Do something progress.report({ message: "Next step completed", work: { done: 80, total: 100 }, }); // Do something progress.report({ message: "Complete", work: { done: 100, total: 100 }, }); progress.cancel(); })
请注意,progress.cancel 也用于表示进度已完成。 上面的代码示例将显示如下:


浙公网安备 33010602011771号