From 6826feb7848488af78737493165184c4f6fcc260 Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 3 Nov 2022 12:16:49 +0100 Subject: [PATCH 1/9] generate .desktop file --- packages/insomnia/electron-builder.config.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/insomnia/electron-builder.config.js b/packages/insomnia/electron-builder.config.js index a626cf41d73..25b5a24fc75 100644 --- a/packages/insomnia/electron-builder.config.js +++ b/packages/insomnia/electron-builder.config.js @@ -102,6 +102,11 @@ const config = { executableName: 'insomnia', synopsis: 'The Collaborative API Client and Design Tool', category: 'Development', + desktop: { + Name: 'Insomnia', + Comment: 'Insomnia is a cross-platform REST client, built on top of Electron.', + Categories: 'Development', + }, target: [ { target: 'AppImage', From ec9a428eeb2db16eae34cbc7697e79abbe438c2d Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 3 Nov 2022 12:29:19 +0100 Subject: [PATCH 2/9] allow only one single instance --- packages/insomnia/src/main.development.ts | 40 ++++++++++++----------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/insomnia/src/main.development.ts b/packages/insomnia/src/main.development.ts index 31a7d0d0f5d..3d6a881efb2 100644 --- a/packages/insomnia/src/main.development.ts +++ b/packages/insomnia/src/main.development.ts @@ -162,31 +162,33 @@ const _launchApp = async () => { // @TODO: Investigate why this closes electron when using playwright (tested on macOS) // and find a better solution. if (!process.env.PLAYWRIGHT) { + // Deep linking logic - https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app const gotTheLock = app.requestSingleInstanceLock(); if (!gotTheLock) { console.error('[app] Failed to get instance lock'); - return; + app.quit(); + } else { + app.on('second-instance', () => { + // Someone tried to run a second instance, we should focus our window. + if (window) { + if (window.isMinimized()) { + window.restore(); + } + window.focus(); + } + }); + // Handle URLs when app already open + app.addListener('open-url', (_event, url) => { + window.webContents.send('run-command', url); + // Apparently a timeout is needed because Chrome steals back focus immediately + // after opening the URL. + setTimeout(() => { + window.focus(); + }, 100); + }); } } - app.on('second-instance', () => { - // Someone tried to run a second instance, we should focus our window. - if (window) { - if (window.isMinimized()) { - window.restore(); - } - window.focus(); - } - }); - // Handle URLs when app already open - app.addListener('open-url', (_event, url) => { - window.webContents.send('run-command', url); - // Apparently a timeout is needed because Chrome steals back focus immediately - // after opening the URL. - setTimeout(() => { - window.focus(); - }, 100); - }); // Don't send origin header from Insomnia because we're not technically using CORS session.defaultSession.webRequest.onBeforeSendHeaders((details, fn) => { delete details.requestHeaders.Origin; From 46edfa888242895e7ab59ac6ed53ba2bca25f095 Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 3 Nov 2022 12:30:35 +0100 Subject: [PATCH 3/9] fix formatting --- packages/insomnia/src/main.development.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/insomnia/src/main.development.ts b/packages/insomnia/src/main.development.ts index 3d6a881efb2..3ef94c73005 100644 --- a/packages/insomnia/src/main.development.ts +++ b/packages/insomnia/src/main.development.ts @@ -162,7 +162,7 @@ const _launchApp = async () => { // @TODO: Investigate why this closes electron when using playwright (tested on macOS) // and find a better solution. if (!process.env.PLAYWRIGHT) { - // Deep linking logic - https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app + // Deep linking logic - https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app const gotTheLock = app.requestSingleInstanceLock(); if (!gotTheLock) { console.error('[app] Failed to get instance lock'); From 55cb57c94aab954ce96786c42ec1926dbfba65d0 Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 3 Nov 2022 12:31:53 +0100 Subject: [PATCH 4/9] remove focus hack --- packages/insomnia/src/main.development.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/insomnia/src/main.development.ts b/packages/insomnia/src/main.development.ts index 3ef94c73005..24bd43c8aa5 100644 --- a/packages/insomnia/src/main.development.ts +++ b/packages/insomnia/src/main.development.ts @@ -180,11 +180,6 @@ const _launchApp = async () => { // Handle URLs when app already open app.addListener('open-url', (_event, url) => { window.webContents.send('run-command', url); - // Apparently a timeout is needed because Chrome steals back focus immediately - // after opening the URL. - setTimeout(() => { - window.focus(); - }, 100); }); } } From 337934b2a3015e9f53af2fe15b929cac41bb46df Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 3 Nov 2022 12:36:48 +0100 Subject: [PATCH 5/9] rename and clean up open-url listeners --- packages/insomnia/src/main.development.ts | 4 ++-- packages/insomnia/src/ui/hooks/use-app-commands.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/insomnia/src/main.development.ts b/packages/insomnia/src/main.development.ts index 24bd43c8aa5..8f9836789a7 100644 --- a/packages/insomnia/src/main.development.ts +++ b/packages/insomnia/src/main.development.ts @@ -156,7 +156,7 @@ const _launchApp = async () => { // Handle URLs sent via command line args ipcMain.once('window-ready', () => { // @ts-expect-error -- TSCONVERSION - commandLineArgs.length && window.send('run-command', commandLineArgs[0]); + commandLineArgs.length && window.send('shell:open', commandLineArgs[0]); }); // Called when second instance launched with args (Windows) // @TODO: Investigate why this closes electron when using playwright (tested on macOS) @@ -179,7 +179,7 @@ const _launchApp = async () => { }); // Handle URLs when app already open app.addListener('open-url', (_event, url) => { - window.webContents.send('run-command', url); + window.webContents.send('shell:open', url); }); } } diff --git a/packages/insomnia/src/ui/hooks/use-app-commands.ts b/packages/insomnia/src/ui/hooks/use-app-commands.ts index b707b12c021..1e8675532c1 100644 --- a/packages/insomnia/src/ui/hooks/use-app-commands.ts +++ b/packages/insomnia/src/ui/hooks/use-app-commands.ts @@ -1,4 +1,4 @@ -import { ipcRenderer } from 'electron'; +import { IpcRendererEvent } from 'electron/renderer'; import { useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { parse } from 'url'; @@ -12,8 +12,8 @@ export const useAppCommands = () => { // Handle Application Commands useEffect(() => { - ipcRenderer.on('run-command', (_, commandUri) => { - const parsed = parse(commandUri, true); + return window.main.on('shell:open', (_: IpcRendererEvent, url: string) => { + const parsed = parse(url, true); const command = `${parsed.hostname}${parsed.pathname}`; const args = JSON.parse(JSON.stringify(parsed.query)); args.workspaceId = args.workspaceId || activeWorkspace?._id; From 862de1a135e87401b6aca83202dae2738fe0eed7 Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 3 Nov 2022 17:10:42 +0100 Subject: [PATCH 6/9] remove second open-url listener --- packages/insomnia/src/main.development.ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/packages/insomnia/src/main.development.ts b/packages/insomnia/src/main.development.ts index 8f9836789a7..e741470f840 100644 --- a/packages/insomnia/src/main.development.ts +++ b/packages/insomnia/src/main.development.ts @@ -1,4 +1,5 @@ import electron, { app, ipcMain, session } from 'electron'; +import { BrowserWindow } from 'electron/main'; import contextMenu from 'electron-context-menu'; import installExtension, { REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } from 'electron-devtools-installer'; import path from 'path'; @@ -30,7 +31,6 @@ if (checkIfRestartNeeded()) { } initializeLogging(); -const commandLineArgs = process.argv.slice(1); log.info(`Running version ${getAppVersion()}`); // Override the Electron userData path @@ -121,12 +121,6 @@ if (defaultProtocolSuccessful) { } } -function _addUrlToOpen(event: Electron.Event, url: string) { - event.preventDefault(); - commandLineArgs.push(url); -} - -app.on('open-url', _addUrlToOpen); // Quit when all windows are closed (except on Mac). app.on('window-all-closed', () => { if (!isMac()) { @@ -149,14 +143,11 @@ app.on('activate', (_error, hasVisibleWindows) => { const _launchApp = async () => { await _trackStats(); - - app.removeListener('open-url', _addUrlToOpen); - const window = windowUtils.createWindow(); - + let window: BrowserWindow; // Handle URLs sent via command line args ipcMain.once('window-ready', () => { - // @ts-expect-error -- TSCONVERSION - commandLineArgs.length && window.send('shell:open', commandLineArgs[0]); + console.log('[main] Window ready, handling command line arguments', process.argv); + window.webContents.send('shell:open', process.argv.slice(1)); }); // Called when second instance launched with args (Windows) // @TODO: Investigate why this closes electron when using playwright (tested on macOS) @@ -177,6 +168,8 @@ const _launchApp = async () => { window.focus(); } }); + window = windowUtils.createWindow(); + // Handle URLs when app already open app.addListener('open-url', (_event, url) => { window.webContents.send('shell:open', url); From 860d94ada4d943103249b4ce88d76df35126d1d0 Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 3 Nov 2022 17:27:51 +0100 Subject: [PATCH 7/9] add debug logging and second instance event --- packages/insomnia/src/main.development.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/insomnia/src/main.development.ts b/packages/insomnia/src/main.development.ts index e741470f840..62c5375de2b 100644 --- a/packages/insomnia/src/main.development.ts +++ b/packages/insomnia/src/main.development.ts @@ -132,6 +132,7 @@ app.on('activate', (_error, hasVisibleWindows) => { // Create a new window when clicking the doc icon if there isn't one open if (!hasVisibleWindows) { try { + console.log('[main] creating new window for MacOS activate event'); windowUtils.createWindow(); } catch (error) { // This might happen if 'ready' hasn't fired yet. So we're just going @@ -159,19 +160,22 @@ const _launchApp = async () => { console.error('[app] Failed to get instance lock'); app.quit(); } else { - app.on('second-instance', () => { - // Someone tried to run a second instance, we should focus our window. + app.on('second-instance', (_1, args) => { + console.log('Second instance listener recieved:', args.join('||')); if (window) { if (window.isMinimized()) { window.restore(); } window.focus(); } + const lastArg = args.slice(-1).join(); + console.log('[main] Open Deep Link URL sent from second instance', lastArg); + window.webContents.send('shell:open', lastArg); }); window = windowUtils.createWindow(); - // Handle URLs when app already open - app.addListener('open-url', (_event, url) => { + app.on('open-url', (_event, url) => { + console.log('[main] Open Deep Link URL', url); window.webContents.send('shell:open', url); }); } From 225a823083e0747d4eda86b481381cbb93aa13d3 Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 3 Nov 2022 17:48:37 +0100 Subject: [PATCH 8/9] comments --- packages/insomnia/src/main.development.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/insomnia/src/main.development.ts b/packages/insomnia/src/main.development.ts index 62c5375de2b..a7bcca8600b 100644 --- a/packages/insomnia/src/main.development.ts +++ b/packages/insomnia/src/main.development.ts @@ -150,9 +150,7 @@ const _launchApp = async () => { console.log('[main] Window ready, handling command line arguments', process.argv); window.webContents.send('shell:open', process.argv.slice(1)); }); - // Called when second instance launched with args (Windows) - // @TODO: Investigate why this closes electron when using playwright (tested on macOS) - // and find a better solution. + // Disable deep linking in playwright e2e tests in order to run multiple tests in parallel if (!process.env.PLAYWRIGHT) { // Deep linking logic - https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app const gotTheLock = app.requestSingleInstanceLock(); @@ -160,6 +158,7 @@ const _launchApp = async () => { console.error('[app] Failed to get instance lock'); app.quit(); } else { + // Called when second instance launched with args (Windows/Linux) app.on('second-instance', (_1, args) => { console.log('Second instance listener recieved:', args.join('||')); if (window) { @@ -179,6 +178,8 @@ const _launchApp = async () => { window.webContents.send('shell:open', url); }); } + } else { + window = windowUtils.createWindow(); } // Don't send origin header from Insomnia because we're not technically using CORS From 92610668ff1ae8b5da916e7303ab8d5c22deba55 Mon Sep 17 00:00:00 2001 From: Filipe Freire Date: Thu, 3 Nov 2022 16:50:01 +0000 Subject: [PATCH 9/9] fix typo --- packages/insomnia/src/main.development.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/insomnia/src/main.development.ts b/packages/insomnia/src/main.development.ts index a7bcca8600b..287f30d8c0e 100644 --- a/packages/insomnia/src/main.development.ts +++ b/packages/insomnia/src/main.development.ts @@ -160,7 +160,7 @@ const _launchApp = async () => { } else { // Called when second instance launched with args (Windows/Linux) app.on('second-instance', (_1, args) => { - console.log('Second instance listener recieved:', args.join('||')); + console.log('Second instance listener received:', args.join('||')); if (window) { if (window.isMinimized()) { window.restore();