cc-pets 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/CC_BRIDGE.md +5 -2
  2. package/CC_BRIDGE.zh-CN.md +4 -2
  3. package/CHANGELOG.md +85 -0
  4. package/CHANGELOG.zh-CN.md +85 -0
  5. package/CONTRIBUTING.md +28 -11
  6. package/CONTRIBUTING.zh-CN.md +23 -11
  7. package/README.md +127 -27
  8. package/README.zh-CN.md +104 -30
  9. package/Resources/zh-Hans.lproj/Localizable.strings +573 -17
  10. package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
  11. package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +89 -31
  12. package/Sources/CCPets/{CCPetsAppDelegate+Bridge.m → App/CCPetsAppDelegate+Bridge.m} +35 -24
  13. package/Sources/CCPets/{CCPetsAppDelegate+Pets.m → App/CCPetsAppDelegate+Pets.m} +18 -4
  14. package/Sources/CCPets/{CCPetsAppDelegate+Private.h → App/CCPetsAppDelegate+Private.h} +90 -21
  15. package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +238 -0
  16. package/Sources/CCPets/{CCPetsAppDelegate+Quota.m → App/CCPetsAppDelegate+Quota.m} +47 -32
  17. package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +269 -0
  18. package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +13 -7
  19. package/Sources/CCPets/{CCPetsAppDelegate+Update.m → App/CCPetsAppDelegate+Update.m} +71 -47
  20. package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
  21. package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +16 -2
  22. package/Sources/CCPets/{CCPetsAppDelegate.m → App/CCPetsAppDelegate.m} +110 -12
  23. package/Sources/CCPets/App/CCPetsOpenCommand.h +13 -0
  24. package/Sources/CCPets/App/CCPetsOpenCommand.m +99 -0
  25. package/Sources/CCPets/{main.m → App/main.m} +68 -1
  26. package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
  27. package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +32 -17
  28. package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
  29. package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +792 -0
  30. package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +60 -0
  31. package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +558 -0
  32. package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
  33. package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +501 -0
  34. package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +103 -0
  35. package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +779 -0
  36. package/Sources/CCPets/Integrations/CCPetsCLISupport.h +75 -0
  37. package/Sources/CCPets/Integrations/CCPetsCLISupport.m +342 -0
  38. package/Sources/CCPets/Integrations/CCPetsEvents.h +15 -0
  39. package/Sources/CCPets/{CCPetsEvents.m → Integrations/CCPetsEvents.m} +23 -0
  40. package/Sources/CCPets/Integrations/CCPetsIntegrations.h +51 -0
  41. package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
  42. package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
  43. package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
  44. package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +42 -12
  45. package/Sources/CCPets/Pet/CCPetsImageLoader.h +5 -0
  46. package/Sources/CCPets/{CCPetsImageLoader.m → Pet/CCPetsImageLoader.m} +10 -2
  47. package/Sources/CCPets/Pet/CCPetsPetSize.h +10 -0
  48. package/Sources/CCPets/Pet/CCPetsPetSize.m +21 -0
  49. package/Sources/CCPets/Pet/CCPetsPetStore.h +12 -0
  50. package/Sources/CCPets/Pet/CCPetsPetStore.m +476 -0
  51. package/Sources/CCPets/{CCPetsPhrases.h → Pet/CCPetsPhrases.h} +18 -2
  52. package/Sources/CCPets/{CCPetsPhrases.m → Pet/CCPetsPhrases.m} +53 -22
  53. package/Sources/CCPets/{CCPetsPhrasesEditor.m → Pet/CCPetsPhrasesEditor.m} +42 -2
  54. package/Sources/CCPets/Pet/PetView+Interaction.m +207 -0
  55. package/Sources/CCPets/Pet/PetView+Menu.m +598 -0
  56. package/Sources/CCPets/Pet/PetView+MicroBehavior.m +234 -0
  57. package/Sources/CCPets/Pet/PetView+Private.h +81 -0
  58. package/Sources/CCPets/{PetView.h → Pet/PetView.h} +3 -5
  59. package/Sources/CCPets/Pet/PetView.m +635 -0
  60. package/Sources/CCPets/Support/CCPetsDoctor.h +25 -0
  61. package/Sources/CCPets/Support/CCPetsDoctor.m +632 -0
  62. package/Sources/CCPets/Support/CCPetsJSON.h +27 -0
  63. package/Sources/CCPets/Support/CCPetsJSON.m +397 -0
  64. package/Sources/CCPets/{CCPetsL10n.h → Support/CCPetsL10n.h} +3 -1
  65. package/Sources/CCPets/{CCPetsL10n.m → Support/CCPetsL10n.m} +30 -1
  66. package/Sources/CCPets/Support/CCPetsPreferences.h +31 -0
  67. package/Sources/CCPets/Support/CCPetsPreferences.m +85 -0
  68. package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
  69. package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
  70. package/Sources/CCPets/UI/CCPetsFormBuilder.h +42 -0
  71. package/Sources/CCPets/UI/CCPetsFormBuilder.m +268 -0
  72. package/Sources/CCPets/{CCPetsGlassView.h → UI/CCPetsGlassView.h} +5 -1
  73. package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +25 -7
  74. package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
  75. package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
  76. package/Sources/CCPets/UI/CCPetsSettingsPage.h +62 -0
  77. package/Sources/CCPets/UI/CCPetsSettingsPage.m +917 -0
  78. package/Sources/CCPets/UI/CCPetsUIStyle.h +132 -0
  79. package/Sources/CCPets/UI/CCPetsUIStyle.m +816 -0
  80. package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
  81. package/Sources/CCPets/UI/CCPetsUsageChartView.m +284 -0
  82. package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
  83. package/Sources/CCPets/UI/CCPetsUsagePage.m +717 -0
  84. package/Sources/CCPets/UI/CCPetsUsageShared.h +53 -0
  85. package/Sources/CCPets/UI/CCPetsUsageStore.m +120 -0
  86. package/Sources/CCPets/UI/CCPetsUsageWidgets.h +30 -0
  87. package/Sources/CCPets/UI/CCPetsUsageWidgets.m +201 -0
  88. package/Sources/CCPets/UI/CCPetsWorkbench.h +40 -0
  89. package/Sources/CCPets/UI/CCPetsWorkbenchPages.h +13 -0
  90. package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +783 -0
  91. package/Sources/CCPets/{MenuChoiceRow.m → UI/MenuChoiceRow.m} +3 -0
  92. package/Sources/CCPets/UI/MenuPetSizeView.h +5 -0
  93. package/Sources/CCPets/UI/MenuPetSizeView.m +73 -0
  94. package/Sources/CCPets/{MenuToggleSwitch.m → UI/MenuToggleSwitch.m} +1 -0
  95. package/Sources/CCPets/{CCPetsCodexRateLimits.h → Usage/CCPetsCodexRateLimits.h} +1 -0
  96. package/Sources/CCPets/{CCPetsCodexRateLimits.m → Usage/CCPetsCodexRateLimits.m} +10 -3
  97. package/Sources/CCPets/{CCPetsQuotaHistory.h → Usage/CCPetsQuotaHistory.h} +3 -0
  98. package/Sources/CCPets/{CCPetsQuotaHistory.m → Usage/CCPetsQuotaHistory.m} +58 -0
  99. package/Sources/CCPets/Usage/CCPetsUsage+ClaudeReader.m +436 -0
  100. package/Sources/CCPets/Usage/CCPetsUsage+CodexReader.m +392 -0
  101. package/Sources/CCPets/Usage/CCPetsUsage+CodexSessions.m +484 -0
  102. package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +96 -0
  103. package/Sources/CCPets/Usage/CCPetsUsage+Models.m +285 -0
  104. package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
  105. package/Sources/CCPets/Usage/CCPetsUsage.h +57 -0
  106. package/Sources/CCPets/Usage/CCPetsUsage.m +225 -0
  107. package/Sources/CCPets/Usage/CCPetsUsageMode.h +10 -0
  108. package/Sources/CCPets/Usage/CCPetsUsageMode.m +163 -0
  109. package/Sources/CCPets/{CCPetsUsageMonitor.h → Usage/CCPetsUsageMonitor.h} +3 -0
  110. package/Sources/CCPets/{CCPetsUsageMonitor.m → Usage/CCPetsUsageMonitor.m} +163 -6
  111. package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +328 -0
  112. package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +103 -0
  113. package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +616 -0
  114. package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
  115. package/Sources/CCPets/Usage/QuotaDashboardView.m +587 -0
  116. package/THIRD_PARTY_NOTICES.md +1 -1
  117. package/THIRD_PARTY_NOTICES.zh-CN.md +1 -1
  118. package/bin/cc-pets +8 -6
  119. package/bin/claude-with-pet +10 -2
  120. package/bin/codex-with-pet +13 -5
  121. package/package.json +1 -1
  122. package/scripts/build.sh +7 -3
  123. package/scripts/i18n.zsh +9 -52
  124. package/scripts/install-app.sh +4 -0
  125. package/scripts/install-shell-integration.sh +17 -10
  126. package/scripts/uninstall-shell-integration.sh +6 -1
  127. package/Sources/CCPets/CCPetsAppDelegate+Settings.m +0 -226
  128. package/Sources/CCPets/CCPetsEvents.h +0 -10
  129. package/Sources/CCPets/CCPetsImageLoader.h +0 -4
  130. package/Sources/CCPets/CCPetsUsage.h +0 -36
  131. package/Sources/CCPets/CCPetsUsage.m +0 -2069
  132. package/Sources/CCPets/PetView.m +0 -1806
  133. package/Sources/CCPets/QuotaDashboardView.m +0 -1145
  134. package/scripts/bridge/cli.mjs +0 -237
  135. package/scripts/bridge/core.mjs +0 -228
  136. package/scripts/bridge/hook.mjs +0 -150
  137. package/scripts/bridge/install.mjs +0 -311
  138. package/scripts/bridge/mcp-server.mjs +0 -308
  139. package/scripts/bridge/options.mjs +0 -87
  140. package/scripts/bridge/process.mjs +0 -151
  141. package/scripts/bridge/reservations.mjs +0 -248
  142. package/scripts/bridge/store.mjs +0 -433
  143. package/scripts/bridge/watch.mjs +0 -69
  144. package/scripts/check-l10n.mjs +0 -168
  145. package/scripts/configure-updater.mjs +0 -43
  146. package/scripts/detect-cli.mjs +0 -68
  147. package/scripts/doctor.mjs +0 -470
  148. package/scripts/i18n.mjs +0 -122
  149. package/scripts/install-claude-hooks.mjs +0 -253
  150. package/scripts/install-codex-hooks.mjs +0 -78
  151. package/scripts/integration-markers.mjs +0 -51
  152. package/scripts/locales/zh-Hans.json +0 -366
  153. package/scripts/pet-store.mjs +0 -415
  154. package/scripts/uninstall-integrations.mjs +0 -205
  155. /package/Sources/CCPets/{CCPetsVersion.h → App/CCPetsVersion.h} +0 -0
  156. /package/Sources/CCPets/{CCPetsVersion.m → App/CCPetsVersion.m} +0 -0
  157. /package/Sources/CCPets/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +0 -0
  158. /package/Sources/CCPets/{CCPetsPhrasesEditor.h → Pet/CCPetsPhrasesEditor.h} +0 -0
  159. /package/Sources/CCPets/{CCPetsPreviews.h → Pet/CCPetsPreviews.h} +0 -0
  160. /package/Sources/CCPets/{CCPetsPreviews.m → Pet/CCPetsPreviews.m} +0 -0
  161. /package/Sources/CCPets/Resources/{/351/273/230/350/256/244.webp" → ByteMochi.webp} +0 -0
  162. /package/Sources/CCPets/{CCPetsCleanup.h → Support/CCPetsCleanup.h} +0 -0
  163. /package/Sources/CCPets/{CCPetsCleanup.m → Support/CCPetsCleanup.m} +0 -0
  164. /package/Sources/CCPets/{CCPetsPaths.h → Support/CCPetsPaths.h} +0 -0
  165. /package/Sources/CCPets/{CCPetsPaths.m → Support/CCPetsPaths.m} +0 -0
  166. /package/Sources/CCPets/{CCPetsSystemMonitor.h → Support/CCPetsSystemMonitor.h} +0 -0
  167. /package/Sources/CCPets/{CCPetsSystemMonitor.m → Support/CCPetsSystemMonitor.m} +0 -0
  168. /package/Sources/CCPets/{CCPetsGlassMenu.h → UI/CCPetsGlassMenu.h} +0 -0
  169. /package/Sources/CCPets/{CCPetsGlassMenu.m → UI/CCPetsGlassMenu.m} +0 -0
  170. /package/Sources/CCPets/{MenuChoiceRow.h → UI/MenuChoiceRow.h} +0 -0
  171. /package/Sources/CCPets/{MenuHintView.h → UI/MenuHintView.h} +0 -0
  172. /package/Sources/CCPets/{MenuHintView.m → UI/MenuHintView.m} +0 -0
  173. /package/Sources/CCPets/{MenuToggleSwitch.h → UI/MenuToggleSwitch.h} +0 -0
@@ -0,0 +1,635 @@
1
+ #import "PetView+Private.h"
2
+ #import "CCPetsPaths.h"
3
+
4
+ // 呼吸:只向上浮,不做上下对称摆动。宠物视图在 230×170 的窗口里位于 (43,0,144,150),
5
+ // 下方一点余量都没有,往下 1px 就被窗口裁掉。静止位不变,所以离屏基准图不受影响。
6
+ static const CGFloat PetBreathRise = 1.5;
7
+ static const NSTimeInterval PetBreathPeriod = 3.6;
8
+
9
+ @implementation PetView
10
+ - (instancetype)initWithFrame:(NSRect)frame sheet:(NSImage *)sheet rowCount:(NSInteger)rowCount {
11
+ if ((self = [super initWithFrame:frame])) {
12
+ _sheet = sheet;
13
+ _spriteRowCount = rowCount;
14
+ _lookDirection = -1;
15
+ _frames = @[@0, @1, @2, @3, @4, @5];
16
+ _frameHolds = @[@1, @1, @1, @1, @1, @1];
17
+ _petMenuPreviewCache = [NSCache new];
18
+ _petMenuPreviewCache.countLimit = 24;
19
+ _petMenuPreviewCache.totalCostLimit = 4 * 1024 * 1024;
20
+ _cellCache = [NSCache new];
21
+ _cellCache.totalCostLimit = 6 * 1024 * 1024;
22
+ self.wantsLayer = YES;
23
+ _spriteLayer = [CALayer layer];
24
+ // 像素素材放大必须用最近邻,等价于原来 drawRect: 里的 NSImageInterpolationNone。
25
+ _spriteLayer.magnificationFilter = kCAFilterNearest;
26
+ _spriteLayer.minificationFilter = kCAFilterNearest;
27
+ // contentsScale 由 rebuildSpriteCache 按 backingScaleFactor 定,这里不预设。
28
+ // 锚点放在脚底:呼吸和之后的落地 squash 都该以脚为支点,锚在正中会变成
29
+ // "整体缩放",看着像忽远忽近而不是有重量。
30
+ _spriteLayer.anchorPoint = CGPointMake(0.5, 0.0);
31
+ [self.layer addSublayer:_spriteLayer];
32
+ [self layoutSpriteLayer];
33
+ [self rebuildSpriteCache];
34
+ [NSNotificationCenter.defaultCenter addObserver:self
35
+ selector:@selector(powerStateDidChange:)
36
+ name:NSProcessInfoPowerStateDidChangeNotification object:nil];
37
+ [NSWorkspace.sharedWorkspace.notificationCenter addObserver:self
38
+ selector:@selector(powerStateDidChange:)
39
+ name:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification object:nil];
40
+ [self updatePowerPreferences];
41
+ [self updateFrameTimer];
42
+ NSTrackingArea *tracking = [[NSTrackingArea alloc] initWithRect:NSZeroRect
43
+ options:NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited |
44
+ NSTrackingActiveAlways | NSTrackingInVisibleRect
45
+ owner:self userInfo:nil];
46
+ [self addTrackingArea:tracking];
47
+ [self rebuildCapabilities];
48
+ [self updateLookTracking];
49
+ }
50
+ return self;
51
+ }
52
+ // 只碰 ivar,不走属性 setter:setter 会调用 updateFrameTimer,在 dealloc 期间
53
+ // 重新排一个定时器出来。
54
+ - (void)dealloc {
55
+ [_frameTimer invalidate];
56
+ if (_globalMouseMonitor) [NSEvent removeMonitor:_globalMouseMonitor];
57
+ if (_localMouseMonitor) [NSEvent removeMonitor:_localMouseMonitor];
58
+ [NSNotificationCenter.defaultCenter removeObserver:self];
59
+ [NSWorkspace.sharedWorkspace.notificationCenter removeObserver:self];
60
+ }
61
+ - (void)powerStateDidChange:(NSNotification *)notification {
62
+ [self updatePowerPreferences];
63
+ }
64
+ - (void)updatePowerPreferences {
65
+ BOOL lowPower = NSProcessInfo.processInfo.lowPowerModeEnabled;
66
+ BOOL reduceMotion = NSWorkspace.sharedWorkspace.accessibilityDisplayShouldReduceMotion;
67
+ if (self.lowPowerMode == lowPower && self.reduceMotion == reduceMotion) return;
68
+ self.lowPowerMode = lowPower;
69
+ self.reduceMotion = reduceMotion;
70
+ [self.frameTimer invalidate];
71
+ self.frameTimer = nil;
72
+ [self updateFrameTimer];
73
+ [self updateBreathingAnimation];
74
+ }
75
+ // 定时器只看可见性。十六向跟随期间它也要转——注意力现在是有时限的,到点必须有人
76
+ // 把它收回来,而 updateLookForScreenPoint: 只在鼠标移动时才被调用,鼠标一停就没人
77
+ // 收尾了。跟随期间 nextFrame: 除了这个到期检查之外仍然什么都不做。
78
+ - (void)updateFrameTimer {
79
+ BOOL shouldRun = !self.animationSuspended;
80
+ if (shouldRun == self.frameTimer.isValid) return;
81
+ if (!shouldRun) {
82
+ [self.frameTimer invalidate];
83
+ self.frameTimer = nil;
84
+ return;
85
+ }
86
+ // block 版定时器 + weak self,避免 NSTimer 持有 target 造成 PetView 永不释放。
87
+ __weak PetView *weakSelf = self;
88
+ NSTimeInterval interval = PetFrameInterval;
89
+ if (self.lowPowerMode) interval *= 1.5;
90
+ if (self.reduceMotion) interval *= 2.0;
91
+ // 必须注册到 common modes:performWindowDragWithEvent: 期间主 runloop 处于
92
+ // NSEventTrackingRunLoopMode,只挂在默认 mode 上的定时器不会触发,宠物会
93
+ // 定格在拖动起手的那一帧上,直到松手才继续动。
94
+ self.frameTimer = [NSTimer timerWithTimeInterval:interval repeats:YES
95
+ block:^(NSTimer *timer) { [weakSelf nextFrame:timer]; }];
96
+ self.frameTimer.tolerance = interval * 0.25;
97
+ [NSRunLoop.mainRunLoop addTimer:self.frameTimer forMode:NSRunLoopCommonModes];
98
+ }
99
+ - (void)setLookDirection:(NSInteger)value {
100
+ if (_lookDirection == value) return;
101
+ _lookDirection = value;
102
+ [self updateFrameTimer];
103
+ }
104
+ - (void)setAnimationSuspended:(BOOL)suspended {
105
+ if (_animationSuspended == suspended) return;
106
+ _animationSuspended = suspended;
107
+ [self updateFrameTimer];
108
+ // 停表只管得住 NSTimer 驱动的逐帧动画。呼吸、弹簧这些走 CA,时钟在 render server
109
+ // 那边,屏幕睡了照跑——必须把这一层的本地时间也冻住,否则 P0 省下来的电又漏回去。
110
+ [self setSpriteLayerPaused:suspended];
111
+ if (suspended) {
112
+ // 屏幕睡了就把栅格化缓存整个放掉:这是常驻内存里最大的一块,而睡眠期间一格都用不上。
113
+ // 醒来重新栅格化只是几毫秒的事,换十几兆常驻内存很划算。
114
+ [self.cellCache removeAllObjects];
115
+ } else {
116
+ [self updateSpriteContents];
117
+ }
118
+ }
119
+ // 冻结/恢复 layer 自己的时间轴。恢复时要把暂停期间流逝的时间补回 beginTime,
120
+ // 否则动画会从"睡了多久就跳多远"的位置继续。
121
+ - (void)setSpriteLayerPaused:(BOOL)paused {
122
+ CALayer *layer = self.spriteLayer;
123
+ if (!layer) return;
124
+ if (paused) {
125
+ if (layer.speed == 0.0) return;
126
+ CFTimeInterval now = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
127
+ layer.speed = 0.0;
128
+ layer.timeOffset = now;
129
+ } else {
130
+ if (layer.speed != 0.0) return;
131
+ CFTimeInterval pausedAt = layer.timeOffset;
132
+ layer.speed = 1.0;
133
+ layer.timeOffset = 0.0;
134
+ layer.beginTime = 0.0;
135
+ layer.beginTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedAt;
136
+ }
137
+ }
138
+ - (void)startLookTracking {
139
+ if (!self.hasLookRows || self.globalMouseMonitor || self.localMouseMonitor) return;
140
+ __weak PetView *weakSelf = self;
141
+ self.globalMouseMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskMouseMoved
142
+ handler:^(NSEvent *event) {
143
+ [weakSelf updateLookForScreenPoint:NSEvent.mouseLocation];
144
+ }];
145
+ self.localMouseMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskMouseMoved
146
+ handler:^NSEvent *(NSEvent *event) {
147
+ [weakSelf updateLookForScreenPoint:NSEvent.mouseLocation];
148
+ return event;
149
+ }];
150
+ }
151
+ - (void)stopLookTracking {
152
+ if (self.globalMouseMonitor) {
153
+ [NSEvent removeMonitor:self.globalMouseMonitor];
154
+ self.globalMouseMonitor = nil;
155
+ }
156
+ if (self.localMouseMonitor) {
157
+ [NSEvent removeMonitor:self.localMouseMonitor];
158
+ self.localMouseMonitor = nil;
159
+ }
160
+ self.lookDirection = -1;
161
+ }
162
+ - (void)updateLookTracking {
163
+ if (self.hasLookRows) [self startLookTracking];
164
+ else [self stopLookTracking];
165
+ }
166
+ // 注意力有惯性也有衰减,这是活物和传感器的分水岭。
167
+ //
168
+ // 改造前是鼠标一动就实时跟随——那是摄像头。现在是:多数移动它压根不理会,偶尔才
169
+ // "注意到",看一阵子就失去兴趣回正,而且短期内不会再被同一波移动勾走。
170
+ // 副作用是开销大幅下降:原来屏幕上每个鼠标事件都要换一次朝向定格并重绘,现在只有
171
+ // 真正处于注意状态的那几秒才跟。
172
+ - (void)releaseAttention {
173
+ if (self.lookDirection < 0) return;
174
+ self.lookDirection = -1;
175
+ self.attentionCooldownUntil = CFAbsoluteTimeGetCurrent() +
176
+ 4.0 + 8.0 * (arc4random_uniform(1000) / 1000.0);
177
+ [self playRow:0 throughFrame:5 oneShot:NO];
178
+ }
179
+ - (void)updateLookForScreenPoint:(NSPoint)screenPoint {
180
+ if (!self.hasLookRows || self.draggingPet || self.oneShot || self.agentActive ||
181
+ self.microActive || !self.window) return;
182
+ NSTimeInterval now = CFAbsoluteTimeGetCurrent();
183
+ if (self.lookDirection < 0) {
184
+ if (now < self.attentionCooldownUntil) return;
185
+ // 绝大多数移动都不理会。这一条就是"惰性"本身。
186
+ if (arc4random_uniform(100) >= 12) return;
187
+ self.attentionUntil = now + 3.0 + 5.0 * (arc4random_uniform(1000) / 1000.0);
188
+ }
189
+
190
+ NSPoint windowPoint = [self.window convertPointFromScreen:screenPoint];
191
+ NSPoint localPoint = [self convertPoint:windowPoint fromView:nil];
192
+ if (NSPointInRect(localPoint, self.bounds)) return;
193
+
194
+ NSPoint center = [self convertPoint:NSMakePoint(NSMidX(self.bounds), NSMidY(self.bounds)) toView:nil];
195
+ center = [self.window convertPointToScreen:center];
196
+ CGFloat dx = screenPoint.x - center.x;
197
+ CGFloat dy = screenPoint.y - center.y;
198
+ if (hypot(dx, dy) <= 1.0) return;
199
+
200
+ CGFloat degrees = fmod(atan2(dx, dy) * 180.0 / M_PI + 360.0, 360.0);
201
+ NSInteger direction = ((NSInteger)llround(degrees / 22.5)) % 16;
202
+ if (direction == self.lookDirection) return;
203
+
204
+ self.lookDirection = direction;
205
+ self.rowIndex = 9 + direction / 8;
206
+ self.frameIndex = direction % 8;
207
+ self.needsDisplay = YES;
208
+ }
209
+ - (NSArray<NSNumber *> *)framesFromZeroThrough:(NSInteger)last {
210
+ NSMutableArray<NSNumber *> *result = [NSMutableArray arrayWithCapacity:last + 1];
211
+ for (NSInteger index = 0; index <= last; index++) [result addObject:@(index)];
212
+ return result;
213
+ }
214
+ // 全 1 的停留表 = 原来的匀速播放。
215
+ - (NSArray<NSNumber *> *)uniformHoldsForCount:(NSUInteger)count {
216
+ NSMutableArray<NSNumber *> *result = [NSMutableArray arrayWithCapacity:count];
217
+ for (NSUInteger index = 0; index < count; index++) [result addObject:@1];
218
+ return result;
219
+ }
220
+ // 待机的停留表:整轮统一放慢或加快,**绝不定格某一帧**。
221
+ //
222
+ // 试过"随机挑一帧长停 8~18 拍"做发呆,实测很别扭,别改回去。原因是素材不受控:
223
+ // 每套素材的待机行内容都不一样,第 0 帧也不保证是自然站姿,随便定格一帧经常停在
224
+ // 动作做到一半的姿势上,读起来是"动画卡住了"而不是"它在休息"。
225
+ // 改成只调速度:慢下来同样能读出"它安静了",而且因为始终在动,任何素材都不会翻车。
226
+ //
227
+ // 速度在 1~3 拍/帧之间随机游走(0.22~0.66s 一帧),并在一轮之内从上一档平滑过渡到
228
+ // 新一档,避免在循环接缝处突然变速。上限压在 3 拍,再慢就接近定格了。
229
+ // 现在返回匀速,等于关掉了这个行为。frameHolds 机制本身保留:微动层要用它做
230
+ // "慢放一个已有动作"和"定格朝向若干拍",而且全 1 时行为和改造前完全一致,留着零成本。
231
+ //
232
+ // 两版都试过,都不行,别再往回走:
233
+ // 1. 随机挑一帧长停 8~18 拍做"发呆"——素材不受控,第 0 帧也不保证是自然站姿,
234
+ // 经常停在动作做到一半的姿势上,读起来是"动画卡住了"。
235
+ // 2. 整轮 1~3 拍随机游走做"慢下来"——同样是在改一条连续动画的播放节奏,
236
+ // 观感是掉帧,不是休息。
237
+ // 结论:待机行本身已经是完整循环,动它的节奏一律读成故障。待机的活物感要靠
238
+ // **往流里插入有始有终的动作**(微动层),而不是改流本身的速度。
239
+ - (NSArray<NSNumber *> *)idleHoldsForCount:(NSUInteger)count {
240
+ return [self uniformHoldsForCount:count];
241
+ }
242
+ - (BOOL)isIdleRow:(NSInteger)row {
243
+ return row == 0;
244
+ }
245
+ - (void)playRow:(NSInteger)row throughFrame:(NSInteger)last oneShot:(BOOL)oneShot {
246
+ if (!oneShot && !self.oneShot && self.rowIndex == row) return;
247
+ // 任何外部动作(悬停、拖动、agent 事件)都直接接管,微动无条件让位。
248
+ if (self.microActive) {
249
+ self.microActive = NO;
250
+ [self scheduleNextMicroBehavior];
251
+ }
252
+ self.lookDirection = -1;
253
+ self.frameRows = nil;
254
+ self.rowIndex = row;
255
+ self.frames = [self framesFromZeroThrough:last];
256
+ self.frameHolds = [self isIdleRow:row] && !oneShot
257
+ ? [self idleHoldsForCount:self.frames.count]
258
+ : [self uniformHoldsForCount:self.frames.count];
259
+ self.holdTicks = 0;
260
+ self.sequenceIndex = 0;
261
+ self.frameIndex = 0;
262
+ self.oneShot = oneShot;
263
+ self.needsDisplay = YES;
264
+ }
265
+ - (BOOL)isPocketPoint:(NSPoint)point {
266
+ CGFloat centerX = NSMidX(self.bounds);
267
+ CGFloat centerY = NSHeight(self.bounds) * 0.32;
268
+ CGFloat dx = (point.x - centerX) / (NSWidth(self.bounds) * 0.22);
269
+ CGFloat dy = (point.y - centerY) / (NSHeight(self.bounds) * 0.16);
270
+ return dx * dx + dy * dy <= 1.0;
271
+ }
272
+ - (BOOL)updatePocketHoverForPoint:(NSPoint)point {
273
+ BOOL overPocket = [self isPocketPoint:point];
274
+ if (self.pocketHoverChanged) self.pocketHoverChanged(overPocket);
275
+ if (overPocket) [NSCursor.pointingHandCursor set];
276
+ else [NSCursor.arrowCursor set];
277
+ return overPocket;
278
+ }
279
+ - (void)updateHoverForPoint:(NSPoint)point {
280
+ [self noteActivity];
281
+ BOOL overPocket = [self updatePocketHoverForPoint:point];
282
+ if (overPocket) {
283
+ [self playRow:5 throughFrame:7 oneShot:NO];
284
+ } else if (point.y >= NSHeight(self.bounds) * 0.58) {
285
+ [NSCursor.pointingHandCursor set];
286
+ [self playRow:4 throughFrame:4 oneShot:NO];
287
+ } else if (point.y <= NSHeight(self.bounds) * 0.20) {
288
+ [NSCursor.pointingHandCursor set];
289
+ [self playRow:6 throughFrame:5 oneShot:NO];
290
+ } else if (point.x >= NSWidth(self.bounds) * 0.66) {
291
+ [NSCursor.arrowCursor set];
292
+ [self playRow:3 throughFrame:3 oneShot:NO];
293
+ } else if (point.x <= NSWidth(self.bounds) * 0.34) {
294
+ [NSCursor.arrowCursor set];
295
+ [self playRow:8 throughFrame:5 oneShot:NO];
296
+ } else {
297
+ [NSCursor.arrowCursor set];
298
+ [self playRow:0 throughFrame:5 oneShot:NO];
299
+ }
300
+ }
301
+ - (void)mouseEntered:(NSEvent *)event {
302
+ [self mouseMoved:event];
303
+ }
304
+ - (void)mouseMoved:(NSEvent *)event {
305
+ if (self.draggingPet) return;
306
+ NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
307
+ // 一次性动作只锁定动画,不应锁住面板热区。否则鼠标在动作期间进入后停住,
308
+ // oneShot 结束时不会再收到 mouseMoved,额度面板就一直没有机会展示。
309
+ if (self.oneShot || self.agentActive) {
310
+ [self updatePocketHoverForPoint:point];
311
+ return;
312
+ }
313
+ [self updateHoverForPoint:point];
314
+ }
315
+ - (void)mouseExited:(NSEvent *)event {
316
+ if (self.draggingPet) return;
317
+ if (self.pocketHoverChanged) self.pocketHoverChanged(NO);
318
+ [NSCursor.arrowCursor set];
319
+ if (self.agentActive || self.oneShot) return;
320
+ [self playRow:0 throughFrame:5 oneShot:NO];
321
+ }
322
+ - (void)nextFrame:(NSTimer *)timer {
323
+ if (self.lookDirection >= 0) {
324
+ if (CFAbsoluteTimeGetCurrent() >= self.attentionUntil) [self releaseAttention];
325
+ return;
326
+ }
327
+ // performWindowDragWithEvent: 会立即返回,且窗口服务器可能吞掉 mouseUp。
328
+ // 动画时钟同时作为释放兜底,避免拖动状态永久停留在 running 行。
329
+ if (self.draggingPet &&
330
+ !CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, kCGMouseButtonLeft)) {
331
+ [self endPetDrag];
332
+ return;
333
+ }
334
+ // 用力程度随时间衰减,掉档时把呼吸调回慢档。
335
+ if (self.breathLevel != 0) [self updateBreathingIfExertionChanged];
336
+
337
+ // 微动调度:只在待机且无人打扰时倒数,到点起一个小动作。
338
+ if ([self canStartMicroBehavior]) {
339
+ self.microCountdown -= 1;
340
+ if (self.microCountdown <= 0) {
341
+ [self startMicroBehavior];
342
+ return;
343
+ }
344
+ }
345
+
346
+ // 当前帧还没停够拍数就原地不动。停留期间连 needsDisplay 都不置,这一拍是纯空转。
347
+ self.holdTicks += 1;
348
+ if (self.holdTicks < [self holdForSequenceIndex:self.sequenceIndex]) return;
349
+ self.holdTicks = 0;
350
+
351
+ self.sequenceIndex += 1;
352
+ if (self.sequenceIndex >= self.frames.count) {
353
+ if (self.oneShot) {
354
+ self.rowIndex = 0;
355
+ self.frames = [self framesFromZeroThrough:5];
356
+ self.frameHolds = [self idleHoldsForCount:self.frames.count];
357
+ self.oneShot = NO;
358
+ self.agentActive = NO;
359
+ self.microActive = NO;
360
+ [self scheduleNextMicroBehavior];
361
+ } else if (self.microActive) {
362
+ // 微动播完就回待机。这条分支独立于 oneShot,所以不会清 agentActive。
363
+ [self endMicroBehavior];
364
+ self.needsDisplay = YES;
365
+ return;
366
+ }
367
+ self.sequenceIndex = 0;
368
+ }
369
+ self.frameIndex = self.frames[self.sequenceIndex].integerValue;
370
+ self.needsDisplay = YES;
371
+ }
372
+ - (NSUInteger)holdForSequenceIndex:(NSUInteger)index {
373
+ if (index >= self.frameHolds.count) return 1;
374
+ NSUInteger hold = self.frameHolds[index].unsignedIntegerValue;
375
+ return hold > 0 ? hold : 1;
376
+ }
377
+ - (void)handleAgentEvent:(NSString *)event tool:(NSString *)tool failed:(BOOL)failed {
378
+ [self noteActivity];
379
+ [self bumpExertion];
380
+ if ([event isEqualToString:@"SessionStart"]) {
381
+ self.agentActive = YES;
382
+ [self playRow:4 throughFrame:4 oneShot:YES];
383
+ } else if ([event isEqualToString:@"UserPromptSubmit"]) {
384
+ self.agentActive = YES;
385
+ [self playRow:5 throughFrame:7 oneShot:NO];
386
+ } else if ([event isEqualToString:@"PermissionRequest"]) {
387
+ self.agentActive = YES;
388
+ [self playRow:4 throughFrame:4 oneShot:NO];
389
+ } else if ([event isEqualToString:@"AutoReviewRequest"]) {
390
+ self.agentActive = YES;
391
+ [self playRow:8 throughFrame:5 oneShot:NO];
392
+ } else if ([event isEqualToString:@"PreToolUse"]) {
393
+ self.agentActive = YES;
394
+ NSString *lower = tool.lowercaseString;
395
+ if ([lower containsString:@"bash"] || [lower containsString:@"exec"]) {
396
+ [self playRow:1 throughFrame:7 oneShot:NO];
397
+ } else if ([lower containsString:@"patch"] || [lower containsString:@"edit"] || [lower containsString:@"write"]) {
398
+ [self playRow:7 throughFrame:5 oneShot:NO];
399
+ } else if ([lower containsString:@"read"] || [lower containsString:@"search"] || [lower containsString:@"find"] ||
400
+ [lower containsString:@"grep"] || [lower containsString:@"glob"] || [lower containsString:@"web"] ||
401
+ [lower hasPrefix:@"mcp__"]) {
402
+ [self playRow:3 throughFrame:3 oneShot:NO];
403
+ } else if ([lower isEqualToString:@"task"] || [lower containsString:@"agent"]) {
404
+ [self playRow:6 throughFrame:5 oneShot:NO];
405
+ } else {
406
+ [self playRow:8 throughFrame:5 oneShot:NO];
407
+ }
408
+ } else if ([event isEqualToString:@"SubagentStart"] || [event isEqualToString:@"TaskCreated"]) {
409
+ self.agentActive = YES;
410
+ [self playRow:6 throughFrame:5 oneShot:NO];
411
+ } else if ([event isEqualToString:@"PostToolUse"] || [event isEqualToString:@"PostToolUseFailure"]) {
412
+ self.agentActive = YES;
413
+ [self playRow:failed ? 5 : 7 throughFrame:failed ? 7 : 5 oneShot:YES];
414
+ } else if ([event isEqualToString:@"StopFailure"]) {
415
+ self.agentActive = YES;
416
+ [self playRow:5 throughFrame:7 oneShot:YES];
417
+ } else if ([event isEqualToString:@"Notification"]) {
418
+ self.agentActive = YES;
419
+ [self playRow:4 throughFrame:4 oneShot:YES];
420
+ } else if ([event isEqualToString:@"Stop"] || [event isEqualToString:@"SubagentStop"] ||
421
+ [event isEqualToString:@"TaskCompleted"] || [event isEqualToString:@"SessionEnd"]) {
422
+ self.agentActive = YES;
423
+ [self playRow:3 throughFrame:3 oneShot:YES];
424
+ }
425
+ }
426
+ // 每一格都用**原来 drawRect: 那一次绘制**预先栅格化成位图,再交给 layer。
427
+ //
428
+ // 试过两条更"聪明"的路,都不行,别改回去:
429
+ // 1. CGImageCreateWithImageInRect 预裁——素材经 LoadPetSpriteImage 缩略后不保证能被
430
+ // 8 列 rowCount 行整除(超大素材缩略后可能得到小数尺寸的源格),
431
+ // 小数矩形会被取整,采样相位偏掉,边缘整行错位。
432
+ // 2. 整表交给 layer + contentsRect 选帧——CA 自己的最近邻缩放相位同样和 NSImage 对不上,
433
+ // 实测形状吻合度反而从 99.4% 掉到 93.5%。
434
+ // 照搬原来的 drawInRect:fromRect: 才能保证逐像素等价:栅格化的是同一段代码,只是从
435
+ // 每帧执行挪到了每格一次。
436
+ - (void)rebuildSpriteCache {
437
+ [self.cellCache removeAllObjects];
438
+ CGFloat scale = self.spriteScale;
439
+ self.spriteLayer.contentsScale = scale;
440
+ // 上限按倍率和行数定,不能写死:11 行素材光十六向朝向就占 16 格,加上待机和动作帧,
441
+ // 固定 6MB(视网膜下约 18 格)会卡在淘汰边缘,鼠标绕着宠物转就不停重栅格化。
442
+ //
443
+ // 但也不能给太多。实测这一块是常驻内存最大的单一新增项:视网膜下每格 328KB,
444
+ // 原来给 48 格 = 15.4MB,而真实活跃工作集只有待机 6 帧 + 当前动作 ≤8 帧 +
445
+ // 偶尔几个朝向格,20 格足够,超出的部分纯粹是白占着。
446
+ NSSize pointSize = NSInsetRect(self.bounds, 2, 0).size;
447
+ self.spriteCacheSize = pointSize;
448
+ NSUInteger cellCost = (NSUInteger)(ceil(pointSize.width * scale) *
449
+ ceil(pointSize.height * scale) * 4);
450
+ NSUInteger affordable = MIN((NSUInteger)(self.spriteRowCount * 8), (NSUInteger)20);
451
+ self.cellCache.totalCostLimit = MAX(cellCost * affordable, (NSUInteger)(2 * 1024 * 1024));
452
+ [self updateSpriteContents];
453
+ }
454
+ // 视网膜屏上必须按 backingScaleFactor 栅格化。原来 drawRect: 是直接画进 2x 的后备存储,
455
+ // 等于按实际屏幕分辨率采样源格;预栅格化成 1x 位图再让 layer 放大,
456
+ // 会比改造前糊一档。
457
+ - (CGFloat)spriteScale {
458
+ CGFloat scale = self.window.backingScaleFactor;
459
+ return scale > 0 ? scale : 1.0;
460
+ }
461
+ - (void)viewDidChangeBackingProperties {
462
+ [super viewDidChangeBackingProperties];
463
+ // 换显示器会改 backingScaleFactor,缓存里全是按旧倍率栅格化的位图,必须整体作废。
464
+ [self rebuildSpriteCache];
465
+ }
466
+ - (CGImageRef)cellImageForRow:(NSInteger)row frame:(NSInteger)frame {
467
+ if (!self.sheet || self.spriteRowCount <= 0) return NULL;
468
+ NSNumber *key = @(row * 8 + frame);
469
+ id cached = [self.cellCache objectForKey:key];
470
+ if (cached) return (__bridge CGImageRef)cached;
471
+
472
+ NSRect target = NSInsetRect(self.bounds, 2, 0);
473
+ NSSize pointSize = target.size;
474
+ if (pointSize.width <= 0 || pointSize.height <= 0) return NULL;
475
+ CGFloat scale = self.spriteScale;
476
+ NSInteger pixelWidth = (NSInteger)ceil(pointSize.width * scale);
477
+ NSInteger pixelHeight = (NSInteger)ceil(pointSize.height * scale);
478
+
479
+ NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
480
+ initWithBitmapDataPlanes:NULL pixelsWide:pixelWidth pixelsHigh:pixelHeight
481
+ bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
482
+ colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:0 bitsPerPixel:0];
483
+ rep.size = pointSize;
484
+
485
+ CGFloat cellWidth = self.sheet.size.width / 8.0;
486
+ CGFloat cellHeight = self.sheet.size.height / self.spriteRowCount;
487
+ NSRect source = NSMakeRect(frame * cellWidth,
488
+ self.sheet.size.height - (row + 1) * cellHeight, cellWidth, cellHeight);
489
+
490
+ NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];
491
+ [NSGraphicsContext saveGraphicsState];
492
+ NSGraphicsContext.currentContext = context;
493
+ [self.sheet drawInRect:NSMakeRect(0, 0, pointSize.width, pointSize.height) fromRect:source
494
+ operation:NSCompositingOperationSourceOver fraction:1.0
495
+ respectFlipped:NO hints:@{NSImageHintInterpolation: @(NSImageInterpolationNone)}];
496
+ [NSGraphicsContext restoreGraphicsState];
497
+
498
+ CGImageRef cell = rep.CGImage;
499
+ if (!cell) return NULL;
500
+ [self.cellCache setObject:(__bridge id)cell forKey:key
501
+ cost:(NSUInteger)(pixelWidth * pixelHeight * 4)];
502
+ return cell;
503
+ }
504
+ // 只打标记,真正换图交给 AppKit 的显示周期。
505
+ //
506
+ // 不要改回"在 setter 里同步换 contents":十六向跟随装的是全局鼠标监听,屏幕上每个
507
+ // 鼠标移动事件都会写 rowIndex/frameIndex(一次方向变化还是连写两个)。同步更新等于
508
+ // 几百次/秒一次不落,实测疯狂动鼠标时 CPU 6~8%,冷缓存期峰值 12.5%。改造前那两个是
509
+ // 无副作用的纯属性 + needsDisplay,由 AppKit 合并成每刷新周期最多一次——这里要还原的
510
+ // 就是那个语义。
511
+ - (void)updateSpriteContents {
512
+ self.needsDisplay = YES;
513
+ }
514
+ // layer-backed 视图走 updateLayer 而不是 drawRect:,AppKit 每个显示周期最多调一次。
515
+ - (BOOL)wantsUpdateLayer {
516
+ return YES;
517
+ }
518
+ // 换 contents 会触发 CA 默认的 0.25s 淡入。逐帧动画上那就是一层永久的拖影,必须关掉。
519
+ - (void)updateLayer {
520
+ CGImageRef cell = [self cellImageForRow:self.rowIndex frame:self.frameIndex];
521
+ if (!cell) return;
522
+ // 逐帧动画默认必须关掉隐式动画,否则每一帧都带 0.25s 淡入,等于一层永久拖影。
523
+ // 只有进出微动那一下例外:那是两个不相干姿势之间的硬切,给一次短淡化才不突兀。
524
+ if (self.fadeNextContents) {
525
+ self.fadeNextContents = NO;
526
+ CATransition *fade = [CATransition animation];
527
+ fade.type = kCATransitionFade;
528
+ fade.duration = 0.13;
529
+ [self.spriteLayer addAnimation:fade forKey:@"contentsFade"];
530
+ }
531
+ [CATransaction begin];
532
+ [CATransaction setDisableActions:YES];
533
+ self.spriteLayer.contents = (__bridge id)cell;
534
+ [CATransaction commit];
535
+ }
536
+ - (void)layout {
537
+ [super layout];
538
+ [self refreshSpriteSize];
539
+ }
540
+ - (void)refreshSpriteSize {
541
+ [self layoutSpriteLayer];
542
+ // 每格缓存的键只包含行/帧,尺寸改变后必须清掉旧尺寸的位图。
543
+ if (!NSEqualSizes(self.spriteCacheSize, NSInsetRect(self.bounds, 2, 0).size)) {
544
+ [self rebuildSpriteCache];
545
+ }
546
+ }
547
+ - (void)setFrame:(NSRect)newFrame {
548
+ [super setFrame:newFrame];
549
+ [self refreshSpriteSize];
550
+ }
551
+ - (void)setFrameSize:(NSSize)newSize {
552
+ [super setFrameSize:newSize];
553
+ [self refreshSpriteSize];
554
+ }
555
+ // 锚点是脚底 (0.5, 0),所以不能直接设 frame——那会把锚点当左下角算。改用 bounds + position。
556
+ - (void)layoutSpriteLayer {
557
+ NSRect target = NSInsetRect(self.bounds, 2, 0);
558
+ [CATransaction begin];
559
+ [CATransaction setDisableActions:YES];
560
+ self.spriteLayer.bounds = CGRectMake(0, 0, NSWidth(target), NSHeight(target));
561
+ self.spriteLayer.position = CGPointMake(NSMidX(target), NSMinY(target));
562
+ [CATransaction commit];
563
+ }
564
+ // 呼吸是常驻 CA 动画:插值和合成都在 render server 侧,主进程零唤醒。
565
+ // 用 byValue 而不是 toValue,动画相对当前 model 值走,换素材或重新布局都不用重设。
566
+ - (void)updateBreathingAnimation {
567
+ // reduceMotion 下彻底不做。这不是省电问题——系统开了减弱动效,持续浮动本身就是
568
+ // 用户明确不想要的东西。
569
+ BOOL shouldBreathe = self.window != nil && !self.reduceMotion;
570
+ if (!shouldBreathe) {
571
+ [self.spriteLayer removeAnimationForKey:@"breath"];
572
+ return;
573
+ }
574
+ if ([self.spriteLayer animationForKey:@"breath"]) return;
575
+ // 越用力呼吸越快、起伏越大。平复回基线是自然衰减出来的,不用单独写过渡。
576
+ NSInteger level = [self exertionLevel];
577
+ double tempo = level == 0 ? 1.0 : (level == 1 ? 0.72 : 0.52);
578
+ double rise = level == 0 ? PetBreathRise : PetBreathRise * (level == 1 ? 1.3 : 1.7);
579
+ CABasicAnimation *breath = [CABasicAnimation animationWithKeyPath:@"position.y"];
580
+ breath.byValue = @(rise);
581
+ breath.duration = PetBreathPeriod * tempo / 2.0;
582
+ breath.autoreverses = YES;
583
+ breath.repeatCount = HUGE_VALF;
584
+ breath.timingFunction = [CAMediaTimingFunction
585
+ functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
586
+ [self.spriteLayer addAnimation:breath forKey:@"breath"];
587
+ }
588
+ - (void)viewDidMoveToWindow {
589
+ [super viewDidMoveToWindow];
590
+ // 进窗口才知道 backingScaleFactor,缓存要按真实倍率重建;呼吸也从这里起步。
591
+ [self rebuildSpriteCache];
592
+ [self updateBreathingAnimation];
593
+ [self restoreLastActivity];
594
+ }
595
+ - (void)setRowIndex:(NSInteger)value {
596
+ if (_rowIndex == value) return;
597
+ _rowIndex = value;
598
+ [self updateSpriteContents];
599
+ }
600
+ - (void)setFrameIndex:(NSInteger)value {
601
+ if (_frameIndex == value) return;
602
+ _frameIndex = value;
603
+ [self updateSpriteContents];
604
+ }
605
+ - (void)setSheet:(NSImage *)sheet {
606
+ if (_sheet == sheet) return;
607
+ _sheet = sheet;
608
+ [self rebuildSpriteCache];
609
+ }
610
+ - (void)setSpriteRowCount:(NSInteger)count {
611
+ if (_spriteRowCount == count) return;
612
+ _spriteRowCount = count;
613
+ [self rebuildSpriteCache];
614
+ }
615
+ - (void)applySheet:(NSImage *)sheet petID:(NSString *)petID rowCount:(NSInteger)rowCount {
616
+ // 直接写 ivar 再统一重建:走 setter 会因为 sheet 和 rowCount 分两次设置而拿旧行数
617
+ // 白裁一遍缓存。
618
+ _sheet = sheet;
619
+ _spriteRowCount = rowCount;
620
+ _rowIndex = 0;
621
+ _frameIndex = 0;
622
+ self.lookDirection = -1;
623
+ self.currentPetID = petID;
624
+ self.sequenceIndex = 0;
625
+ self.frames = [self framesFromZeroThrough:5];
626
+ self.frameHolds = [self idleHoldsForCount:self.frames.count];
627
+ self.holdTicks = 0;
628
+ self.oneShot = NO;
629
+ self.interactionClickCount = 0;
630
+ self.lastInteractionClickAt = 0;
631
+ [self rebuildSpriteCache];
632
+ [self rebuildCapabilities];
633
+ [self updateLookTracking];
634
+ }
635
+ @end
@@ -0,0 +1,25 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ // `cc-pets paths [--json]` 与 `cc-pets doctor`。
4
+ //
5
+ // 默认只读:不改任何配置、不拉起桌宠。显式 doctor --repair-profile <id> 会备份后修复该账号的集成。
6
+ // 输出常被贴进 issue,所以 home 一律显示成 `~`,
7
+ // 不打印 status line 原命令、会话内容或 token。
8
+ //
9
+ // arguments 以子命令开头("paths" / "doctor")。返回进程退出码:doctor 有错误项时为 1。
10
+ int CCPetsDoctorMain(NSArray<NSString *> *arguments);
11
+
12
+ // doctor 的检查结果,`cc-pets doctor` 与主窗口诊断页同源:
13
+ // @[@{title, results: @[@{level: ok | warn | fail | info, title, fix?, repairProfileID?}]}]。
14
+ // fix 是建议执行的命令,repairProfileID 是允许自动修复的账号;不能自动处理的问题没有这个键。
15
+ // 会读 Claude / Codex 配置、探测 CLI 与 codex queue,可能耗时数百毫秒,不要放在主线程。
16
+ NSArray<NSDictionary *> *CCPetsDoctorSections(void);
17
+ // 主窗口诊断页用的版本。检查跑在桌宠进程里,读到的 PATH 是桌宠启动那一刻继承来的(比如被 npm
18
+ // 拉起时 node 目录会插在最前),不是用户终端的 PATH;所以"shim 是否排在 PATH 最前"这一项不判对错,
19
+ // 改成一条提示,让用户在终端里跑 cc-pets doctor 确认。其余各项与 CCPetsDoctorSections() 相同。
20
+ NSArray<NSDictionary *> *CCPetsDoctorSectionsForApp(void);
21
+ // 完整报告文本(标题行、各节、汇总),与 `cc-pets doctor` 的标准输出逐字一致,不含末尾换行。
22
+ // 诊断页的"复制报告"用它;和 CLI 一样把 home 显示成 `~`,可以直接贴进 issue。
23
+ NSString *CCPetsDoctorReport(NSArray<NSDictionary *> *sections);
24
+ // 有 fail 级别的项。`cc-pets doctor` 据此以 1 退出。
25
+ BOOL CCPetsDoctorHasFailures(NSArray<NSDictionary *> *sections);