Skip to content

Commit

Permalink
feat: zulip:// URI Scheme for electron #470
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
avijitdas126 committed Feb 8, 2025
1 parent 13f3818 commit a573141
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions app/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {clipboard} from "electron/common";
import {clipboard, shell} from "electron/common";
import {
BrowserWindow,
type IpcMainEvent,
Expand Down Expand Up @@ -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(
Expand All @@ -174,14 +176,19 @@ function createMainWindow(): BrowserWindow {

remoteMain.initialize();

app.on("second-instance", () => {
app.on("second-instance", (event, commandLine) => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}

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(
Expand All @@ -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) => {
Expand Down

0 comments on commit a573141

Please sign in to comment.