Skip to content

Commit a573141

Browse files
committed
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.
1 parent 13f3818 commit a573141

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

app/main/index.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {clipboard} from "electron/common";
1+
import {clipboard, shell} from "electron/common";
22
import {
33
BrowserWindow,
44
type IpcMainEvent,
@@ -157,6 +157,8 @@ function createMainWindow(): BrowserWindow {
157157
}
158158

159159
await app.whenReady();
160+
// Register custom protocol handler
161+
app.setAsDefaultProtocolClient("zulip");
160162

161163
if (process.env.GDK_BACKEND !== GDK_BACKEND) {
162164
console.warn(
@@ -174,14 +176,19 @@ function createMainWindow(): BrowserWindow {
174176

175177
remoteMain.initialize();
176178

177-
app.on("second-instance", () => {
179+
app.on("second-instance", (event, commandLine) => {
178180
if (mainWindow) {
179181
if (mainWindow.isMinimized()) {
180182
mainWindow.restore();
181183
}
182184

183185
mainWindow.show();
184186
}
187+
// Handle deep link when opened from protocol
188+
const url = commandLine.find((argument) => argument.startsWith("zulip://"));
189+
if (url) {
190+
mainWindow.webContents.send("open-url", url);
191+
}
185192
});
186193

187194
ipcMain.on(
@@ -196,6 +203,24 @@ function createMainWindow(): BrowserWindow {
196203
app.on("activate", () => {
197204
mainWindow.show();
198205
});
206+
// Handle deep linking when app is already running (macOS)
207+
app.on("open-url", async (event, url) => {
208+
event.preventDefault();
209+
210+
const response = await dialog.showMessageBox({
211+
type: "question",
212+
buttons: ["Open in App", "Open in Browser"],
213+
defaultId: 0,
214+
message: `Do you want to open this link in the desktop app or browser?`,
215+
detail: url,
216+
});
217+
218+
if (response.response === 0) {
219+
mainWindow.webContents.send("open-url", url);
220+
} else {
221+
await shell.openExternal(url); // Open in browser
222+
}
223+
});
199224

200225
app.on("web-contents-created", (_event, contents: WebContents) => {
201226
contents.setWindowOpenHandler((details) => {

0 commit comments

Comments
 (0)