From f529460eda8c14839ca64802651f81d1c661abc0 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 12 Jun 2024 14:33:07 +0530 Subject: [PATCH] Handle deeplinks on Linux --- desktop/src/main.ts | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/desktop/src/main.ts b/desktop/src/main.ts index 180fce2acf..50f69759da 100644 --- a/desktop/src/main.ts +++ b/desktop/src/main.ts @@ -83,13 +83,26 @@ const main = () => { registerPrivilegedSchemes(); migrateLegacyWatchStoreIfNeeded(); - app.on("second-instance", () => { + /** + * Handle an open URL request, but ensuring that we have a mainWindow. + */ + const handleOpenURLEnsuringWindow = (url: string) => { + log.info(`Attempting to handle request to open URL: ${url}`); + if (mainWindow) handleEnteLinks(mainWindow, url); + else setTimeout(() => handleOpenURLEnsuringWindow(url), 1000); + }; + + app.on("second-instance", (_, argv: string[]) => { // Someone tried to run a second instance, we should focus our window. if (mainWindow) { mainWindow.show(); if (mainWindow.isMinimized()) mainWindow.restore(); mainWindow.focus(); } + // On Windows and Linux, this is how we get deeplinks. + // See: registerForEnteLinks + const url = argv.pop(); + if (url) handleOpenURLEnsuringWindow(url); }); // Emitted once, when Electron has finished initializing. @@ -141,12 +154,8 @@ const main = () => { allowWindowClose(); }); - const handleOpenURLWithWindow = (url: string) => { - log.info(`Attempting to handle request to open URL: ${url}`); - if (mainWindow) handleEnteLinks(mainWindow, url); - else setTimeout(() => handleOpenURLWithWindow(url), 1000); - }; - app.on("open-url", (_, url) => handleOpenURLWithWindow(url)); + // On macOS, this is how we get deeplinks. See: registerForEnteLinks + app.on("open-url", (_, url) => handleOpenURLEnsuringWindow(url)); }; /** @@ -233,6 +242,8 @@ const registerPrivilegedSchemes = () => { * Implementation notes: * - https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app * - This works only when the app is packaged. + * - On Windows and Linux, we get the deeplink in the "second-instance" event. + * - On macOS, we get the deeplink in the "open-url" event. */ const registerForEnteLinks = () => app.setAsDefaultProtocolClient("ente");