hydra/src/main/services/window-manager.ts

111 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-04-21 08:26:29 +03:00
import { BrowserWindow, Menu, Tray, app } from "electron";
2024-04-25 07:52:19 +03:00
import { is } from "@electron-toolkit/utils";
2024-04-21 08:26:29 +03:00
import { t } from "i18next";
import path from "node:path";
2024-04-29 13:01:34 +03:00
import icon from "@resources/icon.png?asset";
import trayIcon from "@resources/tray-icon.png?asset";
2024-04-21 08:26:29 +03:00
export class WindowManager {
public static mainWindow: Electron.BrowserWindow | null = null;
2024-04-25 07:52:19 +03:00
private static loadURL(hash = "") {
// HMR for renderer base on electron-vite cli.
// Load the remote URL for development or the local html file for production.
if (is.dev && process.env["ELECTRON_RENDERER_URL"]) {
2024-04-25 22:54:38 +03:00
this.mainWindow?.loadURL(
2024-04-25 07:52:19 +03:00
`${process.env["ELECTRON_RENDERER_URL"]}#/${hash}`
);
} else {
2024-04-25 22:54:38 +03:00
this.mainWindow?.loadFile(
path.join(__dirname, "../renderer/index.html"),
{
hash,
}
);
2024-04-25 07:52:19 +03:00
}
}
2024-04-21 08:26:29 +03:00
public static createMainWindow() {
// Create the browser window.
this.mainWindow = new BrowserWindow({
width: 1200,
height: 720,
minWidth: 1024,
minHeight: 540,
titleBarStyle: "hidden",
2024-04-25 07:52:19 +03:00
...(process.platform === "linux" ? { icon } : {}),
2024-04-21 08:26:29 +03:00
trafficLightPosition: { x: 16, y: 16 },
titleBarOverlay: {
symbolColor: "#DADBE1",
color: "#151515",
height: 34,
},
webPreferences: {
preload: path.join(__dirname, "../preload/index.mjs"),
sandbox: false,
},
});
2024-04-25 07:52:19 +03:00
this.loadURL();
2024-04-21 08:26:29 +03:00
this.mainWindow.removeMenu();
this.mainWindow.on("close", () => {
2024-04-25 22:54:38 +03:00
WindowManager.mainWindow?.setProgressBar(-1);
2024-04-21 08:26:29 +03:00
});
}
2024-04-25 07:52:19 +03:00
public static redirect(hash: string) {
2024-04-21 08:26:29 +03:00
if (!this.mainWindow) this.createMainWindow();
2024-04-25 07:52:19 +03:00
this.loadURL(hash);
2024-04-21 08:26:29 +03:00
2024-04-25 22:54:38 +03:00
if (this.mainWindow?.isMinimized()) this.mainWindow.restore();
this.mainWindow?.focus();
2024-04-21 08:26:29 +03:00
}
public static createSystemTray(language: string) {
2024-04-25 07:52:19 +03:00
const tray = new Tray(trayIcon);
2024-04-21 08:26:29 +03:00
const contextMenu = Menu.buildFromTemplate([
{
label: t("open", {
ns: "system_tray",
lng: language,
}),
type: "normal",
click: () => {
if (this.mainWindow) {
this.mainWindow.show();
} else {
this.createMainWindow();
}
},
},
{
label: t("quit", {
ns: "system_tray",
lng: language,
}),
type: "normal",
click: () => app.quit(),
},
]);
tray.setToolTip("Hydra");
tray.setContextMenu(contextMenu);
if (process.platform === "win32") {
tray.addListener("click", () => {
if (this.mainWindow) {
2024-04-25 22:54:38 +03:00
if (WindowManager.mainWindow?.isMinimized())
2024-04-21 08:26:29 +03:00
WindowManager.mainWindow.restore();
2024-04-25 22:54:38 +03:00
WindowManager.mainWindow?.focus();
2024-04-21 08:26:29 +03:00
return;
}
this.createMainWindow();
});
}
}
}