refactor(server); electron imports causing issues in bundle

This commit is contained in:
Elian Doran 2025-05-21 16:32:51 +03:00
parent fe8b905922
commit 9d6758b315
No known key found for this signature in database
4 changed files with 23 additions and 23 deletions

View File

@ -137,7 +137,7 @@ export default async function buildApp() {
startScheduledCleanup(); startScheduledCleanup();
if (utils.isElectron) { if (utils.isElectron) {
(await import("@electron/remote/main")).initialize(); (await import("@electron/remote/main/index.js")).initialize();
} }
return app; return app;

View File

@ -1,4 +1,4 @@
import { ipcMain } from "electron"; import electron from "electron";
interface Response { interface Response {
statusCode: number; statusCode: number;
@ -10,7 +10,7 @@ interface Response {
} }
function init(app: Express.Application) { function init(app: Express.Application) {
ipcMain.on("server-request", (event, arg) => { electron.ipcMain.on("server-request", (event, arg) => {
const req = { const req = {
url: arg.url, url: arg.url,
method: arg.method, method: arg.method,

View File

@ -1,7 +1,7 @@
import { BrowserWindow, Menu, Tray, ipcMain, nativeTheme } from "electron"; import electron from "electron";
import type { BrowserWindow, Tray } from "electron";
import { default as i18next, t } from "i18next"; import { default as i18next, t } from "i18next";
import path from "path"; import path from "path";
import { fileURLToPath } from "url";
import becca from "../becca/becca.js"; import becca from "../becca/becca.js";
import becca_service from "../becca/becca_service.js"; import becca_service from "../becca/becca_service.js";
@ -33,7 +33,7 @@ function getTrayIconPath() {
} }
function getIconPath(name: string) { function getIconPath(name: string) {
const suffix = !isMac && nativeTheme.shouldUseDarkColors ? "-inverted" : ""; const suffix = !isMac && electron.nativeTheme.shouldUseDarkColors ? "-inverted" : "";
return path.resolve(path.join(getResourceDir(), "assets", "images", "tray", `${name}Template${suffix}.png`)); return path.resolve(path.join(getResourceDir(), "assets", "images", "tray", `${name}Template${suffix}.png`));
} }
@ -216,7 +216,7 @@ function updateTrayMenu() {
} }
const contextMenu = Menu.buildFromTemplate([ const contextMenu = electron.Menu.buildFromTemplate([
...windowVisibilityMenuItems, ...windowVisibilityMenuItems,
{ type: "separator" }, { type: "separator" },
{ {
@ -255,7 +255,7 @@ function updateTrayMenu() {
type: "normal", type: "normal",
icon: getIconPath("close"), icon: getIconPath("close"),
click: () => { click: () => {
const windows = BrowserWindow.getAllWindows(); const windows = electron.BrowserWindow.getAllWindows();
windows.forEach(window => { windows.forEach(window => {
window.close(); window.close();
}); });
@ -287,7 +287,7 @@ function createTray() {
return; return;
} }
tray = new Tray(getTrayIconPath()); tray = new electron.Tray(getTrayIconPath());
tray.setToolTip(t("tray.tooltip")); tray.setToolTip(t("tray.tooltip"));
// Restore focus // Restore focus
tray.on("click", changeVisibility); tray.on("click", changeVisibility);
@ -295,9 +295,9 @@ function createTray() {
if (!isMac) { if (!isMac) {
// macOS uses template icons which work great on dark & light themes. // macOS uses template icons which work great on dark & light themes.
nativeTheme.on("updated", updateTrayMenu); electron.nativeTheme.on("updated", updateTrayMenu);
} }
ipcMain.on("reload-tray", updateTrayMenu); electron.ipcMain.on("reload-tray", updateTrayMenu);
i18next.on("languageChanged", updateTrayMenu); i18next.on("languageChanged", updateTrayMenu);
} }

View File

@ -7,9 +7,9 @@ import log from "./log.js";
import sqlInit from "./sql_init.js"; import sqlInit from "./sql_init.js";
import cls from "./cls.js"; import cls from "./cls.js";
import keyboardActionsService from "./keyboard_actions.js"; import keyboardActionsService from "./keyboard_actions.js";
import * as remoteMain from "@electron/remote/main"; import * as remoteMain from "@electron/remote/main/index.js";
import { BrowserWindow, shell, type App, type BrowserWindowConstructorOptions, type WebContents } from "electron"; import electron from "electron";
import { dialog, ipcMain } from "electron"; import type { App, BrowserWindowConstructorOptions, BrowserWindow, WebContents } from "electron";
import { formatDownloadTitle, isDev, isMac, isWindows } from "./utils.js"; import { formatDownloadTitle, isDev, isMac, isWindows } from "./utils.js";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
@ -28,14 +28,14 @@ function trackWindowFocus(win: BrowserWindow) {
allWindows = allWindows.filter(w => !w.isDestroyed() && w !== win); allWindows = allWindows.filter(w => !w.isDestroyed() && w !== win);
allWindows.push(win); allWindows.push(win);
if (!optionService.getOptionBool("disableTray")) { if (!optionService.getOptionBool("disableTray")) {
ipcMain.emit("reload-tray"); electron.ipcMain.emit("reload-tray");
} }
}); });
win.on("closed", () => { win.on("closed", () => {
allWindows = allWindows.filter(w => !w.isDestroyed()); allWindows = allWindows.filter(w => !w.isDestroyed());
if (!optionService.getOptionBool("disableTray")) { if (!optionService.getOptionBool("disableTray")) {
ipcMain.emit("reload-tray"); electron.ipcMain.emit("reload-tray");
} }
}); });
} }
@ -66,7 +66,7 @@ async function createExtraWindow(extraWindowHash: string) {
trackWindowFocus(win); trackWindowFocus(win);
} }
ipcMain.on("create-extra-window", (event, arg) => { electron.ipcMain.on("create-extra-window", (event, arg) => {
createExtraWindow(arg.extraWindowHash); createExtraWindow(arg.extraWindowHash);
}); });
@ -76,13 +76,13 @@ interface ExportAsPdfOpts {
pageSize: "A0" | "A1" | "A2" | "A3" | "A4" | "A5" | "A6" | "Legal" | "Letter" | "Tabloid" | "Ledger"; pageSize: "A0" | "A1" | "A2" | "A3" | "A4" | "A5" | "A6" | "Legal" | "Letter" | "Tabloid" | "Ledger";
} }
ipcMain.on("export-as-pdf", async (e, opts: ExportAsPdfOpts) => { electron.ipcMain.on("export-as-pdf", async (e, opts: ExportAsPdfOpts) => {
const browserWindow = BrowserWindow.fromWebContents(e.sender); const browserWindow = electron.BrowserWindow.fromWebContents(e.sender);
if (!browserWindow) { if (!browserWindow) {
return; return;
} }
const filePath = dialog.showSaveDialogSync(browserWindow, { const filePath = electron.dialog.showSaveDialogSync(browserWindow, {
defaultPath: formatDownloadTitle(opts.title, "file", "application/pdf"), defaultPath: formatDownloadTitle(opts.title, "file", "application/pdf"),
filters: [ filters: [
{ {
@ -111,18 +111,18 @@ ipcMain.on("export-as-pdf", async (e, opts: ExportAsPdfOpts) => {
` `
}); });
} catch (e) { } catch (e) {
dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-export-message")); electron.dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-export-message"));
return; return;
} }
try { try {
await fs.writeFile(filePath, buffer); await fs.writeFile(filePath, buffer);
} catch (e) { } catch (e) {
dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-save-message")); electron.dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-save-message"));
return; return;
} }
shell.openPath(filePath); electron.shell.openPath(filePath);
}); });
async function createMainWindow(app: App) { async function createMainWindow(app: App) {