diff --git a/tests/test_css_animations.py b/tests/test_css_animations.py new file mode 100644 index 0000000..34f7c84 --- /dev/null +++ b/tests/test_css_animations.py @@ -0,0 +1,152 @@ +# -*- coding: utf-8 -*- +"""无限动画只能动可合成的属性。 + +**这条规则是真金白银换来的。** `.status-dot-online` 的呼吸光晕原先动画的是 +`box-shadow` —— 那是个**不可合成**的属性,每一帧都要整块重新光栅化。配上 +`infinite`,页面从打开那一刻起就永不空闲;而这些圆点背后又全是 +`backdrop-filter` 玻璃面板,每次重绘都连带把模糊整块重算。 + +CDP trace 实测(静置 6 秒,监控页上有 4 个这样的圆点): + + 动画动 box-shadow:光栅任务 2115 次 / 689ms,GPU 2115 次 / 699ms,主帧 790 + 动画只动 opacity :光栅任务 4 次 / 1ms,GPU 5 次 / 1ms,主帧 0 + +差三个数量级。在开发机上完全感觉不到,在慢机器上就是「点什么都卡、整台电脑发僵」, +而且**在浏览器 Network 面板里会伪装成「每个接口都下载 20 秒」** —— 主线程被占死, +响应体读不进来,那个阶段就一直挂着。 + +所以这里把规则钉住:凡是 `infinite` 的动画,它引用的 @keyframes 里只能出现 +opacity / transform 这类由合成器处理的属性。 +""" + +import pathlib +import re + +import pytest + +CSS = pathlib.Path("webui/src/index.css") +TAILWIND = pathlib.Path("webui/tailwind.config.ts") + +# 由合成器处理、改它们不触发重绘的属性。照抄 Chrome 的可合成动画白名单。 +COMPOSITABLE = {"opacity", "transform", "translate", "rotate", "scale"} + +# Tailwind 自带的那几个关键帧(pulse / spin / ping / bounce)全是可合成的, +# 定义在它的内部源码里,这里读不到 —— 遇到不认识的动画名就跳过。 +_ANIMATION_DECL = re.compile( + r"animation(?:-name)?\s*:\s*['\"]?([A-Za-z][\w-]*)", re.IGNORECASE +) +_PROPERTY = re.compile(r"([A-Za-z][A-Za-z-]*)\s*:") + + +def _blocks(text: str, opener: str) -> str: + """取出 text 里从 ``opener`` 开始的那个花括号区块(含嵌套)。""" + start = text.find(opener) + if start < 0: + return "" + start = text.find("{", start) + depth = 0 + for index in range(start, len(text)): + if text[index] == "{": + depth += 1 + elif text[index] == "}": + depth -= 1 + if depth == 0: + return text[start + 1 : index] + return "" + + +def _keyframes(source: str, is_css: bool) -> dict[str, str]: + """把 ``名字 -> 关键帧正文`` 收集出来。 + + CSS 用 ``@keyframes name { ... }``;Tailwind 配置里是 ``name: { ... }`` + 的对象字面量 —— 后者靠 camelCase 属性名(boxShadow 等)也能认出来。 + """ + region = source if is_css else _blocks(source, "keyframes") + found: dict[str, str] = {} + pattern = ( + re.compile(r"@keyframes\s+([\w-]+)\s*\{") + if is_css + else re.compile(r"([A-Za-z][\w-]*)\s*:\s*\{") + ) + for match in pattern.finditer(region): + name = match.group(1) + brace = region.find("{", match.start()) + depth = 0 + for index in range(brace, len(region)): + if region[index] == "{": + depth += 1 + elif region[index] == "}": + depth -= 1 + if depth == 0: + found[name] = region[brace + 1 : index] + break + return found + + +def _properties(body: str) -> set[str]: + """关键帧正文里出现的 CSS 属性名,camelCase 归一成 kebab-case。""" + names = set() + for raw in _PROPERTY.findall(body): + # boxShadow -> box-shadow;顺带丢掉百分比选择器残渣(100% 之类) + kebab = re.sub(r"(? list[tuple[str, str]]: + """所有写了 infinite 的动画,返回 (动画名, 出处)。""" + found = [] + for match in re.finditer(r"animation[^;{}]*infinite[^;{}]*", css, re.IGNORECASE): + names = _ANIMATION_DECL.findall(match.group(0)) + for name in names: + found.append((name, "index.css")) + anim_block = _blocks(tailwind, "animation") + for match in re.finditer(r"'([\w-]+)'\s*:\s*'([^']*infinite[^']*)'", anim_block): + names = _ANIMATION_DECL.findall(match.group(2)) + for name in names: + found.append((name, f"tailwind.config.ts ({match.group(1)})")) + return found + + +@pytest.mark.skipif(not CSS.exists(), reason="没有前端源码") +def test_infinite_animations_only_touch_compositable_properties(): + css = CSS.read_text(encoding="utf-8") + tailwind = TAILWIND.read_text(encoding="utf-8") if TAILWIND.exists() else "" + + known = {**_keyframes(css, True), **_keyframes(tailwind, False)} + assert known, "一个 @keyframes 都没解析出来 —— 解析器坏了,这条测试也就失去意义了" + + offenders = [] + for name, origin in _infinite_animations(css, tailwind): + body = known.get(name) + if body is None: + # Tailwind 自带的关键帧(pulse / spin / ping / bounce)都是可合成的。 + continue + bad = _properties(body) - COMPOSITABLE + if bad: + offenders.append(f"{name}({origin})动画了 {sorted(bad)}") + + assert not offenders, ( + "无限动画动了不可合成的属性,会让页面永远重绘(慢机器上表现为整台电脑卡、" + "且接口在 Network 面板里显示成「下载 20 秒」)。\n" + "要发光就把光晕画在伪元素上、动画只改 opacity —— 见 index.css 的 " + ".status-dot-online。\n " + "\n ".join(offenders) + ) + + +@pytest.mark.skipif(not CSS.exists(), reason="没有前端源码") +def test_the_online_dot_glow_is_pseudo_element_opacity(): + """把修好的写法钉住:光晕在 ::after 上,动画只动 opacity。""" + css = CSS.read_text(encoding="utf-8") + + assert ".status-dot-online::after" in css + assert "statusDotGlow" in css + + rule = css.split(".status-dot-online::after", 1)[1].split("}", 1)[0] + assert "box-shadow" in rule, "光晕本身还得在伪元素上画出来" + assert "animation" in rule + + glow = _keyframes(css, True)["statusDotGlow"] + assert _properties(glow) == {"opacity"}, ( + "呼吸光晕只能动画 opacity;换回 box-shadow 就是那个把整台电脑拖死的写法" + ) diff --git a/webui/src/index.css b/webui/src/index.css index 3a5cf7f..75a92dc 100644 --- a/webui/src/index.css +++ b/webui/src/index.css @@ -414,6 +414,7 @@ ============================ */ .status-dot { + position: relative; width: 8px; height: 8px; border-radius: 50%; @@ -421,8 +422,42 @@ .status-dot-online { background: rgb(var(--cyber-neon-green)); - box-shadow: 0 0 8px rgb(var(--cyber-neon-green) / 0.6); - animation: pulseGlow 2s ease-in-out infinite; +} + +/* + 「在线」圆点的呼吸光晕。 + + **绝不要把 box-shadow 放进无限动画里。** 这是踩过的坑,而且是量出来的: + box-shadow 不可合成,每一帧都要整块重新光栅化;动画写成 infinite,页面就从 + 打开那一刻起永不空闲。这个圆点在监控页上有好几处(登录态、webhook、每个任务卡、 + 常驻侧边栏),而它们背后又全是 backdrop-filter 玻璃面板 —— 每次重绘都连带把 + 模糊整块重算,4 个 8 像素的小点就足以让浏览器持续满负荷。 + + CDP trace 实测(静置 6 秒,页面上 4 个这样的圆点): + + 动画动 box-shadow :光栅任务 2115 次 / 689ms,GPU 2115 次 / 699ms,主帧 790 + 动画只动 opacity :光栅任务 4 次 / 1ms,GPU 5 次 / 1ms,主帧 0 + + 差三个数量级。所以光晕画在伪元素上,动画只改 opacity —— 那由合成器处理, + 不触发重绘。 +*/ +.status-dot-online::after { + content: ''; + position: absolute; + inset: 0; + border-radius: 50%; + box-shadow: + 0 0 6px rgb(var(--cyber-neon-green) / 0.9), + 0 0 14px rgb(var(--cyber-neon-green) / 0.5); + /* 明确要求提升为合成层,否则这个 8px 的伪元素未必会被提升, + opacity 动画就会退化成所属图层的重绘。 */ + will-change: opacity; + animation: statusDotGlow 2s ease-in-out infinite; +} + +@keyframes statusDotGlow { + 0%, 100% { opacity: 0.35; } + 50% { opacity: 1; } } .status-dot-offline { @@ -439,15 +474,6 @@ box-shadow: 0 0 8px rgb(var(--cyber-neon-pink) / 0.6); } -@keyframes pulseGlow { - 0%, 100% { - box-shadow: 0 0 5px currentColor; - } - 50% { - box-shadow: 0 0 15px currentColor, 0 0 25px currentColor; - } -} - /* ============================ Input Focus Ring ============================ */ diff --git a/webui/tailwind.config.ts b/webui/tailwind.config.ts index c024a6c..1da4f75 100644 --- a/webui/tailwind.config.ts +++ b/webui/tailwind.config.ts @@ -104,26 +104,22 @@ const config: Config = { animation: { 'slide-up': 'slideUp 0.3s ease-out forwards', 'pulse-fast': 'pulse 1.5s cubic-bezier(0.4, 0, 0.6, 1) infinite', - 'pulse-glow': 'pulseGlow 2s ease-in-out infinite', 'scanline': 'scanline 8s linear infinite', 'cursor-blink': 'cursorBlink 1s step-end infinite', - 'border-glow': 'borderGlow 3s linear infinite', 'accordion-down': 'accordion-down 0.2s ease-out', 'accordion-up': 'accordion-up 0.2s ease-out', }, + // 这里只剩动 opacity / transform 的无限动画。**无限动画不能动不可合成的属性** + // (box-shadow、border-color、width/height/left/top…)—— 那会让页面从打开起 + // 永远重绘。原来的 `pulse-glow`(box-shadow)和 `border-glow`(border-color) + // 就是这么两个坑,而它们并没人用,所以删掉了;要加"发光"效果请照 + // index.css 里 .status-dot-online 的做法:光晕画在伪元素上,动画只改 opacity。 + // tests/test_css_animations.py 会挡住这类写法再溜回来。 keyframes: { slideUp: { from: { opacity: '0', transform: 'translateY(10px)' }, to: { opacity: '1', transform: 'translateY(0)' }, }, - pulseGlow: { - '0%, 100%': { - boxShadow: '0 0 5px rgb(var(--cyber-neon-cyan) / 0.5), 0 0 10px rgb(var(--cyber-neon-cyan) / 0.3)' - }, - '50%': { - boxShadow: '0 0 15px rgb(var(--cyber-neon-cyan) / 0.8), 0 0 25px rgb(var(--cyber-neon-cyan) / 0.5), 0 0 35px rgb(var(--cyber-neon-cyan) / 0.3)' - }, - }, scanline: { '0%': { transform: 'translateY(-100%)' }, '100%': { transform: 'translateY(100vh)' }, @@ -132,10 +128,6 @@ const config: Config = { '0%, 100%': { opacity: '1' }, '50%': { opacity: '0' }, }, - borderGlow: { - '0%, 100%': { borderColor: 'rgb(var(--cyber-neon-cyan) / 0.3)' }, - '50%': { borderColor: 'rgb(var(--cyber-neon-cyan) / 0.6)' }, - }, 'accordion-down': { from: { height: '0' }, to: { height: 'var(--radix-accordion-content-height)' },