Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 11f7983

Browse files
author
Akos Kitta
committedOct 28, 2022
feat: Create remote sketch
Closes #1580 Signed-off-by: Akos Kitta <[email protected]>
1 parent 0d05509 commit 11f7983

16 files changed

+572
-65
lines changed
 

‎arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ import { UserFields } from './contributions/user-fields';
335335
import { UpdateIndexes } from './contributions/update-indexes';
336336
import { InterfaceScale } from './contributions/interface-scale';
337337
import { OpenHandler } from '@theia/core/lib/browser/opener-service';
338+
import { NewCloudSketch } from './contributions/new-cloud-sketch';
339+
import { SketchbookCompositeWidget } from './widgets/sketchbook/sketchbook-composite-widget';
338340

339341
const registerArduinoThemes = () => {
340342
const themes: MonacoThemeJson[] = [
@@ -751,6 +753,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
751753
Contribution.configure(bind, DeleteSketch);
752754
Contribution.configure(bind, UpdateIndexes);
753755
Contribution.configure(bind, InterfaceScale);
756+
Contribution.configure(bind, NewCloudSketch);
754757

755758
bindContributionProvider(bind, StartupTaskProvider);
756759
bind(StartupTaskProvider).toService(BoardsServiceProvider); // to reuse the boards config in another window
@@ -905,6 +908,11 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
905908
id: 'arduino-sketchbook-widget',
906909
createWidget: () => container.get(SketchbookWidget),
907910
}));
911+
bind(SketchbookCompositeWidget).toSelf();
912+
bind<WidgetFactory>(WidgetFactory).toDynamicValue((ctx) => ({
913+
id: 'sketchbook-composite-widget',
914+
createWidget: () => ctx.container.get(SketchbookCompositeWidget),
915+
}));
908916

909917
bind(CloudSketchbookWidget).toSelf();
910918
rebind(SketchbookWidget).toService(CloudSketchbookWidget);

‎arduino-ide-extension/src/browser/contributions/close.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class Close extends SketchContribution {
6565
registry.registerMenuAction(ArduinoMenus.FILE__SKETCH_GROUP, {
6666
commandId: Close.Commands.CLOSE.id,
6767
label: nls.localize('vscode/editor.contribution/close', 'Close'),
68-
order: '5',
68+
order: '6',
6969
});
7070
}
7171

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
import { MenuModelRegistry } from '@theia/core';
2+
import { CompositeTreeNode } from '@theia/core/lib/browser/tree';
3+
import { DisposableCollection } from '@theia/core/lib/common/disposable';
4+
import { nls } from '@theia/core/lib/common/nls';
5+
import { inject, injectable } from '@theia/core/shared/inversify';
6+
import { MainMenuManager } from '../../common/main-menu-manager';
7+
import type { AuthenticationSession } from '../../node/auth/types';
8+
import { AuthenticationClientService } from '../auth/authentication-client-service';
9+
import { CreateApi } from '../create/create-api';
10+
import { CreateUri } from '../create/create-uri';
11+
import { Create } from '../create/typings';
12+
import { ArduinoMenus } from '../menu/arduino-menus';
13+
import { WorkspaceInputDialog } from '../theia/workspace/workspace-input-dialog';
14+
import { CloudSketchbookTree } from '../widgets/cloud-sketchbook/cloud-sketchbook-tree';
15+
import { CloudSketchbookTreeModel } from '../widgets/cloud-sketchbook/cloud-sketchbook-tree-model';
16+
import { CloudSketchbookTreeWidget } from '../widgets/cloud-sketchbook/cloud-sketchbook-tree-widget';
17+
import { SketchbookCommands } from '../widgets/sketchbook/sketchbook-commands';
18+
import { SketchbookWidget } from '../widgets/sketchbook/sketchbook-widget';
19+
import { SketchbookWidgetContribution } from '../widgets/sketchbook/sketchbook-widget-contribution';
20+
import { Command, CommandRegistry, Contribution, URI } from './contribution';
21+
22+
@injectable()
23+
export class NewCloudSketch extends Contribution {
24+
@inject(CreateApi)
25+
private readonly createApi: CreateApi;
26+
@inject(SketchbookWidgetContribution)
27+
private readonly widgetContribution: SketchbookWidgetContribution;
28+
@inject(AuthenticationClientService)
29+
private readonly authenticationService: AuthenticationClientService;
30+
@inject(MainMenuManager)
31+
private readonly mainMenuManager: MainMenuManager;
32+
33+
private _session: AuthenticationSession | undefined;
34+
private readonly toDispose = new DisposableCollection();
35+
36+
override onReady(): void {
37+
this.toDispose.push(
38+
this.authenticationService.onSessionDidChange((session) => {
39+
const oldSession = this._session;
40+
this._session = session;
41+
if (!!oldSession !== !!this._session) {
42+
this.mainMenuManager.update();
43+
}
44+
})
45+
);
46+
this._session = this.authenticationService.session;
47+
if (this._session) {
48+
this.mainMenuManager.update();
49+
}
50+
}
51+
52+
onStop(): void {
53+
this.toDispose.dispose();
54+
}
55+
56+
override registerCommands(registry: CommandRegistry): void {
57+
registry.registerCommand(NewCloudSketch.Commands.NEW_CLOUD_SKETCH, {
58+
execute: () => this.createNewSketch(),
59+
isEnabled: () => !!this._session,
60+
});
61+
}
62+
63+
override registerMenus(registry: MenuModelRegistry): void {
64+
registry.registerMenuAction(ArduinoMenus.FILE__SKETCH_GROUP, {
65+
commandId: NewCloudSketch.Commands.NEW_CLOUD_SKETCH.id,
66+
label: nls.localize('arduino/cloudSketch/new', 'New Remote Sketch'),
67+
order: '1',
68+
});
69+
}
70+
71+
private async createNewSketch(
72+
initialValue?: string | undefined
73+
): Promise<URI | undefined> {
74+
const widget = await this.widgetContribution.widget;
75+
const treeModel = this.treeModelFrom(widget);
76+
if (!treeModel) {
77+
return undefined;
78+
}
79+
const rootNode = CompositeTreeNode.is(treeModel.root)
80+
? treeModel.root
81+
: undefined;
82+
if (!rootNode) {
83+
return undefined;
84+
}
85+
86+
const newSketchName = await this.newSketchName(rootNode, initialValue);
87+
if (!newSketchName) {
88+
return undefined;
89+
}
90+
let result: Create.Sketch | undefined | 'conflict';
91+
try {
92+
result = await this.createApi.createSketch(newSketchName);
93+
} catch (err) {
94+
if (isConflict(err)) {
95+
result = 'conflict';
96+
} else {
97+
throw err;
98+
}
99+
} finally {
100+
if (result) {
101+
await treeModel.updateRoot();
102+
await treeModel.refresh();
103+
}
104+
}
105+
106+
if (result === 'conflict') {
107+
return this.createNewSketch(newSketchName);
108+
}
109+
110+
if (result) {
111+
const newSketch = result;
112+
const yes = nls.localize('vscode/extensionsUtils/yes', 'Yes');
113+
this.messageService
114+
.info(
115+
nls.localize(
116+
'arduino/newCloudSketch/openNewSketch',
117+
"Do you want to pull the new remote sketch '{0}' and open it in a new window?",
118+
newSketchName
119+
),
120+
yes
121+
)
122+
.then(async (answer) => {
123+
if (answer === yes) {
124+
const node = treeModel.getNode(
125+
CreateUri.toUri(newSketch).path.toString()
126+
);
127+
if (!node) {
128+
return;
129+
}
130+
if (CloudSketchbookTree.CloudSketchDirNode.is(node)) {
131+
try {
132+
await treeModel.sketchbookTree().pull({ node });
133+
} catch (err) {
134+
if (isNotFound(err)) {
135+
await treeModel.updateRoot();
136+
await treeModel.refresh();
137+
this.messageService.error(
138+
nls.localize(
139+
'arduino/newCloudSketch/notFound',
140+
"Could not pull the remote sketch '{0}'. It does not exist.",
141+
newSketchName
142+
)
143+
);
144+
return;
145+
}
146+
throw err;
147+
}
148+
return this.commandService.executeCommand(
149+
SketchbookCommands.OPEN_NEW_WINDOW.id,
150+
{ node }
151+
);
152+
}
153+
}
154+
});
155+
}
156+
return undefined;
157+
}
158+
159+
private treeModelFrom(
160+
widget: SketchbookWidget
161+
): CloudSketchbookTreeModel | undefined {
162+
const treeWidget = widget.getTreeWidget();
163+
if (treeWidget instanceof CloudSketchbookTreeWidget) {
164+
const model = treeWidget.model;
165+
if (model instanceof CloudSketchbookTreeModel) {
166+
return model;
167+
}
168+
}
169+
return undefined;
170+
}
171+
172+
private async newSketchName(
173+
rootNode: CompositeTreeNode,
174+
initialValue?: string | undefined
175+
): Promise<string | undefined> {
176+
const existingNames = rootNode.children
177+
.filter(CloudSketchbookTree.CloudSketchDirNode.is)
178+
.map(({ fileStat }) => fileStat.name);
179+
return new WorkspaceInputDialog(
180+
{
181+
title: nls.localize(
182+
'arduino/newCloudSketch/newSketchTitle',
183+
'Name of a new Remote Sketch'
184+
),
185+
parentUri: CreateUri.root,
186+
initialValue,
187+
validate: (input) => {
188+
if (existingNames.includes(input)) {
189+
return nls.localize(
190+
'arduino/newCloudSketch/sketchAlreadyExists',
191+
"Remote sketch '{0}' already exists.",
192+
input
193+
);
194+
}
195+
// This is how https://create.arduino.cc/editor/ works when renaming a sketch.
196+
if (/^[0-9a-zA-Z_]{1,36}$/.test(input)) {
197+
return '';
198+
}
199+
return nls.localize(
200+
'arduino/newCloudSketch/invalidSketchName',
201+
'The name must consist of basic letters, numbers, or underscores. The maximum length is 36 characters.'
202+
);
203+
},
204+
},
205+
this.labelProvider
206+
).open();
207+
}
208+
}
209+
export namespace NewCloudSketch {
210+
export namespace Commands {
211+
export const NEW_CLOUD_SKETCH: Command = {
212+
id: 'arduino-new-cloud-sketch',
213+
};
214+
}
215+
}
216+
217+
function isConflict(err: unknown): boolean {
218+
return isErrorWithStatusOf(err, 409);
219+
}
220+
function isNotFound(err: unknown): boolean {
221+
return isErrorWithStatusOf(err, 404);
222+
}
223+
function isErrorWithStatusOf(
224+
err: unknown,
225+
status: number
226+
): err is Error & { status: number } {
227+
if (err instanceof Error) {
228+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
229+
const object = err as any;
230+
return 'status' in object && object.status === status;
231+
}
232+
return false;
233+
}

‎arduino-ide-extension/src/browser/contributions/open-sketch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class OpenSketch extends SketchContribution {
5454
registry.registerMenuAction(ArduinoMenus.FILE__SKETCH_GROUP, {
5555
commandId: OpenSketch.Commands.OPEN_SKETCH.id,
5656
label: nls.localize('vscode/workspaceActions/openFileFolder', 'Open...'),
57-
order: '1',
57+
order: '2',
5858
});
5959
}
6060

‎arduino-ide-extension/src/browser/contributions/save-sketch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class SaveSketch extends SketchContribution {
2424
registry.registerMenuAction(ArduinoMenus.FILE__SKETCH_GROUP, {
2525
commandId: SaveSketch.Commands.SAVE_SKETCH.id,
2626
label: nls.localize('vscode/fileCommands/save', 'Save'),
27-
order: '6',
27+
order: '7',
2828
});
2929
}
3030

‎arduino-ide-extension/src/browser/create/create-uri.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export namespace CreateUri {
77
export const scheme = 'arduino-create';
88
export const root = toUri(posix.sep);
99

10-
export function toUri(posixPathOrResource: string | Create.Resource): URI {
10+
export function toUri(
11+
posixPathOrResource: string | Create.Resource | Create.Sketch
12+
): URI {
1113
const posixPath =
1214
typeof posixPathOrResource === 'string'
1315
? posixPathOrResource

‎arduino-ide-extension/src/browser/style/dialogs.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
min-height: 0;
3030
}
3131

32+
.p-Widget.dialogOverlay .dialogBlock .dialogControl .error {
33+
word-break: normal;
34+
}
35+
3236
.p-Widget.dialogOverlay .dialogBlock .dialogContent {
3337
padding: 0;
3438
overflow: auto;
@@ -80,10 +84,8 @@
8084
opacity: .4;
8185
}
8286

83-
8487
@media only screen and (max-height: 560px) {
8588
.p-Widget.dialogOverlay .dialogBlock {
8689
max-height: 400px;
8790
}
8891
}
89-

‎arduino-ide-extension/src/browser/style/sketchbook.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@
3333
height: 100%;
3434
}
3535

36+
.sketchbook-trees-container .create-new {
37+
min-height: 58px;
38+
height: 58px;
39+
display: flex;
40+
align-items: center;
41+
justify-content: center;
42+
}
43+
/*
44+
By default, theia-button has a left-margin. IDE2 does not need the left margin
45+
for the _New Remote? Sketch_. Otherwise, the button does not fit the default
46+
widget width.
47+
*/
48+
.sketchbook-trees-container .create-new .theia-button {
49+
margin-left: unset;
50+
}
51+
3652
.sketchbook-tree__opts {
3753
background-color: var(--theia-foreground);
3854
-webkit-mask: url(./sketchbook-opts-icon.svg);
Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,78 @@
11
import * as React from '@theia/core/shared/react';
22
import * as ReactDOM from '@theia/core/shared/react-dom';
3-
import { inject, injectable } from '@theia/core/shared/inversify';
4-
import { Widget } from '@theia/core/shared/@phosphor/widgets';
5-
import { Message, MessageLoop } from '@theia/core/shared/@phosphor/messaging';
6-
import { Disposable } from '@theia/core/lib/common/disposable';
7-
import { BaseWidget } from '@theia/core/lib/browser/widgets/widget';
3+
import {
4+
inject,
5+
injectable,
6+
postConstruct,
7+
} from '@theia/core/shared/inversify';
88
import { UserStatus } from './cloud-user-status';
9+
import { nls } from '@theia/core/lib/common/nls';
910
import { CloudSketchbookTreeWidget } from './cloud-sketchbook-tree-widget';
1011
import { AuthenticationClientService } from '../../auth/authentication-client-service';
1112
import { CloudSketchbookTreeModel } from './cloud-sketchbook-tree-model';
12-
import { nls } from '@theia/core/lib/common';
13+
import { BaseSketchbookCompositeWidget } from '../sketchbook/sketchbook-composite-widget';
14+
import { CreateNew } from '../sketchbook/create-new';
15+
import { AuthenticationSession } from '../../../node/auth/types';
1316

1417
@injectable()
15-
export class CloudSketchbookCompositeWidget extends BaseWidget {
18+
export class CloudSketchbookCompositeWidget extends BaseSketchbookCompositeWidget<CloudSketchbookTreeWidget> {
1619
@inject(AuthenticationClientService)
17-
protected readonly authenticationService: AuthenticationClientService;
18-
20+
private readonly authenticationService: AuthenticationClientService;
1921
@inject(CloudSketchbookTreeWidget)
20-
protected readonly cloudSketchbookTreeWidget: CloudSketchbookTreeWidget;
21-
22-
private compositeNode: HTMLElement;
23-
private cloudUserStatusNode: HTMLElement;
22+
private readonly cloudSketchbookTreeWidget: CloudSketchbookTreeWidget;
23+
private _session: AuthenticationSession | undefined;
2424

2525
constructor() {
2626
super();
27-
this.compositeNode = document.createElement('div');
28-
this.compositeNode.classList.add('composite-node');
29-
this.cloudUserStatusNode = document.createElement('div');
30-
this.cloudUserStatusNode.classList.add('cloud-status-node');
31-
this.compositeNode.appendChild(this.cloudUserStatusNode);
32-
this.node.appendChild(this.compositeNode);
27+
this.id = 'cloud-sketchbook-composite-widget';
3328
this.title.caption = nls.localize(
3429
'arduino/cloud/remoteSketchbook',
3530
'Remote Sketchbook'
3631
);
3732
this.title.iconClass = 'cloud-sketchbook-tree-icon';
38-
this.title.closable = false;
39-
this.id = 'cloud-sketchbook-composite-widget';
40-
}
41-
42-
public getTreeWidget(): CloudSketchbookTreeWidget {
43-
return this.cloudSketchbookTreeWidget;
4433
}
4534

46-
protected override onAfterAttach(message: Message): void {
47-
super.onAfterAttach(message);
48-
Widget.attach(this.cloudSketchbookTreeWidget, this.compositeNode);
49-
ReactDOM.render(
50-
<UserStatus
51-
model={this.cloudSketchbookTreeWidget.model as CloudSketchbookTreeModel}
52-
authenticationService={this.authenticationService}
53-
/>,
54-
this.cloudUserStatusNode
55-
);
56-
this.toDisposeOnDetach.push(
57-
Disposable.create(() => Widget.detach(this.cloudSketchbookTreeWidget))
35+
@postConstruct()
36+
protected init(): void {
37+
this.toDispose.push(
38+
this.authenticationService.onSessionDidChange((session) => {
39+
const oldSession = this._session;
40+
this._session = session;
41+
if (!!oldSession !== !!this._session) {
42+
this.updateFooter();
43+
}
44+
})
5845
);
5946
}
6047

61-
protected override onActivateRequest(msg: Message): void {
62-
super.onActivateRequest(msg);
63-
64-
/*
65-
Sending a resize message is needed because otherwise the cloudSketchbookTreeWidget
66-
would render empty
67-
*/
68-
this.onResize(Widget.ResizeMessage.UnknownSize);
48+
get treeWidget(): CloudSketchbookTreeWidget {
49+
return this.cloudSketchbookTreeWidget;
6950
}
7051

71-
protected override onResize(message: Widget.ResizeMessage): void {
72-
super.onResize(message);
73-
MessageLoop.sendMessage(
74-
this.cloudSketchbookTreeWidget,
75-
Widget.ResizeMessage.UnknownSize
52+
protected renderFooter(footerNode: HTMLElement): void {
53+
ReactDOM.render(
54+
<>
55+
{this._session && (
56+
<CreateNew
57+
label={nls.localize(
58+
'arduino/sketchbook/newRemoteSketch',
59+
'New Remote Sketch'
60+
)}
61+
onClick={this.onDidClickCreateNew}
62+
/>
63+
)}
64+
<UserStatus
65+
model={
66+
this.cloudSketchbookTreeWidget.model as CloudSketchbookTreeModel
67+
}
68+
authenticationService={this.authenticationService}
69+
/>
70+
</>,
71+
footerNode
7672
);
7773
}
74+
75+
private onDidClickCreateNew: () => void = () => {
76+
this.commandService.executeCommand('arduino-new-cloud-sketch');
77+
};
7878
}

‎arduino-ide-extension/src/browser/widgets/cloud-sketchbook/cloud-sketchbook-tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export class CloudSketchbookTree extends SketchbookTree {
136136
return;
137137
}
138138
}
139-
this.runWithState(node, 'pulling', async (node) => {
139+
return this.runWithState(node, 'pulling', async (node) => {
140140
const commandsCopy = node.commands;
141141
node.commands = [];
142142

@@ -196,7 +196,7 @@ export class CloudSketchbookTree extends SketchbookTree {
196196
return;
197197
}
198198
}
199-
this.runWithState(node, 'pushing', async (node) => {
199+
return this.runWithState(node, 'pushing', async (node) => {
200200
if (!CloudSketchbookTree.CloudSketchTreeNode.isSynced(node)) {
201201
throw new Error(
202202
nls.localize(

‎arduino-ide-extension/src/browser/widgets/cloud-sketchbook/cloud-sketchbook-widget.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { inject, injectable, postConstruct } from '@theia/core/shared/inversify'
22
import { CloudSketchbookCompositeWidget } from './cloud-sketchbook-composite-widget';
33
import { SketchbookWidget } from '../sketchbook/sketchbook-widget';
44
import { ArduinoPreferences } from '../../arduino-preferences';
5+
import { BaseSketchbookCompositeWidget } from '../sketchbook/sketchbook-composite-widget';
56

67
@injectable()
78
export class CloudSketchbookWidget extends SketchbookWidget {
@@ -19,8 +20,8 @@ export class CloudSketchbookWidget extends SketchbookWidget {
1920
override getTreeWidget(): any {
2021
const widget: any = this.sketchbookTreesContainer.selectedWidgets().next();
2122

22-
if (widget && typeof widget.getTreeWidget !== 'undefined') {
23-
return (widget as CloudSketchbookCompositeWidget).getTreeWidget();
23+
if (widget instanceof BaseSketchbookCompositeWidget) {
24+
return widget.treeWidget;
2425
}
2526
return widget;
2627
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as React from '@theia/core/shared/react';
2+
3+
export class CreateNew extends React.Component<CreateNew.Props> {
4+
override render(): React.ReactNode {
5+
return (
6+
<div className="create-new">
7+
<button className="theia-button secondary" onClick={this.props.onClick}>
8+
{this.props.label}
9+
</button>
10+
</div>
11+
);
12+
}
13+
}
14+
15+
export namespace CreateNew {
16+
export interface Props {
17+
readonly label: string;
18+
readonly onClick: () => void;
19+
}
20+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import * as React from '@theia/core/shared/react';
2+
import * as ReactDOM from '@theia/core/shared/react-dom';
3+
import { inject, injectable } from '@theia/core/shared/inversify';
4+
import { nls } from '@theia/core/lib/common/nls';
5+
import { Widget } from '@theia/core/shared/@phosphor/widgets';
6+
import { Message, MessageLoop } from '@theia/core/shared/@phosphor/messaging';
7+
import { Disposable } from '@theia/core/lib/common/disposable';
8+
import { BaseWidget } from '@theia/core/lib/browser/widgets/widget';
9+
import { CommandService } from '@theia/core/lib/common/command';
10+
import { SketchbookTreeWidget } from './sketchbook-tree-widget';
11+
import { CreateNew } from '../sketchbook/create-new';
12+
13+
@injectable()
14+
export abstract class BaseSketchbookCompositeWidget<
15+
TW extends SketchbookTreeWidget
16+
> extends BaseWidget {
17+
@inject(CommandService)
18+
protected readonly commandService: CommandService;
19+
20+
private readonly compositeNode: HTMLElement;
21+
private readonly footerNode: HTMLElement;
22+
23+
constructor() {
24+
super();
25+
this.compositeNode = document.createElement('div');
26+
this.compositeNode.classList.add('composite-node');
27+
this.footerNode = document.createElement('div');
28+
this.footerNode.classList.add('footer-node');
29+
this.compositeNode.appendChild(this.footerNode);
30+
this.node.appendChild(this.compositeNode);
31+
this.title.closable = false;
32+
}
33+
34+
abstract get treeWidget(): TW;
35+
protected abstract renderFooter(footerNode: HTMLElement): void;
36+
protected updateFooter(): void {
37+
this.renderFooter(this.footerNode);
38+
}
39+
40+
protected override onAfterAttach(message: Message): void {
41+
super.onAfterAttach(message);
42+
Widget.attach(this.treeWidget, this.compositeNode);
43+
this.renderFooter(this.footerNode);
44+
this.toDisposeOnDetach.push(
45+
Disposable.create(() => Widget.detach(this.treeWidget))
46+
);
47+
}
48+
49+
protected override onActivateRequest(message: Message): void {
50+
super.onActivateRequest(message);
51+
// Sending a resize message is needed because otherwise the tree widget would render empty
52+
this.onResize(Widget.ResizeMessage.UnknownSize);
53+
}
54+
55+
protected override onResize(message: Widget.ResizeMessage): void {
56+
super.onResize(message);
57+
MessageLoop.sendMessage(this.treeWidget, Widget.ResizeMessage.UnknownSize);
58+
}
59+
}
60+
61+
@injectable()
62+
export class SketchbookCompositeWidget extends BaseSketchbookCompositeWidget<SketchbookTreeWidget> {
63+
@inject(SketchbookTreeWidget)
64+
private readonly sketchbookTreeWidget: SketchbookTreeWidget;
65+
66+
constructor() {
67+
super();
68+
this.id = 'sketchbook-composite-widget';
69+
this.title.caption = nls.localize(
70+
'arduino/sketch/titleLocalSketchbook',
71+
'Local Sketchbook'
72+
);
73+
this.title.iconClass = 'sketchbook-tree-icon';
74+
}
75+
76+
get treeWidget(): SketchbookTreeWidget {
77+
return this.sketchbookTreeWidget;
78+
}
79+
80+
protected renderFooter(footerNode: HTMLElement): void {
81+
ReactDOM.render(
82+
<CreateNew
83+
label={nls.localize('arduino/sketchbook/newSketch', 'New Sketch')}
84+
onClick={this.onDidClickCreateNew}
85+
/>,
86+
footerNode
87+
);
88+
}
89+
90+
private onDidClickCreateNew: () => void = () => {
91+
this.commandService.executeCommand('arduino-new-sketch');
92+
};
93+
}

‎arduino-ide-extension/src/browser/widgets/sketchbook/sketchbook-widget.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ import { SketchbookTreeWidget } from './sketchbook-tree-widget';
1313
import { nls } from '@theia/core/lib/common';
1414
import { CloudSketchbookCompositeWidget } from '../cloud-sketchbook/cloud-sketchbook-composite-widget';
1515
import { URI } from '../../contributions/contribution';
16+
import { SketchbookCompositeWidget } from './sketchbook-composite-widget';
1617

1718
@injectable()
1819
export class SketchbookWidget extends BaseWidget {
1920
static LABEL = nls.localize('arduino/sketch/titleSketchbook', 'Sketchbook');
2021

21-
@inject(SketchbookTreeWidget)
22-
protected readonly localSketchbookTreeWidget: SketchbookTreeWidget;
22+
@inject(SketchbookCompositeWidget)
23+
protected readonly localSketchbookTreeWidget: SketchbookCompositeWidget;
2324

2425
protected readonly sketchbookTreesContainer: DockPanel;
2526

@@ -48,7 +49,7 @@ export class SketchbookWidget extends BaseWidget {
4849
}
4950

5051
getTreeWidget(): SketchbookTreeWidget {
51-
return this.localSketchbookTreeWidget;
52+
return this.localSketchbookTreeWidget.treeWidget;
5253
}
5354

5455
activeTreeWidgetId(): string | undefined {
@@ -81,7 +82,7 @@ export class SketchbookWidget extends BaseWidget {
8182
return widget;
8283
}
8384
if (widget instanceof CloudSketchbookCompositeWidget) {
84-
return widget.getTreeWidget();
85+
return widget.treeWidget;
8586
}
8687
return undefined;
8788
};

‎arduino-ide-extension/src/electron-browser/theia/core/electron-main-menu-factory.ts

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { inject, injectable } from '@theia/core/shared/inversify';
22
import * as remote from '@theia/core/electron-shared/@electron/remote';
33
import { isOSX } from '@theia/core/lib/common/os';
44
import {
5+
ActionMenuNode,
56
CompositeMenuNode,
67
MAIN_MENU_BAR,
8+
MenuNode,
79
MenuPath,
810
} from '@theia/core/lib/common/menu';
911
import {
@@ -134,7 +136,7 @@ export class ElectronMainMenuFactory extends TheiaElectronMainMenuFactory {
134136
}
135137

136138
protected override handleElectronDefault(
137-
menuNode: CompositeMenuNode,
139+
menuNode: MenuNode,
138140
args: any[] = [],
139141
options?: ElectronMenuOptions
140142
): Electron.MenuItemConstructorOptions[] {
@@ -149,4 +151,119 @@ export class ElectronMainMenuFactory extends TheiaElectronMainMenuFactory {
149151
}
150152
return [];
151153
}
154+
155+
// Copied from 1.25.0 Theia as is to customize the enablement of the menu items.
156+
// Source: https://github.com/eclipse-theia/theia/blob/ca417a31e402bd35717d3314bf6254049d1dae44/packages/core/src/electron-browser/menu/electron-main-menu-factory.ts#L125-L220
157+
// See https://github.com/arduino/arduino-ide/issues/1533
158+
protected override fillMenuTemplate(
159+
items: Electron.MenuItemConstructorOptions[],
160+
menuModel: CompositeMenuNode,
161+
args: any[] = [],
162+
options?: ElectronMenuOptions
163+
): Electron.MenuItemConstructorOptions[] {
164+
const showDisabled =
165+
options?.showDisabled === undefined ? true : options?.showDisabled;
166+
for (const menu of menuModel.children) {
167+
if (menu instanceof CompositeMenuNode) {
168+
if (menu.children.length > 0) {
169+
// do not render empty nodes
170+
171+
if (menu.isSubmenu) {
172+
// submenu node
173+
174+
const submenu = this.fillMenuTemplate([], menu, args, options);
175+
if (submenu.length === 0) {
176+
continue;
177+
}
178+
179+
items.push({
180+
label: menu.label,
181+
submenu,
182+
});
183+
} else {
184+
// group node
185+
186+
// process children
187+
const submenu = this.fillMenuTemplate([], menu, args, options);
188+
if (submenu.length === 0) {
189+
continue;
190+
}
191+
192+
if (items.length > 0) {
193+
// do not put a separator above the first group
194+
195+
items.push({
196+
type: 'separator',
197+
});
198+
}
199+
200+
// render children
201+
items.push(...submenu);
202+
}
203+
}
204+
} else if (menu instanceof ActionMenuNode) {
205+
const node =
206+
menu.altNode && this.context.altPressed ? menu.altNode : menu;
207+
const commandId = node.action.commandId;
208+
209+
// That is only a sanity check at application startup.
210+
if (!this.commandRegistry.getCommand(commandId)) {
211+
console.debug(
212+
`Skipping menu item with missing command: "${commandId}".`
213+
);
214+
continue;
215+
}
216+
217+
if (
218+
!this.commandRegistry.isVisible(commandId, ...args) ||
219+
(!!node.action.when &&
220+
!this.contextKeyService.match(node.action.when))
221+
) {
222+
continue;
223+
}
224+
225+
// We should omit rendering context-menu items which are disabled.
226+
if (
227+
!showDisabled &&
228+
!this.commandRegistry.isEnabled(commandId, ...args)
229+
) {
230+
continue;
231+
}
232+
233+
const bindings =
234+
this.keybindingRegistry.getKeybindingsForCommand(commandId);
235+
236+
const accelerator = bindings[0] && this.acceleratorFor(bindings[0]);
237+
238+
const menuItem: Electron.MenuItemConstructorOptions = {
239+
id: node.id,
240+
label: node.label,
241+
type: this.commandRegistry.getToggledHandler(commandId, ...args)
242+
? 'checkbox'
243+
: 'normal',
244+
checked: this.commandRegistry.isToggled(commandId, ...args),
245+
enabled: this.commandRegistry.isEnabled(commandId, ...args), // Unlike Theia https://github.com/eclipse-theia/theia/blob/ca417a31e402bd35717d3314bf6254049d1dae44/packages/core/src/electron-browser/menu/electron-main-menu-factory.ts#L197
246+
visible: true,
247+
accelerator,
248+
click: () => this.execute(commandId, args),
249+
};
250+
251+
if (isOSX) {
252+
const role = this.roleFor(node.id);
253+
if (role) {
254+
menuItem.role = role;
255+
delete menuItem.click;
256+
}
257+
}
258+
items.push(menuItem);
259+
260+
if (this.commandRegistry.getToggledHandler(commandId, ...args)) {
261+
this._toggledCommands.add(commandId);
262+
}
263+
} else {
264+
items.push(...this.handleElectronDefault(menu, args, options));
265+
}
266+
}
267+
return items;
268+
}
152269
}

‎i18n/en.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@
119119
"syncEditSketches": "Sync and edit your Arduino Cloud Sketches",
120120
"visitArduinoCloud": "Visit Arduino Cloud to create Cloud Sketches."
121121
},
122+
"cloudSketch": {
123+
"new": "New Remote Sketch"
124+
},
122125
"common": {
123126
"all": "All",
124127
"contributed": "Contributed",
@@ -299,6 +302,13 @@
299302
"unableToCloseWebSocket": "Unable to close websocket",
300303
"unableToConnectToWebSocket": "Unable to connect to websocket"
301304
},
305+
"newCloudSketch": {
306+
"invalidSketchName": "The name must consist of basic letters, numbers, or underscores. The maximum length is 36 characters.",
307+
"newSketchTitle": "Name of a new Remote Sketch",
308+
"notFound": "Could not pull the remote sketch '{0}'. It does not exist.",
309+
"openNewSketch": "Do you want to pull the new remote sketch '{0}' and open it in a new window?",
310+
"sketchAlreadyExists": "Remote sketch '{0}' already exists."
311+
},
302312
"portProtocol": {
303313
"network": "Network",
304314
"serial": "Serial"
@@ -407,6 +417,10 @@
407417
"verify": "Verify",
408418
"verifyOrCompile": "Verify/Compile"
409419
},
420+
"sketchbook": {
421+
"newRemoteSketch": "New Remote Sketch",
422+
"newSketch": "New Sketch"
423+
},
410424
"survey": {
411425
"answerSurvey": "Answer survey",
412426
"dismissSurvey": "Don't show again",

0 commit comments

Comments
 (0)
Please sign in to comment.