panelPart.ts 6.4 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';
I
isidor 已提交
9
import {KeyMod, KeyCode, CommonKeybindings} from 'vs/base/common/keyCodes';
I
isidor 已提交
10
import {Action, IAction} from 'vs/base/common/actions';
11 12
import Event, {Emitter} from 'vs/base/common/event';
import {IComposite} from 'vs/workbench/common/composite';
I
isidor 已提交
13
import {Builder} from 'vs/base/browser/builder';
I
isidor 已提交
14
import dom = require('vs/base/browser/dom');
I
isidor 已提交
15
import {Registry} from 'vs/platform/platform';
16
import {Scope} from 'vs/workbench/browser/actionBarRegistry';
I
isidor 已提交
17 18
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
import {IWorkbenchActionRegistry, Extensions as WorkbenchExtensions} from 'vs/workbench/common/actionRegistry';
I
isidor 已提交
19 20 21 22 23
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 已提交
24
import {IStorageService} from 'vs/platform/storage/common/storage';
I
isidor 已提交
25
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
B
Benjamin Pasero 已提交
26
import {IMessageService} from 'vs/platform/message/common/message';
I
isidor 已提交
27
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
A
Alex Dima 已提交
28
import {IKeybindingService2} from 'vs/platform/keybinding/common/keybinding';
A
Cleanup  
Alex Dima 已提交
29
import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent';
30
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
I
isidor 已提交
31 32 33 34 35

export class PanelPart extends CompositePart<Panel> implements IPanelService {

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

36
	public _serviceBrand: any;
37 38 39

	private _onDidPanelOpen = new Emitter<IPanel>();
	private _onDidPanelClose = new Emitter<IPanel>();
I
isidor 已提交
40 41 42
	private blockOpeningPanel: boolean;

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

69 70 71 72 73 74 75 76
	public get onDidPanelOpen(): Event<IPanel> {
		return this._onDidPanelOpen.event;
	}

	public get onDidPanelClose(): Event<IPanel> {
		return this._onDidPanelClose.event;
	}

I
isidor 已提交
77 78
	public create(parent: Builder): void {
		super.create(parent);
B
Benjamin Pasero 已提交
79

A
Cleanup  
Alex Dima 已提交
80
		dom.addStandardDisposableListener(this.getContainer().getHTMLElement(), 'keyup', (e: IKeyboardEvent) => {
I
isidor 已提交
81 82 83 84 85 86 87
			if (e.equals(CommonKeybindings.ESCAPE)) {
				this.partService.setPanelHidden(true);
				e.preventDefault();
			}
		});
	}

I
isidor 已提交
88 89 90 91 92
	public openPanel(id: string, focus?: boolean): TPromise<Panel> {
		if (this.blockOpeningPanel) {
			return TPromise.as(null); // Workaround against a potential race condition
		}

93
		// First check if panel is hidden and show if so
I
isidor 已提交
94
		if (this.partService.isPanelHidden()) {
I
isidor 已提交
95 96
			try {
				this.blockOpeningPanel = true;
I
isidor 已提交
97
				this.partService.setPanelHidden(false);
I
isidor 已提交
98 99 100 101 102
			} finally {
				this.blockOpeningPanel = false;
			}
		}

103 104 105 106
		return this.openComposite(id, focus).then(composite => {
			this._onDidPanelOpen.fire(composite as IComposite as IPanel);
			return composite;
		});
I
isidor 已提交
107 108
	}

I
isidor 已提交
109
	protected getActions(): IAction[] {
B
Benjamin Pasero 已提交
110
		return [this.instantiationService.createInstance(ClosePanelAction, ClosePanelAction.ID, ClosePanelAction.LABEL)];
I
isidor 已提交
111 112
	}

I
isidor 已提交
113 114 115 116 117 118 119 120 121
	public getActivePanel(): IPanel {
		return this.getActiveComposite();
	}

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

	public hideActivePanel(): TPromise<void> {
122
		return this.hideActiveComposite().then(composite => this._onDidPanelClose.fire(composite as IComposite as IPanel));
I
isidor 已提交
123 124
	}
}
I
isidor 已提交
125 126


I
isidor 已提交
127
class ClosePanelAction extends Action {
I
isidor 已提交
128
	static ID = 'workbench.action.closePanel';
I
isidor 已提交
129 130 131 132 133 134 135
	static LABEL = nls.localize('closePanel', "Close");

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

139
	public run(): TPromise<boolean> {
I
isidor 已提交
140
		this.partService.setPanelHidden(true);
A
Alex Dima 已提交
141
		return TPromise.as(true);
I
isidor 已提交
142 143 144
	}
}

I
isidor 已提交
145
class TogglePanelAction extends Action {
I
isidor 已提交
146
	static ID = 'workbench.action.togglePanel';
I
isidor 已提交
147 148 149 150 151
	static LABEL = nls.localize('togglePanel', "Toggle Panel Visibility");

	constructor(
		id: string,
		name: string,
152
		@IPartService private partService: IPartService
I
isidor 已提交
153
	) {
154
		super(id, name, null);
I
isidor 已提交
155 156
	}

157
	public run(): TPromise<boolean> {
I
isidor 已提交
158
		this.partService.setPanelHidden(!this.partService.isPanelHidden());
A
Alex Dima 已提交
159
		return TPromise.as(true);
I
isidor 已提交
160 161 162
	}
}

I
isidor 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
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 已提交
196
let actionRegistry = <IWorkbenchActionRegistry>Registry.as(WorkbenchExtensions.WorkbenchActions);
197
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 已提交
198
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(FocusPanelAction, FocusPanelAction.ID, FocusPanelAction.LABEL), 'View: Focus into Panel', nls.localize('view', "View"));