-
Notifications
You must be signed in to change notification settings - Fork 30.7k
/
Copy pathactions.ts
238 lines (194 loc) · 5.6 KB
/
actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IDisposable, Disposable } from 'vs/base/common/lifecycle';
import { Event, Emitter } from 'vs/base/common/event';
export interface ITelemetryData {
readonly from?: string;
readonly target?: string;
[key: string]: any;
}
export type WorkbenchActionExecutedClassification = {
id: { classification: 'SystemMetaData', purpose: 'FeatureInsight' };
from: { classification: 'SystemMetaData', purpose: 'FeatureInsight' };
};
export type WorkbenchActionExecutedEvent = {
id: string;
from: string;
};
export interface IAction extends IDisposable {
readonly id: string;
label: string;
tooltip: string;
class: string | undefined;
enabled: boolean;
checked: boolean;
radio: boolean;
run(event?: any): Promise<any>;
}
export interface IActionRunner extends IDisposable {
run(action: IAction, context?: any): Promise<any>;
readonly onDidRun: Event<IRunEvent>;
readonly onDidBeforeRun: Event<IRunEvent>;
}
export interface IActionViewItem extends IDisposable {
readonly actionRunner: IActionRunner;
setActionContext(context: any): void;
render(element: any /* HTMLElement */): void;
isEnabled(): boolean;
focus(): void;
blur(): void;
}
export interface IActionChangeEvent {
readonly label?: string;
readonly tooltip?: string;
readonly class?: string;
readonly enabled?: boolean;
readonly checked?: boolean;
readonly radio?: boolean;
}
export class Action extends Disposable implements IAction {
protected _onDidChange = this._register(new Emitter<IActionChangeEvent>());
readonly onDidChange: Event<IActionChangeEvent> = this._onDidChange.event;
protected readonly _id: string;
protected _label: string;
protected _tooltip: string;
protected _cssClass: string | undefined;
protected _enabled: boolean;
protected _checked: boolean;
protected _radio: boolean;
protected readonly _actionCallback?: (event?: any) => Promise<any>;
constructor(id: string, label: string = '', cssClass: string = '', enabled: boolean = true, actionCallback?: (event?: any) => Promise<any>) {
super();
this._id = id;
this._label = label;
this._cssClass = cssClass;
this._enabled = enabled;
this._actionCallback = actionCallback;
}
get id(): string {
return this._id;
}
get label(): string {
return this._label;
}
set label(value: string) {
this._setLabel(value);
}
private _setLabel(value: string): void {
if (this._label !== value) {
this._label = value;
this._onDidChange.fire({ label: value });
}
}
get tooltip(): string {
return this._tooltip;
}
set tooltip(value: string) {
this._setTooltip(value);
}
protected _setTooltip(value: string): void {
if (this._tooltip !== value) {
this._tooltip = value;
this._onDidChange.fire({ tooltip: value });
}
}
get class(): string | undefined {
return this._cssClass;
}
set class(value: string | undefined) {
this._setClass(value);
}
protected _setClass(value: string | undefined): void {
if (this._cssClass !== value) {
this._cssClass = value;
this._onDidChange.fire({ class: value });
}
}
get enabled(): boolean {
return this._enabled;
}
set enabled(value: boolean) {
this._setEnabled(value);
}
protected _setEnabled(value: boolean): void {
if (this._enabled !== value) {
this._enabled = value;
this._onDidChange.fire({ enabled: value });
}
}
get checked(): boolean {
return this._checked;
}
set checked(value: boolean) {
this._setChecked(value);
}
get radio(): boolean {
return this._radio;
}
set radio(value: boolean) {
this._setRadio(value);
}
protected _setChecked(value: boolean): void {
if (this._checked !== value) {
this._checked = value;
this._onDidChange.fire({ checked: value });
}
}
protected _setRadio(value: boolean): void {
if (this._radio !== value) {
this._radio = value;
this._onDidChange.fire({ radio: value });
}
}
run(event?: any, _data?: ITelemetryData): Promise<any> {
if (this._actionCallback) {
return this._actionCallback(event);
}
return Promise.resolve(true);
}
}
export interface IRunEvent {
readonly action: IAction;
readonly result?: any;
readonly error?: any;
}
export class ActionRunner extends Disposable implements IActionRunner {
private _onDidBeforeRun = this._register(new Emitter<IRunEvent>());
readonly onDidBeforeRun: Event<IRunEvent> = this._onDidBeforeRun.event;
private _onDidRun = this._register(new Emitter<IRunEvent>());
readonly onDidRun: Event<IRunEvent> = this._onDidRun.event;
async run(action: IAction, context?: any): Promise<any> {
if (!action.enabled) {
return Promise.resolve(null);
}
this._onDidBeforeRun.fire({ action: action });
try {
const result = await this.runAction(action, context);
this._onDidRun.fire({ action: action, result: result });
} catch (error) {
this._onDidRun.fire({ action: action, error: error });
}
}
protected runAction(action: IAction, context?: any): Promise<any> {
const res = context ? action.run(context) : action.run();
return Promise.resolve(res);
}
}
export class RadioGroup extends Disposable {
constructor(readonly actions: Action[]) {
super();
for (const action of actions) {
this._register(action.onDidChange(e => {
if (e.checked && action.checked) {
for (const candidate of actions) {
if (candidate !== action) {
candidate.checked = false;
}
}
}
}));
}
}
}