diff --git a/apps/desktop/src/autofill/main/main-desktop-autotype-mvp.service.spec.ts b/apps/desktop/src/autofill/main/main-desktop-autotype-mvp.service.spec.ts index f296720d3fc0..798575696752 100644 --- a/apps/desktop/src/autofill/main/main-desktop-autotype-mvp.service.spec.ts +++ b/apps/desktop/src/autofill/main/main-desktop-autotype-mvp.service.spec.ts @@ -414,5 +414,21 @@ describe("MainDesktopAutotypeMvpService", () => { { windowTitle: "Notepad" }, ); }); + + it("should not throw on shortcut activation if the window does not exist", () => { + const toggleHandler = ipcHandlers.get(AUTOTYPE_MVP_IPC_CHANNELS.TOGGLE); + toggleHandler({}, true); + + mockWindowMain.win = null; + + // Get the registered callback + const registeredCallback = (globalShortcut.register as jest.Mock).mock.calls[0][1]; + + expect(() => registeredCallback()).not.toThrow(); + expect(autotype_mvp.getForegroundWindowTitle).not.toHaveBeenCalled(); + expect(mockLogService.debug).toHaveBeenCalledWith( + "Autotype keyboard shortcut activated, but the main window does not exist.", + ); + }); }); }); diff --git a/apps/desktop/src/autofill/main/main-desktop-autotype-mvp.service.ts b/apps/desktop/src/autofill/main/main-desktop-autotype-mvp.service.ts index 1c2c1db88605..7004cbdeb013 100644 --- a/apps/desktop/src/autofill/main/main-desktop-autotype-mvp.service.ts +++ b/apps/desktop/src/autofill/main/main-desktop-autotype-mvp.service.ts @@ -102,11 +102,17 @@ export class MainDesktopAutotypeMvpService { const result = globalShortcut.register( this.autotypeKeyboardShortcut.getElectronFormat(), () => { - const windowTitle = autotype_mvp.getForegroundWindowTitle(); - - this.windowMain.win.webContents.send(AUTOTYPE_MVP_IPC_CHANNELS.LISTEN, { - windowTitle, - }); + if (this.windowMain.win != null) { + const windowTitle = autotype_mvp.getForegroundWindowTitle(); + + this.windowMain.win.webContents.send(AUTOTYPE_MVP_IPC_CHANNELS.LISTEN, { + windowTitle, + }); + } else { + this.logService.debug( + "Autotype keyboard shortcut activated, but the main window does not exist.", + ); + } }, );