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
		if (!this.partService.isVisible(Parts.PANEL_PART)) {
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';
115
	static LABEL = nls.localize('closePanel', "Close Panel");
I
isidor 已提交
116 117 118 119 120 121

	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
	) {
140
		super(id, name, partService.isVisible(Parts.PANEL_PART) ? 'panel expanded' : 'panel');
I
isidor 已提交
141 142
	}

143
	public run(): TPromise<boolean> {
144
		this.partService.setPanelHidden(this.partService.isVisible(Parts.PANEL_PART));
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
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
166
		if (!this.partService.isVisible(Parts.PANEL_PART)) {
I
isidor 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
			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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
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);
	}

	public run(): TPromise<boolean> {
		// Show panel
		this.partService.setPanelHidden(false);
		this.partService.toggleMaximizedPanel();

		return TPromise.as(true);
	}
}

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