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

206 lines
5.2 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-03 16:51:27 +08:00
import registerIpcMainHandlers from "./ipcMainHandlers";
2024-09-05 15:28:22 +08:00
import { spawn } from "node:child_process";
2024-09-06 08:59:59 +08:00
import { ChildProcessWithoutNullStreams } from "child_process";
2024-09-10 16:26:54 +08:00
import log from 'electron-log';
import { existsSync } from "node:fs";
2024-08-29 16:59:25 +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-06 08:59:59 +08:00
let python_process: ChildProcessWithoutNullStreams | 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-06 14:27:41 +08:00
const platform = process.platform === "darwin" ? "macos" : "windows";
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 16:26:54 +08:00
if (VITE_DEV_SERVER_URL) {
win.loadURL(VITE_DEV_SERVER_URL);
registerIpcMainHandlers(win);
const pacsPath =
platform !== "macos"
? path.join(
2024-09-10 14:09:34 +08:00
process.env.VITE_PUBLIC!,
2024-09-10 16:26:54 +08:00
"../extraResources",
2024-09-10 14:09:34 +08:00
"orthanc-win64-1.12.4",
"Orthanc.exe"
)
2024-09-10 16:26:54 +08:00
: path.join(process.env.VITE_PUBLIC!, '../extraResources', "orthanc-mac-24.8.1", "Orthanc");
spawn(pacsPath);
2024-09-10 14:09:34 +08:00
2024-09-06 14:27:41 +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"));
// }
const pacsPath =
platform !== "macos"
? path.join(
process.resourcesPath, 'lib',
"orthanc-win64-1.12.4",
"Orthanc.exe"
)
: path.join(process.resourcesPath, 'lib', "orthanc-mac-24.8.1", "Orthanc");
console.log(pacsPath)
log.info(pacsPath);
if (existsSync(pacsPath))
spawn(pacsPath);
2024-09-09 21:58:49 +08:00
2024-09-03 16:40:09 +08:00
win.loadFile(path.join(RENDERER_DIST, "index.html")).then(() => {
if (process.argv.length >= 2) {
2024-09-03 17:01:55 +08:00
const folderPath = process.argv[2];
2024-09-06 14:27:41 +08:00
win?.webContents.send("context-menu-launch", folderPath);
2024-09-03 17:01:55 +08:00
registerIpcMainHandlers(win);
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-06 08:59:59 +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-06 14:27:41 +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();
});