From a573141108d40045fccf3bf076854237187f7691 Mon Sep 17 00:00:00 2001 From: Avijit Das Date: Sat, 8 Feb 2025 17:52:11 +0530 Subject: [PATCH] feat: zulip:// URI Scheme for electron #470 Added deep linking support with zulip://, registered the protocol handler, and implemented a dialog to choose between opening the link in the app or browser. --- app/main/index.ts | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/app/main/index.ts b/app/main/index.ts index e04c59936..3a4da14c0 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, @@ -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,11 @@ 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 +203,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) => {