sidebarPart.ts 6.3 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';
E
Erich Gamma 已提交
8
import nls = require('vs/nls');
J
Johannes Rieken 已提交
9 10 11 12 13 14
import { Registry } from 'vs/platform/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 { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actionRegistry';
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
import { IViewlet } from 'vs/workbench/common/viewlet';
18
import { Scope } from 'vs/workbench/browser/actions';
J
Johannes Rieken 已提交
19 20 21 22 23 24 25
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IMessageService } from 'vs/platform/message/common/message';
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';
26
import Event from 'vs/base/common/event';
27
import { IThemeService } from 'vs/platform/theme/common/themeService';
28
import { contrastBorder } from 'vs/platform/theme/common/colorRegistry';
29
import { SIDE_BAR_TITLE_FOREGROUND, SIDE_BAR_BACKGROUND, SIDE_BAR_FOREGROUND, SIDE_BAR_BORDER } from 'vs/workbench/common/theme';
E
Erich Gamma 已提交
30

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

33 34
	public static activeViewletSettingsKey = 'workbench.sidebar.activeviewletid';

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

	private blockOpeningViewlet: boolean;

	constructor(
40 41 42 43 44 45
		id: string,
		@IMessageService messageService: IMessageService,
		@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 51 52 53 54 55 56
		super(
			messageService,
			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 64
			'sideBar',
			'viewlet',
			Scope.VIEWLET,
65
			SIDE_BAR_TITLE_FOREGROUND,
66
			id,
67
			{ hasTitle: true, borderWidth: () => (this.getColor(SIDE_BAR_BORDER) || this.getColor(contrastBorder)) ? 1 : 0 }
68
		);
E
Erich Gamma 已提交
69 70
	}

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

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

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

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

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

88
		const borderColor = this.getColor(SIDE_BAR_BORDER) || this.getColor(contrastBorder);
89
		const isPositionLeft = this.partService.getSideBarPosition() === SideBarPosition.LEFT;
90 91 92 93 94 95
		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);
96 97
	}

E
Erich Gamma 已提交
98 99 100 101 102 103
	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
B
Benjamin Pasero 已提交
104
		let promise = TPromise.as<void>(null);
105
		if (!this.partService.isVisible(Parts.SIDEBAR_PART)) {
E
Erich Gamma 已提交
106 107
			try {
				this.blockOpeningViewlet = true;
108
				promise = this.partService.setSideBarHidden(false);
E
Erich Gamma 已提交
109 110 111 112 113
			} finally {
				this.blockOpeningViewlet = false;
			}
		}

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

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

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

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

I
isidor 已提交
130
class FocusSideBarAction extends Action {
E
Erich Gamma 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143

	public static ID = 'workbench.action.focusSideBar';
	public static LABEL = nls.localize('focusSideBar', "Focus into Side Bar");

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

144
	public run(): TPromise<any> {
E
Erich Gamma 已提交
145 146

		// Show side bar
147
		if (!this.partService.isVisible(Parts.SIDEBAR_PART)) {
148
			return this.partService.setSideBarHidden(false);
E
Erich Gamma 已提交
149 150 151
		}

		// Focus into active viewlet
152 153 154
		let viewlet = this.viewletService.getActiveViewlet();
		if (viewlet) {
			viewlet.focus();
E
Erich Gamma 已提交
155 156 157 158 159
		}
		return TPromise.as(true);
	}
}

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