-
-
Notifications
You must be signed in to change notification settings - Fork 399
/
cloud-sketchbook-contributions.ts
379 lines (341 loc) · 12.4 KB
/
cloud-sketchbook-contributions.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { inject, injectable } from '@theia/core/shared/inversify';
import { TreeNode } from '@theia/core/lib/browser/tree';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { Command, CommandRegistry } from '@theia/core/lib/common/command';
import {
ContextMenuRenderer,
RenderContextMenuOptions,
} from '@theia/core/lib/browser';
import {
Disposable,
DisposableCollection,
} from '@theia/core/lib/common/disposable';
import { WindowService } from '@theia/core/lib/browser/window/window-service';
import { MenuModelRegistry } from '@theia/core/lib/common/menu';
import { CloudSketchbookTree } from './cloud-sketchbook-tree';
import { CloudSketchbookTreeModel } from './cloud-sketchbook-tree-model';
import { CloudUserCommands } from '../../auth/cloud-user-commands';
import { ShareSketchDialog } from '../../dialogs/cloud-share-sketch-dialog';
import { CreateApi } from '../../create/create-api';
import {
PreferenceService,
PreferenceScope,
} from '@theia/core/lib/browser/preferences/preference-service';
import { ArduinoMenus, PlaceholderMenuNode } from '../../menu/arduino-menus';
import { SketchbookCommands } from '../sketchbook/sketchbook-commands';
import {
CurrentSketch,
SketchesServiceClientImpl,
} from '../../sketches-service-client-impl';
import { Contribution } from '../../contributions/contribution';
import { ArduinoPreferences } from '../../arduino-preferences';
import { MainMenuManager } from '../../../common/main-menu-manager';
import { nls } from '@theia/core/lib/common';
export const SKETCHBOOKSYNC__CONTEXT = ['arduino-sketchbook-sync--context'];
// `Open Folder`, `Open in New Window`
export const SKETCHBOOKSYNC__CONTEXT__MAIN_GROUP = [
...SKETCHBOOKSYNC__CONTEXT,
'0_main',
];
export const CLOUD_USER__CONTEXT = ['arduino-cloud-user--context'];
export const CLOUD_USER__CONTEXT__USERNAME = [
...CLOUD_USER__CONTEXT,
'0_username',
];
export const CLOUD_USER__CONTEXT__MAIN_GROUP = [
...CLOUD_USER__CONTEXT,
'1_main',
];
export namespace CloudSketchbookCommands {
export interface Arg {
model: CloudSketchbookTreeModel;
node: TreeNode;
event?: MouseEvent;
}
export namespace Arg {
export function is(arg: Partial<Arg> | undefined): arg is Arg {
return (
!!arg && !!arg.node && arg.model instanceof CloudSketchbookTreeModel
);
}
}
export const TOGGLE_CLOUD_SKETCHBOOK = Command.toLocalizedCommand(
{
id: 'arduino-cloud-sketchbook--disable',
label: 'Show/Hide Cloud Sketchbook',
},
'arduino/cloud/showHideSketchbook'
);
export const PULL_SKETCH = Command.toLocalizedCommand(
{
id: 'arduino-cloud-sketchbook--pull-sketch',
label: 'Pull Sketch',
iconClass: 'pull-sketch-icon',
},
'arduino/cloud/pullSketch'
);
export const PUSH_SKETCH = Command.toLocalizedCommand(
{
id: 'arduino-cloud-sketchbook--push-sketch',
label: 'Push Sketch',
iconClass: 'push-sketch-icon',
},
'arduino/cloud/pullSketch'
);
export const OPEN_IN_CLOUD_EDITOR = Command.toLocalizedCommand(
{
id: 'arduino-cloud-sketchbook--open-in-cloud-editor',
label: 'Open in Cloud Editor',
},
'arduino/cloud/openInCloudEditor'
);
export const OPEN_SKETCHBOOKSYNC_CONTEXT_MENU = Command.toLocalizedCommand(
{
id: 'arduino-sketchbook-sync--open-sketch-context-menu',
label: 'Options...',
iconClass: 'sketchbook-tree__opts',
},
'arduino/cloud/options'
);
export const OPEN_SKETCH_SHARE_DIALOG = Command.toLocalizedCommand(
{
id: 'arduino-cloud-sketchbook--share-modal',
label: 'Share...',
},
'arduino/cloud/share'
);
export const OPEN_PROFILE_CONTEXT_MENU: Command = {
id: 'arduino-cloud-sketchbook--open-profile-menu',
label: 'Contextual menu',
};
}
@injectable()
export class CloudSketchbookContribution extends Contribution {
@inject(FileService)
protected readonly fileService: FileService;
@inject(ContextMenuRenderer)
protected readonly contextMenuRenderer: ContextMenuRenderer;
@inject(MenuModelRegistry)
protected readonly menuRegistry: MenuModelRegistry;
@inject(SketchesServiceClientImpl)
protected readonly sketchServiceClient: SketchesServiceClientImpl;
@inject(WindowService)
protected readonly windowService: WindowService;
@inject(CreateApi)
protected readonly createApi: CreateApi;
@inject(ArduinoPreferences)
protected readonly arduinoPreferences: ArduinoPreferences;
@inject(PreferenceService)
protected readonly preferenceService: PreferenceService;
@inject(MainMenuManager)
protected readonly mainMenuManager: MainMenuManager;
protected readonly toDisposeBeforeNewContextMenu = new DisposableCollection();
override registerMenus(menus: MenuModelRegistry): void {
menus.registerMenuAction(ArduinoMenus.FILE__ADVANCED_SUBMENU, {
commandId: CloudSketchbookCommands.TOGGLE_CLOUD_SKETCHBOOK.id,
label: CloudSketchbookCommands.TOGGLE_CLOUD_SKETCHBOOK.label,
order: '2',
});
}
override registerCommands(registry: CommandRegistry): void {
registry.registerCommand(CloudSketchbookCommands.TOGGLE_CLOUD_SKETCHBOOK, {
execute: () => {
this.preferenceService.set(
'arduino.cloud.enabled',
!this.arduinoPreferences['arduino.cloud.enabled'],
PreferenceScope.User
);
},
isEnabled: () => true,
isVisible: () => true,
});
registry.registerCommand(CloudSketchbookCommands.PULL_SKETCH, {
execute: (arg) => arg.model.sketchbookTree().pull(arg),
isEnabled: (arg) =>
CloudSketchbookCommands.Arg.is(arg) &&
CloudSketchbookTree.CloudSketchDirNode.is(arg.node),
isVisible: (arg) =>
CloudSketchbookCommands.Arg.is(arg) &&
CloudSketchbookTree.CloudSketchDirNode.is(arg.node),
});
registry.registerCommand(CloudSketchbookCommands.PUSH_SKETCH, {
execute: (arg) => arg.model.sketchbookTree().push(arg.node),
isEnabled: (arg) =>
CloudSketchbookCommands.Arg.is(arg) &&
CloudSketchbookTree.CloudSketchDirNode.is(arg.node) &&
CloudSketchbookTree.CloudSketchTreeNode.isSynced(arg.node),
isVisible: (arg) =>
CloudSketchbookCommands.Arg.is(arg) &&
CloudSketchbookTree.CloudSketchDirNode.is(arg.node) &&
CloudSketchbookTree.CloudSketchTreeNode.isSynced(arg.node),
});
registry.registerCommand(CloudSketchbookCommands.OPEN_IN_CLOUD_EDITOR, {
execute: (arg) => {
this.windowService.openNewWindow(
`https://create.arduino.cc/editor/${arg.username}/${arg.node.sketchId}`,
{ external: true }
);
},
isEnabled: (arg) =>
CloudSketchbookCommands.Arg.is(arg) &&
CloudSketchbookTree.CloudSketchDirNode.is(arg.node),
isVisible: (arg) =>
CloudSketchbookCommands.Arg.is(arg) &&
CloudSketchbookTree.CloudSketchDirNode.is(arg.node),
});
registry.registerCommand(CloudSketchbookCommands.OPEN_SKETCH_SHARE_DIALOG, {
execute: (arg) => {
new ShareSketchDialog({
node: arg.node,
title: nls.localize('arduino/cloud/shareSketch', 'Share Sketch'),
createApi: this.createApi,
}).open();
},
isEnabled: (arg) =>
CloudSketchbookCommands.Arg.is(arg) &&
CloudSketchbookTree.CloudSketchDirNode.is(arg.node),
isVisible: (arg) =>
CloudSketchbookCommands.Arg.is(arg) &&
CloudSketchbookTree.CloudSketchDirNode.is(arg.node),
});
registry.registerCommand(
CloudSketchbookCommands.OPEN_SKETCHBOOKSYNC_CONTEXT_MENU,
{
isEnabled: (arg) =>
!!arg &&
'node' in arg &&
CloudSketchbookTree.CloudSketchDirNode.is(arg.node),
isVisible: (arg) =>
!!arg &&
'node' in arg &&
CloudSketchbookTree.CloudSketchDirNode.is(arg.node),
execute: async (arg) => {
// cleanup previous context menu entries
this.toDisposeBeforeNewContextMenu.dispose();
const container = arg.event.target;
if (!container) {
return;
}
this.menuRegistry.registerMenuAction(
SKETCHBOOKSYNC__CONTEXT__MAIN_GROUP,
{
commandId: CloudSketchbookCommands.OPEN_IN_CLOUD_EDITOR.id,
label: CloudSketchbookCommands.OPEN_IN_CLOUD_EDITOR.label,
order: '0',
}
);
this.toDisposeBeforeNewContextMenu.push(
Disposable.create(() =>
this.menuRegistry.unregisterMenuAction(
CloudSketchbookCommands.OPEN_IN_CLOUD_EDITOR
)
)
);
this.menuRegistry.registerMenuAction(
SKETCHBOOKSYNC__CONTEXT__MAIN_GROUP,
{
commandId: CloudSketchbookCommands.OPEN_SKETCH_SHARE_DIALOG.id,
label: CloudSketchbookCommands.OPEN_SKETCH_SHARE_DIALOG.label,
order: '1',
}
);
this.toDisposeBeforeNewContextMenu.push(
Disposable.create(() =>
this.menuRegistry.unregisterMenuAction(
CloudSketchbookCommands.OPEN_SKETCH_SHARE_DIALOG
)
)
);
const currentSketch = await this.sketchServiceClient.currentSketch();
// disable the "open sketch" command for the current sketch and for those not in sync
if (
!CloudSketchbookTree.CloudSketchTreeNode.isSynced(arg.node) ||
(CurrentSketch.isValid(currentSketch) &&
currentSketch.uri === arg.node.uri.toString())
) {
const placeholder = new PlaceholderMenuNode(
SKETCHBOOKSYNC__CONTEXT__MAIN_GROUP,
SketchbookCommands.OPEN_NEW_WINDOW.label!
);
this.menuRegistry.registerMenuNode(
SKETCHBOOKSYNC__CONTEXT__MAIN_GROUP,
placeholder
);
this.toDisposeBeforeNewContextMenu.push(
Disposable.create(() =>
this.menuRegistry.unregisterMenuNode(placeholder.id)
)
);
} else {
this.menuRegistry.registerMenuAction(
SKETCHBOOKSYNC__CONTEXT__MAIN_GROUP,
{
commandId: SketchbookCommands.OPEN_NEW_WINDOW.id,
label: SketchbookCommands.OPEN_NEW_WINDOW.label,
}
);
this.toDisposeBeforeNewContextMenu.push(
Disposable.create(() =>
this.menuRegistry.unregisterMenuAction(
SketchbookCommands.OPEN_NEW_WINDOW
)
)
);
}
const options: RenderContextMenuOptions = {
menuPath: SKETCHBOOKSYNC__CONTEXT,
anchor: {
x: container.getBoundingClientRect().left,
y: container.getBoundingClientRect().top + container.offsetHeight,
},
args: [arg],
};
this.contextMenuRenderer.render(options);
},
}
);
registry.registerCommand(CloudUserCommands.OPEN_PROFILE_CONTEXT_MENU, {
execute: async (arg) => {
this.toDisposeBeforeNewContextMenu.dispose();
const container = arg.event.target;
if (!container) {
return;
}
this.menuRegistry.registerMenuAction(CLOUD_USER__CONTEXT__MAIN_GROUP, {
commandId: CloudUserCommands.LOGOUT.id,
label: CloudUserCommands.LOGOUT.label,
});
this.toDisposeBeforeNewContextMenu.push(
Disposable.create(() =>
this.menuRegistry.unregisterMenuAction(CloudUserCommands.LOGOUT)
)
);
const placeholder = new PlaceholderMenuNode(
CLOUD_USER__CONTEXT__USERNAME,
arg.username
);
this.menuRegistry.registerMenuNode(
CLOUD_USER__CONTEXT__USERNAME,
placeholder
);
this.toDisposeBeforeNewContextMenu.push(
Disposable.create(() =>
this.menuRegistry.unregisterMenuNode(placeholder.id)
)
);
const options: RenderContextMenuOptions = {
menuPath: CLOUD_USER__CONTEXT,
anchor: {
x: container.getBoundingClientRect().left,
y:
container.getBoundingClientRect().top -
3.5 * container.offsetHeight,
},
args: [arg],
};
this.contextMenuRenderer.render(options);
},
});
this.registerMenus(this.menuRegistry);
}
}