Skip to content

Commit

Permalink
more window swapping fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed May 15, 2024
1 parent 9b7d511 commit 737ee0e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 66 deletions.
30 changes: 8 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"dependencies": {
"@anthropic-ai/sdk": "^0.20.4",
"@el3um4s/run-vbs": "^1.1.2",
"@excalidraw/markdown-to-text": "^0.1.2",
"@google/generative-ai": "^0.11.1",
"@iktakahiro/markdown-it-katex": "^4.0.1",
"@mistralai/mistralai": "^0.1.3",
Expand All @@ -81,7 +82,6 @@
"katex": "^0.16.10",
"markdown-it": "^14.1.0",
"markdown-it-mark": "^4.0.0",
"markdown-to-text": "^0.1.1",
"minimatch": "^9.0.4",
"mitt": "^3.0.1",
"officeparser": "^4.1.1",
Expand Down
16 changes: 12 additions & 4 deletions src/automations/anywhere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { App } from 'electron'
import { loadSettings } from '../main/config'
import { igniteEngine } from '../services/llm'
import { LlmResponse } from '../types/llm.d'
import removeMarkdown from 'markdown-to-text'
import { removeMarkdown } from '@excalidraw/markdown-to-text'
import LlmEngine from '../services/engine'
import Automator from './automator'
import Message from '../models/message'
Expand All @@ -20,7 +20,7 @@ export default class PromptAnywhere {
this.cancelled = false
}

cancel = async () => {
cancel = async (): Promise<void> => {

// close stuff
await window.closeWaitingPanel();
Expand All @@ -32,6 +32,11 @@ export default class PromptAnywhere {

}

static initPrompt = async (): Promise<void> => {
await window.hideWindows();
await window.openPromptAnywhere();
}

execPrompt = async (app: App, prompt: string): Promise<void> => {

try {
Expand Down Expand Up @@ -68,7 +73,6 @@ export default class PromptAnywhere {

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

// now paste
Expand All @@ -78,13 +82,17 @@ export default class PromptAnywhere {
const automator = new Automator();
await automator.pasteText(result)

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

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

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

Expand Down
7 changes: 4 additions & 3 deletions src/automations/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LlmResponse } from '../types/llm.d'
import { App, BrowserWindow, Notification } from 'electron'
import { loadSettings } from '../main/config'
import { igniteEngine } from '../services/llm'
import removeMarkdown from 'markdown-to-text'
import { removeMarkdown } from '@excalidraw/markdown-to-text'
import Message from '../models/message'
import Automator from './automator'
import LlmEngine from '../services/engine'
Expand Down Expand Up @@ -158,14 +158,15 @@ 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
await window.restoreWindows();
await window.releaseFocus();
return result;

}
Expand All @@ -175,7 +176,7 @@ export default class Commander {
}

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

Expand Down
8 changes: 5 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const registerShortcuts = () => {
shortcuts.registerShortcuts(app, {
chat: window.openMainWindow,
command: Commander.initCommand,
anywhere: window.openPromptAnywhere,
anywhere: PromptAnywhere.initPrompt,
});
}

Expand All @@ -79,7 +79,7 @@ const buildTrayMenu = () => {

return [
{ label: 'New Chat', accelerator: shortcuts.shortcutAccelerator(configShortcuts?.chat), click: window.openMainWindow },
{ label: 'Prompt Anywhere', accelerator: shortcuts.shortcutAccelerator(configShortcuts?.anywhere), click: window.openPromptAnywhere },
{ label: 'Prompt Anywhere', accelerator: shortcuts.shortcutAccelerator(configShortcuts?.anywhere), click: PromptAnywhere.initPrompt },
{ label: 'Run AI Command', accelerator: shortcuts.shortcutAccelerator(configShortcuts?.command), click: Commander.initCommand },
{ type: 'separator'},
{ label: 'Settings…', click: window.openSettingsWindow },
Expand Down Expand Up @@ -365,6 +365,9 @@ ipcMain.on('run-python-code', async (event, payload) => {

ipcMain.on('prompt-anywhere', async (event, payload) => {

// if cancel on prompt window
await window.closePromptAnywhere();

// cancel previous
if (anywhere != null) {
await anywhere.cancel();
Expand All @@ -384,7 +387,6 @@ ipcMain.on('cancel-anywhere', async () => {

// if cancel on prompt window
await window.closePromptAnywhere();
await window.releaseFocus();

// if cancel on waiting panel
if (anywhere != null) {
Expand Down
71 changes: 38 additions & 33 deletions src/main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,14 @@ export const hideWindows = async () => {
// remember to restore all windows
windowsToRestore = [];
try {
console.log('Hiding windows');
// console.log('Hiding windows');
const windows = BrowserWindow.getAllWindows();
for (const window of windows) {
if (!window.isDestroyed() && window.isVisible() && !window.isMinimized()) {
windowsToRestore.push(window);
window.hide();
}
}
await releaseFocus();
} catch (error) {
console.error('Error while hiding active windows', error);
}
Expand All @@ -204,7 +203,7 @@ export const hideWindows = async () => {
export const restoreWindows = () => {

// log
console.log(`Restoring ${windowsToRestore.length} windows`)
// console.log(`Restoring ${windowsToRestore.length} windows`)

// restore main window first
windowsToRestore.sort((a, b) => {
Expand Down Expand Up @@ -232,9 +231,11 @@ let commandPalette: BrowserWindow = null;
export const closeCommandPalette = async () => {
try {
if (commandPalette && !commandPalette.isDestroyed()) {
// console.log('Closing command palette')
commandPalette?.close()
await wait();
}
commandPalette = null;
} catch (error) {
console.error('Error while closing command palette', error);
}
Expand Down Expand Up @@ -274,11 +275,11 @@ export const openCommandPalette = async (textId: string) => {
}

let waitingPanel: BrowserWindow = null;
export const closeWaitingPanel = async (destroy?: boolean) => {
export const closeWaitingPanel = async () => {
try {
if (waitingPanel && !waitingPanel.isDestroyed()) {
if (destroy) waitingPanel?.destroy()
else waitingPanel?.close()
// console.log('Closing waiting panel')
waitingPanel?.close()
await wait();
}
waitingPanel = null;
Expand All @@ -289,23 +290,26 @@ export const closeWaitingPanel = async (destroy?: boolean) => {

export const openWaitingPanel = () => {

// try to show existig one
// try to close existig one
// console.log('Opening waiting panel')
closeWaitingPanel();

// get bounds
const width = 100;
const height = 20;
const { x, y } = screen.getCursorScreenPoint();

// else open a new one
// open a new one
waitingPanel = createWindow({
hash: '/wait',
x: x - width/2,
y: y - height*2,
y: y - height*1.5,
width: width,
height: height,
frame: false,
skipTaskbar: true,
alwaysOnTop: true,
hiddenInMissionControl: true,
hasShadow: false,
});

Expand Down Expand Up @@ -333,37 +337,38 @@ export const openSettingsWindow = () => {
let promptAnywhereWindow: BrowserWindow = null;
export const openPromptAnywhere = () => {

// try to show existig one
closePromptAnywhere();
// try to close existig one
closePromptAnywhere();

// get bounds
const width = 500;
const height = 48;
const { x, y } = screen.getCursorScreenPoint();
// open a new one
promptAnywhereWindow = createWindow({
hash: '/prompt',
x: x - width/2,
y: y - 24,
width: width,
height: height,
frame: false,
skipTaskbar: true,
alwaysOnTop: true,
hiddenInMissionControl: true,
});
promptAnywhereWindow.on('blur', () => {
closePromptAnywhere();
restoreWindows();
});
// get bounds
const width = 500;
const height = 48;
const { x, y } = screen.getCursorScreenPoint();

// open a new one
promptAnywhereWindow = createWindow({
hash: '/prompt',
x: x - width/2,
y: y - 24,
width: width,
height: height,
frame: false,
skipTaskbar: true,
alwaysOnTop: true,
hiddenInMissionControl: true,
});

promptAnywhereWindow.on('blur', () => {
closePromptAnywhere();
restoreWindows();
});

};

export const closePromptAnywhere = async () => {
try {
if (promptAnywhereWindow && !promptAnywhereWindow.isDestroyed()) {
// console.log('Closing prompt anywhere window')
promptAnywhereWindow?.close()
await wait();
}
Expand Down

0 comments on commit 737ee0e

Please sign in to comment.