Skip to content

Commit

Permalink
Merge pull request #420 from caorushizi/fix/macOs_scheme
Browse files Browse the repository at this point in the history
fix: scheme open app in macOs
  • Loading branch information
caorushizi authored Feb 14, 2025
2 parents b35440f + 34db4a0 commit eb339c1
Show file tree
Hide file tree
Showing 10 changed files with 9,712 additions and 4,704 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@
"patchedDependencies": {
"@ghostery/adblocker@2.3.1": "patches/@ghostery__adblocker@2.3.1.patch"
}
},
"dependencies": {
"react-dom": "18.3.1"
}
}
1 change: 1 addition & 0 deletions packages/main/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist
bin/Logs
release
types
.DS_Store
8 changes: 8 additions & 0 deletions packages/main/scripts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ export function getReleaseConfig(): Configuration {
arch: [process.arch === "arm64" ? "arm64" : "x64"],
},
],
extendInfo: {
CFBundleURLTypes: [
{
CFBundleURLName: "Mediago URL Scheme",
CFBundleURLSchemes: ["mediago"],
},
],
},
},
linux: {
category: "Utility",
Expand Down
22 changes: 20 additions & 2 deletions packages/main/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { inject, injectable } from "inversify";
import { DownloadStatus } from "./interfaces.ts";
import { TYPES } from "./types.ts";
import { Menu, Tray, app, nativeImage, nativeTheme, Event } from "electron";
import {
Menu,
Tray,
app,
nativeImage,
nativeTheme,
Event,
BrowserWindow,
} from "electron";
import TrayIcon from "./tray-icon.png";
import TrayIconLight from "./tray-icon-light.png";
import path from "path";
Expand Down Expand Up @@ -64,7 +72,9 @@ export default class ElectronApp {
await this.serviceInit();

app.on("activate", () => {
this.mainWindow.init();
if (BrowserWindow.getAllWindows().length === 0) {
this.mainWindow.init();
}
});

this.initAppTheme();
Expand Down Expand Up @@ -120,4 +130,12 @@ export default class ElectronApp {
const url = commandLine.pop() || "";
this.mainWindow.showWindow(url);
};

handleOpenUrl(url: string): void {
this.mainWindow.handleUrl(url);
}

send(url: string): void {
this.mainWindow.send("url-params", url);
}
}
26 changes: 25 additions & 1 deletion packages/main/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TYPES } from "./types.ts";
import ElectronApp from "./app.ts";

const gotTheLock = app.requestSingleInstanceLock();
app.setAsDefaultProtocolClient("mediago");
const start = async (): Promise<void> => {
if (!gotTheLock) {
app.quit();
Expand All @@ -23,8 +24,31 @@ const start = async (): Promise<void> => {
]);
await app.whenReady();
const mediago = container.get<ElectronApp>(TYPES.ElectronApp);
// let initialUrl: string = "";
// if (process.defaultApp) {
// // dev
// if (process.argv.length >= 2) {
// const urlArg = process.argv.find((arg) => arg.startsWith("mediago://"));
// if (urlArg) {
// initialUrl = urlArg;
// }
// }
// } else {
// // prod
// if (process.argv.length >= 2) {
// const urlArg = process.argv[1];
// if (urlArg.startsWith("mediago://")) {
// initialUrl = urlArg;
// }
// }
// }
app.on("open-url", function (event, url) {
event.preventDefault();
if (mediago) {
mediago.handleOpenUrl(url);
}
});
mediago.init();

app.on("window-all-closed", noop);
app.on("second-instance", mediago.secondInstance);
};
Expand Down
6 changes: 6 additions & 0 deletions packages/main/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ const electronApi = {
getVideoFolders(): Promise<string[]> {
return ipcRenderer.invoke("get-video-folders");
},
// ipc with main process to get url params
onUrlParams(callback: (url: string) => void): void {
ipcRenderer.on("url-params", (event, url) => {
callback(url);
});
},
};

contextBridge.exposeInMainWorld(apiKey, electronApi);
54 changes: 54 additions & 0 deletions packages/main/src/windows/MainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { isWin } from "../helper/variables.ts";
@injectable()
export default class MainWindow extends Window {
url = isDev ? "http://localhost:8555/" : "mediago://index.html/";
private initialUrl: string | null = null;
constructor(
@inject(TYPES.ElectronLogger)
private readonly logger: ElectronLogger,
Expand Down Expand Up @@ -81,6 +82,32 @@ export default class MainWindow extends Window {
// Handle current window resize
this.window.on("resized", this.handleResize);
this.window.on("close", this.closeMainWindow);
// if (process.defaultApp) {
// // dev
// if (process.argv.length >= 2) {
// const urlArg = process.argv.find((arg) => arg.startsWith("mediago://"));
// if (urlArg) {
// this.initialUrl = urlArg;
// }
// }
// } else {
// // prod
// if (process.argv.length >= 2) {
// const urlArg = process.argv[1];
// if (urlArg.startsWith("mediago://")) {
// this.initialUrl = urlArg;
// }
// }
// }
// this.window.webContents.on("did-finish-load", () => {
// if (this.initialUrl) {
// this.send("url-params", this.initialUrl);
// }
// });

// if (this.initialUrl) {
// this.window.webContents.send("url-params", this.initialUrl);
// }
}

handleResize = () => {
Expand Down Expand Up @@ -150,6 +177,13 @@ export default class MainWindow extends Window {
if (!this.window) return;

this.window.webContents.send(channel, ...args);
if (!this.window) {
this.init(); // If the window is closed, reinitialize the window
}

if (this.window) {
this.window.webContents.send(channel, ...args); // Send message to renderer process
}
}

showWindow(url?: string) {
Expand All @@ -172,4 +206,24 @@ export default class MainWindow extends Window {
}
}
}

// Handle URL in the form of mediago://
handleUrl(url: string) {
if (!this.window) {
this.init();
}

if (this.window) {
if (this.window.isMinimized()) {
this.window.restore();
}
this.window.focus();
}

this.send("url-params", url); // Send the URL to the renderer process

if (url) {
this.window!.loadURL(url);
}
}
}
4 changes: 4 additions & 0 deletions packages/renderer/src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ const apis: ElectronApi = {
getVideoFolders: async () => {
return api.post("get-video-folders");
},
onUrlParams: (callback: (url: string) => void) => {
// Implement the logic for handling URL parameters here
// For now, you can leave it as a placeholder
},
};

export default apis;
25 changes: 25 additions & 0 deletions packages/renderer/src/pages/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@ const HomePage: FC<Props> = ({ filter = DownloadFilter.list }) => {
}
}, [location.search]);

// mac ipc event get url params in macos schceme
useEffect(() => {
const handleUrlEvent = (url: string) => {
const searchParams = new URLSearchParams(url.split("?")[1]);
if (searchParams.get("n") === "true") {
const name = searchParams.get("name") || randomName();
const urlParam = searchParams.get("url") || "";
const item: DownloadFormType = {
batch: false,
type: urlDownloadType(urlParam),
url: urlParam,
name,
headers: "",
};
newFormRef.current?.openModal(item);
}
};

window.electron.onUrlParams(handleUrlEvent);

return () => {
window.electron.onUrlParams(handleUrlEvent);
};
}, []);

useMount(async () => {
const ip = await getLocalIP();
setLocalIP(ip);
Expand Down
Loading

0 comments on commit eb339c1

Please sign in to comment.