Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): foreground Windows OAuth return (#2349)

Yingze Hand3afa318b27e
diff --git a/apps/desktop/src/main/__tests__/deepLink.test.ts b/apps/desktop/src/main/__tests__/deepLink.test.ts
index 45bec117da..89f6f81933 100644
--- a/apps/desktop/src/main/__tests__/deepLink.test.ts
+++ b/apps/desktop/src/main/__tests__/deepLink.test.ts
@@ -6,7 +6,7 @@
*/
import { describe, it, expect } from 'vitest';
-import type { BrowserWindow } from 'electron';
+import { app, type BrowserWindow } from 'electron';
// vitest 跑测试时不会真的初始化 Electron app, 单 import 需要 mock electron。
// deepLink.ts 只在 registerDeepLinkProtocol / dispatchDeepLink 用到 app /
@@ -39,10 +39,33 @@ import {
DEEP_LINK_PROTOCOL,
OPEN_FOLDER_FLAG,
OPEN_SHARE_FILE_FLAG,
+ focusMainWindow,
openMainWindowVoiceSettings,
setDeepLinkMainWindow,
} from '../deepLink';
+describe('user-initiated main-window focus', () => {
+ it('activates and raises the target window before focusing on Windows', () => {
+ const calls: string[] = [];
+ const mainWindow = {
+ isDestroyed: () => false,
+ isVisible: () => false,
+ isMinimized: () => true,
+ show: vi.fn(() => calls.push('show')),
+ restore: vi.fn(() => calls.push('restore')),
+ moveTop: vi.fn(() => calls.push('moveTop')),
+ focus: vi.fn(() => calls.push('window.focus')),
+ };
+ vi.mocked(app.focus).mockImplementation(() => calls.push('app.focus'));
+ setDeepLinkMainWindow(mainWindow as unknown as BrowserWindow);
+
+ expect(focusMainWindow('win32')).toBe(true);
+
+ expect(calls).toEqual(['show', 'restore', 'app.focus', 'moveTop', 'window.focus']);
+ setDeepLinkMainWindow(null);
+ });
+});
+
describe('internal main-window navigation', () => {
it('focuses the main window and routes voice settings to the requested tab', () => {
const send = vi.fn();
diff --git a/apps/desktop/src/main/deepLink.ts b/apps/desktop/src/main/deepLink.ts
index 1266f1cf77..8e778dc53f 100644
--- a/apps/desktop/src/main/deepLink.ts
+++ b/apps/desktop/src/main/deepLink.ts
@@ -288,17 +288,26 @@ export function handleIncomingShareFile(filePath: string, source: string): void
}
/**
- * 把主窗口拉回前台 (show + restore + focus;macOS 额外 app.focus steal——从浏览器
- * 等其它前台应用手里抢焦点,单靠 win.focus() 在 macOS 上不可靠)。
+ * 把主窗口拉回前台 (show + restore + focus)。Windows 的外部协议唤起需要先
+ * 激活应用并提升目标窗口 z-order;macOS 额外使用 app.focus steal——
+ * 从浏览器等其它前台应用手里抢焦点,单靠 win.focus() 不可靠。
* 窗口不存在 / 已销毁时返回 false,调用方自行决定是否忽略。
*/
-export function focusMainWindow(): boolean {
+export function focusMainWindow(platform: NodeJS.Platform = process.platform): boolean {
const win = mainWindowRef;
if (!win || win.isDestroyed()) return false;
if (!win.isVisible()) win.show();
if (win.isMinimized()) win.restore();
+ if (platform === 'win32') {
+ // Windows 可以收到 second-instance deep link 但仍把浏览器保留在
+ // 前台。app.focus() 先激活应用,moveTop() 再确保用户主动点击的
+ // OAuth 返回窗口不被原浏览器遮挡。该函数只用于 focus deep link,
+ // 不会把后台 Ghost workspace 请求扩大为强制抢前台。
+ app.focus();
+ win.moveTop();
+ }
win.focus();
- if (process.platform === 'darwin') app.focus({ steal: true });
+ if (platform === 'darwin') app.focus({ steal: true });
return true;
}