fix: wait for theia initalWindow to be set before opening sketch through open-file event (#2693)

* chore: use actual app name as `uriScheme`

* fix: wait for `initialWindow` to be set before opening sketch from file
This commit is contained in:
Giacomo Cusinato 2025-04-08 02:15:59 +09:00 committed by GitHub
parent 56ab874177
commit a669a43449
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 2 deletions

View File

@ -38,3 +38,26 @@ export function uint8ArrayToString(uint8Array: Uint8Array): string {
export function stringToUint8Array(text: string): Uint8Array {
return Uint8Array.from(text, (char) => char.charCodeAt(0));
}
export function poolWhile(
whileCondition: () => boolean,
intervalMs: number,
timeoutMs: number
): Promise<void> {
return new Promise((resolve, reject) => {
if (!whileCondition) {
resolve();
}
const start = Date.now();
const interval = setInterval(() => {
if (!whileCondition()) {
clearInterval(interval);
resolve();
} else if (Date.now() - start > timeoutMs) {
clearInterval(interval);
reject(new Error('Timed out while polling.'));
}
}, intervalMs);
});
}

View File

@ -30,6 +30,7 @@ import type { AddressInfo } from 'node:net';
import { isAbsolute, join, resolve } from 'node:path';
import type { Argv } from 'yargs';
import { Sketch } from '../../common/protocol';
import { poolWhile } from '../../common/utils';
import {
AppInfo,
appInfoPropertyLiterals,
@ -292,6 +293,16 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
);
if (sketchFolderPath) {
this.openFilePromise.reject(new InterruptWorkspaceRestoreError());
// open-file event is triggered before the app is ready and initialWindow is created.
// Wait for initialWindow to be set before opening the sketch on the first instance.
// See https://github.com/arduino/arduino-ide/pull/2693
try {
await app.whenReady();
if (!this.firstWindowId) {
await poolWhile(() => !this.initialWindow, 100, 3000);
}
} catch {}
await this.openSketch(sketchFolderPath);
}
}
@ -890,7 +901,10 @@ const fallbackFrontendAppConfig: FrontendApplicationConfig = {
defaultIconTheme: 'none',
validatePreferencesSchema: false,
defaultLocale: '',
electron: { showWindowEarly: true, uriScheme: 'custom://arduino-ide' },
electron: {
showWindowEarly: true,
uriScheme: 'arduino-ide',
},
reloadOnReconnect: true,
};

View File

@ -67,7 +67,8 @@
"defaultIconTheme": "none",
"validatePreferencesSchema": false,
"electron": {
"showWindowEarly": true
"showWindowEarly": true,
"uriScheme": "arduino-ide"
},
"reloadOnReconnect": true,
"preferences": {