diff --git a/src/main/ipc.ts b/src/main/ipc.ts index a596a9106a..1869000018 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -54,7 +54,7 @@ import powerMonitorService from './services/PowerMonitorService' import { proxyManager } from './services/ProxyManager' import { pythonService } from './services/PythonService' import { FileServiceManager } from './services/remotefile/FileServiceManager' -import { Screenshot } from './services/ScreenshotService' +import { screenshotService } from './services/ScreenshotService' import { searchService } from './services/SearchService' import { SelectionService } from './services/SelectionService' import { registerShortcuts, unregisterAllShortcuts } from './services/ShortcutService' @@ -612,34 +612,24 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { } }) - // Screenshot service singleton for selection flow - let screenshotService: Screenshot | null = null - + // Screenshot ipcMain.handle(IpcChannel.Screenshot_Capture, async (_event, fileName: string) => { - const service = new Screenshot() - return await service.capture(fileName) + return await screenshotService.capture(fileName) }) ipcMain.handle(IpcChannel.Screenshot_CaptureWithSelection, async (_event, fileName: string) => { - if (!screenshotService) { - screenshotService = new Screenshot() - } return await screenshotService.captureWithSelection(fileName) }) ipcMain.handle( IpcChannel.Screenshot_SelectionConfirm, async (_event, selection: { x: number; y: number; width: number; height: number }) => { - if (screenshotService) { - screenshotService.confirmSelection(selection) - } + screenshotService.confirmSelection(selection) } ) ipcMain.handle(IpcChannel.Screenshot_SelectionCancel, async () => { - if (screenshotService) { - screenshotService.cancelSelection() - } + screenshotService.cancelSelection() }) ipcMain.handle(IpcChannel.File_BinaryImage, fileManager.binaryImage.bind(fileManager)) diff --git a/src/main/services/ScreenshotService.ts b/src/main/services/ScreenshotService.ts index 14cf28c442..d685c5338d 100644 --- a/src/main/services/ScreenshotService.ts +++ b/src/main/services/ScreenshotService.ts @@ -29,7 +29,7 @@ interface Rectangle { height: number } -export class ScreenshotService { +class ScreenshotService { private selectionWindow: BrowserWindow | null = null private screenshotBuffer: Buffer | null = null private screenshotData: string | null = null @@ -176,7 +176,7 @@ export class ScreenshotService { } } - public confirmSelection(selection: Rectangle): void { + public async confirmSelection(selection: Rectangle): Promise { if (!this.selectionPromise) { logger.warn('No active selection to confirm') return @@ -189,7 +189,7 @@ export class ScreenshotService { status: 'error', message: 'Selection too small (minimum 10×10 pixels)' }) - void this.cleanupSelection() + await this.cleanupSelection() return } @@ -197,7 +197,7 @@ export class ScreenshotService { this.processSelection(selection) } - public cancelSelection(): void { + public async cancelSelection(): Promise { if (!this.selectionPromise) { logger.warn('No active selection to cancel') return @@ -208,7 +208,7 @@ export class ScreenshotService { status: 'cancelled', message: 'User cancelled selection' }) - void this.cleanupSelection() + await this.cleanupSelection() } private async processSelection(selection: Rectangle): Promise { @@ -349,3 +349,5 @@ export class ScreenshotService { } } } + +export const screenshotService = new ScreenshotService()