diff --git a/app/main/index.ts b/app/main/index.ts index e04c59936..04db0d26e 100644 --- a/app/main/index.ts +++ b/app/main/index.ts @@ -1,4 +1,4 @@ -import {clipboard} from "electron/common"; +import {clipboard, shell} from "electron/common"; import { BrowserWindow, type IpcMainEvent, @@ -37,7 +37,7 @@ import "gatemaker/electron-setup"; // eslint-disable-line import/no-unassigned-i // eslint-disable-next-line @typescript-eslint/naming-convention const {GDK_BACKEND} = process.env; -// Initialize sentry for main process +// Initialize sentry for a main process sentryInit(); let mainWindowState: windowStateKeeper.State; @@ -157,6 +157,8 @@ function createMainWindow(): BrowserWindow { } await app.whenReady(); + // Register custom protocol handler + app.setAsDefaultProtocolClient("zulip"); if (process.env.GDK_BACKEND !== GDK_BACKEND) { console.warn( @@ -174,7 +176,7 @@ function createMainWindow(): BrowserWindow { remoteMain.initialize(); - app.on("second-instance", () => { + app.on("second-instance", (event, commandLine) => { if (mainWindow) { if (mainWindow.isMinimized()) { mainWindow.restore(); @@ -182,6 +184,12 @@ function createMainWindow(): BrowserWindow { mainWindow.show(); } + // Handle deep link when opened from protocol + + const url = commandLine.find((argument) => argument.startsWith("zulip://")); + if (url) { + mainWindow.webContents.send("open-url", url); + } }); ipcMain.on( @@ -196,6 +204,24 @@ function createMainWindow(): BrowserWindow { app.on("activate", () => { mainWindow.show(); }); + // Handle deep linking when app is already running (macOS) + app.on("open-url", async (event, url) => { + event.preventDefault(); + + const response = await dialog.showMessageBox({ + type: "question", + buttons: ["Open in App", "Open in Browser"], + defaultId: 0, + message: `Do you want to open this link in the desktop app or browser?`, + detail: url, + }); + + if (response.response === 0) { + mainWindow.webContents.send("open-url", url); + } else { + await shell.openExternal(url); // Open in browser + } + }); app.on("web-contents-created", (_event, contents: WebContents) => { contents.setWindowOpenHandler((details) => { diff --git a/package.json b/package.json index 15e4b33c9..8f3d05026 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,14 @@ "build": { "appId": "org.zulip.zulip-electron", "asar": true, + "protocols": [ + { + "name": "Zulip", + "schemes": [ + "zulip" + ] + } + ], "asarUnpack": [ "**/*.node" ],