actions.ts 5.5 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
export type WorkbenchActionExecutedClassification = {
16 17 18 19
	id: { classification: 'SystemMetaData', purpose: 'FeatureInsight' };
	from: { classification: 'SystemMetaData', purpose: 'FeatureInsight' };
};

20
export type WorkbenchActionExecutedEvent = {
21 22 23 24
	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;
J
Johannes Rieken 已提交
32
	run(event?: any): Promise<any>;
E
Erich Gamma 已提交
33 34
}

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

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

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

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

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

63
	protected readonly _id: string;
64
	protected _label: string;
J
Johannes Rieken 已提交
65
	protected _tooltip: string | undefined;
A
Alex Dima 已提交
66
	protected _cssClass: string | undefined;
J
Johannes Rieken 已提交
67 68
	protected _enabled: boolean = true;
	protected _checked: boolean = false;
69
	protected readonly _actionCallback?: (event?: any) => Promise<any>;
70

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

B
Benjamin Pasero 已提交
80
	get id(): string {
E
Erich Gamma 已提交
81 82 83
		return this._id;
	}

B
Benjamin Pasero 已提交
84
	get label(): string {
E
Erich Gamma 已提交
85 86 87
		return this._label;
	}

B
Benjamin Pasero 已提交
88
	set label(value: string) {
E
Erich Gamma 已提交
89 90 91
		this._setLabel(value);
	}

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

B
Benjamin Pasero 已提交
99
	get tooltip(): string {
J
Johannes Rieken 已提交
100
		return this._tooltip || '';
E
Erich Gamma 已提交
101 102
	}

B
Benjamin Pasero 已提交
103
	set tooltip(value: string) {
E
Erich Gamma 已提交
104 105 106
		this._setTooltip(value);
	}

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

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

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

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

B
Benjamin Pasero 已提交
129
	get enabled(): boolean {
E
Erich Gamma 已提交
130 131 132
		return this._enabled;
	}

B
Benjamin Pasero 已提交
133
	set enabled(value: boolean) {
E
Erich Gamma 已提交
134 135 136
		this._setEnabled(value);
	}

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

B
Benjamin Pasero 已提交
144
	get checked(): boolean {
E
Erich Gamma 已提交
145 146 147
		return this._checked;
	}

B
Benjamin Pasero 已提交
148
	set checked(value: boolean) {
E
Erich Gamma 已提交
149 150 151
		this._setChecked(value);
	}

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

J
Johannes Rieken 已提交
159
	run(event?: any, _data?: ITelemetryData): Promise<any> {
160
		if (this._actionCallback) {
E
Erich Gamma 已提交
161 162
			return this._actionCallback(event);
		}
B
Benjamin Pasero 已提交
163

164
		return Promise.resolve(true);
E
Erich Gamma 已提交
165 166 167 168
	}
}

export interface IRunEvent {
169 170 171
	readonly action: IAction;
	readonly result?: any;
	readonly error?: any;
E
Erich Gamma 已提交
172 173
}

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

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

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

182
	async run(action: IAction, context?: any): Promise<any> {
E
Erich Gamma 已提交
183
		if (!action.enabled) {
184
			return Promise.resolve(null);
E
Erich Gamma 已提交
185 186
		}

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

189 190
		try {
			const result = await this.runAction(action, context);
I
isidor 已提交
191
			this._onDidRun.fire({ action: action, result: result });
192
		} catch (error) {
I
isidor 已提交
193
			this._onDidRun.fire({ action: action, error: error });
194
		}
E
Erich Gamma 已提交
195
	}
196

J
Johannes Rieken 已提交
197
	protected runAction(action: IAction, context?: any): Promise<any> {
198
		const res = context ? action.run(context) : action.run();
199
		return Promise.resolve(res);
200
	}
E
Erich Gamma 已提交
201
}
202

B
Benjamin Pasero 已提交
203
export class RadioGroup extends Disposable {
204 205

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

208 209
		for (const action of actions) {
			this._register(action.onDidChange(e => {
210 211 212 213 214 215 216
				if (e.checked && action.checked) {
					for (const candidate of actions) {
						if (candidate !== action) {
							candidate.checked = false;
						}
					}
				}
217 218
			}));
		}
219 220
	}
}
J
João Moreno 已提交
221 222 223 224 225 226

export class SubmenuAction extends Action {
	constructor(label: string, public entries: ReadonlyArray<SubmenuAction | IAction>, cssClass?: string) {
		super(!!cssClass ? cssClass : 'submenu', label, '', true);
	}
}