diff --git a/electron/ipcMainHandlers.ts b/electron/ipcMainHandlers.ts index c20e4ee..1d6ee24 100644 --- a/electron/ipcMainHandlers.ts +++ b/electron/ipcMainHandlers.ts @@ -13,9 +13,12 @@ import { EVENT_PARSE_DICOM } from "./ipcEvent"; const registerIpcMainHandlers = (mainWindow: Electron.BrowserWindow | null) => { if (!mainWindow) return; - ipcMain.handleOnce('ipc-loaded', () => { - mainWindow.show() - }) + ipcMain.removeAllListeners(); + + /** + * 等待渲染完成再显示窗口 + */ + ipcMain.on("ipc-loaded", () => mainWindow.show()); ipcMain.on(EVENT_PARSE_DICOM, async (event, file: string) => { const dirDialog = await dialog.showOpenDialog(mainWindow, { @@ -31,8 +34,6 @@ const registerIpcMainHandlers = (mainWindow: Electron.BrowserWindow | null) => { event.reply(EVENT_PARSE_DICOM + ":RES", result); } }); - - }; export default registerIpcMainHandlers; diff --git a/electron/main.ts b/electron/main.ts index 55dbbdf..7dbb3fe 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -27,12 +27,12 @@ process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL let win: BrowserWindow | null; let tray: Tray | null = null; -const theme: 'dark' | 'light' = 'dark' +const theme: "dark" | "light" = "dark"; const themeTitleBarStyles = { - dark: { color: "rgb(32,32,32)", symbolColor: '#fff' }, - light: {} -} + dark: { color: "rgb(32,32,32)", symbolColor: "#fff" }, + light: {}, +}; function createWindow() { win = new BrowserWindow({ @@ -52,7 +52,7 @@ function createWindow() { win.webContents.on("did-finish-load", () => { win?.webContents.send("main-process-message", { platform: process.platform === "darwin" ? "macos" : "windows", - theme + theme, }); }); @@ -61,10 +61,12 @@ function createWindow() { } else { win.loadFile(path.join(RENDERER_DIST, "index.html")); } + + registerIpcMainHandlers(win); } function createTray() { - if (tray) tray.destroy() + if (tray) tray.destroy(); const iconPath = path.join(process.env.VITE_PUBLIC, "AI.png"); // 使用 PNG 图标 const icon = nativeImage .createFromPath(iconPath) @@ -98,6 +100,8 @@ function createTray() { }); } +console.log(process.platform) + function registerGlobalShortcuts() { // 注册全局快捷键 'CommandOrControl+Shift+S' 来显示应用窗口 globalShortcut.register("Option+N", () => { @@ -124,7 +128,6 @@ app.whenReady().then(() => { createWindow(); createTray(); registerGlobalShortcuts(); - registerIpcMainHandlers(win); // 设置 Dock 图标 if (process.platform === "darwin") { @@ -137,5 +140,4 @@ app.whenReady().then(() => { // 注销全局快捷键,当应用退出时 app.on("will-quit", () => { globalShortcut.unregisterAll(); - tray?.destroy() }); diff --git a/src/App.tsx b/src/App.tsx index ff438f2..caf16ff 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -9,7 +9,7 @@ function App() { const theme = document.querySelector('html')!.getAttribute('theme') as 'dark' | 'light' useEffect(() => { - window.ipcRenderer.invoke('ipc-loaded') + window.ipcRenderer.send('ipc-loaded') }, []) return (