panelPart.ts 6.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');
J
Johannes Rieken 已提交
8 9 10
import { TPromise } from 'vs/base/common/winjs.base';
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
import { Action, IAction } from 'vs/base/common/actions';
11
import Event from 'vs/base/common/event';
J
Johannes Rieken 已提交
12 13
import { Builder } from 'vs/base/browser/builder';
import { Registry } from 'vs/platform/platform';
14
import { ActivityAction } from 'vs/workbench/browser/parts/activitybar/activitybarActions';
J
Johannes Rieken 已提交
15 16 17 18 19 20 21
import { Scope } from 'vs/workbench/browser/actionBarRegistry';
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
import { IWorkbenchActionRegistry, Extensions as WorkbenchExtensions } from 'vs/workbench/common/actionRegistry';
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';
22
import { IPartService, Parts } from 'vs/workbench/services/part/common/partService';
J
Johannes Rieken 已提交
23 24 25 26 27 28
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 { 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
			Registry.as<PanelRegistry>(PanelExtensions.Panels),
57 58 59 60 61 62
			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
83
		let promise = TPromise.as(null);
84
		if (!this.partService.isVisible(Parts.PANEL_PART)) {
I
isidor 已提交
85 86
			try {
				this.blockOpeningPanel = true;
87
				promise = this.partService.setPanelHidden(false);
I
isidor 已提交
88 89 90 91 92
			} finally {
				this.blockOpeningPanel = false;
			}
		}

93
		return promise.then(() => this.openComposite(id, focus));
I
isidor 已提交
94 95
	}

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

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

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

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


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

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

126 127
	public run(): TPromise<any> {
		return this.partService.setPanelHidden(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
	) {
140
		super(id, name, partService.isVisible(Parts.PANEL_PART) ? 'panel expanded' : 'panel');
I
isidor 已提交
141 142
	}

143 144
	public run(): TPromise<any> {
		return this.partService.setPanelHidden(this.partService.isVisible(Parts.PANEL_PART));
I
isidor 已提交
145 146 147
	}
}

I
isidor 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161
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);
	}

162
	public run(): TPromise<any> {
I
isidor 已提交
163 164

		// Show panel
165
		if (!this.partService.isVisible(Parts.PANEL_PART)) {
166
			return this.partService.setPanelHidden(false);
I
isidor 已提交
167 168 169
		}

		// Focus into active panel
170 171 172
		let panel = this.panelService.getActivePanel();
		if (panel) {
			panel.focus();
I
isidor 已提交
173 174 175 176 177
		}
		return TPromise.as(true);
	}
}

I
isidor 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190
class ToggleMaximizedPanelAction extends Action {

	public static ID = 'workbench.action.toggleMaximizedPanel';
	public static LABEL = nls.localize('toggleMaximizedPanel', "Toggle Maximized Panel");

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

191
	public run(): TPromise<any> {
I
isidor 已提交
192
		// Show panel
193 194
		return this.partService.setPanelHidden(false)
			.then(() => this.partService.toggleMaximizedPanel());
I
isidor 已提交
195 196 197
	}
}

B
Benjamin Pasero 已提交
198
const actionRegistry = Registry.as<IWorkbenchActionRegistry>(WorkbenchExtensions.WorkbenchActions);
199
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 已提交
200
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(FocusPanelAction, FocusPanelAction.ID, FocusPanelAction.LABEL), 'View: Focus into Panel', nls.localize('view', "View"));
I
isidor 已提交
201
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(ToggleMaximizedPanelAction, ToggleMaximizedPanelAction.ID, ToggleMaximizedPanelAction.LABEL), 'View: Toggle Maximized Panel', nls.localize('view', "View"));
B
Benjamin Pasero 已提交
202
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(ClosePanelAction, ClosePanelAction.ID, ClosePanelAction.LABEL), 'View: Close Panel', nls.localize('view', "View"));