diff --git a/web/src/lib/blink.ts b/web/src/lib/blink.ts new file mode 100644 index 0000000..c16cb43 --- /dev/null +++ b/web/src/lib/blink.ts @@ -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(); +} diff --git a/web/src/main.tsx b/web/src/main.tsx index b36a544..a976e18 100644 --- a/web/src/main.tsx +++ b/web/src/main.tsx @@ -1,13 +1,14 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { RouterProvider } from "@tanstack/react-router"; import { ThemeProvider } from "next-themes"; -import { StrictMode } from "react"; +import { StrictMode, useEffect } from "react"; import { createRoot } from "react-dom/client"; import "@/styles/app.css"; import "@/lib/i18n"; import { Toaster } from "@/components/ui/toast"; +import { startBlinkClock } from "@/lib/blink"; import { consumeHandoff } from "@/lib/handoff"; import { router } from "@/router"; import { useConnectionStore } from "@/stores/connection"; @@ -23,6 +24,8 @@ const queryClient = new QueryClient({ }); function App() { + useEffect(() => startBlinkClock(), []); + return ( 根节点上的单一全局时钟驱动(data-pi-blink-* 属性,见 app.css 与 blink.ts), + 不再使用每个圆点独立的 CSS animation,避免相位漂移。当前聚焦会话叠加高亮背景。 */ +.tree-row .dot--current { + background: var(--info); } /* 子会话 / 子代理行:更矮、更小、灰一档;状态只由圆点表达。 */