From 55cbf197b483d644bb8ac32ed03c14ea94638ad8 Mon Sep 17 00:00:00 2001 From: isidor Date: Tue, 11 Dec 2018 12:11:44 +0100 Subject: [PATCH] merge viewletService into sidebarPart fixes #63145 --- .../browser/parts/sidebar/sidebarPart.ts | 104 +++++++++++++---- .../workbench/electron-browser/workbench.ts | 15 +-- .../viewlet/browser/viewletService.ts | 109 ------------------ 3 files changed, 89 insertions(+), 139 deletions(-) delete mode 100644 src/vs/workbench/services/viewlet/browser/viewletService.ts diff --git a/src/vs/workbench/browser/parts/sidebar/sidebarPart.ts b/src/vs/workbench/browser/parts/sidebar/sidebarPart.ts index 239258f3a5e..7485f78800e 100644 --- a/src/vs/workbench/browser/parts/sidebar/sidebarPart.ts +++ b/src/vs/workbench/browser/parts/sidebar/sidebarPart.ts @@ -8,7 +8,7 @@ import * as nls from 'vs/nls'; import { Registry } from 'vs/platform/registry/common/platform'; import { Action } from 'vs/base/common/actions'; import { CompositePart } from 'vs/workbench/browser/parts/compositePart'; -import { Viewlet, ViewletRegistry, Extensions as ViewletExtensions } from 'vs/workbench/browser/viewlet'; +import { Viewlet, ViewletRegistry, Extensions as ViewletExtensions, ViewletDescriptor } from 'vs/workbench/browser/viewlet'; import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions'; import { SyncActionDescriptor } from 'vs/platform/actions/common/actions'; import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet'; @@ -20,7 +20,7 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; import { KeyMod, KeyCode } from 'vs/base/common/keyCodes'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; -import { Event } from 'vs/base/common/event'; +import { Event, Emitter } from 'vs/base/common/event'; import { IThemeService } from 'vs/platform/theme/common/themeService'; import { contrastBorder } from 'vs/platform/theme/common/colorRegistry'; import { SIDE_BAR_TITLE_FOREGROUND, SIDE_BAR_BACKGROUND, SIDE_BAR_FOREGROUND, SIDE_BAR_BORDER } from 'vs/workbench/common/theme'; @@ -29,16 +29,21 @@ import { Dimension, EventType, addDisposableListener, trackFocus } from 'vs/base import { StandardMouseEvent } from 'vs/base/browser/mouseEvent'; import { RawContextKey, IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { AnchorAlignment } from 'vs/base/browser/ui/contextview/contextview'; +import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions'; -const SideBarFocusContextId = 'sideBarFocus'; -export const SidebarFocusContext = new RawContextKey(SideBarFocusContextId, false); +export const SidebarFocusContext = new RawContextKey('sideBarFocus', false); +export const ActiveViewletContext = new RawContextKey('activeViewlet', ''); -export class SidebarPart extends CompositePart { +export class SidebarPart extends CompositePart implements IViewletService { + _serviceBrand: any; static readonly activeViewletSettingsKey = 'workbench.sidebar.activeviewletid'; + private viewletRegistry: ViewletRegistry; private sideBarFocusContextKey: IContextKey; + private activeViewletContextKey: IContextKey; private blockOpeningViewlet: boolean; + private _onDidViewletEnable = new Emitter<{ id: string, enabled: boolean }>(); constructor( id: string, @@ -51,6 +56,7 @@ export class SidebarPart extends CompositePart { @IInstantiationService instantiationService: IInstantiationService, @IThemeService themeService: IThemeService, @IContextKeyService contextKeyService: IContextKeyService, + @IExtensionService private extensionService: IExtensionService ) { super( notificationService, @@ -72,8 +78,23 @@ export class SidebarPart extends CompositePart { ); this.sideBarFocusContextKey = SidebarFocusContext.bindTo(contextKeyService); + this.viewletRegistry = Registry.as(ViewletExtensions.Viewlets); + + this.activeViewletContextKey = ActiveViewletContext.bindTo(contextKeyService); + + this._register(this.onDidViewletOpen(viewlet => { + this.activeViewletContextKey.set(viewlet.getId()); + })); + this._register(this.onDidViewletClose(viewlet => { + if (this.activeViewletContextKey.get() === viewlet.getId()) { + this.activeViewletContextKey.reset(); + } + })); } + get onDidViewletRegister(): Event { return >this.viewletRegistry.onDidRegister; } + get onDidViewletEnablementChange(): Event<{ id: string, enabled: boolean }> { return this._onDidViewletEnable.event; } + get onDidViewletOpen(): Event { return Event.map(this._onDidCompositeOpen.event, compositeEvent => compositeEvent.composite); } @@ -124,24 +145,16 @@ export class SidebarPart extends CompositePart { container.style.borderLeftColor = !isPositionLeft ? borderColor : null; } - openViewlet(id: string, focus?: boolean): Viewlet { - if (this.blockOpeningViewlet) { - return null; // Workaround against a potential race condition - } - - // First check if sidebar is hidden and show if so + layout(dimension: Dimension): Dimension[] { if (!this.partService.isVisible(Parts.SIDEBAR_PART)) { - try { - this.blockOpeningViewlet = true; - this.partService.setSideBarHidden(false); - } finally { - this.blockOpeningViewlet = false; - } + return [dimension]; } - return this.openComposite(id, focus) as Viewlet; + return super.layout(dimension); } + // Viewlet service + getActiveViewlet(): IViewlet { return this.getActiveComposite(); } @@ -154,12 +167,61 @@ export class SidebarPart extends CompositePart { this.hideActiveComposite(); } - layout(dimension: Dimension): Dimension[] { + setViewletEnablement(id: string, enabled: boolean): void { + const descriptor = this.getAllViewlets().filter(desc => desc.id === id).pop(); + if (descriptor && descriptor.enabled !== enabled) { + descriptor.enabled = enabled; + this._onDidViewletEnable.fire({ id, enabled }); + } + } + + openViewlet(id: string, focus?: boolean): Thenable { + if (this.getViewlet(id)) { + return Promise.resolve(this.doOpenViewlet(id, focus)); + } + return this.extensionService.whenInstalledExtensionsRegistered() + .then(() => { + if (this.getViewlet(id)) { + return this.doOpenViewlet(id, focus); + } + return null; + }); + } + + getViewlets(): ViewletDescriptor[] { + return this.getAllViewlets() + .filter(v => v.enabled); + } + + getAllViewlets(): ViewletDescriptor[] { + return this.viewletRegistry.getViewlets() + .sort((v1, v2) => v1.order - v2.order); + } + + getDefaultViewletId(): string { + return this.viewletRegistry.getDefaultViewletId(); + } + + getViewlet(id: string): ViewletDescriptor { + return this.getViewlets().filter(viewlet => viewlet.id === id)[0]; + } + + private doOpenViewlet(id: string, focus?: boolean): Viewlet { + if (this.blockOpeningViewlet) { + return null; // Workaround against a potential race condition + } + + // First check if sidebar is hidden and show if so if (!this.partService.isVisible(Parts.SIDEBAR_PART)) { - return [dimension]; + try { + this.blockOpeningViewlet = true; + this.partService.setSideBarHidden(false); + } finally { + this.blockOpeningViewlet = false; + } } - return super.layout(dimension); + return this.openComposite(id, focus) as Viewlet; } protected getTitleAreaDropDownAnchorAlignment(): AnchorAlignment { diff --git a/src/vs/workbench/electron-browser/workbench.ts b/src/vs/workbench/electron-browser/workbench.ts index 22f045c6ec6..3a3829df446 100644 --- a/src/vs/workbench/electron-browser/workbench.ts +++ b/src/vs/workbench/electron-browser/workbench.ts @@ -50,7 +50,6 @@ import { IKeybindingEditingService, KeybindingsEditingService } from 'vs/workben import { RawContextKey, IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey'; import { IActivityService } from 'vs/workbench/services/activity/common/activity'; import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet'; -import { ViewletService } from 'vs/workbench/services/viewlet/browser/viewletService'; import { RemoteFileService } from 'vs/workbench/services/files/electron-browser/remoteFileService'; import { IFileService } from 'vs/platform/files/common/files'; import { IConfigurationResolverService } from 'vs/workbench/services/configurationResolver/common/configurationResolver'; @@ -192,7 +191,6 @@ export class Workbench extends Disposable implements IPartService { private editorService: EditorService; private editorGroupService: IEditorGroupsService; - private viewletService: IViewletService; private contextViewService: ContextViewService; private contextKeyService: IContextKeyService; private keybindingService: IKeybindingService; @@ -368,8 +366,7 @@ export class Workbench extends Disposable implements IPartService { this.sidebarPart = this.instantiationService.createInstance(SidebarPart, Identifiers.SIDEBAR_PART); // Viewlet service - this.viewletService = this.instantiationService.createInstance(ViewletService, this.sidebarPart); - serviceCollection.set(IViewletService, this.viewletService); + serviceCollection.set(IViewletService, this.sidebarPart); // Panel service (panel part) this.panelPart = this.instantiationService.createInstance(PanelPart, Identifiers.PANEL_PART); @@ -737,12 +734,12 @@ export class Workbench extends Disposable implements IPartService { } if (!viewletIdToRestore) { - viewletIdToRestore = this.viewletService.getDefaultViewletId(); + viewletIdToRestore = this.sidebarPart.getDefaultViewletId(); } perf.mark('willRestoreViewlet'); - restorePromises.push(this.viewletService.openViewlet(viewletIdToRestore) - .then(viewlet => viewlet || this.viewletService.openViewlet(this.viewletService.getDefaultViewletId())) + restorePromises.push(this.sidebarPart.openViewlet(viewletIdToRestore) + .then(viewlet => viewlet || this.sidebarPart.openViewlet(this.sidebarPart.getDefaultViewletId())) .then(() => perf.mark('didRestoreViewlet'))); } @@ -1382,9 +1379,9 @@ export class Workbench extends Disposable implements IPartService { else if (!hidden && !this.sidebarPart.getActiveViewlet()) { const viewletToOpen = this.sidebarPart.getLastActiveViewletId(); if (viewletToOpen) { - const viewlet = this.viewletService.openViewlet(viewletToOpen, true); + const viewlet = this.sidebarPart.openViewlet(viewletToOpen, true); if (!viewlet) { - this.viewletService.openViewlet(this.viewletService.getDefaultViewletId(), true); + this.sidebarPart.openViewlet(this.sidebarPart.getDefaultViewletId(), true); } } } diff --git a/src/vs/workbench/services/viewlet/browser/viewletService.ts b/src/vs/workbench/services/viewlet/browser/viewletService.ts deleted file mode 100644 index 7b2a42f7390..00000000000 --- a/src/vs/workbench/services/viewlet/browser/viewletService.ts +++ /dev/null @@ -1,109 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -import { IViewlet } from 'vs/workbench/common/viewlet'; -import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet'; -import { Event, Emitter } from 'vs/base/common/event'; -import { SidebarPart } from 'vs/workbench/browser/parts/sidebar/sidebarPart'; -import { Registry } from 'vs/platform/registry/common/platform'; -import { ViewletDescriptor, ViewletRegistry, Extensions as ViewletExtensions } from 'vs/workbench/browser/viewlet'; -import { IProgressService } from 'vs/platform/progress/common/progress'; -import { IContextKeyService, RawContextKey, IContextKey } from 'vs/platform/contextkey/common/contextkey'; -import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions'; -import { Disposable } from 'vs/base/common/lifecycle'; - -const ActiveViewletContextId = 'activeViewlet'; -export const ActiveViewletContext = new RawContextKey(ActiveViewletContextId, ''); - -export class ViewletService extends Disposable implements IViewletService { - - _serviceBrand: any; - - private sidebarPart: SidebarPart; - private viewletRegistry: ViewletRegistry; - - private activeViewletContextKey: IContextKey; - private _onDidViewletEnable = new Emitter<{ id: string, enabled: boolean }>(); - - get onDidViewletRegister(): Event { return >this.viewletRegistry.onDidRegister; } - get onDidViewletOpen(): Event { return this.sidebarPart.onDidViewletOpen; } - get onDidViewletClose(): Event { return this.sidebarPart.onDidViewletClose; } - get onDidViewletEnablementChange(): Event<{ id: string, enabled: boolean }> { return this._onDidViewletEnable.event; } - - constructor( - sidebarPart: SidebarPart, - @IContextKeyService contextKeyService: IContextKeyService, - @IExtensionService private extensionService: IExtensionService - ) { - super(); - - this.sidebarPart = sidebarPart; - this.viewletRegistry = Registry.as(ViewletExtensions.Viewlets); - - this.activeViewletContextKey = ActiveViewletContext.bindTo(contextKeyService); - - this._register(this.onDidViewletOpen(this._onDidViewletOpen, this)); - this._register(this.onDidViewletClose(this._onDidViewletClose, this)); - } - - private _onDidViewletOpen(viewlet: IViewlet): void { - this.activeViewletContextKey.set(viewlet.getId()); - } - - private _onDidViewletClose(viewlet: IViewlet): void { - const id = viewlet.getId(); - - if (this.activeViewletContextKey.get() === id) { - this.activeViewletContextKey.reset(); - } - } - - setViewletEnablement(id: string, enabled: boolean): void { - const descriptor = this.getAllViewlets().filter(desc => desc.id === id).pop(); - if (descriptor && descriptor.enabled !== enabled) { - descriptor.enabled = enabled; - this._onDidViewletEnable.fire({ id, enabled }); - } - } - - openViewlet(id: string, focus?: boolean): Thenable { - if (this.getViewlet(id)) { - return Promise.resolve(this.sidebarPart.openViewlet(id, focus)); - } - return this.extensionService.whenInstalledExtensionsRegistered() - .then(() => { - if (this.getViewlet(id)) { - return this.sidebarPart.openViewlet(id, focus); - } - return null; - }); - } - - getActiveViewlet(): IViewlet { - return this.sidebarPart.getActiveViewlet(); - } - - getViewlets(): ViewletDescriptor[] { - return this.getAllViewlets() - .filter(v => v.enabled); - } - - getAllViewlets(): ViewletDescriptor[] { - return this.viewletRegistry.getViewlets() - .sort((v1, v2) => v1.order - v2.order); - } - - getDefaultViewletId(): string { - return this.viewletRegistry.getDefaultViewletId(); - } - - getViewlet(id: string): ViewletDescriptor { - return this.getViewlets().filter(viewlet => viewlet.id === id)[0]; - } - - getProgressIndicator(id: string): IProgressService { - return this.sidebarPart.getProgressIndicator(id); - } -} -- GitLab