Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

progress api #18519

Merged
merged 18 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/vs/base/common/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ export class Disposables extends Disposable {
}
}

export class OneDisposable implements IDisposable {

private _value: IDisposable;

set value(value: IDisposable) {
if (this._value) {
this._value.dispose();
}
this._value = value;
}

dispose() {
this.value = null;
}
}

export interface IReference<T> extends IDisposable {
readonly object: T;
}
Expand Down
34 changes: 34 additions & 0 deletions src/vs/platform/progress/common/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,37 @@ export interface IProgressRunner {
worked(value: number): void;
done(): void;
}

export interface IProgress<T> {
report(item: T): void;
}

export class Progress<T> implements IProgress<T> {

private _callback: () => void;
private _value: T;

constructor(callback: () => void) {
this._callback = callback;
}

get value() {
return this._value;
}

report(item: T) {
this._value = item;
this._callback();
}
}

export const IProgressService2 = createDecorator<IProgressService2>('progressService2');

export interface IProgressService2 {

_serviceBrand: any;

withWindowProgress(task: (progress: IProgress<string>) => TPromise<any>): void;

withViewletProgress(viewletId: string, task: (progress: IProgress<number>) => TPromise<any>): void;
}
22 changes: 22 additions & 0 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,30 @@

declare module 'vscode' {

/**
* Defines a generalized way of reporing progress updates.
*/
export interface Progress<T> {

/**
* Report a progress update.
* @param value A progress item, like a message or an updated percentage value
*/
report(value: T): void
}

export namespace window {

/**
* Show window-wide progress, e.g. in the status bar, for the provided task. The task is
* considering running as long as the promise it returned isn't resolved or rejected.
*
* @param task A function callback that represents a long running operation.
*/
export function withWindowProgress(task: (progress: Progress<string>, token: CancellationToken) => Thenable<any>): void;

export function withScmProgress(task: (progress: Progress<number>) => Thenable<any>): void;

export function sampleFunction(): Thenable<any>;
}

Expand Down
8 changes: 8 additions & 0 deletions src/vs/workbench/api/node/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ExtHostDiagnostics } from 'vs/workbench/api/node/extHostDiagnostics';
import { ExtHostTreeExplorers } from 'vs/workbench/api/node/extHostTreeExplorers';
import { ExtHostWorkspace } from 'vs/workbench/api/node/extHostWorkspace';
import { ExtHostQuickOpen } from 'vs/workbench/api/node/extHostQuickOpen';
import { ExtHostProgress } from 'vs/workbench/api/node/extHostProgress';
import { ExtHostSCM } from 'vs/workbench/api/node/extHostSCM';
import { ExtHostHeapService } from 'vs/workbench/api/node/extHostHeapService';
import { ExtHostStatusBar } from 'vs/workbench/api/node/extHostStatusBar';
Expand Down Expand Up @@ -113,6 +114,7 @@ export function createApiFactory(initData: IInitData, threadService: IThreadServ
// Other instances
const extHostMessageService = new ExtHostMessageService(threadService);
const extHostStatusBar = new ExtHostStatusBar(threadService);
const extHostProgress = new ExtHostProgress(threadService.get(MainContext.MainThreadProgress));
const extHostOutputService = new ExtHostOutputService(threadService);
const workspacePath = contextService.hasWorkspace() ? contextService.getWorkspace().resource.fsPath : undefined;
const extHostWorkspace = new ExtHostWorkspace(threadService, workspacePath);
Expand Down Expand Up @@ -306,6 +308,12 @@ export function createApiFactory(initData: IInitData, threadService: IThreadServ
setStatusBarMessage(text: string, timeoutOrThenable?: number | Thenable<any>): vscode.Disposable {
return extHostStatusBar.setStatusBarMessage(text, timeoutOrThenable);
},
withWindowProgress: proposedApiFunction(extension, <R>(task: (progress: vscode.Progress<string>, token: vscode.CancellationToken) => Thenable<R>): Thenable<R> => {
return extHostProgress.withWindowProgress(extension, task);
}),
withScmProgress: proposedApiFunction(extension, (task: (progress: vscode.Progress<number>) => Thenable<any>) => {
return extHostProgress.withScmProgress(extension, task);
}),
createOutputChannel(name: string): vscode.OutputChannel {
return extHostOutputService.createOutputChannel(name);
},
Expand Down
4 changes: 3 additions & 1 deletion src/vs/workbench/api/node/extHost.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { MainThreadLanguageFeatures } from './mainThreadLanguageFeatures';
import { MainThreadLanguages } from './mainThreadLanguages';
import { MainThreadMessageService } from './mainThreadMessageService';
import { MainThreadOutputService } from './mainThreadOutputService';
import { MainThreadProgress } from './mainThreadProgress';
import { MainThreadQuickOpen } from './mainThreadQuickOpen';
import { MainThreadStatusBar } from './mainThreadStatusBar';
import { MainThreadStorage } from './mainThreadStorage';
Expand Down Expand Up @@ -75,6 +76,7 @@ export class ExtHostContribution implements IWorkbenchContribution {
col.define(MainContext.MainThreadLanguages).set(create(MainThreadLanguages));
col.define(MainContext.MainThreadMessageService).set(create(MainThreadMessageService));
col.define(MainContext.MainThreadOutputService).set(create(MainThreadOutputService));
col.define(MainContext.MainThreadProgress).set(create(MainThreadProgress));
col.define(MainContext.MainThreadQuickOpen).set(create(MainThreadQuickOpen));
col.define(MainContext.MainThreadStatusBar).set(create(MainThreadStatusBar));
col.define(MainContext.MainThreadStorage).set(create(MainThreadStorage));
Expand All @@ -99,4 +101,4 @@ export class ExtHostContribution implements IWorkbenchContribution {
// Register File Tracker
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(
ExtHostContribution
);
);
7 changes: 7 additions & 0 deletions src/vs/workbench/api/node/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ export abstract class MainThreadOutputServiceShape {
$close(channelId: string): TPromise<void> { throw ni(); }
}

export abstract class MainThreadProgressShape {
$progressStart(handle: number, extensionId: string, location: string): void { throw ni(); }
$progressReport(handle: number, message: string): void { throw ni(); }
$progressEnd(handle: number, err?: any): void { throw ni(); }
}

export abstract class MainThreadTerminalServiceShape {
$createTerminal(name?: string, shellPath?: string, shellArgs?: string[], waitOnExit?: boolean): TPromise<number> { throw ni(); }
$dispose(terminalId: number): void { throw ni(); }
Expand Down Expand Up @@ -400,6 +406,7 @@ export const MainContext = {
MainThreadLanguages: createMainId<MainThreadLanguagesShape>('MainThreadLanguages', MainThreadLanguagesShape),
MainThreadMessageService: createMainId<MainThreadMessageServiceShape>('MainThreadMessageService', MainThreadMessageServiceShape),
MainThreadOutputService: createMainId<MainThreadOutputServiceShape>('MainThreadOutputService', MainThreadOutputServiceShape),
MainThreadProgress: createMainId<MainThreadProgressShape>('MainThreadProgress', MainThreadProgressShape),
MainThreadQuickOpen: createMainId<MainThreadQuickOpenShape>('MainThreadQuickOpen', MainThreadQuickOpenShape),
MainThreadStatusBar: createMainId<MainThreadStatusBarShape>('MainThreadStatusBar', MainThreadStatusBarShape),
MainThreadStorage: createMainId<MainThreadStorageShape>('MainThreadStorage', MainThreadStorageShape),
Expand Down
55 changes: 55 additions & 0 deletions src/vs/workbench/api/node/extHostProgress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';

import { Progress, CancellationToken } from 'vscode';
import { MainThreadProgressShape } from './extHost.protocol';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';

export class ExtHostProgress {

private _proxy: MainThreadProgressShape;
private _handles: number = 0;

constructor(proxy: MainThreadProgressShape) {
this._proxy = proxy;
}

withWindowProgress<R>(extension: IExtensionDescription, task: (progress: Progress<string>, token: CancellationToken) => Thenable<R>): Thenable<R> {
return this._withProgress(extension, 'window', task);
}

withScmProgress<R>(extension: IExtensionDescription, task: (progress: Progress<number>) => Thenable<R>): Thenable<R> {
return this._withProgress(extension, 'scm', task);
}

private _withProgress<R>(extension: IExtensionDescription, type: string, task: (progress: Progress<any>, token: CancellationToken) => Thenable<R>): Thenable<R> {
const handle = this._handles++;

this._proxy.$progressStart(handle, extension.id, type);
const progress = {
report: (message: string) => {
this._proxy.$progressReport(handle, message);
}
};

let p: Thenable<R>;

try {
p = task(progress, null);
} catch (err) {
this._proxy.$progressEnd(handle);
throw err;
}

return p.then(result => {
this._proxy.$progressEnd(handle);
return result;
}, err => {
this._proxy.$progressEnd(handle, err);
throw err;
});
}
}
55 changes: 55 additions & 0 deletions src/vs/workbench/api/node/mainThreadProgress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';

import { IProgressService2, IProgress } from 'vs/platform/progress/common/progress';
import { TPromise } from 'vs/base/common/winjs.base';
import { MainThreadProgressShape } from './extHost.protocol';

export class MainThreadProgress extends MainThreadProgressShape {

private _progressService: IProgressService2;
private progress = new Map<number, { resolve: Function, reject: Function, progress: IProgress<any> }>();

constructor(
@IProgressService2 progressService: IProgressService2
) {
super();
this._progressService = progressService;
}


$progressStart(handle: number, extensionId: string, where: string): void {

const task = (progress: IProgress<any>) => {
return new TPromise<any>((resolve, reject) => {
this.progress.set(handle, { resolve, reject, progress });
});
};

switch (where) {
case 'window':
this._progressService.withWindowProgress(task);
break;
case 'scm':
this._progressService.withViewletProgress('workbench.view.scm', task);
break;
}

}

$progressReport(handle: number, message: any): void {
this.progress.get(handle).progress.report(message);
}

$progressEnd(handle: number, err: any): void {
if (err) {
this.progress.get(handle).reject(err);
} else {
this.progress.get(handle).resolve();
}
this.progress.delete(handle);
}
}
69 changes: 44 additions & 25 deletions src/vs/workbench/browser/parts/activitybar/activitybarPart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { IStorageService } from 'vs/platform/storage/common/storage';
import { Scope as MementoScope } from 'vs/workbench/common/memento';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
import { dispose } from 'vs/base/common/lifecycle';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { ToggleActivityBarVisibilityAction } from 'vs/workbench/browser/actions/toggleActivityBarVisibility';

interface IViewletActivity {
Expand All @@ -49,7 +49,7 @@ export class ActivitybarPart extends Part implements IActivityBarService {

private viewletIdToActions: { [viewletId: string]: ActivityAction; };
private viewletIdToActionItems: { [viewletId: string]: IActionItem; };
private viewletIdToActivity: { [viewletId: string]: IViewletActivity; };
private viewletIdToActivityStack: { [viewletId: string]: IViewletActivity[]; };

private memento: any;
private pinnedViewlets: string[];
Expand All @@ -68,7 +68,7 @@ export class ActivitybarPart extends Part implements IActivityBarService {

this.viewletIdToActionItems = Object.create(null);
this.viewletIdToActions = Object.create(null);
this.viewletIdToActivity = Object.create(null);
this.viewletIdToActivityStack = Object.create(null);

this.memento = this.getMemento(this.storageService, MementoScope.GLOBAL);

Expand Down Expand Up @@ -121,27 +121,51 @@ export class ActivitybarPart extends Part implements IActivityBarService {
}
}

public showActivity(viewletId: string, badge?: IBadge, clazz?: string): void {
public showActivity(viewletId: string, badge: IBadge, clazz?: string): IDisposable {

// Update Action with activity
const activity = <IViewletActivity>{ badge, clazz };
const stack = this.viewletIdToActivityStack[viewletId] || (this.viewletIdToActivityStack[viewletId] = []);
stack.unshift(activity);

this.updateActivity(viewletId);

return {
dispose: () => {
const stack = this.viewletIdToActivityStack[viewletId];
if (!stack) {
return;
}
const idx = stack.indexOf(activity);
if (idx < 0) {
return;
}
stack.splice(idx, 1);
if (stack.length === 0) {
delete this.viewletIdToActivityStack[viewletId];
}
this.updateActivity(viewletId);
}
};
}

private updateActivity(viewletId: string) {
const action = this.viewletIdToActions[viewletId];
if (action) {
if (!action) {
return;
}
const stack = this.viewletIdToActivityStack[viewletId];
if (!stack || !stack.length) {
// reset
action.setBadge(undefined);

} else {
// update
const [{badge, clazz}] = stack;
action.setBadge(badge);
if (clazz) {
action.class = clazz;
}
}

// Keep for future use
if (badge) {
this.viewletIdToActivity[viewletId] = { badge, clazz };
} else {
delete this.viewletIdToActivity[viewletId];
}
}

public clearActivity(viewletId: string): void {
this.showActivity(viewletId, null);
}

public createContentArea(parent: Builder): Builder {
Expand Down Expand Up @@ -268,19 +292,14 @@ export class ActivitybarPart extends Part implements IActivityBarService {

// Make sure to restore activity
Object.keys(this.viewletIdToActions).forEach(viewletId => {
const activity = this.viewletIdToActivity[viewletId];
if (activity) {
this.showActivity(viewletId, activity.badge, activity.clazz);
} else {
this.showActivity(viewletId);
}
this.updateActivity(viewletId);
});
}

// Add overflow action as needed
if (visibleViewletsChange && overflows) {
this.viewletOverflowAction = this.instantiationService.createInstance(ViewletOverflowActivityAction, () => this.viewletOverflowActionItem.showMenu());
this.viewletOverflowActionItem = this.instantiationService.createInstance(ViewletOverflowActivityActionItem, this.viewletOverflowAction, () => this.getOverflowingViewlets(), (viewlet: ViewletDescriptor) => this.viewletIdToActivity[viewlet.id] && this.viewletIdToActivity[viewlet.id].badge);
this.viewletOverflowActionItem = this.instantiationService.createInstance(ViewletOverflowActivityActionItem, this.viewletOverflowAction, () => this.getOverflowingViewlets(), (viewlet: ViewletDescriptor) => this.viewletIdToActivityStack[viewlet.id] && this.viewletIdToActivityStack[viewlet.id][0].badge);

this.viewletSwitcherBar.push(this.viewletOverflowAction, { label: true, icon: true });
}
Expand Down Expand Up @@ -460,4 +479,4 @@ export class ActivitybarPart extends Part implements IActivityBarService {
// Pass to super
super.shutdown();
}
}
}
Loading