diff --git a/src/features/agents/Session.module.css b/src/features/agents/Session.module.css index 5eadb784f0..cf1de13d6e 100644 --- a/src/features/agents/Session.module.css +++ b/src/features/agents/Session.module.css @@ -228,6 +228,10 @@ .conversationRow[data-measured] { content-visibility: auto; } +/* WebKit leaves a stale selection highlight on rows that skipped rendering (`useOffscreenRows`). */ +.stream[data-selection] > .conversationRow[data-measured] { + content-visibility: visible; +} /* Keep turns distinct and the work within a turn close to its author. Not `* + *`: a universal adjacent-sibling rule makes Chromium restyle whole sibling subtrees on every DOM insertion anywhere in the window, which costs every editor scroll frame. */ diff --git a/src/features/agents/session/Stream.tsx b/src/features/agents/session/Stream.tsx index 94e8372848..4d19cc6895 100644 --- a/src/features/agents/session/Stream.tsx +++ b/src/features/agents/session/Stream.tsx @@ -144,6 +144,39 @@ function useOffscreenRows( resize.disconnect(); }; }, [ref, visible, enabled, fitWindowWidth, fontZoom]); + // WebKit never clears the selection highlight of a row that skips rendering: once the + // selection moves, rows it covered off screen still show it when scrolled back. While the + // stream holds a selection every row renders (`data-selection`), and keeps rendering until + // the frame after it goes, when WebKit applies the change. + useEffect(() => { + const el = ref.current; + if (!el || !enabled || !visible) return; + let frame = 0; + const selected = () => { + const sel = window.getSelection(); + return !!sel && !sel.isCollapsed && sel.rangeCount > 0 && sel.getRangeAt(0).intersectsNode(el); + }; + const update = () => { + cancelAnimationFrame(frame); + if (selected()) { + el.setAttribute("data-selection", ""); + return; + } + if (!el.hasAttribute("data-selection")) return; + frame = requestAnimationFrame(() => { + frame = requestAnimationFrame(() => { + if (!selected()) el.removeAttribute("data-selection"); + }); + }); + }; + update(); + document.addEventListener("selectionchange", update); + return () => { + cancelAnimationFrame(frame); + document.removeEventListener("selectionchange", update); + el.removeAttribute("data-selection"); + }; + }, [ref, visible, enabled]); } interface StreamProps {