hamster-desktop/electron/main.ts

144 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-07-16 13:05:03 +08:00
import {
app,
BrowserWindow,
Tray,
Menu,
globalShortcut,
nativeImage,
} from "electron";
2024-07-15 16:47:51 +08:00
import { createRequire } from "node:module";
import { fileURLToPath } from "node:url";
import path from "node:path";
2024-08-05 16:50:11 +08:00
import registerIpcMainHandlers from "./ipcMainHandlers";
2024-07-15 16:47:51 +08:00
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;
2024-07-16 13:05:03 +08:00
let tray: Tray | null = null;
2024-07-15 16:47:51 +08:00
2024-08-12 09:36:55 +08:00
const theme: "dark" | "light" = "dark";
const themeTitleBarStyles = {
2024-08-12 09:36:55 +08:00
dark: { color: "rgb(32,32,32)", symbolColor: "#fff" },
light: {},
};
2024-07-15 16:47:51 +08:00
function createWindow() {
win = new BrowserWindow({
2024-08-10 12:12:21 +08:00
width: 1280,
height: 800,
show: false, // 先隐藏。等待渲染完成,防止闪烁
2024-08-10 12:12:21 +08:00
icon: path.join(process.env.VITE_PUBLIC, "AI.png"),
2024-07-18 00:32:15 +08:00
// frame: false,
2024-07-15 16:47:51 +08:00
titleBarStyle: "hidden", // customButtonsOnHover || hidden || hiddenInset
titleBarOverlay: { height: 36, ...themeTitleBarStyles[theme] }, // 渲染进程发消息动态改变这个
2024-07-15 16:47:51 +08:00
webPreferences: {
2024-08-07 10:28:05 +08:00
preload: path.join(__dirname, "preload.mjs"),
2024-07-15 16:47:51 +08:00
},
});
// Test active push message to Renderer-process.
win.webContents.on("did-finish-load", () => {
2024-07-18 00:32:15 +08:00
win?.webContents.send("main-process-message", {
2024-08-05 16:50:11 +08:00
platform: process.platform === "darwin" ? "macos" : "windows",
2024-08-12 09:36:55 +08:00
theme,
2024-07-18 00:32:15 +08:00
});
2024-07-15 16:47:51 +08:00
});
if (VITE_DEV_SERVER_URL) {
win.loadURL(VITE_DEV_SERVER_URL);
} else {
win.loadFile(path.join(RENDERER_DIST, "index.html"));
}
2024-08-12 09:36:55 +08:00
registerIpcMainHandlers(win);
2024-07-16 13:05:03 +08:00
}
function createTray() {
2024-08-12 09:36:55 +08:00
if (tray) tray.destroy();
2024-08-10 12:12:21 +08:00
const iconPath = path.join(process.env.VITE_PUBLIC, "AI.png"); // 使用 PNG 图标
2024-07-16 13:05:03 +08:00
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("My Electron App");
tray.setContextMenu(contextMenu);
tray.on("click", () => {
if (win) {
win.isVisible() ? win.hide() : win.show();
}
});
}
2024-08-12 09:36:55 +08:00
console.log(process.platform)
2024-07-16 13:05:03 +08:00
function registerGlobalShortcuts() {
// 注册全局快捷键 'CommandOrControl+Shift+S' 来显示应用窗口
globalShortcut.register("Option+N", () => {
if (win) {
win.isVisible() ? win.hide() : win.show();
}
});
2024-07-15 16:47:51 +08:00
}
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
win = null;
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
2024-07-16 13:05:03 +08:00
app.whenReady().then(() => {
createWindow();
2024-08-11 21:29:23 +08:00
createTray();
registerGlobalShortcuts();
2024-07-16 13:05:03 +08:00
// 设置 Dock 图标
if (process.platform === "darwin") {
2024-08-08 14:21:24 +08:00
const dockIconPath = path.join(process.env.VITE_PUBLIC, "girl.png");
2024-07-16 13:05:03 +08:00
const dockIcon = nativeImage.createFromPath(dockIconPath);
app.dock.setIcon(dockIcon);
}
});
// 注销全局快捷键,当应用退出时
app.on("will-quit", () => {
globalShortcut.unregisterAll();
2024-08-07 13:46:02 +08:00
});