Skip to content

Commit

Permalink
fix: MacOS uri logic
Browse files Browse the repository at this point in the history
I actually tested it inside a vm this time instead of guessing :p
  • Loading branch information
Covkie committed Nov 30, 2024
1 parent 59e51d3 commit 4464752
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
14 changes: 12 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { autoUpdater } from "electron-updater";

import { DATA_DIR } from "./constants";
import { createFirstLaunchTour } from "./firstLaunch";
import { createWindows, restoreVesktop } from "./mainWindow";
import { createWindows, mainWin } from "./mainWindow";
import { registerMediaPermissionsHandler } from "./mediaPermissions";
import { registerScreenShareHandler } from "./screenShare";
import { Settings, State } from "./settings";
Expand Down Expand Up @@ -73,7 +73,11 @@ function init() {

app.on("second-instance", (_event, _cmdLine, _cwd, data: any) => {
if (data.IS_DEV) app.quit();
else restoreVesktop();
else if (mainWin) {
if (mainWin.isMinimized()) mainWin.restore();
if (!mainWin.isVisible()) mainWin.show();
mainWin.focus();
}
});

app.whenReady().then(async () => {
Expand Down Expand Up @@ -110,6 +114,12 @@ async function bootstrap() {
}
}

// MacOS only event
export var darwinURL: string | undefined;
app.on("open-url", (_, url) => {
darwinURL = url;
});

app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit();
});
22 changes: 4 additions & 18 deletions src/main/mainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
MIN_WIDTH,
VENCORD_FILES_DIR
} from "./constants";
import { darwinURL } from "./index";
import { Settings, State, VencordSettings } from "./settings";
import { createSplashWindow } from "./splash";
import { makeLinksOpenExternally } from "./utils/makeLinksOpenExternally";
Expand Down Expand Up @@ -455,35 +456,20 @@ function createMainWindow() {

win.webContents.setUserAgent(BrowserUserAgent);

let uriFiredDarwin = false;
app.on("open-url", (_, url) => {
if (uriFiredDarwin) restoreVesktop();
else loadUrl(url);
uriFiredDarwin = true;
});

const uri = process.argv.find(arg => arg.startsWith("discord://"));
if (!uriFiredDarwin) loadUrl(uri);
// if the open-url event is fired (in index.ts) while starting up, darwinURL will be set. If not fall back to checking the process args (which Windows and Linux use for URI calling.)
loadUrl(darwinURL || process.argv.find(arg => arg.startsWith("discord://")));

return win;
}

const runVencordMain = once(() => require(join(VENCORD_FILES_DIR, "vencordDesktopMain.js")));

function loadUrl(uri: string | undefined) {
export function loadUrl(uri: string | undefined) {
const branch = Settings.store.discordBranch;
const subdomain = branch === "canary" || branch === "ptb" ? `${branch}.` : "";
mainWin.loadURL(`https://${subdomain}discord.com/${uri ? new URL(uri).pathname.slice(1) || "app" : "app"}`);
}

export function restoreVesktop() {
if (mainWin) {
if (mainWin.isMinimized()) mainWin.restore();
if (!mainWin.isVisible()) mainWin.show();
mainWin.focus();
}
}

export async function createWindows() {
const startMinimized = process.argv.includes("--start-minimized");
const splash = createSplashWindow(startMinimized);
Expand Down

0 comments on commit 4464752

Please sign in to comment.