cvpilot-tool/apps/desktop/electron/main.ts

180 lines
4.7 KiB
TypeScript

import {
app,
BrowserWindow,
Tray,
Menu,
globalShortcut,
nativeImage,
} from "electron";
// import { createRequire } from "node:module";
import { fileURLToPath } from "node:url";
import path from "node:path";
import { createDatabase } from "./core/db";
import { getMachineId } from "./core/auth";
import { getPacsPath, runOrthancServer } from "./core/pacs";
import { registerIpcMainHandlers } from "./ipcEvent";
// const require = createRequire(import.meta.url);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
process.env.APP_ROOT = path.join(__dirname, "..");
export const VITE_DEV_SERVER_URL = process.env["VITE_DEV_SERVER_URL"];
export const MAIN_DIST = path.join(process.env.APP_ROOT, "dist-electron");
export const RENDERER_DIST = path.join(process.env.APP_ROOT, "dist");
process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL
? path.join(process.env.APP_ROOT, "public")
: RENDERER_DIST;
let win: BrowserWindow | null;
let tray: Tray | null = null;
const theme: "dark" | "light" = "dark";
const themeTitleBarStyles = {
dark: { color: "rgb(32,32,32)", symbolColor: "#fff" },
light: {},
};
export const platform = process.platform === "darwin" ? "macos" : "windows";
app.commandLine.appendSwitch("disable-web-security");
app.commandLine.appendSwitch("ignore-gpu-blocklist");
app.commandLine.appendSwitch("use-angle", "gl");
// app.disableHardwareAcceleration()
function createWindow() {
win = new BrowserWindow({
width: 1280,
height: 800,
show: false, // 先隐藏。等待渲染完成,防止闪烁
icon: path.join(process.env.VITE_PUBLIC, "AI.png"),
// frame: false,
titleBarStyle: "hidden", // customButtonsOnHover || hidden || hiddenInset
titleBarOverlay: { height: 36, ...themeTitleBarStyles[theme] }, // 渲染进程发消息动态改变这个
webPreferences: {
preload: path.join(__dirname, "preload.mjs"),
nodeIntegration: true,
},
});
/**
* 渲染进程加载完成
*/
win.webContents.on("did-finish-load", () => {
win?.webContents.send("mount", {
platform,
theme,
});
});
// 开发环境
if (VITE_DEV_SERVER_URL) {
win.loadURL(VITE_DEV_SERVER_URL);
registerIpcMainHandlers(win);
runOrthancServer(getPacsPath(platform, true));
// if (platform !== "macos") {
// python_process = spawn(path.join(process.env.VITE_PUBLIC!, "main.exe"));
// }
} else {
// if (platform !== "macos") {
// python_process = spawn(path.join(process.env.VITE_PUBLIC!, "main.exe"));
// }
win.loadFile(path.join(RENDERER_DIST, "index.html")).then(() => {
registerIpcMainHandlers(win!);
runOrthancServer(getPacsPath(platform, false));
// windows右键打开的目录路径
if (process.argv.length >= 2) {
const folderPath = process.argv[2];
win!.webContents.send("context-menu-launch", folderPath);
}
});
}
}
/**
* 通知区域/状态栏的小图标
*/
function createTray() {
if (tray) tray.destroy();
const iconPath = path.join(process.env.VITE_PUBLIC, "AI.png"); // 使用 PNG 图标
const icon = nativeImage
.createFromPath(iconPath)
.resize({ width: 20, height: 20 });
tray = new Tray(icon);
const contextMenu = Menu.buildFromTemplate([
{
label: "Show App",
click: function () {
if (win) {
win.show();
}
},
},
{
label: "Quit",
click: function () {
app.quit();
},
},
]);
tray.setToolTip("Cvpilot Tool");
tray.setContextMenu(contextMenu);
const toggle = () => win && (win.isVisible() ? win.hide() : win.show());
tray.on("click", () => toggle);
}
function registerGlobalShortcuts() {
// 注册全局快捷键 'CommandOrControl+Shift+S' 来显示应用窗口
globalShortcut.register("Option+N", () => {
if (win) {
win.isVisible() ? win.hide() : win.show();
}
});
}
app.on("window-all-closed", async () => {
if (platform !== "macos") {
app.quit();
win = null;
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.on("before-quit", () => {
// console.log(python_process?.pid);
});
app.whenReady().then(() => {
getMachineId();
createWindow();
createTray();
registerGlobalShortcuts();
createDatabase({ name: "cvpilot.json" });
console.log("userData:", path.join(app.getPath("userData")));
// 设置 Dock 图标
if (platform === "macos") {
const dockIconPath = path.join(process.env.VITE_PUBLIC, "girl.png");
const dockIcon = nativeImage.createFromPath(dockIconPath);
app.dock.setIcon(dockIcon);
}
});
// 注销全局快捷键,当应用退出时
app.on("will-quit", () => {
globalShortcut.unregisterAll();
});