fix: 拾取/框选时最小化应用自身窗口,完成后恢复

This commit is contained in:
yu 2026-10-29 17:12:15 +08:00
parent f353254a86
commit e59034d700

View File

@ -28,6 +28,8 @@ export interface DesktopPickResult {
}
export async function pickDesktopElement(): Promise<DesktopPickResult> {
const appWindows = BrowserWindow.getAllWindows().filter(w => !w.isDestroyed() && w.isVisible());
for (const w of appWindows) w.minimize();
const { stdout } = await run("powershell.exe", ["-NoProfile", "-STA", "-ExecutionPolicy", "Bypass", "-File", pickerScriptPath()], { timeout: 180000, windowsHide: false, maxBuffer: 4 * 1024 * 1024 });
const jsonLine = stdout.trim().split(/\\r?\\n/).reverse().find(l => l.trim().startsWith("{"));
if (!jsonLine) throw new RuntimeError("picker_failed", "拾取未返回结果");
@ -48,6 +50,8 @@ export async function listVisibleWindows(): Promise<Array<{ title: string; proce
return JSON.parse(jsonLine) as Array<{ title: string; process: string }>;
}
export async function pickScreenRegion(): Promise<{ x: number; y: number; width: number; height: number; imagePath: string }> {
const appWindows = BrowserWindow.getAllWindows().filter(w => !w.isDestroyed() && w.isVisible());
for (const w of appWindows) w.minimize();
const display = screen.getPrimaryDisplay();
const sx = display.bounds.x, sy = display.bounds.y, sw = display.bounds.width, sh = display.bounds.height;
const win = new BrowserWindow({
@ -85,6 +89,7 @@ export async function pickScreenRegion(): Promise<{ x: number; y: number; width:
if ((error as Error).message.includes("已取消框选")) throw error;
}
}
for (const w of appWindows) { if (!w.isDestroyed()) w.restore(); }
win.destroy();
if (!region) throw new RuntimeError("pick_cancelled", "未完成框选");
await new Promise(r => setTimeout(r, 300));
@ -103,4 +108,4 @@ export async function pickScreenRegion(): Promise<{ x: number; y: number; width:
].join("");
await run("powershell.exe", ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", psScript], { timeout: 30000, windowsHide: true });
return { ...region, imagePath };
}
}