cvpilot-tool/apps/desktop/electron/main.ts
2024-09-10 14:09:34 +08:00

181 lines
4.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 registerIpcMainHandlers from "./ipcMainHandlers";
import { spawn } from "node:child_process";
import { ChildProcessWithoutNullStreams } from "child_process";
// 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;
let python_process: ChildProcessWithoutNullStreams | null = null;
const theme: "dark" | "light" = "light";
const themeTitleBarStyles = {
dark: { color: "rgb(32,32,32)", symbolColor: "#fff" },
light: {},
};
const platform = process.platform === "darwin" ? "macos" : "windows";
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,
});
});
const pacsPath =
platform !== "macos"
? path.join(
process.env.VITE_PUBLIC!,
"orthanc-win64-1.12.4",
"Orthanc.exe"
)
: path.join(process.env.VITE_PUBLIC!, "orthanc-mac-24.8.1", "Orthanc");
spawn(pacsPath);
if (VITE_DEV_SERVER_URL) {
win.loadURL(VITE_DEV_SERVER_URL);
registerIpcMainHandlers(win);
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(() => {
if (process.argv.length >= 2) {
const folderPath = process.argv[2];
win?.webContents.send("context-menu-launch", folderPath);
registerIpcMainHandlers(win);
}
});
}
}
/**
* 通知区域/状态栏的小图标
*/
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();
});