Skip to content

Commit e1ef5e2

Browse files
author
Alberto Iannaccone
committed
opening sketch in new window will open sketchbook
1 parent 7d17dd5 commit e1ef5e2

File tree

4 files changed

+84
-21
lines changed

4 files changed

+84
-21
lines changed

arduino-ide-extension/src/browser/contributions/sketch-control.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class SketchControl extends SketchContribution {
250250
});
251251
}
252252

253-
protected isCloudSketch(uri: string): boolean {
253+
isCloudSketch(uri: string): boolean {
254254
try {
255255
const cloudCacheLocation = this.localCacheFsProvider.from(new URI(uri));
256256

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import {
2323
} from '@theia/core/lib/browser/preferences/preference-service';
2424
import { ArduinoMenus, PlaceholderMenuNode } from '../../menu/arduino-menus';
2525
import { SketchbookCommands } from '../sketchbook/sketchbook-commands';
26-
import { CurrentSketch, SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
26+
import {
27+
CurrentSketch,
28+
SketchesServiceClientImpl,
29+
} from '../../../common/protocol/sketches-service-client-impl';
2730
import { Contribution } from '../../contributions/contribution';
2831
import { ArduinoPreferences } from '../../arduino-preferences';
2932
import { MainMenuManager } from '../../../common/main-menu-manager';
@@ -61,6 +64,14 @@ export namespace CloudSketchbookCommands {
6164
}
6265
}
6366

67+
export const SHOW_CLOUD_SKETCHBOOK_WIDGET = Command.toLocalizedCommand(
68+
{
69+
id: 'arduino-cloud-sketchbook--show-cloud-sketchbook-widget',
70+
label: 'Show Cloud Sketchbook Widget',
71+
},
72+
'arduino/sketch/showCloudSketchbookWidget'
73+
);
74+
6475
export const TOGGLE_CLOUD_SKETCHBOOK = Command.toLocalizedCommand(
6576
{
6677
id: 'arduino-cloud-sketchbook--disable',

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@ import {
66
import { CloudSketchbookCompositeWidget } from './cloud-sketchbook-composite-widget';
77
import { SketchbookWidget } from '../sketchbook/sketchbook-widget';
88
import { ArduinoPreferences } from '../../arduino-preferences';
9-
import { Command, CommandContribution, CommandRegistry } from '@theia/core';
9+
import { CommandContribution, CommandRegistry } from '@theia/core';
1010
import { ApplicationShell } from '@theia/core/lib/browser';
11-
12-
export const SHOW_CLOUD_SKETCHBOOK_WIDGET = Command.toLocalizedCommand(
13-
{
14-
id: 'arduino-sketchbook--show-cloud-sketchbook-widget',
15-
label: 'Show Cloud Sketchbook Widget',
16-
},
17-
'arduino/sketch/showCloudSketchbookWidget'
18-
);
11+
import { CloudSketchbookCommands } from './cloud-sketchbook-contributions';
12+
import { EditorManager } from '@theia/editor/lib/browser';
1913

2014
@injectable()
2115
export class CloudSketchbookWidget
@@ -31,6 +25,9 @@ export class CloudSketchbookWidget
3125
@inject(ApplicationShell)
3226
protected readonly shell: ApplicationShell;
3327

28+
@inject(EditorManager)
29+
protected readonly editorManager: EditorManager;
30+
3431
@postConstruct()
3532
protected override init(): void {
3633
super.init();
@@ -83,9 +80,12 @@ export class CloudSketchbookWidget
8380
this.sketchbookTreesContainer.addWidget(
8481
this.cloudSketchbookCompositeWidget
8582
);
86-
registry.registerCommand(SHOW_CLOUD_SKETCHBOOK_WIDGET, {
87-
execute: this.showCloudSketchbookWidget.bind(this),
88-
});
83+
registry.registerCommand(
84+
CloudSketchbookCommands.SHOW_CLOUD_SKETCHBOOK_WIDGET,
85+
{
86+
execute: () => this.showCloudSketchbookWidget(),
87+
}
88+
);
8989
}
9090

9191
showCloudSketchbookWidget(): void {
@@ -94,6 +94,8 @@ export class CloudSketchbookWidget
9494
if (widget instanceof CloudSketchbookWidget) {
9595
widget.activateTreeWidget(this.cloudSketchbookCompositeWidget.id);
9696
}
97+
if (this.editorManager.currentEditor)
98+
this.shell.activateWidget(this.editorManager.currentEditor.id);
9799
});
98100
}
99101
}

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

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as remote from '@theia/core/electron-shared/@electron/remote';
22
import { inject, injectable } from '@theia/core/shared/inversify';
3-
import { CommandRegistry } from '@theia/core/lib/common/command';
3+
import { Command, CommandRegistry } from '@theia/core/lib/common/command';
44
import { MenuModelRegistry } from '@theia/core/lib/common/menu';
55
import { PreferenceService } from '@theia/core/lib/browser/preferences/preference-service';
66
import { AbstractViewContribution } from '@theia/core/lib/browser/shell/view-contribution';
@@ -29,6 +29,10 @@ import {
2929
} from '../../../common/protocol/sketches-service-client-impl';
3030
import { FileService } from '@theia/filesystem/lib/browser/file-service';
3131
import { URI } from '../../contributions/contribution';
32+
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
33+
import { EditorManager } from '@theia/editor/lib/browser';
34+
import { SketchControl } from '../../contributions/sketch-control';
35+
import { CloudSketchbookCommands } from '../cloud-sketchbook/cloud-sketchbook-contributions';
3236

3337
export const SKETCHBOOK__CONTEXT = ['arduino-sketchbook--context'];
3438

@@ -67,6 +71,18 @@ export class SketchbookWidgetContribution
6771
@inject(FileService)
6872
protected readonly fileService: FileService;
6973

74+
@inject(CommandRegistry)
75+
protected readonly commandRegistry: CommandRegistry;
76+
77+
@inject(FrontendApplicationStateService)
78+
private readonly app: FrontendApplicationStateService;
79+
80+
@inject(EditorManager)
81+
protected readonly editorManager: EditorManager;
82+
83+
@inject(SketchControl)
84+
protected readonly sketchControl: SketchControl;
85+
7086
protected readonly toDisposeBeforeNewContextMenu = new DisposableCollection();
7187

7288
constructor() {
@@ -92,6 +108,12 @@ export class SketchbookWidgetContribution
92108
this.mainMenuManager.update();
93109
}
94110
});
111+
112+
this.app.reachedState('ready').then(() => this.onReady());
113+
}
114+
115+
onReady(): void {
116+
this.runEncodedCommands();
95117
}
96118

97119
async initializeLayout(): Promise<void> {
@@ -104,9 +126,7 @@ export class SketchbookWidgetContribution
104126
execute: () => this.showSketchbookWidget(),
105127
});
106128
registry.registerCommand(SketchbookCommands.OPEN_NEW_WINDOW, {
107-
execute: async (arg) => {
108-
return this.workspaceService.open(arg.node.uri);
109-
},
129+
execute: (arg) => this.openSketchInNewWindow(arg),
110130
isEnabled: (arg) =>
111131
!!arg && 'node' in arg && SketchbookTree.SketchDirNode.is(arg.node),
112132
isVisible: (arg) =>
@@ -194,6 +214,15 @@ export class SketchbookWidgetContribution
194214
});
195215
}
196216

217+
openSketchInNewWindow(arg: any): any {
218+
const openSketchbookCommand = this.sketchControl.isCloudSketch(arg.node.uri)
219+
? CloudSketchbookCommands.SHOW_CLOUD_SKETCHBOOK_WIDGET
220+
: SketchbookCommands.SHOW_SKETCHBOOK_WIDGET;
221+
return this.workspaceService.open(arg.node.uri, {
222+
commands: [openSketchbookCommand],
223+
});
224+
}
225+
197226
override registerMenus(registry: MenuModelRegistry): void {
198227
super.registerMenus(registry);
199228

@@ -234,8 +263,29 @@ export class SketchbookWidgetContribution
234263
}
235264

236265
protected async showSketchbookWidget(): Promise<void> {
237-
const widget = await this.widget;
238-
await this.shell.activateWidget(widget.id);
239-
widget.activateTreeWidget(widget.getTreeWidget().id);
266+
this.widget
267+
.then((widget) => this.shell.activateWidget(widget.id))
268+
.then((widget) => {
269+
if (widget instanceof SketchbookWidget) {
270+
widget.activateTreeWidget(widget.getTreeWidget().id);
271+
if (this.editorManager.currentEditor)
272+
this.shell.activateWidget(this.editorManager.currentEditor.id);
273+
}
274+
});
275+
}
276+
277+
protected runEncodedCommands(): void {
278+
const params = new URLSearchParams(window.location.search);
279+
const encoded = params.get('commands');
280+
if (!encoded) return;
281+
282+
const commands = JSON.parse(decodeURIComponent(encoded));
283+
284+
if (commands && Array.isArray(commands)) {
285+
commands.forEach((c: Command) => {
286+
if (this.commandRegistry.commandIds.includes(c.id))
287+
this.commandRegistry.executeCommand(c.id);
288+
});
289+
}
240290
}
241291
}

0 commit comments

Comments
 (0)