cc-pets 1.3.1 → 1.3.3

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.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [1.3.2] - 2026-08-17
8
+ - 优化“切换桌宠”子菜单:素材名称按 5 个中文字符的视觉宽度展示,超出后追加 `...`;外部素材增加
9
+ 垃圾桶删除入口,首次点击后原位置变为红字“确认”,再次点击才执行删除。切换或删除素材时
10
+ 子菜单保持展开,并立即更新当前项勾选和删除状态;内置素材及当前已应用素材不显示删除图标。
11
+
12
+ ## [1.3.1] - 2026-08-17
7
13
  - 修复长期使用后桌宠崩溃、且此后每次启动都在 0.2 秒内再崩(表现为"打不开了"):Claude
8
14
  Token 摘要缓存落盘后,下次启动读回时会对同一块缓冲区继续追加新记录,而判断它可不可变
9
15
  用的是 `isKindOfClass:NSMutableData`——`NSData` 是类簇,二进制 plist 解析出来的**不可变**
@@ -19,6 +25,7 @@
19
25
  - `cc-pets --clean` 补上 Token 摘要缓存(`*-token-cache-*.plist`、`*-monthly-usage-*.plist`
20
26
  及落盘中断留下的 `.tmp`)。它同样只是缓存,丢了只意味着重新解析一遍会话文件,原先却漏在
21
27
  清理之外,于是缓存一旦出问题,`clean` 救不回来,只能让用户手动删文件。
28
+ ## [1.3.0] - 2026-08-12
22
29
  - 修复面板上的剩余额度来回跳(实测 Claude 5 小时窗口 99% → 94% → 又回 99%):多个会话的
23
30
  status line 各写各的额度快照到同一个文件,而"响应更早、落盘更晚"在并发下完全正常,
24
31
  无条件覆盖会让旧快照盖掉新快照。改为逐窗口按"同一 `resets_at` 内用量只增不减"取舍,
@@ -63,4 +63,5 @@
63
63
  - (void)setUsageDisplayModeFromControl:(NSButton *)sender;
64
64
  - (void)applyCodexUsage:(NSDictionary *)codexUsage claudeUsage:(NSDictionary *)claudeUsage;
65
65
  - (void)switchPetToID:(NSString *)petID;
66
+ - (BOOL)deletePetWithID:(NSString *)petID;
66
67
  @end
@@ -813,6 +813,9 @@ static void TrimUpdateLog(NSString *path) {
813
813
  self.petView.switchPetRequested = ^(NSString *petID) {
814
814
  [weakSelf switchPetToID:petID];
815
815
  };
816
+ self.petView.deletePetRequested = ^BOOL(NSString *petID) {
817
+ return [weakSelf deletePetWithID:petID];
818
+ };
816
819
  self.petView.dragStateChanged = ^(BOOL dragging) {
817
820
  weakSelf.petDragging = dragging;
818
821
  if (!dragging) [weakSelf scheduleQuotaDashboardHide];
@@ -1133,6 +1136,36 @@ static const NSTimeInterval UsageRefreshCoalesceWindow = 2.0;
1133
1136
  [self.petView applySheet:image petID:petID rowCount:spriteRowCount];
1134
1137
  [NSUserDefaults.standardUserDefaults setObject:petID forKey:@"CCPetsSelectedSprite"];
1135
1138
  }
1139
+ - (BOOL)deletePetWithID:(NSString *)petID {
1140
+ if (![petID hasPrefix:@"external:"]) return NO;
1141
+ NSArray<NSDictionary *> *options = [self petOptions];
1142
+ NSDictionary *option = [self petOptionWithID:petID inOptions:options];
1143
+ if (!option) return NO;
1144
+
1145
+ NSString *name = [petID substringFromIndex:@"external:".length];
1146
+ if (name.length == 0 || [name containsString:@"/"] || [name isEqualToString:@"."] ||
1147
+ [name isEqualToString:@".."]) return NO;
1148
+ NSString *root = OwnPetsDirectory().stringByStandardizingPath;
1149
+ NSString *directory = [root stringByAppendingPathComponent:name].stringByStandardizingPath;
1150
+ NSString *optionDirectory = [[option[@"path"] stringByDeletingLastPathComponent] stringByStandardizingPath];
1151
+ if (![directory.stringByDeletingLastPathComponent isEqualToString:root] ||
1152
+ ![optionDirectory isEqualToString:directory]) return NO;
1153
+
1154
+ NSError *error = nil;
1155
+ if (![NSFileManager.defaultManager removeItemAtPath:directory error:&error]) {
1156
+ [self showUpdateAlertWithTitle:@"无法删除素材"
1157
+ message:error.localizedDescription ?: @"素材目录删除失败。"];
1158
+ return NO;
1159
+ }
1160
+
1161
+ [self invalidatePetOptionsCache];
1162
+ if ([self.petView.currentPetID isEqualToString:petID]) {
1163
+ NSDictionary *fallback = [self petOptions].firstObject;
1164
+ if (fallback) [self switchPetToID:fallback[@"id"]];
1165
+ else [NSUserDefaults.standardUserDefaults removeObjectForKey:@"CCPetsSelectedSprite"];
1166
+ }
1167
+ return YES;
1168
+ }
1136
1169
  - (void)prepareAgentEventReader {
1137
1170
  NSString *path = AgentEventPath();
1138
1171
  NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path error:nil];
@@ -19,6 +19,7 @@
19
19
  @property(copy) void (^dragStateChanged)(BOOL dragging);
20
20
  @property(copy) NSArray<NSDictionary *> *(^petOptionsRequested)(void);
21
21
  @property(copy) void (^switchPetRequested)(NSString *petID);
22
+ @property(copy) BOOL (^deletePetRequested)(NSString *petID);
22
23
  @property NSCache<NSString *, NSImage *> *petMenuPreviewCache;
23
24
  @property NSPanel *petMenuPreviewPanel;
24
25
  @property NSImageView *petMenuPreviewImageView;
@@ -9,6 +9,15 @@ static const CGFloat PetDragThreshold = 3.0;
9
9
  // 三个子菜单(用量展示模式 / 系统通知 / 系统状态)用同一个宽度,看起来才是一套。
10
10
  static const CGFloat PetMenuRowWidth = 194.0;
11
11
  static const CGFloat PetSubmenuRowWidth = 134.0;
12
+ // 与“系统通知”里文案到开关的间距共用,素材名称到删除按钮也保持一致。
13
+ static const CGFloat PetMenuControlGap = 8.0;
14
+ static const NSInteger PetMenuCheckViewTag = 1001;
15
+ static const NSInteger PetMenuDeleteButtonTag = 1002;
16
+
17
+ @interface PetView ()
18
+ @property(weak) NSMenu *activePetSwitchMenu;
19
+ @property(copy) NSString *pendingDeletePetID;
20
+ @end
12
21
 
13
22
  @implementation PetView
14
23
  - (instancetype)initWithFrame:(NSRect)frame sheet:(NSImage *)sheet rowCount:(NSInteger)rowCount {
@@ -348,11 +357,176 @@ static const CGFloat PetSubmenuRowWidth = 134.0;
348
357
  [self updateLookTracking];
349
358
  self.needsDisplay = YES;
350
359
  }
351
- - (void)selectPet:(NSMenuItem *)sender {
352
- NSDictionary *pet = [sender.representedObject isKindOfClass:NSDictionary.class]
353
- ? sender.representedObject : nil;
354
- NSString *petID = pet[@"id"];
355
- if (petID.length > 0 && self.switchPetRequested) self.switchPetRequested(petID);
360
+ - (void)selectPetFromButton:(NSButton *)sender {
361
+ NSString *petID = sender.identifier;
362
+ if (petID.length == 0) return;
363
+ [self resetPendingDeleteButton];
364
+ if (self.switchPetRequested) self.switchPetRequested(petID);
365
+ [self refreshPetMenuRows];
366
+ }
367
+ - (void)deletePetFromButton:(NSButton *)sender {
368
+ NSString *petID = sender.identifier;
369
+ if (petID.length == 0) return;
370
+ if (![self.pendingDeletePetID isEqualToString:petID]) {
371
+ [self resetPendingDeleteButton];
372
+ self.pendingDeletePetID = petID;
373
+ [self configureDeleteButton:sender confirming:YES];
374
+ return;
375
+ }
376
+
377
+ BOOL deleted = self.deletePetRequested && self.deletePetRequested(petID);
378
+ if (!deleted) {
379
+ [self resetPendingDeleteButton];
380
+ return;
381
+ }
382
+ self.pendingDeletePetID = nil;
383
+ for (NSMenuItem *item in self.activePetSwitchMenu.itemArray.copy) {
384
+ NSDictionary *pet = [item.representedObject isKindOfClass:NSDictionary.class]
385
+ ? item.representedObject : nil;
386
+ if ([pet[@"id"] isEqualToString:petID]) {
387
+ [self.activePetSwitchMenu removeItem:item];
388
+ break;
389
+ }
390
+ }
391
+ [self refreshPetMenuRows];
392
+ }
393
+ - (void)configureDeleteButton:(NSButton *)button confirming:(BOOL)confirming {
394
+ button.bordered = confirming;
395
+ button.title = @"";
396
+ button.attributedTitle = confirming
397
+ ? [[NSAttributedString alloc] initWithString:@"确认" attributes:@{
398
+ NSFontAttributeName: [NSFont systemFontOfSize:11 weight:NSFontWeightMedium],
399
+ NSForegroundColorAttributeName: NSColor.systemRedColor,
400
+ }]
401
+ : [[NSAttributedString alloc] initWithString:@""];
402
+ button.image = confirming ? nil : [NSImage imageNamed:NSImageNameTrashEmpty];
403
+ button.imagePosition = confirming ? NSNoImage : NSImageOnly;
404
+ button.contentTintColor = nil;
405
+ button.bezelStyle = NSBezelStyleRounded;
406
+ button.hasDestructiveAction = YES;
407
+ button.toolTip = confirming ? @"再次点击确认删除" : @"删除素材";
408
+ [button setAccessibilityLabel:button.toolTip];
409
+ }
410
+ - (void)resetPendingDeleteButton {
411
+ if (self.pendingDeletePetID.length == 0) return;
412
+ for (NSMenuItem *item in self.activePetSwitchMenu.itemArray) {
413
+ NSButton *button = (NSButton *)[item.view viewWithTag:PetMenuDeleteButtonTag];
414
+ if ([button isKindOfClass:NSButton.class] &&
415
+ [button.identifier isEqualToString:self.pendingDeletePetID]) {
416
+ [self configureDeleteButton:button confirming:NO];
417
+ break;
418
+ }
419
+ }
420
+ self.pendingDeletePetID = nil;
421
+ }
422
+ - (void)refreshPetMenuRows {
423
+ for (NSMenuItem *item in self.activePetSwitchMenu.itemArray) {
424
+ NSDictionary *pet = [item.representedObject isKindOfClass:NSDictionary.class]
425
+ ? item.representedObject : nil;
426
+ NSString *petID = [pet[@"id"] isKindOfClass:NSString.class] ? pet[@"id"] : @"";
427
+ BOOL selected = [petID isEqualToString:self.currentPetID];
428
+ item.state = selected ? NSControlStateValueOn : NSControlStateValueOff;
429
+
430
+ NSTextField *check = (NSTextField *)[item.view viewWithTag:PetMenuCheckViewTag];
431
+ if ([check isKindOfClass:NSTextField.class]) check.stringValue = selected ? @"✓" : @"";
432
+
433
+ NSButton *deleteButton = (NSButton *)[item.view viewWithTag:PetMenuDeleteButtonTag];
434
+ if (![deleteButton isKindOfClass:NSButton.class]) continue;
435
+ BOOL deletable = [petID hasPrefix:@"external:"] && !selected;
436
+ deleteButton.hidden = !deletable;
437
+ deleteButton.enabled = deletable;
438
+ if (!deletable && [self.pendingDeletePetID isEqualToString:petID]) {
439
+ [self configureDeleteButton:deleteButton confirming:NO];
440
+ self.pendingDeletePetID = nil;
441
+ }
442
+ }
443
+ }
444
+ - (NSString *)displayNameForPetName:(NSString *)name font:(NSFont *)font maxWidth:(CGFloat)maxWidth {
445
+ if (![name isKindOfClass:NSString.class]) return @"";
446
+ NSDictionary<NSAttributedStringKey, id> *attributes = @{NSFontAttributeName: font};
447
+ if ([name sizeWithAttributes:attributes].width <= maxWidth) return name;
448
+
449
+ NSString *suffix = @"...";
450
+ NSMutableString *prefix = [NSMutableString string];
451
+ [name enumerateSubstringsInRange:NSMakeRange(0, name.length)
452
+ options:NSStringEnumerationByComposedCharacterSequences
453
+ usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
454
+ NSString *candidate = [NSString stringWithFormat:@"%@%@%@", prefix, substring, suffix];
455
+ if ([candidate sizeWithAttributes:attributes].width > maxWidth) {
456
+ *stop = YES;
457
+ return;
458
+ }
459
+ [prefix appendString:substring];
460
+ }];
461
+ return [prefix stringByAppendingString:suffix];
462
+ }
463
+ - (NSMenuItem *)addPetOption:(NSDictionary *)pet toMenu:(NSMenu *)menu {
464
+ NSString *petID = [pet[@"id"] isKindOfClass:NSString.class] ? pet[@"id"] : @"";
465
+ NSString *name = [pet[@"name"] isKindOfClass:NSString.class] ? pet[@"name"] : @"";
466
+ BOOL selected = [petID isEqualToString:self.currentPetID];
467
+ BOOL deletable = [petID hasPrefix:@"external:"];
468
+
469
+ NSFont *font = [NSFont menuFontOfSize:13];
470
+ CGFloat nameWidth = ceil([@"桌宠素材名..." sizeWithAttributes:@{NSFontAttributeName: font}].width);
471
+ NSString *displayName = [self displayNameForPetName:name font:font maxWidth:nameWidth];
472
+ const CGFloat checkWidth = 14;
473
+ const CGFloat leadingInset = 6;
474
+ const CGFloat nameX = leadingInset + checkWidth;
475
+ const CGFloat deleteWidth = 40;
476
+ const CGFloat trailingInset = 2;
477
+ CGFloat deleteX = nameX + nameWidth + PetMenuControlGap;
478
+ CGFloat rowWidth = deleteX + deleteWidth + trailingInset;
479
+
480
+ NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:displayName action:nil keyEquivalent:@""];
481
+ item.representedObject = pet;
482
+ item.state = selected ? NSControlStateValueOn : NSControlStateValueOff;
483
+ NSView *row = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, rowWidth, 28)];
484
+
485
+ NSTextField *check = [NSTextField labelWithString:selected ? @"✓" : @""];
486
+ check.frame = NSMakeRect(leadingInset, 5, checkWidth, 18);
487
+ check.tag = PetMenuCheckViewTag;
488
+ check.font = font;
489
+ check.textColor = NSColor.labelColor;
490
+ [row addSubview:check];
491
+
492
+ NSTextField *nameLabel = [NSTextField labelWithString:displayName];
493
+ nameLabel.frame = NSMakeRect(nameX, 5, nameWidth, 18);
494
+ nameLabel.font = font;
495
+ nameLabel.textColor = NSColor.labelColor;
496
+ nameLabel.lineBreakMode = NSLineBreakByClipping;
497
+ nameLabel.toolTip = name;
498
+ [row addSubview:nameLabel];
499
+
500
+ // 透明按钮覆盖勾选和名称区域,既让整块都能点击,也避免 NSButton 自带的标题
501
+ // 内边距破坏“最大文案末尾到删除按钮 8pt”的精确间距。
502
+ NSButton *selectButton = [[NSButton alloc]
503
+ initWithFrame:NSMakeRect(0, 0, nameX + nameWidth, 28)];
504
+ selectButton.bordered = NO;
505
+ selectButton.title = @"";
506
+ selectButton.identifier = petID;
507
+ selectButton.target = self;
508
+ selectButton.action = @selector(selectPetFromButton:);
509
+ selectButton.focusRingType = NSFocusRingTypeNone;
510
+ selectButton.toolTip = name;
511
+ [selectButton setAccessibilityLabel:[NSString stringWithFormat:@"切换到桌宠 %@", name]];
512
+ [row addSubview:selectButton];
513
+
514
+ NSButton *deleteButton = [[NSButton alloc] initWithFrame:NSMakeRect(deleteX, 3, deleteWidth, 22)];
515
+ deleteButton.tag = PetMenuDeleteButtonTag;
516
+ deleteButton.imageScaling = NSImageScaleProportionallyDown;
517
+ deleteButton.identifier = petID;
518
+ deleteButton.target = self;
519
+ deleteButton.action = @selector(deletePetFromButton:);
520
+ deleteButton.enabled = deletable && !selected;
521
+ deleteButton.hidden = !deleteButton.enabled;
522
+ [self configureDeleteButton:deleteButton confirming:NO];
523
+ deleteButton.toolTip = [NSString stringWithFormat:@"删除素材 %@", name];
524
+ [deleteButton setAccessibilityLabel:deleteButton.toolTip];
525
+ [row addSubview:deleteButton];
526
+
527
+ item.view = row;
528
+ [menu addItem:item];
529
+ return item;
356
530
  }
357
531
  - (NSImage *)menuPreviewForPet:(NSDictionary *)pet {
358
532
  NSString *path = [pet[@"path"] isKindOfClass:NSString.class] ? pet[@"path"] : nil;
@@ -469,6 +643,7 @@ static const CGFloat PetSubmenuRowWidth = 134.0;
469
643
  else [self.petMenuPreviewPanel orderOut:nil];
470
644
  }
471
645
  - (void)menuDidClose:(NSMenu *)menu {
646
+ [self resetPendingDeleteButton];
472
647
  [self.petMenuPreviewPanel orderOut:nil];
473
648
  }
474
649
  // 主菜单的行要撑满 menu.minimumWidth;子菜单只有自己这几项,撑到 194 会让短标题和开关
@@ -480,7 +655,7 @@ static const CGFloat PetSubmenuRowWidth = 134.0;
480
655
  width:(CGFloat)width
481
656
  tag:(NSInteger)tag {
482
657
  const CGFloat toggleWidth = 36;
483
- const CGFloat trailingInset = 8;
658
+ const CGFloat trailingInset = PetMenuControlGap;
484
659
  const CGFloat leadingInset = 12;
485
660
  CGFloat toggleX = width - trailingInset - toggleWidth;
486
661
  NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
@@ -531,17 +706,13 @@ static const CGFloat PetSubmenuRowWidth = 134.0;
531
706
  NSArray<NSDictionary *> *availablePets = self.petOptionsRequested ? self.petOptionsRequested() : @[];
532
707
  NSMenu *menu = [NSMenu new];
533
708
  menu.minimumWidth = 194.0;
534
- NSMenuItem *switchItem = [menu addItemWithTitle:@"切换桌宠" action:nil keyEquivalent:@""];
709
+ NSMenuItem *switchItem = [menu addItemWithTitle:@"管理桌宠" action:nil keyEquivalent:@""];
535
710
  NSMenu *switchMenu = [NSMenu new];
536
711
  switchMenu.delegate = self;
537
712
  for (NSDictionary *pet in availablePets) {
538
- NSString *petID = pet[@"id"];
539
- NSMenuItem *item = [switchMenu addItemWithTitle:pet[@"name"]
540
- action:@selector(selectPet:) keyEquivalent:@""];
541
- item.target = self;
542
- item.representedObject = pet;
543
- item.state = [petID isEqualToString:self.currentPetID] ? NSControlStateValueOn : NSControlStateValueOff;
713
+ [self addPetOption:pet toMenu:switchMenu];
544
714
  }
715
+ self.activePetSwitchMenu = switchMenu;
545
716
  switchItem.submenu = switchMenu;
546
717
  [menu addItem:NSMenuItem.separatorItem];
547
718
  NSMenuItem *refreshItem = [menu addItemWithTitle:@"刷新用量" action:@selector(refreshUsage:) keyEquivalent:@"r"];
@@ -603,6 +774,7 @@ static const CGFloat PetSubmenuRowWidth = 134.0;
603
774
  NSMenuItem *quitItem = [menu addItemWithTitle:@"退出桌宠" action:@selector(terminate:) keyEquivalent:@"q"];
604
775
  quitItem.target = NSApp;
605
776
  [NSMenu popUpContextMenu:menu withEvent:event forView:self];
777
+ self.activePetSwitchMenu = nil;
606
778
  [self.petMenuPreviewPanel orderOut:nil];
607
779
  }
608
780
  @end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cc-pets",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "A native macOS desktop pet for Codex CLI and Claude Code",
5
5
  "author": "wangxinyusqn",
6
6
  "license": "MIT",