Skip to content

feat: add "animate mascots while expanded" setting (fixes hover lag) - #391

Closed
conghuhu wants to merge 1 commit into
wxtsky:mainfrom
conghuhu:feat/mascot-animation-expanded
Closed

conghuhu wants to merge 1 commit into
wxtsky:mainfrom
conghuhu:feat/mascot-animation-expanded

Conversation

@conghuhu

@conghuhu conghuhu commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

背景

v1.0.40 在鼠标 hover 展开灵动岛后会出现明显卡顿(外接 60Hz 屏幕 + 几个会话时尤其明显)。用 sample 对 CodeIsland 主线程采样(1ms 间隔)得到三组对照:

场景 主线程忙碌占比
收起 + 全部会话空闲 4.5%
hover 展开、鼠标完全静止 41.2%
hover 展开、鼠标在卡片上移动 66.9%

hover 期间热栈几乎全是 NSHostingView.layout → ViewGraphRootValueUpdater.render → AttributeGraph 布局 → RootGeometry/LayoutEngineBox.sizeThatFits,CodeIsland 自己的业务代码只占主线程采样的 ~0.1%——也就是说不是某个函数慢,而是整个面板的 SwiftUI layout 在被每帧重跑。

根因

吉祥物 TimelineView 目前只在「收起且所有会话 idle」时停用(#299):

  • MascotAnimationGate.shouldAnimate = visible && awake && !isIdleSettled && !reduceMotion;
  • PanelWindowController.isIslandSettled() 对 surface.isExpanded 恒返回 false。

所以 hover 一展开,动画立刻恢复;而一个活跃的 TimelineView 会把 SwiftUI 的更新机制挂在 display link 上,每个 display cycle 都会重跑整个面板的 layout pass(MascotAnimationGate.swift 里 #299 的注释已经写明这个结论)。展开面板是最大的一棵树,60Hz 下每帧约 6–11ms 主线程,鼠标移动时再叠加 hover 动画,就掉帧/发涩。

改动

新增 设置 → 角色(Mascots)→「展开面板时播放吉祥物动画」,默认开启(不改变现有行为):

image
  • 关闭后:面板展开期间吉祥物保持静止,收起状态的动画不受影响;
  • MascotAnimationGate 增加 isExpanded / animatesWhenExpanded 两个状态,恢复播放时 bump epoch 重新锚定 schedules;
  • PanelWindowController.syncMascotAnimationGate() 在面板显隐、展开/收起、设置变更时统一同步给 gate;
  • 设置页开关 + 7 种语言文案 + README 设置表说明;
  • 新增 3 个 MascotAnimationGateTests 用例:默认开启、关闭后展开静止但收起仍动画、恢复时 epoch 重锚。

验证

  • swift build -c release --build-system native --arch arm64 通过;
  • 打包为 App 后以 CODEISLAND_ISOLATED=1 启动验证通过;
  • 在同一台机器上关掉该开关后,hover 展开恢复顺滑(设置面板里能看到新开关);
  • 新增的单元测试覆盖 gate 判定与 epoch 重新锚定逻辑(本机未跑完整 swift test,仓库也没有 PR CI 测试 workflow)。

备注

默认开启、只是给用户一个出口;如果后续想保留动画又彻底消除这部分开销,可以把吉祥物动画移出 SwiftUI layout 图(例如每个 mascot 独立 NSHostingView,或用 CADisplayLink + 自绘),这次先做最小、可回退的一步。

The mascot TimelineViews only go static while the island is collapsed and
every session is idle. Expanding the panel (hover, approval, completion)
re-enables them, and a live TimelineView keeps SwiftUI's update machinery
on a display link: every display cycle re-runs the whole panel's layout
pass (wxtsky#299). The expanded panel is the largest tree, so hovering it costs
~6-11ms of main-thread layout per frame at 60Hz and drops frames.

Add a Settings -> Mascots switch, on by default, that holds the mascots
still while the panel is expanded instead:

- MascotAnimationGate learns `isExpanded` / `animatesWhenExpanded`, and
  re-anchors the schedules (epoch bump) when animation resumes.
- PanelWindowController.syncMascotAnimationGate() feeds the gate the
  panel visibility/surface and the setting on every relevant change.
- Mascots page toggle + 7 locales + README settings-table note.
- Three gate tests: on by default, off holds while expanded but the
  collapsed bar still animates, and resuming re-anchors the schedules.
@conghuhu

Copy link
Copy Markdown
Contributor Author

@wxtsky cc

wxtsky added a commit that referenced this pull request Oct 28, 2026
…lay frame

With the island open on a ProMotion screen, CodeIsland laid the whole
panel out 120 times a second, every session card, the header and the
footer, for as long as a mascot moved (#381, #391). The mascots drew on
TimelineView(.periodic) at 20 or 8 fps, but a TimelineView whose entries
are less than about a quarter second apart updates on every display
frame, whatever its schedule says, and each update re-ran the panel's
layout from its root. The glow behind a waiting mascot (a repeatForever
animation in 18 of the mascots, pulsing on with Reduce Motion on) and
the shimmer on "Thinking…" and on a running tool's name did the same.
Offscreen, where no display paces it, v1.0.40 lays the panel out
6,000-47,000 times a second; v1.0.35 did too.

FrameClock replaces all of it: one timer, 20 ticks a second on a grid of
the reference date, running only while something on screen moves. Every
clock due at a tick sees the same instant, so a tick is one wakeup and
one update of the island however many mascots and labels move; clocks
ticking on their own woke the island several times a frame. Sleep scenes
ask for 0.12 s and tick on every other frame of the rest.

The glow runs on the clock too (MascotAlertGlow: the same once-a-second
pulse, from full strength), and pulses through its opacity so a frame
doesn't blur the circle again; four waiting mascots went from 709M to
87M instructions a second offscreen. It holds still with Reduce Motion
or with the panel hidden, and so does the shimmer. Animation speed "Off"
holds the mascots still instead of redrawing the same frame. The iOS
companion keeps TimelineView.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
wxtsky added a commit that referenced this pull request Oct 28, 2026
… idle removal and more

Unreleased notes for this round: the island's 20 fps clock (#381, #391),
"Always allow" holding in its session and Codex rules Codex can use
(#380), shortcuts and pinned displays with a second monitor (#384), pi
and Oh My Pi ringing once a task (#386), the Claude Desktop answer and
background compaction fixes (#389, #388), removing idle cards (#385),
the Codex Desktop thread jump (#387), the DeepSeek whale (#390) and the
Grok / DeepSeek Harness sections in Group by CLI.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wxtsky

wxtsky commented Oct 28, 2026

Copy link
Copy Markdown
Owner

感谢 @conghuhu!你的采样和诊断方向完全正确:吉祥物的 TimelineView 让 SwiftUI 每个显示周期都重跑整个面板的 layout。顺着这个思路我们继续往下查,结论是开关治不了根:

  • 每个在动的元素都会让面板每帧重排一次,不只是吉祥物。等待中吉祥物身后的光晕(repeatForever)、"思考中"和工具名上的流光、收起状态栏上闪动的提醒图标(symbolEffect(.pulse, .repeating))都是如此。实测间隔 ≤ 0.25 秒的 TimelineView 不管 schedule 写多少,都是每帧更新。
  • 所以关掉这个开关后,展开面板仍然每秒排版 120 次(ProMotion)。实测 multi 场景从 468M 指令/秒降到 196M,排版次数依旧 120 次/秒。
  • 你在备注里提的"每个 mascot 独立 NSHostingView"我们也试了:嵌套 host 的更新会经 AppKitPlatformViewHost 反过来让外层面板失效,实测比 main 还多(629–673M/s 对 464–476M/s)。

最终的修法(已进 main:c360cfd、c22e907、8146525)是让岛上所有会动的东西共用一个每秒 20 帧的时钟(FrameClock),并且每一帧只改变"怎么画",不改变排版。实测展开面板从 462–469M 指令/秒降到 72–76M/秒(−84%),排版从每秒 120 次降到 20 次;收起且有会话在跑时减半;空闲时依然为零。在这个基础上,全部动画开着也比 main 关掉开关还省。想彻底关掉动画的用户,可以用系统的"减弱动态效果"(现在也覆盖光晕、流光和徽标)或"动画速度:关闭"。

所以这个 PR 就不合了,先关掉。非常感谢你的采样数据,它直接指向了根因。新的 changelog 条目里也注明了 #391。

@wxtsky wxtsky closed this Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants