mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-15 18:25:18 +02:00
feat: enhance keyboard shortcut management and improve app responsiveness
- Updated the development script to include a build step before launching the app. - Refactored the registration of quick ask and autocomplete functionalities to be asynchronous, ensuring proper initialization. - Introduced IPC channels for getting and setting keyboard shortcuts, allowing users to customize their experience. - Enhanced the platform module to support better interaction with the Electron API for clipboard operations. - Improved the user interface for managing keyboard shortcuts in the settings dialog, providing a more intuitive experience.
This commit is contained in:
parent
e920923fa4
commit
49441233e7
30 changed files with 923 additions and 191 deletions
|
|
@ -1,10 +1,11 @@
|
|||
import { BrowserWindow, clipboard, globalShortcut, ipcMain, screen, shell } from 'electron';
|
||||
import path from 'path';
|
||||
import { IPC_CHANNELS } from '../ipc/channels';
|
||||
import { checkAccessibilityPermission, getFrontmostApp, simulatePaste } from './platform';
|
||||
import { checkAccessibilityPermission, getFrontmostApp, simulateCopy, simulatePaste } from './platform';
|
||||
import { getServerPort } from './server';
|
||||
import { getShortcuts } from './shortcuts';
|
||||
|
||||
const SHORTCUT = 'CommandOrControl+Option+S';
|
||||
let currentShortcut = '';
|
||||
let quickAskWindow: BrowserWindow | null = null;
|
||||
let pendingText = '';
|
||||
let pendingMode = '';
|
||||
|
|
@ -77,29 +78,52 @@ function createQuickAskWindow(x: number, y: number): BrowserWindow {
|
|||
return quickAskWindow;
|
||||
}
|
||||
|
||||
export function registerQuickAsk(): void {
|
||||
const ok = globalShortcut.register(SHORTCUT, () => {
|
||||
if (quickAskWindow && !quickAskWindow.isDestroyed()) {
|
||||
destroyQuickAsk();
|
||||
return;
|
||||
}
|
||||
function openQuickAsk(text: string): void {
|
||||
pendingText = text;
|
||||
const cursor = screen.getCursorScreenPoint();
|
||||
const pos = clampToScreen(cursor.x, cursor.y, 450, 750);
|
||||
createQuickAskWindow(pos.x, pos.y);
|
||||
}
|
||||
|
||||
sourceApp = getFrontmostApp();
|
||||
savedClipboard = clipboard.readText();
|
||||
async function quickAskHandler(): Promise<void> {
|
||||
console.log('[quick-ask] Handler triggered');
|
||||
|
||||
const text = savedClipboard.trim();
|
||||
if (!text) return;
|
||||
|
||||
pendingText = text;
|
||||
const cursor = screen.getCursorScreenPoint();
|
||||
const pos = clampToScreen(cursor.x, cursor.y, 450, 750);
|
||||
createQuickAskWindow(pos.x, pos.y);
|
||||
});
|
||||
|
||||
if (!ok) {
|
||||
console.log(`Quick-ask: failed to register ${SHORTCUT}`);
|
||||
if (quickAskWindow && !quickAskWindow.isDestroyed()) {
|
||||
console.log('[quick-ask] Window already open, closing');
|
||||
destroyQuickAsk();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!checkAccessibilityPermission()) {
|
||||
console.log('[quick-ask] Accessibility permission denied');
|
||||
return;
|
||||
}
|
||||
|
||||
savedClipboard = clipboard.readText();
|
||||
console.log('[quick-ask] Saved clipboard length:', savedClipboard.length);
|
||||
|
||||
const copyOk = simulateCopy();
|
||||
console.log('[quick-ask] simulateCopy result:', copyOk);
|
||||
|
||||
await new Promise((r) => setTimeout(r, 300));
|
||||
|
||||
const afterCopy = clipboard.readText();
|
||||
const selected = afterCopy.trim();
|
||||
console.log('[quick-ask] Clipboard after copy length:', afterCopy.length, 'changed:', afterCopy !== savedClipboard);
|
||||
|
||||
const text = selected || savedClipboard.trim();
|
||||
|
||||
sourceApp = getFrontmostApp();
|
||||
console.log('[quick-ask] Source app:', sourceApp, '| Opening Quick Ask with', text.length, 'chars', selected ? '(selected)' : text ? '(clipboard fallback)' : '(empty)');
|
||||
openQuickAsk(text);
|
||||
}
|
||||
|
||||
let ipcRegistered = false;
|
||||
|
||||
function registerIpcHandlers(): void {
|
||||
if (ipcRegistered) return;
|
||||
ipcRegistered = true;
|
||||
|
||||
ipcMain.handle(IPC_CHANNELS.QUICK_ASK_TEXT, () => {
|
||||
const text = pendingText;
|
||||
pendingText = '';
|
||||
|
|
@ -136,6 +160,24 @@ export function registerQuickAsk(): void {
|
|||
});
|
||||
}
|
||||
|
||||
export function unregisterQuickAsk(): void {
|
||||
globalShortcut.unregister(SHORTCUT);
|
||||
async function registerShortcut(): Promise<void> {
|
||||
const shortcuts = await getShortcuts();
|
||||
currentShortcut = shortcuts.quickAsk;
|
||||
|
||||
const ok = globalShortcut.register(currentShortcut, () => { quickAskHandler(); });
|
||||
console.log(`[quick-ask] Register ${currentShortcut}: ${ok ? 'OK' : 'FAILED'}`);
|
||||
}
|
||||
|
||||
export async function registerQuickAsk(): Promise<void> {
|
||||
registerIpcHandlers();
|
||||
await registerShortcut();
|
||||
}
|
||||
|
||||
export function unregisterQuickAsk(): void {
|
||||
if (currentShortcut) globalShortcut.unregister(currentShortcut);
|
||||
}
|
||||
|
||||
export async function reregisterQuickAsk(): Promise<void> {
|
||||
unregisterQuickAsk();
|
||||
await registerShortcut();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue