Skip to content

Commit

Permalink
better window management
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed May 14, 2024
1 parent befcc60 commit d78ecd8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
12 changes: 10 additions & 2 deletions src/automations/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default class Commander {

// close stuff
await window.closeWaitingPanel();
await window.restoreWindows();
await window.releaseFocus();

// record
Expand Down Expand Up @@ -154,22 +155,26 @@ export default class Commander {

// done
await window.closeWaitingPanel();
await window.restoreWindows();
await window.releaseFocus();

// now paste
console.debug(`Processing LLM output: ${result.response.slice(0, 50)}…`);
await this.finishCommand(command, result.response, engine, model);

// done
return result;

}

} catch (error) {
console.error('Error while testing', error);
}

// done waiting
console.log('Destroying waiting panel')
await window.closeWaitingPanel(true);
window.releaseFocus();
await window.restoreWindows();

Check failure on line 176 in src/automations/commander.ts

View workflow job for this annotation

GitHub Actions / build

tests/unit/commander.test.ts > Chat Window command

Error: [vitest] No "restoreWindows" export is defined on the "../../src/main/window.ts" mock. Did you forget to return it from "vi.mock"? If you need to partially mock a module, you can use "importOriginal" helper inside: vi.mock("../../src/main/window.ts", async (importOriginal) => { const actual = await importOriginal() return { ...actual, // your mocked methods } }) ❯ Commander.execCommand src/automations/commander.ts:176:18 ❯ tests/unit/commander.test.ts:99:3

Check failure on line 176 in src/automations/commander.ts

View workflow job for this annotation

GitHub Actions / build

tests/unit/commander.test.ts > Paste in-place command

Error: [vitest] No "restoreWindows" export is defined on the "../../src/main/window.ts" mock. Did you forget to return it from "vi.mock"? If you need to partially mock a module, you can use "importOriginal" helper inside: vi.mock("../../src/main/window.ts", async (importOriginal) => { const actual = await importOriginal() return { ...actual, // your mocked methods } }) ❯ Commander.execCommand src/automations/commander.ts:176:18 ❯ tests/unit/commander.test.ts:121:3

Check failure on line 176 in src/automations/commander.ts

View workflow job for this annotation

GitHub Actions / build

tests/unit/commander.test.ts > Paste below command

Error: [vitest] No "restoreWindows" export is defined on the "../../src/main/window.ts" mock. Did you forget to return it from "vi.mock"? If you need to partially mock a module, you can use "importOriginal" helper inside: vi.mock("../../src/main/window.ts", async (importOriginal) => { const actual = await importOriginal() return { ...actual, // your mocked methods } }) ❯ Commander.execCommand src/automations/commander.ts:176:18 ❯ tests/unit/commander.test.ts:136:3

Check failure on line 176 in src/automations/commander.ts

View workflow job for this annotation

GitHub Actions / build

tests/unit/commander.test.ts > Copy to clipboard command

Error: [vitest] No "restoreWindows" export is defined on the "../../src/main/window.ts" mock. Did you forget to return it from "vi.mock"? If you need to partially mock a module, you can use "importOriginal" helper inside: vi.mock("../../src/main/window.ts", async (importOriginal) => { const actual = await importOriginal() return { ...actual, // your mocked methods } }) ❯ Commander.execCommand src/automations/commander.ts:176:18 ❯ tests/unit/commander.test.ts:151:3
await window.releaseFocus();

// done
return result;
Expand All @@ -190,6 +195,9 @@ export default class Commander {

private finishCommand = async (command: Command, text: string, engine: string, model: string): Promise<BrowserWindow|undefined> => {

// log
console.log('Finishing command', command, text, engine, model);

// we need an automator
const automator = new Automator();

Expand Down
10 changes: 3 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,9 @@ ipcMain.on('get-command-prompt', (event, payload) => {
})

ipcMain.on('close-command-palette', async () => {
window.closeCommandPalette();
window.restoreWindows();
await window.closeCommandPalette();
await window.restoreWindows();
await window.releaseFocus();
});

ipcMain.on('run-command', async (event, payload) => {
Expand All @@ -319,7 +320,6 @@ ipcMain.on('run-command', async (event, payload) => {
commander = null;

// cancelled
window.restoreWindows();
if (result.cancelled) return;

// show chat window
Expand All @@ -343,10 +343,6 @@ ipcMain.on('stop-command', async () => {
commander = null;
}

// restore windows
await window.restoreWindows();
await window.releaseFocus();

});

ipcMain.on('run-python-code', async (event, payload) => {
Expand Down
43 changes: 23 additions & 20 deletions src/main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,28 +202,30 @@ export const hideWindows = async () => {
}

export const restoreWindows = () => {
if (windowsToRestore.length) {
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 {
window.showInactive();
} catch (error) {
console.error('Error while restoring window', error);
}
}

// done
windowsToRestore = [];
// log
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 {
window.restore();
//window.showInactive();
} catch (error) {
console.error('Error while restoring window', error);
}
}

// done
windowsToRestore = [];

};

let commandPalette: BrowserWindow = null;
Expand Down Expand Up @@ -277,6 +279,7 @@ export const closeWaitingPanel = async (destroy?: boolean) => {
if (waitingPanel && !waitingPanel.isDestroyed()) {
if (destroy) waitingPanel?.destroy()
else waitingPanel?.close()
waitingPanel = null;
await wait();
}
} catch (error) {
Expand Down

0 comments on commit d78ecd8

Please sign in to comment.