actions.ts 5.2 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.
 *--------------------------------------------------------------------------------------------*/

6
import { IDisposable, Disposable } from 'vs/base/common/lifecycle';
M
Matt Bierner 已提交
7
import { Event, Emitter } from 'vs/base/common/event';
8 9 10 11 12 13

export interface ITelemetryData {
	from?: string;
	target?: string;
	[key: string]: any;
}
E
Erich Gamma 已提交
14

J
Johannes Rieken 已提交
15
export interface IAction extends IDisposable {
E
Erich Gamma 已提交
16 17 18
	id: string;
	label: string;
	tooltip: string;
A
Alex Dima 已提交
19
	class: string | undefined;
E
Erich Gamma 已提交
20 21
	enabled: boolean;
	checked: boolean;
22
	radio: boolean;
J
Johannes Rieken 已提交
23
	run(event?: any): Promise<any>;
E
Erich Gamma 已提交
24 25
}

I
isidor 已提交
26
export interface IActionRunner extends IDisposable {
J
Johannes Rieken 已提交
27
	run(action: IAction, context?: any): Promise<any>;
I
isidor 已提交
28 29
	onDidRun: Event<IRunEvent>;
	onDidBeforeRun: Event<IRunEvent>;
E
Erich Gamma 已提交
30 31
}

32
export interface IActionViewItem {
E
Erich Gamma 已提交
33 34 35 36 37 38 39 40 41
	actionRunner: IActionRunner;
	setActionContext(context: any): void;
	render(element: any /* HTMLElement */): void;
	isEnabled(): boolean;
	focus(): void;
	blur(): void;
	dispose(): void;
}

42 43 44 45 46 47
export interface IActionChangeEvent {
	label?: string;
	tooltip?: string;
	class?: string;
	enabled?: boolean;
	checked?: boolean;
48
	radio?: boolean;
49
}
E
Erich Gamma 已提交
50

M
Matt Bierner 已提交
51
export class Action extends Disposable implements IAction {
E
Erich Gamma 已提交
52

M
Matt Bierner 已提交
53
	protected _onDidChange = this._register(new Emitter<IActionChangeEvent>());
54
	readonly onDidChange: Event<IActionChangeEvent> = this._onDidChange.event;
B
Benjamin Pasero 已提交
55

56 57 58
	protected _id: string;
	protected _label: string;
	protected _tooltip: string;
A
Alex Dima 已提交
59
	protected _cssClass: string | undefined;
60 61
	protected _enabled: boolean;
	protected _checked: boolean;
62
	protected _radio: boolean;
J
Johannes Rieken 已提交
63
	protected _actionCallback?: (event?: any) => Promise<any>;
64

J
Johannes Rieken 已提交
65
	constructor(id: string, label: string = '', cssClass: string = '', enabled: boolean = true, actionCallback?: (event?: any) => Promise<any>) {
M
Matt Bierner 已提交
66
		super();
E
Erich Gamma 已提交
67 68 69 70 71 72 73
		this._id = id;
		this._label = label;
		this._cssClass = cssClass;
		this._enabled = enabled;
		this._actionCallback = actionCallback;
	}

B
Benjamin Pasero 已提交
74
	get id(): string {
E
Erich Gamma 已提交
75 76 77
		return this._id;
	}

B
Benjamin Pasero 已提交
78
	get label(): string {
E
Erich Gamma 已提交
79 80 81
		return this._label;
	}

B
Benjamin Pasero 已提交
82
	set label(value: string) {
E
Erich Gamma 已提交
83 84 85
		this._setLabel(value);
	}

86
	protected _setLabel(value: string): void {
E
Erich Gamma 已提交
87 88
		if (this._label !== value) {
			this._label = value;
89
			this._onDidChange.fire({ label: value });
E
Erich Gamma 已提交
90 91 92
		}
	}

B
Benjamin Pasero 已提交
93
	get tooltip(): string {
E
Erich Gamma 已提交
94 95 96
		return this._tooltip;
	}

B
Benjamin Pasero 已提交
97
	set tooltip(value: string) {
E
Erich Gamma 已提交
98 99 100
		this._setTooltip(value);
	}

101
	protected _setTooltip(value: string): void {
E
Erich Gamma 已提交
102 103
		if (this._tooltip !== value) {
			this._tooltip = value;
104
			this._onDidChange.fire({ tooltip: value });
E
Erich Gamma 已提交
105 106 107
		}
	}

A
Alex Dima 已提交
108
	get class(): string | undefined {
E
Erich Gamma 已提交
109 110 111
		return this._cssClass;
	}

A
Alex Dima 已提交
112
	set class(value: string | undefined) {
E
Erich Gamma 已提交
113 114 115
		this._setClass(value);
	}

A
Alex Dima 已提交
116
	protected _setClass(value: string | undefined): void {
E
Erich Gamma 已提交
117 118
		if (this._cssClass !== value) {
			this._cssClass = value;
119
			this._onDidChange.fire({ class: value });
E
Erich Gamma 已提交
120 121 122
		}
	}

B
Benjamin Pasero 已提交
123
	get enabled(): boolean {
E
Erich Gamma 已提交
124 125 126
		return this._enabled;
	}

B
Benjamin Pasero 已提交
127
	set enabled(value: boolean) {
E
Erich Gamma 已提交
128 129 130
		this._setEnabled(value);
	}

131
	protected _setEnabled(value: boolean): void {
E
Erich Gamma 已提交
132 133
		if (this._enabled !== value) {
			this._enabled = value;
134
			this._onDidChange.fire({ enabled: value });
E
Erich Gamma 已提交
135 136 137
		}
	}

B
Benjamin Pasero 已提交
138
	get checked(): boolean {
E
Erich Gamma 已提交
139 140 141
		return this._checked;
	}

B
Benjamin Pasero 已提交
142
	set checked(value: boolean) {
E
Erich Gamma 已提交
143 144 145
		this._setChecked(value);
	}

B
Benjamin Pasero 已提交
146
	get radio(): boolean {
147 148 149
		return this._radio;
	}

B
Benjamin Pasero 已提交
150
	set radio(value: boolean) {
151 152 153
		this._setRadio(value);
	}

154
	protected _setChecked(value: boolean): void {
E
Erich Gamma 已提交
155 156
		if (this._checked !== value) {
			this._checked = value;
157
			this._onDidChange.fire({ checked: value });
E
Erich Gamma 已提交
158 159 160
		}
	}

161 162 163 164 165 166 167
	protected _setRadio(value: boolean): void {
		if (this._radio !== value) {
			this._radio = value;
			this._onDidChange.fire({ radio: value });
		}
	}

J
Johannes Rieken 已提交
168
	run(event?: any, _data?: ITelemetryData): Promise<any> {
169
		if (this._actionCallback) {
E
Erich Gamma 已提交
170 171
			return this._actionCallback(event);
		}
B
Benjamin Pasero 已提交
172

173
		return Promise.resolve(true);
E
Erich Gamma 已提交
174 175 176 177 178 179 180 181 182
	}
}

export interface IRunEvent {
	action: IAction;
	result?: any;
	error?: any;
}

B
Benjamin Pasero 已提交
183
export class ActionRunner extends Disposable implements IActionRunner {
I
isidor 已提交
184

B
Benjamin Pasero 已提交
185
	private _onDidBeforeRun = this._register(new Emitter<IRunEvent>());
186
	readonly onDidBeforeRun: Event<IRunEvent> = this._onDidBeforeRun.event;
I
isidor 已提交
187

B
Benjamin Pasero 已提交
188
	private _onDidRun = this._register(new Emitter<IRunEvent>());
189
	readonly onDidRun: Event<IRunEvent> = this._onDidRun.event;
E
Erich Gamma 已提交
190

191
	async run(action: IAction, context?: any): Promise<any> {
E
Erich Gamma 已提交
192
		if (!action.enabled) {
193
			return Promise.resolve(null);
E
Erich Gamma 已提交
194 195
		}

I
isidor 已提交
196
		this._onDidBeforeRun.fire({ action: action });
E
Erich Gamma 已提交
197

198 199
		try {
			const result = await this.runAction(action, context);
I
isidor 已提交
200
			this._onDidRun.fire({ action: action, result: result });
201
		} catch (error) {
I
isidor 已提交
202
			this._onDidRun.fire({ action: action, error: error });
203
		}
E
Erich Gamma 已提交
204
	}
205

J
Johannes Rieken 已提交
206
	protected runAction(action: IAction, context?: any): Promise<any> {
207
		const res = context ? action.run(context) : action.run();
208
		return Promise.resolve(res);
209
	}
E
Erich Gamma 已提交
210
}
211

B
Benjamin Pasero 已提交
212
export class RadioGroup extends Disposable {
213 214

	constructor(readonly actions: Action[]) {
B
Benjamin Pasero 已提交
215 216
		super();

217 218
		for (const action of actions) {
			this._register(action.onDidChange(e => {
219 220 221 222 223 224 225
				if (e.checked && action.checked) {
					for (const candidate of actions) {
						if (candidate !== action) {
							candidate.checked = false;
						}
					}
				}
226 227
			}));
		}
228 229
	}
}