Skip to content

Commit a669a43

Browse files
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
1 parent 56ab874 commit a669a43

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

arduino-ide-extension/src/common/utils.ts

+23
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,26 @@ export function uint8ArrayToString(uint8Array: Uint8Array): string {
3838
export function stringToUint8Array(text: string): Uint8Array {
3939
return Uint8Array.from(text, (char) => char.charCodeAt(0));
4040
}
41+
42+
export function poolWhile(
43+
whileCondition: () => boolean,
44+
intervalMs: number,
45+
timeoutMs: number
46+
): Promise<void> {
47+
return new Promise((resolve, reject) => {
48+
if (!whileCondition) {
49+
resolve();
50+
}
51+
52+
const start = Date.now();
53+
const interval = setInterval(() => {
54+
if (!whileCondition()) {
55+
clearInterval(interval);
56+
resolve();
57+
} else if (Date.now() - start > timeoutMs) {
58+
clearInterval(interval);
59+
reject(new Error('Timed out while polling.'));
60+
}
61+
}, intervalMs);
62+
});
63+
}

arduino-ide-extension/src/electron-main/theia/electron-main-application.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type { AddressInfo } from 'node:net';
3030
import { isAbsolute, join, resolve } from 'node:path';
3131
import type { Argv } from 'yargs';
3232
import { Sketch } from '../../common/protocol';
33+
import { poolWhile } from '../../common/utils';
3334
import {
3435
AppInfo,
3536
appInfoPropertyLiterals,
@@ -292,6 +293,16 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
292293
);
293294
if (sketchFolderPath) {
294295
this.openFilePromise.reject(new InterruptWorkspaceRestoreError());
296+
297+
// open-file event is triggered before the app is ready and initialWindow is created.
298+
// Wait for initialWindow to be set before opening the sketch on the first instance.
299+
// See https://github.com/arduino/arduino-ide/pull/2693
300+
try {
301+
await app.whenReady();
302+
if (!this.firstWindowId) {
303+
await poolWhile(() => !this.initialWindow, 100, 3000);
304+
}
305+
} catch {}
295306
await this.openSketch(sketchFolderPath);
296307
}
297308
}
@@ -890,7 +901,10 @@ const fallbackFrontendAppConfig: FrontendApplicationConfig = {
890901
defaultIconTheme: 'none',
891902
validatePreferencesSchema: false,
892903
defaultLocale: '',
893-
electron: { showWindowEarly: true, uriScheme: 'custom://arduino-ide' },
904+
electron: {
905+
showWindowEarly: true,
906+
uriScheme: 'arduino-ide',
907+
},
894908
reloadOnReconnect: true,
895909
};
896910

electron-app/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
"defaultIconTheme": "none",
6868
"validatePreferencesSchema": false,
6969
"electron": {
70-
"showWindowEarly": true
70+
"showWindowEarly": true,
71+
"uriScheme": "arduino-ide"
7172
},
7273
"reloadOnReconnect": true,
7374
"preferences": {

0 commit comments

Comments
 (0)