panelPart.ts 4.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.
 *--------------------------------------------------------------------------------------------*/

I
isidor 已提交
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 27
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
A
Cleanup  
Alex Dima 已提交
28
import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent';
I
isidor 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

export class PanelPart extends CompositePart<Panel> implements IPanelService {

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

	public serviceId = IPanelService;
	private blockOpeningPanel: boolean;

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

I
isidor 已提交
64 65
	public create(parent: Builder): void {
		super.create(parent);
B
Benjamin Pasero 已提交
66

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

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

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

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

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

I
isidor 已提交
97 98 99 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> {
		return this.hideActiveComposite();
	}
}
I
isidor 已提交
109 110


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

	constructor(
		id: string,
		name: string,
		@IPartService private partService: IPartService
	) {
		super(id, name, 'close-editor-action');
	}

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

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

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

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

let actionRegistry = <IWorkbenchActionRegistry>Registry.as(WorkbenchExtensions.WorkbenchActions);
B
Benjamin Pasero 已提交
148
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(TogglePanelAction, TogglePanelAction.ID, TogglePanelAction.LABEL, { primary: KeyMod.CtrlCmd | KeyCode.KEY_J }), nls.localize('view', "View"), ['panel', 'visibility']);