actions.ts 5.6 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

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

15 16 17 18 19 20 21 22 23 24
export type WBActionExecutedClassification = {
	id: { classification: 'SystemMetaData', purpose: 'FeatureInsight' };
	from: { classification: 'SystemMetaData', purpose: 'FeatureInsight' };
};

export type WBActionExecutedEvent = {
	id: string;
	from: string;
};

J
Johannes Rieken 已提交
25
export interface IAction extends IDisposable {
26
	readonly id: string;
E
Erich Gamma 已提交
27 28
	label: string;
	tooltip: string;
A
Alex Dima 已提交
29
	class: string | undefined;
E
Erich Gamma 已提交
30 31
	enabled: boolean;
	checked: boolean;
32
	radio: boolean;
J
Johannes Rieken 已提交
33
	run(event?: any): Promise<any>;
E
Erich Gamma 已提交
34 35
}

I
isidor 已提交
36
export interface IActionRunner extends IDisposable {
J
Johannes Rieken 已提交
37
	run(action: IAction, context?: any): Promise<any>;
38 39
	readonly onDidRun: Event<IRunEvent>;
	readonly onDidBeforeRun: Event<IRunEvent>;
E
Erich Gamma 已提交
40 41
}

42
export interface IActionViewItem extends IDisposable {
43
	readonly actionRunner: IActionRunner;
E
Erich Gamma 已提交
44 45 46 47 48 49 50
	setActionContext(context: any): void;
	render(element: any /* HTMLElement */): void;
	isEnabled(): boolean;
	focus(): void;
	blur(): void;
}

51
export interface IActionChangeEvent {
52 53 54 55 56 57
	readonly label?: string;
	readonly tooltip?: string;
	readonly class?: string;
	readonly enabled?: boolean;
	readonly checked?: boolean;
	readonly radio?: boolean;
58
}
E
Erich Gamma 已提交
59

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

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

65
	protected readonly _id: string;
66 67
	protected _label: string;
	protected _tooltip: string;
A
Alex Dima 已提交
68
	protected _cssClass: string | undefined;
69 70
	protected _enabled: boolean;
	protected _checked: boolean;
71
	protected _radio: boolean;
72
	protected readonly _actionCallback?: (event?: any) => Promise<any>;
73

J
Johannes Rieken 已提交
74
	constructor(id: string, label: string = '', cssClass: string = '', enabled: boolean = true, actionCallback?: (event?: any) => Promise<any>) {
M
Matt Bierner 已提交
75
		super();
E
Erich Gamma 已提交
76 77 78 79 80 81 82
		this._id = id;
		this._label = label;
		this._cssClass = cssClass;
		this._enabled = enabled;
		this._actionCallback = actionCallback;
	}

B
Benjamin Pasero 已提交
83
	get id(): string {
E
Erich Gamma 已提交
84 85 86
		return this._id;
	}

B
Benjamin Pasero 已提交
87
	get label(): string {
E
Erich Gamma 已提交
88 89 90
		return this._label;
	}

B
Benjamin Pasero 已提交
91
	set label(value: string) {
E
Erich Gamma 已提交
92 93 94
		this._setLabel(value);
	}

95
	private _setLabel(value: string): void {
E
Erich Gamma 已提交
96 97
		if (this._label !== value) {
			this._label = value;
98
			this._onDidChange.fire({ label: value });
E
Erich Gamma 已提交
99 100 101
		}
	}

B
Benjamin Pasero 已提交
102
	get tooltip(): string {
E
Erich Gamma 已提交
103 104 105
		return this._tooltip;
	}

B
Benjamin Pasero 已提交
106
	set tooltip(value: string) {
E
Erich Gamma 已提交
107 108 109
		this._setTooltip(value);
	}

110
	protected _setTooltip(value: string): void {
E
Erich Gamma 已提交
111 112
		if (this._tooltip !== value) {
			this._tooltip = value;
113
			this._onDidChange.fire({ tooltip: value });
E
Erich Gamma 已提交
114 115 116
		}
	}

A
Alex Dima 已提交
117
	get class(): string | undefined {
E
Erich Gamma 已提交
118 119 120
		return this._cssClass;
	}

A
Alex Dima 已提交
121
	set class(value: string | undefined) {
E
Erich Gamma 已提交
122 123 124
		this._setClass(value);
	}

A
Alex Dima 已提交
125
	protected _setClass(value: string | undefined): void {
E
Erich Gamma 已提交
126 127
		if (this._cssClass !== value) {
			this._cssClass = value;
128
			this._onDidChange.fire({ class: value });
E
Erich Gamma 已提交
129 130 131
		}
	}

B
Benjamin Pasero 已提交
132
	get enabled(): boolean {
E
Erich Gamma 已提交
133 134 135
		return this._enabled;
	}

B
Benjamin Pasero 已提交
136
	set enabled(value: boolean) {
E
Erich Gamma 已提交
137 138 139
		this._setEnabled(value);
	}

140
	protected _setEnabled(value: boolean): void {
E
Erich Gamma 已提交
141 142
		if (this._enabled !== value) {
			this._enabled = value;
143
			this._onDidChange.fire({ enabled: value });
E
Erich Gamma 已提交
144 145 146
		}
	}

B
Benjamin Pasero 已提交
147
	get checked(): boolean {
E
Erich Gamma 已提交
148 149 150
		return this._checked;
	}

B
Benjamin Pasero 已提交
151
	set checked(value: boolean) {
E
Erich Gamma 已提交
152 153 154
		this._setChecked(value);
	}

B
Benjamin Pasero 已提交
155
	get radio(): boolean {
156 157 158
		return this._radio;
	}

B
Benjamin Pasero 已提交
159
	set radio(value: boolean) {
160 161 162
		this._setRadio(value);
	}

163
	protected _setChecked(value: boolean): void {
E
Erich Gamma 已提交
164 165
		if (this._checked !== value) {
			this._checked = value;
166
			this._onDidChange.fire({ checked: value });
E
Erich Gamma 已提交
167 168 169
		}
	}

170 171 172 173 174 175 176
	protected _setRadio(value: boolean): void {
		if (this._radio !== value) {
			this._radio = value;
			this._onDidChange.fire({ radio: value });
		}
	}

J
Johannes Rieken 已提交
177
	run(event?: any, _data?: ITelemetryData): Promise<any> {
178
		if (this._actionCallback) {
E
Erich Gamma 已提交
179 180
			return this._actionCallback(event);
		}
B
Benjamin Pasero 已提交
181

182
		return Promise.resolve(true);
E
Erich Gamma 已提交
183 184 185 186
	}
}

export interface IRunEvent {
187 188 189
	readonly action: IAction;
	readonly result?: any;
	readonly error?: any;
E
Erich Gamma 已提交
190 191
}

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

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

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

200
	async run(action: IAction, context?: any): Promise<any> {
E
Erich Gamma 已提交
201
		if (!action.enabled) {
202
			return Promise.resolve(null);
E
Erich Gamma 已提交
203 204
		}

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

207 208
		try {
			const result = await this.runAction(action, context);
I
isidor 已提交
209
			this._onDidRun.fire({ action: action, result: result });
210
		} catch (error) {
I
isidor 已提交
211
			this._onDidRun.fire({ action: action, error: error });
212
		}
E
Erich Gamma 已提交
213
	}
214

J
Johannes Rieken 已提交
215
	protected runAction(action: IAction, context?: any): Promise<any> {
216
		const res = context ? action.run(context) : action.run();
217
		return Promise.resolve(res);
218
	}
E
Erich Gamma 已提交
219
}
220

B
Benjamin Pasero 已提交
221
export class RadioGroup extends Disposable {
222 223

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

226 227
		for (const action of actions) {
			this._register(action.onDidChange(e => {
228 229 230 231 232 233 234
				if (e.checked && action.checked) {
					for (const candidate of actions) {
						if (candidate !== action) {
							candidate.checked = false;
						}
					}
				}
235 236
			}));
		}
237 238
	}
}