diff --git a/surfsense_desktop/src/modules/quick-ask.ts b/surfsense_desktop/src/modules/quick-ask.ts index 81b74d986..f7753d1d6 100644 --- a/surfsense_desktop/src/modules/quick-ask.ts +++ b/surfsense_desktop/src/modules/quick-ask.ts @@ -13,6 +13,15 @@ function hideQuickAsk(): void { } } +function clampToScreen(x: number, y: number, w: number, h: number): { x: number; y: number } { + const display = screen.getDisplayNearestPoint({ x, y }); + const { x: dx, y: dy, width: dw, height: dh } = display.workArea; + return { + x: Math.max(dx, Math.min(x, dx + dw - w)), + y: Math.max(dy, Math.min(y, dy + dh - h)), + }; +} + function createQuickAskWindow(x: number, y: number): BrowserWindow { if (quickAskWindow && !quickAskWindow.isDestroyed()) { quickAskWindow.setPosition(x, y); @@ -29,6 +38,7 @@ function createQuickAskWindow(x: number, y: number): BrowserWindow { type: 'panel', resizable: true, fullscreenable: false, + maximizable: false, webPreferences: { preload: path.join(__dirname, 'preload.js'), contextIsolation: true, @@ -45,6 +55,10 @@ function createQuickAskWindow(x: number, y: number): BrowserWindow { quickAskWindow?.show(); }); + quickAskWindow.webContents.on('before-input-event', (_event, input) => { + if (input.key === 'Escape') hideQuickAsk(); + }); + quickAskWindow.webContents.setWindowOpenHandler(({ url }) => { if (url.startsWith('http://localhost')) { return { action: 'allow' }; @@ -72,7 +86,8 @@ export function registerQuickAsk(): void { pendingText = text; const cursor = screen.getCursorScreenPoint(); - createQuickAskWindow(cursor.x, cursor.y); + const pos = clampToScreen(cursor.x, cursor.y, 450, 550); + createQuickAskWindow(pos.x, pos.y); }); if (!ok) {