From 1f618a27aa9439d05a7be29d142b3074ebebddd3 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Thu, 9 Jan 2020 12:46:26 +0100 Subject: [PATCH] add IProgressNotificationOptions#delay, #87449 --- src/vs/platform/progress/common/progress.ts | 1 + .../progress/browser/progressService.ts | 41 ++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/vs/platform/progress/common/progress.ts b/src/vs/platform/progress/common/progress.ts index 560ed3de651..49c827f2bbc 100644 --- a/src/vs/platform/progress/common/progress.ts +++ b/src/vs/platform/progress/common/progress.ts @@ -57,6 +57,7 @@ export interface IProgressNotificationOptions extends IProgressOptions { readonly location: ProgressLocation.Notification; readonly primaryActions?: ReadonlyArray; readonly secondaryActions?: ReadonlyArray; + delay?: number; } export interface IProgressWindowOptions extends IProgressOptions { diff --git a/src/vs/workbench/services/progress/browser/progressService.ts b/src/vs/workbench/services/progress/browser/progressService.ts index 562773832a5..4a5831b5667 100644 --- a/src/vs/workbench/services/progress/browser/progressService.ts +++ b/src/vs/workbench/services/progress/browser/progressService.ts @@ -146,10 +146,7 @@ export class ProgressService extends Disposable implements IProgressService { private withNotificationProgress

, R = unknown>(options: IProgressNotificationOptions, callback: (progress: IProgress<{ message?: string, increment?: number }>) => P, onDidCancel?: (choice?: number) => void): P { const toDispose = new DisposableStore(); - const createNotification = (message: string | undefined, increment?: number): INotificationHandle | undefined => { - if (!message) { - return undefined; // we need a message at least - } + const createNotification = (message: string, increment?: number): INotificationHandle => { const primaryActions = options.primaryActions ? Array.from(options.primaryActions) : []; const secondaryActions = options.secondaryActions ? Array.from(options.secondaryActions) : []; @@ -222,21 +219,34 @@ export class ProgressService extends Disposable implements IProgressService { }; let handle: INotificationHandle | undefined; + let handleSoon: any | undefined; + + let titleAndMessage: string | undefined; // hoisted to make sure a delayed notification shows the most recent message + const updateNotification = (message?: string, increment?: number): void => { - if (!handle) { - handle = createNotification(message, increment); + + // full message (inital or update) + if (message && options.title) { + titleAndMessage = `${options.title}: ${message}`; // always prefix with overall title if we have it (https://github.com/Microsoft/vscode/issues/50932) } else { - if (typeof message === 'string') { - let newMessage: string; - if (typeof options.title === 'string') { - newMessage = `${options.title}: ${message}`; // always prefix with overall title if we have it (https://github.com/Microsoft/vscode/issues/50932) - } else { - newMessage = message; - } + titleAndMessage = options.title || message; + } - handle.updateMessage(newMessage); + if (!handle && titleAndMessage) { + // create notification now or after a delay + if (typeof options.delay === 'number' && options.delay > 0) { + if (typeof handleSoon !== 'number') { + handleSoon = setTimeout(() => handle = createNotification(titleAndMessage!, increment), options.delay); + } + } else { + handle = createNotification(titleAndMessage, increment); } + } + if (handle) { + if (titleAndMessage) { + handle.updateMessage(titleAndMessage); + } if (typeof increment === 'number') { updateProgress(handle, increment); } @@ -244,7 +254,7 @@ export class ProgressService extends Disposable implements IProgressService { }; // Show initially - updateNotification(options.title); + updateNotification(); // Update based on progress const promise = callback({ @@ -255,6 +265,7 @@ export class ProgressService extends Disposable implements IProgressService { // Show progress for at least 800ms and then hide once done or canceled Promise.all([timeout(800), promise]).finally(() => { + clearTimeout(handleSoon); if (handle) { handle.close(); } -- GitLab