Skip to content

Commit

Permalink
window management stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed May 14, 2024
1 parent b0509d5 commit befcc60
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/automations/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class Commander {
static initCommand = async (): Promise<void> => {

// hide active windows
window.hideActiveWindows();
await window.hideWindows();
await window.releaseFocus();

// grab text
Expand Down
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) => {
Expand Down
26 changes: 16 additions & 10 deletions src/main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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 = [];
}
};
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/commander.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit befcc60

Please sign in to comment.