diff --git a/src/automations/commander.ts b/src/automations/commander.ts index b520d00..709a4ce 100644 --- a/src/automations/commander.ts +++ b/src/automations/commander.ts @@ -39,7 +39,7 @@ export default class Commander { static initCommand = async (): Promise => { // hide active windows - window.hideActiveWindows(); + await window.hideWindows(); await window.releaseFocus(); // grab text diff --git a/src/main.ts b/src/main.ts index 8e5d70b..acd1b39 100644 --- a/src/main.ts +++ b/src/main.ts @@ -319,10 +319,10 @@ ipcMain.on('run-command', async (event, payload) => { commander = null; // cancelled + window.restoreWindows(); if (result.cancelled) return; // show chat window - window.restoreWindows(); if (result?.chatWindow) { await wait(); result.chatWindow.show(); @@ -337,11 +337,16 @@ ipcMain.on('run-command', async (event, payload) => { ipcMain.on('stop-command', async () => { + // cancel any running command if (commander !== null) { - commander.cancelCommand(); + await commander.cancelCommand(); commander = null; } + // restore windows + await window.restoreWindows(); + await window.releaseFocus(); + }); ipcMain.on('run-python-code', async (event, payload) => { diff --git a/src/main/window.ts b/src/main/window.ts index d38c10a..67fa660 100644 --- a/src/main/window.ts +++ b/src/main/window.ts @@ -181,15 +181,15 @@ export const openChatWindow = (params: strDict) => { }; let windowsToRestore: BrowserWindow[] = []; -export const hideActiveWindows = async () => { +export const hideWindows = async () => { // remember to restore all windows windowsToRestore = []; try { - console.log('Hiding active windows'); + console.log('Hiding windows'); const windows = BrowserWindow.getAllWindows(); for (const window of windows) { - if (!window.isDestroyed() && window.isVisible() && !window.isFocused() && !window.isMinimized()) { + if (!window.isDestroyed() && window.isVisible() && !window.isMinimized()) { windowsToRestore.push(window); window.hide(); } @@ -203,19 +203,25 @@ export const hideActiveWindows = async () => { export const restoreWindows = () => { if (windowsToRestore.length) { - console.log('Restoring active windows') - if (windowsToRestore.includes(mainWindow)) { - mainWindow.showInactive(); - } + console.log(`Restoring ${windowsToRestore.length} windows`) + + // restore main window first + windowsToRestore.sort((a, b) => { + if (a === mainWindow) return -1; + if (b === mainWindow) return 1; + return 0; + }) + + // now restore for (const window of windowsToRestore) { try { - if (window != mainWindow) { - window.showInactive(); - } + window.showInactive(); } catch (error) { console.error('Error while restoring window', error); } } + + // done windowsToRestore = []; } }; diff --git a/tests/unit/commander.test.ts b/tests/unit/commander.test.ts index 23bd0e5..e25b0b5 100644 --- a/tests/unit/commander.test.ts +++ b/tests/unit/commander.test.ts @@ -24,7 +24,7 @@ vi.mock('../../src/main/window.ts', async () => { openWaitingPanel: vi.fn(), closeWaitingPanel: vi.fn(), openChatWindow: vi.fn(), - hideActiveWindows: vi.fn(), + hideWindows: vi.fn(), releaseFocus: vi.fn() } }) @@ -82,7 +82,7 @@ test('Prepare command', async () => { await Commander.initCommand() - expect(window.hideActiveWindows).toHaveBeenCalledOnce() + expect(window.hideWindows).toHaveBeenCalledOnce() expect(window.releaseFocus).toHaveBeenCalledOnce() expect(Automator.prototype.getSelectedText).toHaveBeenCalledOnce() expect(window.openCommandPalette).toHaveBeenCalledOnce()