Skip to content

Commit 7ba57e1

Browse files
✨ feat(notification): 优化通知体验并修复超时关闭功能
- 为 info, warn, 和 error 通知增加了默认的 3000ms 自动关闭超时。 - 此更改旨在改善用户体验,使非关键信息可以自动消失。 - 修复了用于在超时后关闭通知的命令,从 `closeNotification` 更正为 `workbench.action.closeMessages`,确保其能正常工作。
1 parent 2695bb6 commit 7ba57e1

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/utils/notification/notification-manager.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,13 @@ const showNotification = async (
9696

9797
// 处理超时
9898
if (options?.timeout) {
99-
await Promise.all([
100-
new Promise((resolve) => setTimeout(resolve, options.timeout)),
101-
vscode.commands.executeCommand("closeNotification"),
102-
]);
99+
const closePromise = new Promise<void>((resolve) => {
100+
setTimeout(() => {
101+
vscode.commands.executeCommand("workbench.action.closeMessages");
102+
resolve();
103+
}, options.timeout);
104+
});
105+
await closePromise;
103106
}
104107
return result;
105108
} catch (error) {
@@ -144,13 +147,28 @@ export const notify: INotify = {
144147
showNotification(item.type, item.messageKey, item.args, item.options),
145148

146149
info: (messageKey: string, args?: any[], options?: NotificationOptions) =>
147-
notify.add({ type: "info", messageKey, args, options }),
150+
notify.add({
151+
type: "info",
152+
messageKey,
153+
args,
154+
options: { timeout: DEFAULT_TIMEOUT, ...options },
155+
}),
148156

149157
warn: (messageKey: string, args?: any[], options?: NotificationOptions) =>
150-
notify.add({ type: "warn", messageKey, args, options }),
158+
notify.add({
159+
type: "warn",
160+
messageKey,
161+
args,
162+
options: { timeout: DEFAULT_TIMEOUT, ...options },
163+
}),
151164

152165
error: (messageKey: string, args?: any[], options?: NotificationOptions) =>
153-
notify.add({ type: "error", messageKey, args, options }),
166+
notify.add({
167+
type: "error",
168+
messageKey,
169+
args,
170+
options: { timeout: DEFAULT_TIMEOUT, ...options },
171+
}),
154172

155173
// 快捷方法
156174
confirm: (messageKey: string, onYes: () => void, onNo?: () => void) =>

0 commit comments

Comments
 (0)