Skip to content

Commit

Permalink
chore(main): restructure IPC handlers
Browse files Browse the repository at this point in the history
- Move IPC handlers to dedicated directory.
  • Loading branch information
skjsjhb committed Jan 25, 2025
1 parent 94c2441 commit be5e5e5
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 86 deletions.
5 changes: 5 additions & 0 deletions src/main/api/conf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { conf, type UserConfig } from "@/main/conf/conf";
import { ipcMain } from "@/main/ipc/typed";

ipcMain.handle("getConfig", () => conf());
ipcMain.on("updateConfig", (_, c: UserConfig) => conf.updateWith(c));
14 changes: 14 additions & 0 deletions src/main/api/ext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ipcMain } from "@/main/ipc/typed";
import { dialog, shell } from "electron";

ipcMain.on("openUrl", (_, url: string) => {
void shell.openExternal(url, { activate: true });
});

ipcMain.handle("selectDir", async () => {
const { filePaths } = await dialog.showOpenDialog({
properties: ["openDirectory", "createDirectory", "promptToCreate", "dontAddToRecent"]
});

return filePaths[0] ?? "";
});
3 changes: 3 additions & 0 deletions src/main/api/loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "./conf";
import "./ext";
import "./window";
11 changes: 11 additions & 0 deletions src/main/api/window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ipcMain } from "@/main/ipc/typed";
import { BrowserWindow } from "electron";

ipcMain.on("showWindow", (e) => getWindow(e.sender.id)?.show());
ipcMain.on("hideWindow", (e) => getWindow(e.sender.id)?.hide());
ipcMain.on("minimizeWindow", (e) => getWindow(e.sender.id)?.minimize());
ipcMain.on("closeWindow", (e) => getWindow(e.sender.id)?.close());

function getWindow(id: number): BrowserWindow | null {
return BrowserWindow.getAllWindows().find(w => w.webContents.id === id) ?? null;
}
15 changes: 0 additions & 15 deletions src/main/conf/conf-host.ts

This file was deleted.

9 changes: 2 additions & 7 deletions src/main/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { conf } from "@/main/conf/conf";
import { confHost } from "@/main/conf/conf-host";
import { paths } from "@/main/fs/paths";
import { vanillaInstaller } from "@/main/install/vanilla";
import { aria2 } from "@/main/net/aria2";
import { mirror } from "@/main/net/mirrors";
import { registry } from "@/main/registry/registry";
import { ext } from "@/main/sys/ext";
import { getOSName } from "@/main/sys/os";
import { windowControl } from "@/main/sys/window-control";
import { isTruthy } from "@/main/util/misc";
Expand Down Expand Up @@ -69,8 +67,8 @@ async function main() {

await registry.init();

confHost.setup();
ext.setup();
// Register IPC handlers
await import("@/main/api/loader");

// React DevTools seems unable to load starting from Electron v33
// This can only be enabled when https://github.com/electron/electron/issues/41613 is solved
Expand Down Expand Up @@ -118,9 +116,6 @@ async function main() {

mainWindow.setMenu(null);

windowControl.setup();
windowControl.forWindow(mainWindow);

// Exit app once main window closed
mainWindow.once("closed", () => {
mainWindow = null;
Expand Down
23 changes: 0 additions & 23 deletions src/main/sys/ext.ts

This file was deleted.

43 changes: 2 additions & 41 deletions src/main/sys/window-control.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,4 @@
/**
* Window management module.
*/

import { ipcMain } from "@/main/ipc/typed";
import { type BrowserWindow, screen } from "electron";

const allowedWindows = new Set<BrowserWindow>();

/**
* Setup listeners.
*/
function setup() {
ipcMain.on("showWindow", (e) => getWindow(e.sender.id)?.show());
ipcMain.on("hideWindow", (e) => getWindow(e.sender.id)?.hide());
ipcMain.on("minimizeWindow", (e) => getWindow(e.sender.id)?.minimize());
ipcMain.on("closeWindow", (e) => getWindow(e.sender.id)?.close());
}

/**
* Gets the window by the ID of its web contents.
* @param id Web contents ID.
*/
function getWindow(id: number): BrowserWindow | null {
for (const w of allowedWindows) {
if (w.webContents.id === id) return w;
}
return null;
}


/**
* Enable window management API for the given window.
*/
function forWindow(w: BrowserWindow) {
allowedWindows.add(w);

// Revoke the listeners once closed
w.once("closed", () => allowedWindows.delete(w));
}
import { screen } from "electron";

/**
* Gets an optimal window size for the main window.
Expand All @@ -60,4 +21,4 @@ function optimalSize(): [number, number] {
return [Math.round(width * scaleFactor), Math.round(height * scaleFactor)];
}

export const windowControl = { setup, forWindow, optimalSize };
export const windowControl = { optimalSize };

0 comments on commit be5e5e5

Please sign in to comment.