debugActionItems.ts 3.7 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import lifecycle = require('vs/base/common/lifecycle');
import errors = require('vs/base/common/errors');
import { Promise } from 'vs/base/common/winjs.base';
import dom = require('vs/base/browser/dom');
10 11
import { IAction } from 'vs/base/common/actions';
import { BaseActionItem } from 'vs/base/browser/ui/actionbar/actionbar';
E
Erich Gamma 已提交
12 13 14
import { IDebugService, ServiceEvents, State } from 'vs/workbench/parts/debug/common/debug';
import { IConfigurationService, ConfigurationServiceEventTypes } from 'vs/platform/configuration/common/configuration';

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

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

	constructor(
21
		action: IAction,
E
Erich Gamma 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
		@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);
		}));
		this.toDispose.push(this.debugService.addListener2(ServiceEvents.STATE_CHANGED, () => {
			this.select.disabled = this.debugService.getState() !== State.Inactive;
		}));
		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);
	}

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

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

E
Erich Gamma 已提交
64
	private setOptions(): Promise {
65
		let previousSelectedIndex = this.select.selectedIndex;
E
Erich Gamma 已提交
66 67 68 69 70 71 72 73 74
		this.select.options.length = 0;

		return this.debugService.loadLaunchConfig().then(config => {
			if (!config || !config.configurations) {
				this.select.options.add(this.createOption('<none>'));
				this.select.disabled = true;
				return;
			}

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

			let found = false;
			const configurationName = this.debugService.getConfigurationName();
			for (let i = 0; i < configurations.length; i++) {
E
Erich Gamma 已提交
81
				this.select.options.add(this.createOption(configurations[i].name));
82
				if (configurationName === configurations[i].name) {
E
Erich Gamma 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
					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 {
99
		const option = document.createElement('option');
E
Erich Gamma 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112
		option.value = value;
		option.text = value;

		return option;
	}

	public dispose(): void {
		this.debugService = null;
		this.toDispose = lifecycle.disposeAll(this.toDispose);

		super.dispose();
	}
}