panelPart.ts 5.9 KB
Newer Older
I
isidor 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

B
Benjamin Pasero 已提交
6
import 'vs/css!./media/panelpart';
I
isidor 已提交
7
import nls = require('vs/nls');
8
import {TPromise} from 'vs/base/common/winjs.base';
9
import {KeyMod, KeyCode} from 'vs/base/common/keyCodes';
I
isidor 已提交
10
import {Action, IAction} from 'vs/base/common/actions';
11
import Event from 'vs/base/common/event';
12
import {Builder} from 'vs/base/browser/builder';
I
isidor 已提交
13
import {Registry} from 'vs/platform/platform';
14
import {ActivityAction} from 'vs/workbench/browser/parts/activitybar/activityAction';
15
import {Scope} from 'vs/workbench/browser/actionBarRegistry';
I
isidor 已提交
16 17
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
import {IWorkbenchActionRegistry, Extensions as WorkbenchExtensions} from 'vs/workbench/common/actionRegistry';
I
isidor 已提交
18 19 20 21 22
import {IPanel} from 'vs/workbench/common/panel';
import {CompositePart} from 'vs/workbench/browser/parts/compositePart';
import {Panel, PanelRegistry, Extensions as PanelExtensions} from 'vs/workbench/browser/panel';
import {IPanelService} from 'vs/workbench/services/panel/common/panelService';
import {IPartService} from 'vs/workbench/services/part/common/partService';
B
Benjamin Pasero 已提交
23
import {IStorageService} from 'vs/platform/storage/common/storage';
I
isidor 已提交
24
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
B
Benjamin Pasero 已提交
25
import {IMessageService} from 'vs/platform/message/common/message';
I
isidor 已提交
26
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
27
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
28
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
I
isidor 已提交
29 30 31 32 33

export class PanelPart extends CompositePart<Panel> implements IPanelService {

	public static activePanelSettingsKey = 'workbench.panelpart.activepanelid';

34
	public _serviceBrand: any;
35

I
isidor 已提交
36 37 38
	private blockOpeningPanel: boolean;

	constructor(
39 40 41 42 43 44
		id: string,
		@IMessageService messageService: IMessageService,
		@IStorageService storageService: IStorageService,
		@ITelemetryService telemetryService: ITelemetryService,
		@IContextMenuService contextMenuService: IContextMenuService,
		@IPartService partService: IPartService,
45 46
		@IKeybindingService keybindingService: IKeybindingService,
		@IInstantiationService instantiationService: IInstantiationService
I
isidor 已提交
47
	) {
48 49 50 51 52 53 54
		super(
			messageService,
			storageService,
			telemetryService,
			contextMenuService,
			partService,
			keybindingService,
55
			instantiationService,
56 57 58 59 60 61 62
			(<PanelRegistry>Registry.as(PanelExtensions.Panels)),
			PanelPart.activePanelSettingsKey,
			'panel',
			'panel',
			Scope.PANEL,
			id
		);
I
isidor 已提交
63 64
	}

65
	public get onDidPanelOpen(): Event<IPanel> {
66
		return this._onDidCompositeOpen.event;
67 68 69
	}

	public get onDidPanelClose(): Event<IPanel> {
70
		return this._onDidCompositeClose.event;
71 72
	}

I
isidor 已提交
73 74 75 76
	public create(parent: Builder): void {
		super.create(parent);
	}

I
isidor 已提交
77 78 79 80 81
	public openPanel(id: string, focus?: boolean): TPromise<Panel> {
		if (this.blockOpeningPanel) {
			return TPromise.as(null); // Workaround against a potential race condition
		}

82
		// First check if panel is hidden and show if so
I
isidor 已提交
83
		if (this.partService.isPanelHidden()) {
I
isidor 已提交
84 85
			try {
				this.blockOpeningPanel = true;
I
isidor 已提交
86
				this.partService.setPanelHidden(false);
I
isidor 已提交
87 88 89 90 91
			} finally {
				this.blockOpeningPanel = false;
			}
		}

92
		return this.openComposite(id, focus);
I
isidor 已提交
93 94
	}

I
isidor 已提交
95
	protected getActions(): IAction[] {
B
Benjamin Pasero 已提交
96
		return [this.instantiationService.createInstance(ClosePanelAction, ClosePanelAction.ID, ClosePanelAction.LABEL)];
I
isidor 已提交
97 98
	}

I
isidor 已提交
99 100 101 102 103 104 105 106 107
	public getActivePanel(): IPanel {
		return this.getActiveComposite();
	}

	public getLastActivePanelId(): string {
		return this.getLastActiveCompositetId();
	}

	public hideActivePanel(): TPromise<void> {
108
		return this.hideActiveComposite().then(composite => void 0);
I
isidor 已提交
109 110
	}
}
I
isidor 已提交
111 112


I
isidor 已提交
113
class ClosePanelAction extends Action {
I
isidor 已提交
114
	static ID = 'workbench.action.closePanel';
I
isidor 已提交
115 116 117 118 119 120 121
	static LABEL = nls.localize('closePanel', "Close");

	constructor(
		id: string,
		name: string,
		@IPartService private partService: IPartService
	) {
D
Daniel Imms 已提交
122
		super(id, name, 'hide-panel-action');
I
isidor 已提交
123 124
	}

125
	public run(): TPromise<boolean> {
I
isidor 已提交
126
		this.partService.setPanelHidden(true);
A
Alex Dima 已提交
127
		return TPromise.as(true);
I
isidor 已提交
128 129 130
	}
}

131
export class TogglePanelAction extends ActivityAction {
I
isidor 已提交
132
	static ID = 'workbench.action.togglePanel';
133
	static LABEL = nls.localize('togglePanel', "Toggle Panel");
I
isidor 已提交
134 135 136 137

	constructor(
		id: string,
		name: string,
138
		@IPartService private partService: IPartService
I
isidor 已提交
139
	) {
I
isidor 已提交
140
		super(id, name, partService.isPanelHidden() ? 'panel' : 'panel expanded');
I
isidor 已提交
141 142
	}

143
	public run(): TPromise<boolean> {
I
isidor 已提交
144
		this.partService.setPanelHidden(!this.partService.isPanelHidden());
A
Alex Dima 已提交
145
		return TPromise.as(true);
I
isidor 已提交
146 147 148
	}
}

I
isidor 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
class FocusPanelAction extends Action {

	public static ID = 'workbench.action.focusPanel';
	public static LABEL = nls.localize('focusPanel', "Focus into Panel");

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

	public run(): TPromise<boolean> {

		// Show panel
		if (this.partService.isPanelHidden()) {
			this.partService.setPanelHidden(false);
		}

		// Focus into active panel
		else {
			let panel = this.panelService.getActivePanel();
			if (panel) {
				panel.focus();
			}
		}

		return TPromise.as(true);
	}
}

I
isidor 已提交
182
let actionRegistry = <IWorkbenchActionRegistry>Registry.as(WorkbenchExtensions.WorkbenchActions);
183
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(TogglePanelAction, TogglePanelAction.ID, TogglePanelAction.LABEL, { primary: KeyMod.CtrlCmd | KeyCode.KEY_J }), 'View: Toggle Panel Visibility', nls.localize('view', "View"));
I
isidor 已提交
184
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(FocusPanelAction, FocusPanelAction.ID, FocusPanelAction.LABEL), 'View: Focus into Panel', nls.localize('view', "View"));