debugActionItems.ts 3.8 KB
Newer Older
E
Erich Gamma 已提交
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 nls = require('vs/nls');
E
Erich Gamma 已提交
7 8
import lifecycle = require('vs/base/common/lifecycle');
import errors = require('vs/base/common/errors');
I
isidor 已提交
9
import { TPromise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
10
import dom = require('vs/base/browser/dom');
11 12
import { IAction } from 'vs/base/common/actions';
import { BaseActionItem } from 'vs/base/browser/ui/actionbar/actionbar';
13
import { IDebugService, State } from 'vs/workbench/parts/debug/common/debug';
E
Erich Gamma 已提交
14 15
import { IConfigurationService, ConfigurationServiceEventTypes } from 'vs/platform/configuration/common/configuration';

16
export class SelectConfigActionItem extends BaseActionItem {
E
Erich Gamma 已提交
17 18 19 20 21

	private select: HTMLSelectElement;
	private toDispose: lifecycle.IDisposable[];

	constructor(
22
		action: IAction,
E
Erich Gamma 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
		@IDebugService private debugService: IDebugService,
		@IConfigurationService configurationService: IConfigurationService
	) {
		super(null, action);

		this.select = document.createElement('select');
		this.select.className = 'debug-select action-bar-select';

		this.toDispose = [];
		this.registerListeners(configurationService);
	}

	private registerListeners(configurationService: IConfigurationService): void {
		this.toDispose.push(dom.addStandardDisposableListener(this.select, 'change', (e) => {
			this.actionRunner.run(this._action, e.target.value).done(null, errors.onUnexpectedError);
		}));
39 40
		this.toDispose.push(this.debugService.onDidChangeState(state => {
			this.select.disabled = state !== State.Inactive;
E
Erich Gamma 已提交
41 42 43 44 45 46 47 48 49 50 51 52
		}));
		this.toDispose.push(configurationService.addListener2(ConfigurationServiceEventTypes.UPDATED, e  => {
			this.setOptions().done(null, errors.onUnexpectedError);
		}));
	}

	public render(container: HTMLElement): void {
		dom.addClass(container, 'select-container');
		container.appendChild(this.select);
		this.setOptions().done(null, errors.onUnexpectedError);
	}

53 54 55 56 57 58 59 60 61 62 63 64
	public focus(): void {
		if (this.select) {
			this.select.focus();
		}
	}

	public blur(): void {
		if (this.select) {
			this.select.blur();
		}
	}

I
isidor 已提交
65
	private setOptions(): TPromise<any> {
66
		let previousSelectedIndex = this.select.selectedIndex;
E
Erich Gamma 已提交
67 68
		this.select.options.length = 0;

69
		return this.debugService.getConfigurationManager().loadLaunchConfig().then(config => {
E
Erich Gamma 已提交
70
			if (!config || !config.configurations) {
I
isidor 已提交
71
				this.select.add(this.createOption(`<${ nls.localize('none', "none") }>`));
E
Erich Gamma 已提交
72
				this.select.disabled = true;
73
				return this.actionRunner.run(this._action, null);
E
Erich Gamma 已提交
74 75
			}

76
			const configurations = config.configurations;
E
Erich Gamma 已提交
77
			this.select.disabled = configurations.length < 1;
78 79

			let found = false;
80
			const configurationName = this.debugService.getConfigurationManager().configurationName;
81
			for (let i = 0; i < configurations.length; i++) {
I
isidor 已提交
82
				this.select.add(this.createOption(configurations[i].name));
83
				if (configurationName === configurations[i].name) {
E
Erich Gamma 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
					this.select.selectedIndex = i;
					found = true;
				}
			}

			if (!found && configurations.length > 0) {
				if (!previousSelectedIndex || previousSelectedIndex < 0 || previousSelectedIndex >= configurations.length) {
					previousSelectedIndex = 0;
				}
				this.select.selectedIndex = previousSelectedIndex;
				return this.actionRunner.run(this._action, configurations[previousSelectedIndex].name);
			}
		});
	}

	private createOption(value: string): HTMLOptionElement {
100
		const option = document.createElement('option');
E
Erich Gamma 已提交
101 102 103 104 105 106 107 108
		option.value = value;
		option.text = value;

		return option;
	}

	public dispose(): void {
		this.debugService = null;
J
Joao Moreno 已提交
109
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
110 111 112 113

		super.dispose();
	}
}