sidebarPart.ts 6.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import 'vs/css!./media/sidebarpart';
J
Johannes Rieken 已提交
7
import { TPromise } from 'vs/base/common/winjs.base';
8
import nls = require('vs/nls');
9
import { Registry } from 'vs/platform/registry/common/platform';
S
Sandeep Somavarapu 已提交
10
import { Action } from 'vs/base/common/actions';
J
Johannes Rieken 已提交
11 12
import { CompositePart } from 'vs/workbench/browser/parts/compositePart';
import { Viewlet, ViewletRegistry, Extensions as ViewletExtensions } from 'vs/workbench/browser/viewlet';
B
Benjamin Pasero 已提交
13
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions';
J
Johannes Rieken 已提交
14
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
B
Benjamin Pasero 已提交
15
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
16
import { IPartService, Parts, Position as SideBarPosition } from 'vs/workbench/services/part/common/partService';
J
Johannes Rieken 已提交
17 18 19 20 21 22 23
import { IViewlet } from 'vs/workbench/common/viewlet';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
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';
M
Matt Bierner 已提交
24
import { Event } from 'vs/base/common/event';
25
import { IThemeService } from 'vs/platform/theme/common/themeService';
26
import { contrastBorder } from 'vs/platform/theme/common/colorRegistry';
27
import { SIDE_BAR_TITLE_FOREGROUND, SIDE_BAR_BACKGROUND, SIDE_BAR_FOREGROUND, SIDE_BAR_BORDER } from 'vs/workbench/common/theme';
I
isidor 已提交
28
import { Dimension } from 'vs/base/browser/builder';
29
import { INotificationService } from 'vs/platform/notification/common/notification';
E
Erich Gamma 已提交
30

31
export class SidebarPart extends CompositePart<Viewlet> {
E
Erich Gamma 已提交
32

M
Matt Bierner 已提交
33
	public static readonly activeViewletSettingsKey = 'workbench.sidebar.activeviewletid';
34

35
	public _serviceBrand: any;
E
Erich Gamma 已提交
36 37 38 39

	private blockOpeningViewlet: boolean;

	constructor(
40
		id: string,
41
		@INotificationService notificationService: INotificationService,
42 43 44 45
		@IStorageService storageService: IStorageService,
		@ITelemetryService telemetryService: ITelemetryService,
		@IContextMenuService contextMenuService: IContextMenuService,
		@IPartService partService: IPartService,
46
		@IKeybindingService keybindingService: IKeybindingService,
47 48
		@IInstantiationService instantiationService: IInstantiationService,
		@IThemeService themeService: IThemeService
E
Erich Gamma 已提交
49
	) {
50
		super(
51
			notificationService,
52 53 54 55 56
			storageService,
			telemetryService,
			contextMenuService,
			partService,
			keybindingService,
57
			instantiationService,
58
			themeService,
59
			Registry.as<ViewletRegistry>(ViewletExtensions.Viewlets),
60
			SidebarPart.activeViewletSettingsKey,
61
			Registry.as<ViewletRegistry>(ViewletExtensions.Viewlets).getDefaultViewletId(),
62 63
			'sideBar',
			'viewlet',
64
			SIDE_BAR_TITLE_FOREGROUND,
65
			id,
66
			{ hasTitle: true, borderWidth: () => (this.getColor(SIDE_BAR_BORDER) || this.getColor(contrastBorder)) ? 1 : 0 }
67
		);
E
Erich Gamma 已提交
68 69
	}

70
	public get onDidViewletOpen(): Event<IViewlet> {
B
Benjamin Pasero 已提交
71
		return this._onDidCompositeOpen.event as Event<IViewlet>;
72 73 74
	}

	public get onDidViewletClose(): Event<IViewlet> {
B
Benjamin Pasero 已提交
75
		return this._onDidCompositeClose.event as Event<IViewlet>;
76 77
	}

78 79 80 81 82 83
	public updateStyles(): void {
		super.updateStyles();

		// Part container
		const container = this.getContainer();

84
		container.style('background-color', this.getColor(SIDE_BAR_BACKGROUND));
85
		container.style('color', this.getColor(SIDE_BAR_FOREGROUND));
86

87
		const borderColor = this.getColor(SIDE_BAR_BORDER) || this.getColor(contrastBorder);
88
		const isPositionLeft = this.partService.getSideBarPosition() === SideBarPosition.LEFT;
89 90 91 92 93 94
		container.style('border-right-width', borderColor && isPositionLeft ? '1px' : null);
		container.style('border-right-style', borderColor && isPositionLeft ? 'solid' : null);
		container.style('border-right-color', isPositionLeft ? borderColor : null);
		container.style('border-left-width', borderColor && !isPositionLeft ? '1px' : null);
		container.style('border-left-style', borderColor && !isPositionLeft ? 'solid' : null);
		container.style('border-left-color', !isPositionLeft ? borderColor : null);
95 96
	}

E
Erich Gamma 已提交
97 98 99 100 101 102
	public openViewlet(id: string, focus?: boolean): TPromise<Viewlet> {
		if (this.blockOpeningViewlet) {
			return TPromise.as(null); // Workaround against a potential race condition
		}

		// First check if sidebar is hidden and show if so
103
		let promise = TPromise.wrap<void>(null);
104
		if (!this.partService.isVisible(Parts.SIDEBAR_PART)) {
E
Erich Gamma 已提交
105 106
			try {
				this.blockOpeningViewlet = true;
107
				promise = this.partService.setSideBarHidden(false);
E
Erich Gamma 已提交
108 109 110 111 112
			} finally {
				this.blockOpeningViewlet = false;
			}
		}

B
Benjamin Pasero 已提交
113
		return promise.then(() => this.openComposite(id, focus)) as TPromise<Viewlet>;
E
Erich Gamma 已提交
114 115 116
	}

	public getActiveViewlet(): IViewlet {
M
Maxime Quandalle 已提交
117
		return <IViewlet>this.getActiveComposite();
E
Erich Gamma 已提交
118 119 120
	}

	public getLastActiveViewletId(): string {
121
		return this.getLastActiveCompositetId();
E
Erich Gamma 已提交
122 123 124
	}

	public hideActiveViewlet(): TPromise<void> {
125
		return this.hideActiveComposite().then(composite => void 0);
E
Erich Gamma 已提交
126
	}
S
Sandeep Somavarapu 已提交
127

I
isidor 已提交
128 129 130 131 132 133 134
	public layout(dimension: Dimension): Dimension[] {
		if (!this.partService.isVisible(Parts.SIDEBAR_PART)) {
			return [dimension];
		}

		return super.layout(dimension);
	}
E
Erich Gamma 已提交
135 136
}

I
isidor 已提交
137
class FocusSideBarAction extends Action {
E
Erich Gamma 已提交
138

M
Matt Bierner 已提交
139 140
	public static readonly ID = 'workbench.action.focusSideBar';
	public static readonly LABEL = nls.localize('focusSideBar', "Focus into Side Bar");
E
Erich Gamma 已提交
141 142 143 144 145 146 147 148 149 150

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService,
		@IPartService private partService: IPartService
	) {
		super(id, label);
	}

151
	public run(): TPromise<any> {
E
Erich Gamma 已提交
152 153

		// Show side bar
154
		if (!this.partService.isVisible(Parts.SIDEBAR_PART)) {
155
			return this.partService.setSideBarHidden(false);
E
Erich Gamma 已提交
156 157 158
		}

		// Focus into active viewlet
159 160 161
		let viewlet = this.viewletService.getActiveViewlet();
		if (viewlet) {
			viewlet.focus();
E
Erich Gamma 已提交
162 163 164 165 166
		}
		return TPromise.as(true);
	}
}

B
Benjamin Pasero 已提交
167
const registry = Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions);
E
Erich Gamma 已提交
168 169
registry.registerWorkbenchAction(new SyncActionDescriptor(FocusSideBarAction, FocusSideBarAction.ID, FocusSideBarAction.LABEL, {
	primary: KeyMod.CtrlCmd | KeyCode.KEY_0
170
}), 'View: Focus into Side Bar', nls.localize('viewCategory', "View"));