terminalPanel.ts 8.5 KB
Newer Older
D
Daniel Imms 已提交
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.
 *--------------------------------------------------------------------------------------------*/

6
import DOM = require('vs/base/browser/dom');
7
import lifecycle = require('vs/base/common/lifecycle');
8
import platform = require('vs/base/common/platform');
9
import {Action, IAction} from 'vs/base/common/actions';
D
Daniel Imms 已提交
10
import {Builder, Dimension} from 'vs/base/browser/builder';
D
Daniel Imms 已提交
11
import {KillTerminalAction, CreateNewTerminalAction, SwitchTerminalInstanceAction, SwitchTerminalInstanceActionItem} from 'vs/workbench/parts/terminal/electron-browser/terminalActions';
12
import {IActionItem} from 'vs/base/browser/ui/actionbar/actionbar';
13
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
D
Daniel Imms 已提交
14
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
D
Daniel Imms 已提交
15
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
16
import {ITerminalProcess, ITerminalService, TERMINAL_PANEL_ID} from 'vs/workbench/parts/terminal/electron-browser/terminal';
17
import {IThemeService} from 'vs/workbench/services/themes/common/themeService';
18
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
D
Daniel Imms 已提交
19
import {Panel} from 'vs/workbench/browser/panel';
20
import {TPromise} from 'vs/base/common/winjs.base';
21 22
import {TerminalConfigHelper} from 'vs/workbench/parts/terminal/electron-browser/terminalConfigHelper';
import {TerminalInstance} from 'vs/workbench/parts/terminal/electron-browser/terminalInstance';
D
Daniel Imms 已提交
23 24 25

export class TerminalPanel extends Panel {

26 27 28
	private toDispose: lifecycle.IDisposable[] = [];
	private terminalInstances: TerminalInstance[] = [];

D
Daniel Imms 已提交
29
	private actions: IAction[];
D
Daniel Imms 已提交
30
	private parentDomElement: HTMLElement;
D
Daniel Imms 已提交
31
	private terminalContainer: HTMLElement;
32
	private themeStyleElement: HTMLElement;
33
	private configurationHelper: TerminalConfigHelper;
D
Daniel Imms 已提交
34 35

	constructor(
36
		@ITelemetryService telemetryService: ITelemetryService,
D
Daniel Imms 已提交
37 38
		@IConfigurationService private configurationService: IConfigurationService,
		@IInstantiationService private instantiationService: IInstantiationService,
39
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
40 41
		@ITerminalService private terminalService: ITerminalService,
		@IThemeService private themeService: IThemeService
D
Daniel Imms 已提交
42 43 44 45
	) {
		super(TERMINAL_PANEL_ID, telemetryService);
	}

46
	public layout(dimension?: Dimension): void {
D
Daniel Imms 已提交
47 48 49
		if (!dimension) {
			return;
		}
50
		if (this.terminalInstances.length > 0) {
51
			this.terminalInstances[this.terminalService.getActiveTerminalIndex()].layout(dimension);
52
		}
D
Daniel Imms 已提交
53 54
	}

D
Daniel Imms 已提交
55 56 57
	public getActions(): IAction[] {
		if (!this.actions) {
			this.actions = [
58
				this.instantiationService.createInstance(SwitchTerminalInstanceAction, SwitchTerminalInstanceAction.ID, SwitchTerminalInstanceAction.LABEL),
D
Daniel Imms 已提交
59
				this.instantiationService.createInstance(CreateNewTerminalAction, CreateNewTerminalAction.ID, CreateNewTerminalAction.LABEL),
D
Daniel Imms 已提交
60
				this.instantiationService.createInstance(KillTerminalAction, KillTerminalAction.ID, KillTerminalAction.LABEL)
D
Daniel Imms 已提交
61 62 63 64 65 66 67 68 69 70
			];

			this.actions.forEach(a => {
				this.toDispose.push(a);
			});
		}

		return this.actions;
	}

71 72 73 74 75 76 77 78
	public getActionItem(action: Action): IActionItem {
		if (action.id === SwitchTerminalInstanceAction.ID) {
			return this.instantiationService.createInstance(SwitchTerminalInstanceActionItem, action);
		}

		return super.getActionItem(action);
	}

D
Daniel Imms 已提交
79 80
	public create(parent: Builder): TPromise<void> {
		super.create(parent);
81
		this.parentDomElement = parent.getHTMLElement();
D
Daniel Imms 已提交
82
		this.terminalService.initConfigHelper(parent);
D
Daniel Imms 已提交
83
		DOM.addClass(this.parentDomElement, 'integrated-terminal');
84
		this.themeStyleElement = document.createElement('style');
D
Daniel Imms 已提交
85

D
Daniel Imms 已提交
86
		this.terminalContainer = document.createElement('div');
D
Daniel Imms 已提交
87
		DOM.addClass(this.terminalContainer, 'terminal-outer-container');
88
		this.parentDomElement.appendChild(this.themeStyleElement);
D
Daniel Imms 已提交
89 90
		this.parentDomElement.appendChild(this.terminalContainer);

D
Daniel Imms 已提交
91
		this.configurationHelper = new TerminalConfigHelper(platform.platform, this.configurationService, parent);
D
Daniel Imms 已提交
92
		this.toDispose.push(DOM.addDisposableListener(this.terminalContainer, 'wheel', (event: WheelEvent) => {
93
			this.terminalInstances[this.terminalService.getActiveTerminalIndex()].dispatchEvent(new WheelEvent(event.type, event));
94
		}));
95

96
		return this.terminalService.createNew();
97 98
	}

99 100
	public createNewTerminalInstance(terminalProcess: ITerminalProcess): TPromise<void> {
		return this.createTerminal(terminalProcess).then(() => {
101
			this.updateFont();
D
Daniel Imms 已提交
102
			this.focus();
103 104 105
		});
	}

D
Daniel Imms 已提交
106
	public closeActiveTerminal(): TPromise<void> {
107
		return this.closeTerminal(this.terminalService.getActiveTerminalIndex());
108 109 110
	}

	public closeTerminal(index: number): TPromise<void> {
111
		let self = this;
D
Daniel Imms 已提交
112
		return new TPromise<void>(resolve => {
113
			self.onTerminalInstanceExit(self.terminalInstances[index]);
D
Daniel Imms 已提交
114 115 116
		});
	}

117
	public setVisible(visible: boolean): TPromise<void> {
118
		if (visible) {
119
			if (this.terminalInstances.length > 0) {
120 121 122 123
				this.updateFont();
				this.updateTheme();
			} else {
				return super.setVisible(visible).then(() => {
124
					this.terminalService.createNew();
125 126
				});
			}
127 128 129 130
		}
		return super.setVisible(visible);
	}

131
	private createTerminal(terminalProcess: ITerminalProcess): TPromise<TerminalInstance> {
D
Daniel Imms 已提交
132
		return new TPromise<TerminalInstance>(resolve => {
133
			var terminalInstance = new TerminalInstance(terminalProcess, this.terminalContainer, this.contextService, this.terminalService, this.onTerminalInstanceExit.bind(this));
D
Daniel Imms 已提交
134
			this.terminalInstances.push(terminalInstance);
135
			this.setActiveTerminal(this.terminalInstances.length - 1);
136 137
			this.toDispose.push(this.themeService.onDidThemeChange(this.updateTheme.bind(this)));
			this.toDispose.push(this.configurationService.onDidUpdateConfiguration(this.updateFont.bind(this)));
D
Daniel Imms 已提交
138
			this.toDispose.push(this.configurationService.onDidUpdateConfiguration(this.updateCursorBlink.bind(this)));
D
Daniel Imms 已提交
139
			resolve(terminalInstance);
D
Daniel Imms 已提交
140 141
		});
	}
142

143
	public setActiveTerminal(newActiveIndex: number) {
144
		this.terminalInstances.forEach((terminalInstance, i) => {
145
			terminalInstance.toggleVisibility(i === newActiveIndex);
146 147 148
		});
	}

149
	private onTerminalInstanceExit(terminalInstance: TerminalInstance): void {
150 151
		let index = this.terminalInstances.indexOf(terminalInstance);
		if (index !== -1) {
D
Daniel Imms 已提交
152
			this.terminalInstances[index].dispose();
153
			this.terminalInstances.splice(index, 1);
154
		}
D
Daniel Imms 已提交
155 156
		if (this.terminalInstances.length === 0) {
			this.terminalService.toggle();
D
Daniel Imms 已提交
157 158
		} else {
			this.setActiveTerminal(this.terminalService.getActiveTerminalIndex());
D
Daniel Imms 已提交
159
		}
160 161 162 163 164 165
	}

	private updateTheme(themeId?: string): void {
		if (!themeId) {
			themeId = this.themeService.getTheme();
		}
D
Daniel Imms 已提交
166 167 168 169 170
		let theme = this.configurationHelper.getTheme(themeId);

		let css = '';
		theme.forEach((color: string, index: number) => {
			let rgba = this.convertHexCssColorToRgba(color, 0.996);
D
Daniel Imms 已提交
171 172 173 174
			css += `.monaco-workbench .panel.integrated-terminal .xterm .xterm-color-${index} { color: ${color}; }` +
				`.monaco-workbench .panel.integrated-terminal .xterm .xterm-color-${index}::selection { background-color: ${rgba}; }` +
				`.monaco-workbench .panel.integrated-terminal .xterm .xterm-bg-color-${index} { background-color: ${color}; }` +
				`.monaco-workbench .panel.integrated-terminal .xterm .xterm-bg-color-${index}::selection { color: ${color}; }`;
D
Daniel Imms 已提交
175 176
		});

177
		this.themeStyleElement.innerHTML = css;
D
Daniel Imms 已提交
178 179
	}

D
Daniel Imms 已提交
180 181 182
	/**
	 * Converts a CSS hex color (#rrggbb) to a CSS rgba color (rgba(r, g, b, a)).
	 */
D
Daniel Imms 已提交
183 184 185 186 187
	private convertHexCssColorToRgba(hex: string, alpha: number): string {
		let r = parseInt(hex.substr(1, 2), 16);
		let g = parseInt(hex.substr(3, 2), 16);
		let b = parseInt(hex.substr(5, 2), 16);
		return `rgba(${r}, ${g}, ${b}, ${alpha})`;
188 189
	}

190
	private updateFont(): void {
191
		if (this.terminalInstances.length === 0) {
192 193
			return;
		}
194
		this.terminalInstances[this.terminalService.getActiveTerminalIndex()].setFont(this.configurationHelper.getFont());
195
		this.layout(new Dimension(this.parentDomElement.offsetWidth, this.parentDomElement.offsetHeight));
196 197
	}

D
Daniel Imms 已提交
198 199 200 201 202 203
	private updateCursorBlink(): void {
		this.terminalInstances.forEach((instance) => {
			instance.setCursorBlink(this.configurationHelper.getCursorBlink());
		});
	}

204
	public focus(): void {
205
		if (this.terminalInstances.length > 0) {
206
			this.terminalInstances[this.terminalService.getActiveTerminalIndex()].focus(true);
207 208 209
		}
	}

210 211
	public dispose(): void {
		this.toDispose = lifecycle.dispose(this.toDispose);
212 213
		while (this.terminalInstances.length > 0) {
			this.terminalInstances.pop().dispose();
214
		}
215 216
		super.dispose();
	}
D
Daniel Imms 已提交
217
}