Skip to content

Commit c0af1e6

Browse files
Akos Kittakittaakos
Akos Kitta
authored andcommitted
fix: main sketch file editor focus on layout reset
Closes #643 Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
1 parent ac9cce1 commit c0af1e6

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

Diff for: arduino-ide-extension/src/browser/contributions/open-sketch-files.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { DisposableCollection } from '@theia/core/lib/common/disposable';
2020
export class OpenSketchFiles extends SketchContribution {
2121
override registerCommands(registry: CommandRegistry): void {
2222
registry.registerCommand(OpenSketchFiles.Commands.OPEN_SKETCH_FILES, {
23-
execute: (uri: URI) => this.openSketchFiles(uri),
23+
execute: (uri: URI, focusMainSketchFile) =>
24+
this.openSketchFiles(uri, focusMainSketchFile),
2425
});
2526
registry.registerCommand(OpenSketchFiles.Commands.ENSURE_OPENED, {
2627
execute: (
@@ -33,13 +34,19 @@ export class OpenSketchFiles extends SketchContribution {
3334
});
3435
}
3536

36-
private async openSketchFiles(uri: URI): Promise<void> {
37+
private async openSketchFiles(
38+
uri: URI,
39+
focusMainSketchFile = false
40+
): Promise<void> {
3741
try {
3842
const sketch = await this.sketchService.loadSketch(uri.toString());
3943
const { mainFileUri, rootFolderFileUris } = sketch;
4044
for (const uri of [mainFileUri, ...rootFolderFileUris]) {
4145
await this.ensureOpened(uri);
4246
}
47+
if (focusMainSketchFile) {
48+
await this.ensureOpened(mainFileUri, true, { mode: 'activate' });
49+
}
4350
if (mainFileUri.endsWith('.pde')) {
4451
const message = nls.localize(
4552
'arduino/common/oldFormat',
@@ -126,7 +133,7 @@ export class OpenSketchFiles extends SketchContribution {
126133
uri: string,
127134
forceOpen = false,
128135
options?: EditorOpenerOptions
129-
): Promise<unknown> {
136+
): Promise<EditorWidget | undefined> {
130137
const widget = this.editorManager.all.find(
131138
(widget) => widget.editor.uri.toString() === uri
132139
);
@@ -190,17 +197,18 @@ export class OpenSketchFiles extends SketchContribution {
190197
});
191198

192199
const timeout = 5_000; // number of ms IDE2 waits for the editor to show up in the UI
193-
const result = await Promise.race([
200+
const result: EditorWidget | undefined | 'timeout' = await Promise.race([
194201
deferred.promise,
195202
wait(timeout).then(() => {
196203
disposables.dispose();
197-
return 'timeout';
204+
return 'timeout' as const;
198205
}),
199206
]);
200207
if (result === 'timeout') {
201208
console.warn(
202209
`Timeout after ${timeout} millis. The editor has not shown up in time. URI: ${uri}`
203210
);
211+
return undefined;
204212
}
205213
return result;
206214
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { injectable, inject } from '@theia/core/shared/inversify';
2-
import { FileService } from '@theia/filesystem/lib/browser/file-service';
32
import { CommandService } from '@theia/core/lib/common/command';
43
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
54
import { FrontendApplication as TheiaFrontendApplication } from '@theia/core/lib/browser/frontend-application';
@@ -8,28 +7,33 @@ import { OpenSketchFiles } from '../../contributions/open-sketch-files';
87

98
@injectable()
109
export class FrontendApplication extends TheiaFrontendApplication {
11-
@inject(FileService)
12-
protected readonly fileService: FileService;
13-
1410
@inject(WorkspaceService)
15-
protected readonly workspaceService: WorkspaceService;
11+
private readonly workspaceService: WorkspaceService;
1612

1713
@inject(CommandService)
18-
protected readonly commandService: CommandService;
14+
private readonly commandService: CommandService;
1915

2016
@inject(SketchesService)
21-
protected readonly sketchesService: SketchesService;
17+
private readonly sketchesService: SketchesService;
18+
19+
private layoutWasRestored = false;
2220

2321
protected override async initializeLayout(): Promise<void> {
2422
await super.initializeLayout();
2523
this.workspaceService.roots.then(async (roots) => {
2624
for (const root of roots) {
2725
await this.commandService.executeCommand(
2826
OpenSketchFiles.Commands.OPEN_SKETCH_FILES.id,
29-
root.resource
27+
root.resource,
28+
!this.layoutWasRestored
3029
);
3130
this.sketchesService.markAsRecentlyOpened(root.resource.toString()); // no await, will get the notification later and rebuild the menu
3231
}
3332
});
3433
}
34+
35+
protected override async restoreLayout(): Promise<boolean> {
36+
this.layoutWasRestored = await super.restoreLayout();
37+
return this.layoutWasRestored;
38+
}
3539
}

0 commit comments

Comments
 (0)