feat: macos close

This commit is contained in:
mozzie 2024-08-12 09:36:55 +08:00
parent 5320a5846c
commit 6bf4ec52ec
3 changed files with 17 additions and 14 deletions

View File

@ -13,9 +13,12 @@ import { EVENT_PARSE_DICOM } from "./ipcEvent";
const registerIpcMainHandlers = (mainWindow: Electron.BrowserWindow | null) => { const registerIpcMainHandlers = (mainWindow: Electron.BrowserWindow | null) => {
if (!mainWindow) return; if (!mainWindow) return;
ipcMain.handleOnce('ipc-loaded', () => { ipcMain.removeAllListeners();
mainWindow.show()
}) /**
*
*/
ipcMain.on("ipc-loaded", () => mainWindow.show());
ipcMain.on(EVENT_PARSE_DICOM, async (event, file: string) => { ipcMain.on(EVENT_PARSE_DICOM, async (event, file: string) => {
const dirDialog = await dialog.showOpenDialog(mainWindow, { const dirDialog = await dialog.showOpenDialog(mainWindow, {
@ -31,8 +34,6 @@ const registerIpcMainHandlers = (mainWindow: Electron.BrowserWindow | null) => {
event.reply(EVENT_PARSE_DICOM + ":RES", result); event.reply(EVENT_PARSE_DICOM + ":RES", result);
} }
}); });
}; };
export default registerIpcMainHandlers; export default registerIpcMainHandlers;

View File

@ -27,12 +27,12 @@ process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL
let win: BrowserWindow | null; let win: BrowserWindow | null;
let tray: Tray | null = null; let tray: Tray | null = null;
const theme: 'dark' | 'light' = 'dark' const theme: "dark" | "light" = "dark";
const themeTitleBarStyles = { const themeTitleBarStyles = {
dark: { color: "rgb(32,32,32)", symbolColor: '#fff' }, dark: { color: "rgb(32,32,32)", symbolColor: "#fff" },
light: {} light: {},
} };
function createWindow() { function createWindow() {
win = new BrowserWindow({ win = new BrowserWindow({
@ -52,7 +52,7 @@ function createWindow() {
win.webContents.on("did-finish-load", () => { win.webContents.on("did-finish-load", () => {
win?.webContents.send("main-process-message", { win?.webContents.send("main-process-message", {
platform: process.platform === "darwin" ? "macos" : "windows", platform: process.platform === "darwin" ? "macos" : "windows",
theme theme,
}); });
}); });
@ -61,10 +61,12 @@ function createWindow() {
} else { } else {
win.loadFile(path.join(RENDERER_DIST, "index.html")); win.loadFile(path.join(RENDERER_DIST, "index.html"));
} }
registerIpcMainHandlers(win);
} }
function createTray() { function createTray() {
if (tray) tray.destroy() if (tray) tray.destroy();
const iconPath = path.join(process.env.VITE_PUBLIC, "AI.png"); // 使用 PNG 图标 const iconPath = path.join(process.env.VITE_PUBLIC, "AI.png"); // 使用 PNG 图标
const icon = nativeImage const icon = nativeImage
.createFromPath(iconPath) .createFromPath(iconPath)
@ -98,6 +100,8 @@ function createTray() {
}); });
} }
console.log(process.platform)
function registerGlobalShortcuts() { function registerGlobalShortcuts() {
// 注册全局快捷键 'CommandOrControl+Shift+S' 来显示应用窗口 // 注册全局快捷键 'CommandOrControl+Shift+S' 来显示应用窗口
globalShortcut.register("Option+N", () => { globalShortcut.register("Option+N", () => {
@ -124,7 +128,6 @@ app.whenReady().then(() => {
createWindow(); createWindow();
createTray(); createTray();
registerGlobalShortcuts(); registerGlobalShortcuts();
registerIpcMainHandlers(win);
// 设置 Dock 图标 // 设置 Dock 图标
if (process.platform === "darwin") { if (process.platform === "darwin") {
@ -137,5 +140,4 @@ app.whenReady().then(() => {
// 注销全局快捷键,当应用退出时 // 注销全局快捷键,当应用退出时
app.on("will-quit", () => { app.on("will-quit", () => {
globalShortcut.unregisterAll(); globalShortcut.unregisterAll();
tray?.destroy()
}); });

View File

@ -9,7 +9,7 @@ function App() {
const theme = document.querySelector('html')!.getAttribute('theme') as 'dark' | 'light' const theme = document.querySelector('html')!.getAttribute('theme') as 'dark' | 'light'
useEffect(() => { useEffect(() => {
window.ipcRenderer.invoke('ipc-loaded') window.ipcRenderer.send('ipc-loaded')
}, []) }, [])
return ( return (