fix: main sketch file editor focus on layout reset

Closes #643

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
This commit is contained in:
Akos Kitta 2022-11-14 19:11:29 +01:00 committed by Akos Kitta
parent ac9cce16f7
commit c0af1e62e8
2 changed files with 25 additions and 13 deletions

View File

@ -20,7 +20,8 @@ import { DisposableCollection } from '@theia/core/lib/common/disposable';
export class OpenSketchFiles extends SketchContribution {
override registerCommands(registry: CommandRegistry): void {
registry.registerCommand(OpenSketchFiles.Commands.OPEN_SKETCH_FILES, {
execute: (uri: URI) => this.openSketchFiles(uri),
execute: (uri: URI, focusMainSketchFile) =>
this.openSketchFiles(uri, focusMainSketchFile),
});
registry.registerCommand(OpenSketchFiles.Commands.ENSURE_OPENED, {
execute: (
@ -33,13 +34,19 @@ export class OpenSketchFiles extends SketchContribution {
});
}
private async openSketchFiles(uri: URI): Promise<void> {
private async openSketchFiles(
uri: URI,
focusMainSketchFile = false
): Promise<void> {
try {
const sketch = await this.sketchService.loadSketch(uri.toString());
const { mainFileUri, rootFolderFileUris } = sketch;
for (const uri of [mainFileUri, ...rootFolderFileUris]) {
await this.ensureOpened(uri);
}
if (focusMainSketchFile) {
await this.ensureOpened(mainFileUri, true, { mode: 'activate' });
}
if (mainFileUri.endsWith('.pde')) {
const message = nls.localize(
'arduino/common/oldFormat',
@ -126,7 +133,7 @@ export class OpenSketchFiles extends SketchContribution {
uri: string,
forceOpen = false,
options?: EditorOpenerOptions
): Promise<unknown> {
): Promise<EditorWidget | undefined> {
const widget = this.editorManager.all.find(
(widget) => widget.editor.uri.toString() === uri
);
@ -190,17 +197,18 @@ export class OpenSketchFiles extends SketchContribution {
});
const timeout = 5_000; // number of ms IDE2 waits for the editor to show up in the UI
const result = await Promise.race([
const result: EditorWidget | undefined | 'timeout' = await Promise.race([
deferred.promise,
wait(timeout).then(() => {
disposables.dispose();
return 'timeout';
return 'timeout' as const;
}),
]);
if (result === 'timeout') {
console.warn(
`Timeout after ${timeout} millis. The editor has not shown up in time. URI: ${uri}`
);
return undefined;
}
return result;
}

View File

@ -1,5 +1,4 @@
import { injectable, inject } from '@theia/core/shared/inversify';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { CommandService } from '@theia/core/lib/common/command';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { FrontendApplication as TheiaFrontendApplication } from '@theia/core/lib/browser/frontend-application';
@ -8,17 +7,16 @@ import { OpenSketchFiles } from '../../contributions/open-sketch-files';
@injectable()
export class FrontendApplication extends TheiaFrontendApplication {
@inject(FileService)
protected readonly fileService: FileService;
@inject(WorkspaceService)
protected readonly workspaceService: WorkspaceService;
private readonly workspaceService: WorkspaceService;
@inject(CommandService)
protected readonly commandService: CommandService;
private readonly commandService: CommandService;
@inject(SketchesService)
protected readonly sketchesService: SketchesService;
private readonly sketchesService: SketchesService;
private layoutWasRestored = false;
protected override async initializeLayout(): Promise<void> {
await super.initializeLayout();
@ -26,10 +24,16 @@ export class FrontendApplication extends TheiaFrontendApplication {
for (const root of roots) {
await this.commandService.executeCommand(
OpenSketchFiles.Commands.OPEN_SKETCH_FILES.id,
root.resource
root.resource,
!this.layoutWasRestored
);
this.sketchesService.markAsRecentlyOpened(root.resource.toString()); // no await, will get the notification later and rebuild the menu
}
});
}
protected override async restoreLayout(): Promise<boolean> {
this.layoutWasRestored = await super.restoreLayout();
return this.layoutWasRestored;
}
}