sidebarPart.ts 4.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  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';
import strings = require('vs/base/common/strings');
import {Action, IAction} from 'vs/base/common/actions';
12
import {CompositePart} from 'vs/workbench/browser/parts/compositePart';
13
import {EventType as WorkbenchEventType, CompositeEvent} from 'vs/workbench/common/events';
I
isidor 已提交
14
import {Viewlet, ViewletRegistry, Extensions as ViewletExtensions} from 'vs/workbench/browser/viewlet';
15
import {IWorkbenchActionRegistry, Extensions as ActionExtensions} from 'vs/workbench/common/actionRegistry';
E
Erich Gamma 已提交
16 17 18 19
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';
20
import {Scope} from 'vs/workbench/browser/actionBarRegistry';
E
Erich Gamma 已提交
21 22 23 24 25 26 27 28
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IEventService} from 'vs/platform/event/common/event';
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
import {KeyMod, KeyCode} from 'vs/base/common/keyCodes';

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

31 32
	public static activeViewletSettingsKey = 'workbench.sidebar.activeviewletid';

E
Erich Gamma 已提交
33 34 35 36 37 38
	public serviceId = IViewletService;

	private blockOpeningViewlet: boolean;
	private currentViewletOpenToken: string;

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

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

80
		return this.openComposite(id, focus);
E
Erich Gamma 已提交
81 82 83
	}

	public getActiveViewlet(): IViewlet {
84
		return this.getActiveComposite();
E
Erich Gamma 已提交
85 86 87
	}

	public getLastActiveViewletId(): string {
88
		return this.getLastActiveCompositetId();
E
Erich Gamma 已提交
89 90 91
	}

	public hideActiveViewlet(): TPromise<void> {
92
		return this.hideActiveComposite();
E
Erich Gamma 已提交
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 129 130 131
	}
}

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
132
}), nls.localize('viewCategory', "View"));