sidebarPart.ts 4.3 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  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';
import {TPromise} from 'vs/base/common/winjs.base';
import nls = require('vs/nls');
import {Registry} from 'vs/platform/platform';
B
Benjamin Pasero 已提交
10
import {Action} from 'vs/base/common/actions';
11
import {CompositePart} from 'vs/workbench/browser/parts/compositePart';
I
isidor 已提交
12
import {Viewlet, ViewletRegistry, Extensions as ViewletExtensions} from 'vs/workbench/browser/viewlet';
13
import {IWorkbenchActionRegistry, Extensions as ActionExtensions} from 'vs/workbench/common/actionRegistry';
E
Erich Gamma 已提交
14 15 16 17
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
import {IViewletService} from 'vs/workbench/services/viewlet/common/viewletService';
import {IPartService} from 'vs/workbench/services/part/common/partService';
import {IViewlet} from 'vs/workbench/common/viewlet';
18
import {Scope} from 'vs/workbench/browser/actionBarRegistry';
B
Benjamin Pasero 已提交
19
import {IStorageService} from 'vs/platform/storage/common/storage';
E
Erich Gamma 已提交
20 21
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IEventService} from 'vs/platform/event/common/event';
B
Benjamin Pasero 已提交
22
import {IMessageService} from 'vs/platform/message/common/message';
E
Erich Gamma 已提交
23 24 25 26
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
import {KeyMod, KeyCode} from 'vs/base/common/keyCodes';

27
export class SidebarPart extends CompositePart<Viewlet> implements IViewletService {
E
Erich Gamma 已提交
28

29 30
	public static activeViewletSettingsKey = 'workbench.sidebar.activeviewletid';

E
Erich Gamma 已提交
31 32 33 34 35
	public serviceId = IViewletService;

	private blockOpeningViewlet: boolean;

	constructor(
36 37 38 39 40 41 42 43
		id: string,
		@IMessageService messageService: IMessageService,
		@IStorageService storageService: IStorageService,
		@IEventService eventService: IEventService,
		@ITelemetryService telemetryService: ITelemetryService,
		@IContextMenuService contextMenuService: IContextMenuService,
		@IPartService partService: IPartService,
		@IKeybindingService keybindingService: IKeybindingService
E
Erich Gamma 已提交
44
	) {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
		super(
			messageService,
			storageService,
			eventService,
			telemetryService,
			contextMenuService,
			partService,
			keybindingService,
			(<ViewletRegistry>Registry.as(ViewletExtensions.Viewlets)),
			SidebarPart.activeViewletSettingsKey,
			'sideBar',
			'viewlet',
			Scope.VIEWLET,
			id
		);
E
Erich Gamma 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	}

	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
		if (this.partService.isSideBarHidden()) {
			try {
				this.blockOpeningViewlet = true;
				this.partService.setSideBarHidden(false);
			} finally {
				this.blockOpeningViewlet = false;
			}
		}

77
		return this.openComposite(id, focus);
E
Erich Gamma 已提交
78 79 80
	}

	public getActiveViewlet(): IViewlet {
M
Maxime Quandalle 已提交
81
		return <IViewlet>this.getActiveComposite();
E
Erich Gamma 已提交
82 83 84
	}

	public getLastActiveViewletId(): string {
85
		return this.getLastActiveCompositetId();
E
Erich Gamma 已提交
86 87 88
	}

	public hideActiveViewlet(): TPromise<void> {
89
		return this.hideActiveComposite();
E
Erich Gamma 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	}
}

export class FocusSideBarAction extends Action {

	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);
	}

	public run(): TPromise<boolean> {

		// Show side bar
		if (this.partService.isSideBarHidden()) {
			this.partService.setSideBarHidden(false);
		}

		// Focus into active viewlet
		else {
			let viewlet = this.viewletService.getActiveViewlet();
			if (viewlet) {
				viewlet.focus();
			}
		}

		return TPromise.as(true);
	}
}

let registry = <IWorkbenchActionRegistry>Registry.as(ActionExtensions.WorkbenchActions);
registry.registerWorkbenchAction(new SyncActionDescriptor(FocusSideBarAction, FocusSideBarAction.ID, FocusSideBarAction.LABEL, {
	primary: KeyMod.CtrlCmd | KeyCode.KEY_0
B
Benjamin Pasero 已提交
129
}), nls.localize('viewCategory', "View"), ['focus', 'side', 'bar']);