From 6b7ea56c8ab8530bda4a0c0794b43cfb40b839ab Mon Sep 17 00:00:00 2001 From: Robin Huang Date: Wed, 6 Nov 2024 15:35:50 -0800 Subject: [PATCH] Refactor. --- src/handlers/appInfoHandlers.ts | 16 ++++++++++++++++ src/handlers/pathHandlers.ts | 13 ++++++++++++- src/main.ts | 24 +++--------------------- 3 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 src/handlers/appInfoHandlers.ts diff --git a/src/handlers/appInfoHandlers.ts b/src/handlers/appInfoHandlers.ts new file mode 100644 index 00000000..084abfd9 --- /dev/null +++ b/src/handlers/appInfoHandlers.ts @@ -0,0 +1,16 @@ +import { app, ipcMain } from 'electron'; +import { IPC_CHANNELS } from '../constants'; + +export class AppInfoHandlers { + constructor() {} + + registerHandlers() { + ipcMain.handle(IPC_CHANNELS.IS_PACKAGED, () => { + return app.isPackaged; + }); + + ipcMain.handle(IPC_CHANNELS.GET_ELECTRON_VERSION, () => { + return app.getVersion(); + }); + } +} diff --git a/src/handlers/pathHandlers.ts b/src/handlers/pathHandlers.ts index 85c6f065..41fa6163 100644 --- a/src/handlers/pathHandlers.ts +++ b/src/handlers/pathHandlers.ts @@ -1,15 +1,26 @@ -import { ipcMain } from 'electron'; +import { app, ipcMain, shell } from 'electron'; import { IPC_CHANNELS } from '../constants'; import { determineResourcesPaths } from '../main'; +import log from 'electron-log/main'; export class PathHandlers { constructor() {} registerHandlers() { // Path-related handlers + + ipcMain.on(IPC_CHANNELS.OPEN_LOGS_PATH, () => { + shell.openPath(app.getPath('logs')); + }); + ipcMain.handle(IPC_CHANNELS.GET_BASE_PATH, async () => { const { basePath } = await determineResourcesPaths(); return basePath; }); + + ipcMain.on(IPC_CHANNELS.OPEN_PATH, (event, folderPath: string) => { + log.info(`Opening path: ${folderPath}`); + shell.openPath(folderPath); + }); } } diff --git a/src/main.ts b/src/main.ts index 60553923..9511ff89 100644 --- a/src/main.ts +++ b/src/main.ts @@ -145,10 +145,6 @@ if (!gotTheLock) { log.error('Error getting GPU info: ', e); }); - // This method will be called when Electron has finished - // initialization and is ready to create browser windows. - // Some APIs can only be used after this event occurs. - app.on('ready', async () => { log.info('App ready'); @@ -198,25 +194,15 @@ if (!gotTheLock) { ...options, }); }); - ipcMain.on(IPC_CHANNELS.OPEN_LOGS_PATH, () => { - shell.openPath(app.getPath('logs')); - }); - ipcMain.handle(IPC_CHANNELS.GET_BASE_PATH, () => { - return basePath; - }); + ipcMain.handle(IPC_CHANNELS.GET_MODEL_CONFIG_PATH, () => { return modelConfigPath; }); - ipcMain.on(IPC_CHANNELS.OPEN_PATH, (event, folderPath: string) => { - log.info(`Opening path: ${folderPath}`); - shell.openPath(folderPath); - }); + ipcMain.on(IPC_CHANNELS.OPEN_DEV_TOOLS, () => { mainWindow?.webContents.openDevTools(); }); - ipcMain.handle(IPC_CHANNELS.IS_PACKAGED, () => { - return app.isPackaged; - }); + await handleFirstTimeSetup(); const { appResourcesPath, pythonInstallPath, modelConfigPath, basePath } = await determineResourcesPaths(); if (!basePath || !pythonInstallPath) { @@ -273,10 +259,6 @@ if (!gotTheLock) { } ); - ipcMain.handle(IPC_CHANNELS.GET_ELECTRON_VERSION, () => { - return app.getVersion(); - }); - ipcMain.handle(IPC_CHANNELS.SEND_ERROR_TO_SENTRY, async (_event, { error, extras }): Promise => { try { return Sentry.captureMessage(error, {