From 92abf87f74cfc3436971381a20b50e8a909029e5 Mon Sep 17 00:00:00 2001 From: Giacomo Cusinato <7659518+giacomocusinato@users.noreply.github.com> Date: Mon, 7 Apr 2025 15:11:40 +0700 Subject: [PATCH 1/2] chore: use actual app name as `uriScheme` --- .../src/electron-main/theia/electron-main-application.ts | 5 ++++- electron-app/package.json | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts b/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts index ba1851ff4..26467fe5b 100644 --- a/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts +++ b/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts @@ -890,7 +890,10 @@ const fallbackFrontendAppConfig: FrontendApplicationConfig = { defaultIconTheme: 'none', validatePreferencesSchema: false, defaultLocale: '', - electron: { showWindowEarly: true, uriScheme: 'custom://arduino-ide' }, + electron: { + showWindowEarly: true, + uriScheme: 'arduino-ide', + }, reloadOnReconnect: true, }; diff --git a/electron-app/package.json b/electron-app/package.json index 5bf2b0f18..6a31d222a 100644 --- a/electron-app/package.json +++ b/electron-app/package.json @@ -67,7 +67,8 @@ "defaultIconTheme": "none", "validatePreferencesSchema": false, "electron": { - "showWindowEarly": true + "showWindowEarly": true, + "uriScheme": "arduino-ide" }, "reloadOnReconnect": true, "preferences": { From 0a6ce3e519c1516b735b7a5c411f633a7b9f2504 Mon Sep 17 00:00:00 2001 From: Giacomo Cusinato <7659518+giacomocusinato@users.noreply.github.com> Date: Mon, 7 Apr 2025 23:15:17 +0700 Subject: [PATCH 2/2] fix: wait for `initialWindow` to be set before opening sketch from file --- arduino-ide-extension/src/common/utils.ts | 23 +++++++++++++++++++ .../theia/electron-main-application.ts | 11 +++++++++ 2 files changed, 34 insertions(+) diff --git a/arduino-ide-extension/src/common/utils.ts b/arduino-ide-extension/src/common/utils.ts index 56ed366c1..8a392c770 100644 --- a/arduino-ide-extension/src/common/utils.ts +++ b/arduino-ide-extension/src/common/utils.ts @@ -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 { + 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); + }); +} diff --git a/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts b/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts index 26467fe5b..b6d0ff294 100644 --- a/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts +++ b/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts @@ -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); } }