|
| 1 | +import { app, ipcMain } from 'electron' |
| 2 | +import { existsSync, readFile } from 'node:fs' |
| 3 | +import path from 'node:path' |
| 4 | +import { blockQuit } from '../app-events' |
| 5 | +import { setAutoLaunch, getAutoLaunchStatus } from '../auto-launch' |
| 6 | +import { showInDock } from '../dock-utils' |
| 7 | +import log from '../logger' |
| 8 | +import { showMainWindow, hideMainWindow } from '../main-window' |
| 9 | +import { createApplicationMenu } from '../menu' |
| 10 | +import { |
| 11 | + getSkipQuitConfirmation, |
| 12 | + setSkipQuitConfirmation, |
| 13 | +} from '../quit-confirmation' |
| 14 | +import { updateTrayStatus } from '../system-tray' |
| 15 | +import { isToolhiveRunning } from '../toolhive-manager' |
| 16 | + |
| 17 | +export function register() { |
| 18 | + ipcMain.handle('get-auto-launch-status', () => getAutoLaunchStatus()) |
| 19 | + |
| 20 | + ipcMain.handle('set-auto-launch', (_event, enabled: boolean) => { |
| 21 | + setAutoLaunch(enabled) |
| 22 | + updateTrayStatus(isToolhiveRunning()) |
| 23 | + createApplicationMenu() |
| 24 | + return getAutoLaunchStatus() |
| 25 | + }) |
| 26 | + |
| 27 | + ipcMain.handle('show-app', async () => { |
| 28 | + try { |
| 29 | + showInDock() |
| 30 | + await showMainWindow() |
| 31 | + } catch (error) { |
| 32 | + log.error('Failed to show app:', error) |
| 33 | + } |
| 34 | + }) |
| 35 | + |
| 36 | + ipcMain.handle('hide-app', () => { |
| 37 | + try { |
| 38 | + hideMainWindow() |
| 39 | + } catch (error) { |
| 40 | + log.error('Failed to hide app:', error) |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + ipcMain.handle('quit-app', () => { |
| 45 | + blockQuit('before-quit') |
| 46 | + }) |
| 47 | + |
| 48 | + ipcMain.handle('get-skip-quit-confirmation', () => getSkipQuitConfirmation()) |
| 49 | + ipcMain.handle('set-skip-quit-confirmation', (_e, skip: boolean) => |
| 50 | + setSkipQuitConfirmation(skip) |
| 51 | + ) |
| 52 | + |
| 53 | + ipcMain.handle( |
| 54 | + 'get-main-log-content', |
| 55 | + async (): Promise<string | undefined> => { |
| 56 | + try { |
| 57 | + const logPath = path.join(app.getPath('logs'), 'main.log') |
| 58 | + if (!existsSync(logPath)) { |
| 59 | + log.warn(`Log file does not exist: ${logPath}`) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + const content = await new Promise<string>((resolve, reject) => { |
| 64 | + readFile(logPath, 'utf8', (err, data) => { |
| 65 | + if (err) reject(err) |
| 66 | + else resolve(data) |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + return content |
| 71 | + } catch (error) { |
| 72 | + log.error('Failed to read log file:', error) |
| 73 | + return |
| 74 | + } |
| 75 | + } |
| 76 | + ) |
| 77 | +} |
0 commit comments