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

174 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-08-29 16:59:25 +08:00
import {
app,
BrowserWindow,
Tray,
Menu,
globalShortcut,
nativeImage,
} from "electron";
2024-09-03 15:28:30 +08:00
// import { createRequire } from "node:module";
2024-08-29 16:59:25 +08:00
import { fileURLToPath } from "node:url";
import path from "node:path";
2024-09-02 14:18:06 +08:00
import { createDatabase } from "./core/db";
2024-09-03 15:28:30 +08:00
import { getMachineId } from "./core/auth";
2024-09-10 22:03:12 +08:00
import { getPacsPath, runOrthancServer } from "./core/pacs";
2024-09-11 12:58:50 +08:00
import { registerIpcMainHandlers } from "./ipcEvent";
2024-09-10 16:26:54 +08:00
2024-09-03 15:28:30 +08:00
// const require = createRequire(import.meta.url);
2024-08-29 16:59:25 +08:00
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;
2024-09-02 15:05:38 +08:00
const theme: "dark" | "light" = "light";
2024-08-29 16:59:25 +08:00
const themeTitleBarStyles = {
dark: { color: "rgb(32,32,32)", symbolColor: "#fff" },
light: {},
};
2024-09-10 22:03:12 +08:00
export const platform = process.platform === "darwin" ? "macos" : "windows";
2024-09-06 14:27:41 +08:00
2024-08-29 16:59:25 +08:00
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,
},
});
2024-09-06 14:27:41 +08:00
/**
*
*/
2024-08-29 16:59:25 +08:00
win.webContents.on("did-finish-load", () => {
2024-09-06 14:27:41 +08:00
win?.webContents.send("mount", {
platform,
2024-08-29 16:59:25 +08:00
theme,
});
});
2024-09-10 22:03:12 +08:00
// 开发环境
2024-09-10 16:26:54 +08:00
if (VITE_DEV_SERVER_URL) {
win.loadURL(VITE_DEV_SERVER_URL);
registerIpcMainHandlers(win);
2024-09-11 12:58:50 +08:00
runOrthancServer(getPacsPath(platform, true));
2024-09-10 16:26:54 +08:00
2024-09-10 22:03:12 +08:00
// if (platform !== "macos") {
// python_process = spawn(path.join(process.env.VITE_PUBLIC!, "main.exe"));
// }
2024-08-29 16:59:25 +08:00
} else {
2024-09-10 16:26:54 +08:00
// if (platform !== "macos") {
// python_process = spawn(path.join(process.env.VITE_PUBLIC!, "main.exe"));
// }
2024-09-03 16:40:09 +08:00
win.loadFile(path.join(RENDERER_DIST, "index.html")).then(() => {
2024-09-11 12:58:50 +08:00
registerIpcMainHandlers(win!);
runOrthancServer(getPacsPath(platform, false));
2024-09-10 22:03:12 +08:00
// windows右键打开的目录路径
2024-09-03 16:40:09 +08:00
if (process.argv.length >= 2) {
2024-09-03 17:01:55 +08:00
const folderPath = process.argv[2];
2024-09-11 12:58:50 +08:00
win!.webContents.send("context-menu-launch", folderPath);
2024-09-03 16:40:09 +08:00
}
2024-09-03 16:51:27 +08:00
});
2024-08-29 16:59:25 +08:00
}
}
2024-09-06 14:27:41 +08:00
/**
* /
*/
2024-08-29 16:59:25 +08:00
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();
},
},
]);
2024-09-06 14:27:41 +08:00
tray.setToolTip("Cvpilot Tool");
2024-08-29 16:59:25 +08:00
tray.setContextMenu(contextMenu);
2024-09-06 14:27:41 +08:00
const toggle = () => win && (win.isVisible() ? win.hide() : win.show());
tray.on("click", () => toggle);
2024-08-29 16:59:25 +08:00
}
function registerGlobalShortcuts() {
// 注册全局快捷键 'CommandOrControl+Shift+S' 来显示应用窗口
globalShortcut.register("Option+N", () => {
if (win) {
win.isVisible() ? win.hide() : win.show();
}
});
}
2024-09-06 08:59:59 +08:00
app.on("window-all-closed", async () => {
2024-09-06 14:27:41 +08:00
if (platform !== "macos") {
2024-08-29 16:59:25 +08:00
app.quit();
win = null;
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.on("before-quit", () => {
2024-09-10 22:03:12 +08:00
// console.log(python_process?.pid);
2024-08-29 16:59:25 +08:00
});
app.whenReady().then(() => {
2024-09-03 14:10:36 +08:00
getMachineId();
2024-08-29 16:59:25 +08:00
createWindow();
createTray();
registerGlobalShortcuts();
2024-09-02 14:18:06 +08:00
createDatabase({ name: "cvpilot.json" });
2024-09-10 22:03:12 +08:00
console.log("userData:", path.join(app.getPath("userData")));
2024-08-29 16:59:25 +08:00
// 设置 Dock 图标
2024-09-06 14:27:41 +08:00
if (platform === "macos") {
2024-08-29 16:59:25 +08:00
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();
});