Progress API percentage is ambiguous (fixes #46743)

This commit is contained in:
Benjamin Pasero 2018-03-28 09:44:06 +02:00
parent 95b0cffe72
commit 19001175dd
4 changed files with 23 additions and 21 deletions

View file

@ -80,7 +80,7 @@ export interface IProgressOptions {
export interface IProgressStep {
message?: string;
percentage?: number;
increment?: number;
}
export const IProgressService2 = createDecorator<IProgressService2>('progressService2');

12
src/vs/vscode.d.ts vendored
View file

@ -4038,7 +4038,8 @@ declare module 'vscode' {
/**
* Report a progress update.
* @param value A progress item, like a message or an updated percentage value
* @param value A progress item, like a message and/or an
* report on how much work finished
*/
report(value: T): void;
}
@ -5213,9 +5214,10 @@ declare module 'vscode' {
* @param task A callback returning a promise. Progress state can be reported with
* the provided [progress](#Progress)-object.
*
* To report discrete progress, use `percentage` to indicate how much work has been completed. Each call with
* a `percentage` value will be summed up and reflected as overall progress until 100% is reached. Note that
* currently only `ProgressLocation.Notification` is capable of showing discrete progress.
* To report discrete progress, use `increment` to indicate how much work has been completed. Each call with
* a `increment` value will be summed up and reflected as overall progress until 100% is reached (a value of
* e.g. `10` accounts for `10%` of work done).
* Note that currently only `ProgressLocation.Notification` is capable of showing discrete progress.
*
* To monitor if the operation has been cancelled by the user, use the provided [`CancellationToken`](#CancellationToken).
* Note that currently only `ProgressLocation.Notification` is supporting to show a cancel button to cancel the
@ -5223,7 +5225,7 @@ declare module 'vscode' {
*
* @return The thenable the task-callback returned.
*/
export function withProgress<R>(options: ProgressOptions, task: (progress: Progress<{ message?: string; percentage?: number }>, token: CancellationToken) => Thenable<R>): Thenable<R>;
export function withProgress<R>(options: ProgressOptions, task: (progress: Progress<{ message?: string; increment?: number }>, token: CancellationToken) => Thenable<R>): Thenable<R>;
/**
* Creates a status bar [item](#StatusBarItem).

View file

@ -70,10 +70,10 @@ export class ExtHostProgress implements ExtHostProgressShape {
function mergeProgress(result: IProgressStep, currentValue: IProgressStep): IProgressStep {
result.message = currentValue.message;
if (typeof currentValue.percentage === 'number' && typeof result.message === 'number') {
result.percentage += currentValue.percentage;
} else if (typeof currentValue.percentage === 'number') {
result.percentage = currentValue.percentage;
if (typeof currentValue.increment === 'number' && typeof result.message === 'number') {
result.increment += currentValue.increment;
} else if (typeof currentValue.increment === 'number') {
result.increment = currentValue.increment;
}
return result;
}

View file

@ -166,10 +166,10 @@ export class ProgressService2 implements IProgressService2 {
}
}
private _withNotificationProgress<P extends Thenable<R>, R=any>(options: IProgressOptions, callback: (progress: IProgress<{ message?: string, percentage?: number }>) => P, onDidCancel?: () => void): P {
private _withNotificationProgress<P extends Thenable<R>, R=any>(options: IProgressOptions, callback: (progress: IProgress<{ message?: string, increment?: number }>) => P, onDidCancel?: () => void): P {
const toDispose: IDisposable[] = [];
const createNotification = (message: string, percentage?: number): INotificationHandle => {
const createNotification = (message: string, increment?: number): INotificationHandle => {
if (!message) {
return undefined; // we need a message at least
}
@ -201,7 +201,7 @@ export class ProgressService2 implements IProgressService2 {
actions
});
updateProgress(handle, percentage);
updateProgress(handle, increment);
once(handle.onDidDispose)(() => {
dispose(toDispose);
@ -210,26 +210,26 @@ export class ProgressService2 implements IProgressService2 {
return handle;
};
const updateProgress = (notification: INotificationHandle, percentage?: number): void => {
if (typeof percentage === 'number' && percentage >= 0) {
const updateProgress = (notification: INotificationHandle, increment?: number): void => {
if (typeof increment === 'number' && increment >= 0) {
notification.progress.total(100); // always percentage based
notification.progress.worked(percentage);
notification.progress.worked(increment);
} else {
notification.progress.infinite();
}
};
let handle: INotificationHandle;
const updateNotification = (message?: string, percentage?: number): void => {
const updateNotification = (message?: string, increment?: number): void => {
if (!handle) {
handle = createNotification(message, percentage);
handle = createNotification(message, increment);
} else {
if (typeof message === 'string') {
handle.updateMessage(message);
}
if (typeof percentage === 'number') {
updateProgress(handle, percentage);
if (typeof increment === 'number') {
updateProgress(handle, increment);
}
}
};
@ -240,7 +240,7 @@ export class ProgressService2 implements IProgressService2 {
// Update based on progress
const p = callback({
report: progress => {
updateNotification(progress.message, progress.percentage);
updateNotification(progress.message, progress.increment);
}
});