panelPart.ts 5.2 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';
I
isidor 已提交
11
import {Builder} from 'vs/base/browser/builder';
I
isidor 已提交
12
import dom = require('vs/base/browser/dom');
I
isidor 已提交
13
import {Registry} from 'vs/platform/platform';
14
import {Scope} from 'vs/workbench/browser/actionBarRegistry';
I
isidor 已提交
15 16
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
import {IWorkbenchActionRegistry, Extensions as WorkbenchExtensions} from 'vs/workbench/common/actionRegistry';
I
isidor 已提交
17 18 19 20 21
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 已提交
22
import {IStorageService} from 'vs/platform/storage/common/storage';
I
isidor 已提交
23 24
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IEventService} from 'vs/platform/event/common/event';
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';
A
Cleanup  
Alex Dima 已提交
28
import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent';
29
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
I
isidor 已提交
30 31 32 33 34 35 36 37 38

export class PanelPart extends CompositePart<Panel> implements IPanelService {

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

	public serviceId = IPanelService;
	private blockOpeningPanel: boolean;

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

I
isidor 已提交
67 68
	public create(parent: Builder): void {
		super.create(parent);
B
Benjamin Pasero 已提交
69

A
Cleanup  
Alex Dima 已提交
70
		dom.addStandardDisposableListener(this.getContainer().getHTMLElement(), 'keyup', (e: IKeyboardEvent) => {
I
isidor 已提交
71 72 73 74 75 76 77
			if (e.equals(CommonKeybindings.ESCAPE)) {
				this.partService.setPanelHidden(true);
				e.preventDefault();
			}
		});
	}

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

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

93
		return 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 109 110 111
	public getActivePanel(): IPanel {
		return this.getActiveComposite();
	}

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

	public hideActivePanel(): TPromise<void> {
		return this.hideActiveComposite();
	}
}
I
isidor 已提交
112 113


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

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

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

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

	constructor(
		id: string,
		name: string,
139
		@IPartService private partService: IPartService
I
isidor 已提交
140
	) {
141
		super(id, name, null);
I
isidor 已提交
142 143
	}

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

let actionRegistry = <IWorkbenchActionRegistry>Registry.as(WorkbenchExtensions.WorkbenchActions);
151
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(TogglePanelAction, TogglePanelAction.ID, TogglePanelAction.LABEL, { primary: KeyMod.CtrlCmd | KeyCode.KEY_J }), 'View: Toggle Panel Visibility', nls.localize('view', "View"));