Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
perf(desktop): focusTraversal 卡内先判边缘、只在出卡时扫全文档(Greptile P2×2)
Zhang Yunjina813ef17f5b4
diff --git a/apps/desktop/src/renderer/lib/__tests__/focusTraversal.test.tsx b/apps/desktop/src/renderer/lib/__tests__/focusTraversal.test.tsxindex bbd1cccd3e..e0c144b10c 100644--- a/apps/desktop/src/renderer/lib/__tests__/focusTraversal.test.tsx+++ b/apps/desktop/src/renderer/lib/__tests__/focusTraversal.test.tsx@@ -1,5 +1,5 @@// @vitest-environment jsdom-import { afterEach, describe, expect, it } from 'vitest';+import { afterEach, describe, expect, it, vi } from 'vitest';import { cleanup, fireEvent, render } from '@testing-library/react';import { useState } from 'react';@@ -56,17 +56,29 @@ describe('handOffTabFromCard', () => {expect(document.activeElement).toBe(usage);});- it('卡内中间元素的 Tab 不接管(让浏览器的自然顺序继续走)', () => {+ it('卡内中间元素的 Tab 不接管(自然顺序继续走,且不触发全文档扫描)', () => {render(<CardHarness extraInsideMiddle />);const cardRoot = document.getElementById('card-root')!;document.getElementById('card-region')!.focus();expect(handOffTabFromCard(cardRoot, { shiftKey: false })).toBe(false);- // 自然行为:调用方不 preventDefault,浏览器把焦点走到下一个元素(中间档)。- expect(document.activeElement).toBe(document.getElementById('card-region'));- fireEvent.keyDown(document.getElementById('card-region')!, { key: 'Tab' });+ // 调用方不 preventDefault,浏览器按自然 Tab 顺序移动焦点(jsdom 不执行 Tab 默认+ // 行为,无法在单测里模拟自然移动;出卡方向的移动由上面两条边缘用例直接断言)。expect(document.activeElement).toBe(document.getElementById('card-region'));});+ it('卡内中间的 Tab 只扫卡片子树,不扫全文档(长聊天记录下的键盘性能)', () => {+ render(<CardHarness extraInsideMiddle />);+ const cardRoot = document.getElementById('card-root')!;+ document.getElementById('card-region')!.focus();+ const walkerSpy = vi.spyOn(document, 'createTreeWalker');+ expect(handOffTabFromCard(cardRoot, { shiftKey: false })).toBe(false);+ // 全文档扫描的标志是被 walk 到 documentElement;卡中间只应 walk 卡根。+ for (const call of walkerSpy.mock.calls) {+ expect(call[0]).toBe(cardRoot);+ }+ walkerSpy.mockRestore();+ });+it('焦点不在卡内时不做任何事', () => {render(<CardHarness />);const cardRoot = document.getElementById('card-root')!;diff --git a/apps/desktop/src/renderer/lib/focusTraversal.ts b/apps/desktop/src/renderer/lib/focusTraversal.tsindex da17eadae7..d574ab3f5a 100644--- a/apps/desktop/src/renderer/lib/focusTraversal.ts+++ b/apps/desktop/src/renderer/lib/focusTraversal.ts@@ -11,12 +11,16 @@* 卡内其余 Tab 行为不变(多行卡的行间移动照旧自然进行)。*/-/** 文档序的全部可 Tab 元素(可见性判据与 Radix FocusScope 的 getTabbableCandidates 同源)。 */-function collectTabbables(root: Document): HTMLElement[] {+/**+ * 文档序的可 Tab 元素(可见性判据与 Radix FocusScope 的 getTabbableCandidates 同源)。+ * `scope` 限定扫描子树:卡内边缘判定只扫卡片子树(小),只有真要出卡时才扫全文档。+ */+function collectTabbables(scope: Document | HTMLElement): HTMLElement[] {const nodes: HTMLElement[] = [];- const walkerRoot = root.documentElement ?? root.body;+ const walkerRoot = scope instanceof Document ? (scope.documentElement ?? scope.body) : scope;if (!walkerRoot) return nodes;- const walker = root.createTreeWalker(walkerRoot, NodeFilter.SHOW_ELEMENT, {+ const doc = scope instanceof Document ? scope : scope.ownerDocument;+ const walker = doc.createTreeWalker(walkerRoot, NodeFilter.SHOW_ELEMENT, {acceptNode: (node) => {const element = node as HTMLElement;const isHiddenInput = element instanceof HTMLInputElement && element.type === 'hidden';@@ -27,17 +31,26 @@ function collectTabbables(root: Document): HTMLElement[] {},});while (walker.nextNode()) nodes.push(walker.currentNode as HTMLElement);- return nodes.filter((element) => !isHiddenForFocus(element));+ const hiddenCache = new Map<Element, boolean>();+ return nodes.filter((element) => !isHiddenForFocus(element, hiddenCache));}-function isHiddenForFocus(node: HTMLElement): boolean {- if (node.ownerDocument.defaultView?.getComputedStyle(node).visibility === 'hidden') return true;+/** 隐藏判定带记忆化:一次扫描里共享父节点(卡片根、容器等)只查一次 getComputedStyle。 */+function isHiddenForFocus(node: HTMLElement, hiddenCache: Map<Element, boolean>): boolean {+ const cached = hiddenCache.get(node);+ if (cached !== undefined) return cached;+ const view = node.ownerDocument.defaultView;+ let result = view?.getComputedStyle(node).visibility === 'hidden';let cursor: HTMLElement | null = node;- while (cursor) {- if (cursor.ownerDocument.defaultView?.getComputedStyle(cursor).display === 'none') return true;+ while (!result && cursor) {+ const cursorCached = hiddenCache.get(cursor);+ if (cursorCached !== undefined) { result = cursorCached; break; }+ result = view?.getComputedStyle(cursor).display === 'none';+ hiddenCache.set(cursor, result);cursor = cursor.parentElement;}- return false;+ hiddenCache.set(node, result);+ return result;}/**@@ -48,11 +61,20 @@ export function handOffTabFromCard(cardRoot: HTMLElement,event: { shiftKey: boolean },): boolean {- const tabbables = collectTabbables(cardRoot.ownerDocument);- if (tabbables.length === 0) return false;const active = cardRoot.ownerDocument.activeElement;if (!(active instanceof HTMLElement) || !cardRoot.contains(active)) return false;+ // 先只扫卡片子树判边缘:不在边缘立即放行(不扫全文档 —— Tab 是高频按键,+ // 长聊天记录下每次全页 TreeWalker + getComputedStyle 会拖慢键盘移动)。+ const inside = collectTabbables(cardRoot);+ if (inside.length === 0) return false;+ const atLastEdge = !event.shiftKey && active === inside[inside.length - 1];+ const atFirstEdge = event.shiftKey && active === inside[0]!;+ if (!atLastEdge && !atFirstEdge) return false;++ // 只有边缘的 Tab 才值得做全文档序定位,找出卡外目标。+ const tabbables = collectTabbables(cardRoot.ownerDocument);+ if (tabbables.length === 0) return false;const activeIndex = tabbables.indexOf(active);if (activeIndex === -1) return false;const lastInside = (() => {