feat(web): drive every status dot from one shared blink clock

Each dot used its own CSS animation ("pulse" in app.css, "tree-dot-blink" in
chat.css), so dots drifted out of phase and the tree looked noisy. One
ref-counted 500ms clock in lib/blink.ts now writes data-pi-blink-fast and
data-pi-blink-slow on <html>, and CSS derives the dim phase from those
attributes: one timer for the whole app, no React re-renders, and
prefers-reduced-motion pins every dot solid.
This commit is contained in:
yuez committed 2026-10-27 18:42:54 +08:00
1 parent 48411dccd2
commit 81e7f96a64
4 files changed
+97 -36

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
export const BLINK_MS = 500;
let subscribers = 0;
let timerId: number | null = null;
let tick = 0;
function updateDom(fast: "on" | "dim", slow: "on" | "dim"): void {
if (typeof document === "undefined" || !document.documentElement) return;
document.documentElement.setAttribute("data-pi-blink-fast", fast);
document.documentElement.setAttribute("data-pi-blink-slow", slow);
}
function clearDom(): void {
if (typeof document === "undefined" || !document.documentElement) return;
document.documentElement.removeAttribute("data-pi-blink-fast");
document.documentElement.removeAttribute("data-pi-blink-slow");
}
function syncDomForTick(t: number): void {
const fast = t % 2 === 0 ? "on" : "dim";
const slow = Math.floor(t / 2) % 2 === 0 ? "on" : "dim";
updateDom(fast, slow);
}
export function blinkTick(): number {
return tick;
}
export function startBlinkClock(): () => void {
subscribers++;
if (subscribers === 1) {
tick = 0;
const prefersReducedMotion =
typeof window !== "undefined" &&
typeof window.matchMedia === "function" &&
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (prefersReducedMotion) {
updateDom("on", "on");
} else {
syncDomForTick(tick);
if (typeof window !== "undefined") {
timerId = window.setInterval(() => {
tick++;
syncDomForTick(tick);
}, BLINK_MS);
}
}
}
let unsubscribed = false;
return () => {
if (unsubscribed) return;
unsubscribed = true;
subscribers--;
if (subscribers <= 0) {
subscribers = 0;
if (timerId !== null) {
window.clearInterval(timerId);
timerId = null;
}
tick = 0;
clearDom();
}
};
}
export function resetBlinkClock(): void {
subscribers = 0;
if (timerId !== null) {
window.clearInterval(timerId);
timerId = null;
}
tick = 0;
clearDom();
}
+4 -1
View File
@@ -1,13 +1,14 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { RouterProvider } from "@tanstack/react-router"; import { RouterProvider } from "@tanstack/react-router";
import { ThemeProvider } from "next-themes"; import { ThemeProvider } from "next-themes";
import { StrictMode } from "react"; import { StrictMode, useEffect } from "react";
import { createRoot } from "react-dom/client"; import { createRoot } from "react-dom/client";
import "@/styles/app.css"; import "@/styles/app.css";
import "@/lib/i18n"; import "@/lib/i18n";
import { Toaster } from "@/components/ui/toast"; import { Toaster } from "@/components/ui/toast";
import { startBlinkClock } from "@/lib/blink";
import { consumeHandoff } from "@/lib/handoff"; import { consumeHandoff } from "@/lib/handoff";
import { router } from "@/router"; import { router } from "@/router";
import { useConnectionStore } from "@/stores/connection"; import { useConnectionStore } from "@/stores/connection";
@@ -23,6 +24,8 @@ const queryClient = new QueryClient({
}); });
function App() { function App() {
useEffect(() => startBlinkClock(), []);
return ( return (
<StrictMode> <StrictMode>
<ThemeProvider <ThemeProvider
+10 -3
View File
@@ -379,15 +379,22 @@ code, pre { font-family: var(--font-mono); font-size: 0.875em; }
.dot { display: inline-block; width: .5rem; height: .5rem; border-radius: 50%; background: var(--muted-foreground); flex: none; } .dot { display: inline-block; width: .5rem; height: .5rem; border-radius: 50%; background: var(--muted-foreground); flex: none; }
.dot--lg { width: 9px; height: 9px; } .dot--lg { width: 9px; height: 9px; }
.dot--busy { background: var(--warn); animation: pulse 1.4s ease-in-out infinite; } .dot--busy { background: var(--warn); }
.dot--compacting { background: var(--info); animation: pulse 2s ease-in-out infinite; } .dot--compacting { background: var(--role-thinking); }
.dot--idle { background: var(--faint); } .dot--idle { background: var(--faint); }
.dot--ok { background: var(--ok); } .dot--ok { background: var(--ok); }
.dot--err { background: var(--err); animation: pulse 1.4s ease-in-out infinite; } .dot--err { background: var(--err); }
.dot--dead { background: var(--err); }
.dot--attention { background: var(--warn); box-shadow: 0 0 0 2px color-mix(in oklch, var(--warn) 22%, transparent); } .dot--attention { background: var(--warn); box-shadow: 0 0 0 2px color-mix(in oklch, var(--warn) 22%, transparent); }
.dot--exited { background: transparent; box-shadow: inset 0 0 0 1.5px var(--muted-foreground); } .dot--exited { background: transparent; box-shadow: inset 0 0 0 1.5px var(--muted-foreground); }
@keyframes pulse { 50% { opacity: .35; } } @keyframes pulse { 50% { opacity: .35; } }
/* Shared blink phase driven by html[data-pi-blink-*] attributes (single window clock) */
html[data-pi-blink-fast="dim"] .dot--busy:not(.dot--steady),
html[data-pi-blink-fast="dim"] .dot--err:not(.dot--steady),
html[data-pi-blink-fast="dim"] .tree-row .dot--ok { opacity: .35; }
html[data-pi-blink-slow="dim"] .dot--compacting:not(.dot--steady) { opacity: .35; }
.chip { .chip {
display: inline-flex; align-items: center; gap: var(--space-2); display: inline-flex; align-items: center; gap: var(--space-2);
padding: var(--space-1) var(--space-3); padding: var(--space-1) var(--space-3);
+4 -32
View File
@@ -445,38 +445,10 @@
background: color-mix(in oklch, var(--destructive) 12%, transparent); background: color-mix(in oklch, var(--destructive) 12%, transparent);
} }
/* 状态圆点:pi2.nvim 是离散二值闪烁(busy/error 500ms、compacting 1000ms), /* 状态圆点:由 <html> 根节点上的单一全局时钟驱动(data-pi-blink-* 属性,见 app.css 与 blink.ts),
不是 app.css 里那种平滑 ease 的 pulse;attention / idle / ok / exited 常亮。 不再使用每个圆点独立的 CSS animation,避免相位漂移。当前聚焦会话叠加高亮背景。 */
只作用于会话树,其他页面的圆点保持不变。 */ .tree-row .dot--current {
@keyframes tree-dot-blink { background: var(--info);
0%,
49% {
opacity: 1;
}
50%,
100% {
opacity: 0.35;
}
}
.tree-row .dot--busy,
.tree-row .dot--err {
animation: tree-dot-blink 1s steps(1, end) infinite;
}
.tree-row .dot--compacting {
animation: tree-dot-blink 2s steps(1, end) infinite;
}
.tree-row .dot--attention,
.tree-row .dot--idle,
.tree-row .dot--ok,
.tree-row .dot--exited {
animation: none;
}
@media (prefers-reduced-motion: reduce) {
.tree-row .dot--busy,
.tree-row .dot--err,
.tree-row .dot--compacting {
animation: none;
}
} }
/* 子会话 / 子代理行:更矮、更小、灰一档;状态只由圆点表达。 */ /* 子会话 / 子代理行:更矮、更小、灰一档;状态只由圆点表达。 */