Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: zulip:// URI Scheme for electron #470 #1407

Closed
32 changes: 29 additions & 3 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 @@ -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;
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,20 @@ 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 +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
}
Comment on lines +219 to +223
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you expecting this to accomplish, in either case? Neither an Electron webContents nor an external web browser knows anything about zulip: URLs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you expecting this to accomplish, in either case? Neither an Electron

When a URL is received, the application presents a dialog to the user. If the user confirms (response.response === 0), the app sends an open-url event to the main window's webContents, allowing the app to handle the URL internally. If the user declines, the URL is opened in the default web browser using shell.openExternal(url).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don’t waste the time of open source maintainers with AI generated slop. If you don’t understand what’s going on well enough to see that this completely failed to address my question, then this is not the project for you.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No sir, I cannot solve this issue using AI.I solved this issue using electron deeplinking documentation. When user hit zulip:// endpoint when open a dialog box if user want to open on the desktop click on open with desktop verify response.response===0 go to desktop using mainWindow.webContents.send("open-url", url); otherwise open with browser await shell.openExternal(url);

});

app.on("web-contents-created", (_event, contents: WebContents) => {
contents.setWindowOpenHandler((details) => {
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
"build": {
"appId": "org.zulip.zulip-electron",
"asar": true,
"protocols": [
{
"name": "Zulip",
"schemes": [
"zulip"
]
}
],
"asarUnpack": [
"**/*.node"
],
Expand Down