cc-pets 1.3.0 → 1.3.2
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 +22 -0
- package/Sources/CCPets/CCPetsAppDelegate.h +1 -0
- package/Sources/CCPets/CCPetsAppDelegate.m +33 -0
- package/Sources/CCPets/CCPetsCleanup.m +19 -0
- package/Sources/CCPets/CCPetsUsage.m +30 -5
- package/Sources/CCPets/PetView.h +1 -0
- package/Sources/CCPets/PetView.m +185 -13
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,28 @@
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [1.3.2] - 2026-08-17
|
|
8
|
+
- 优化“切换桌宠”子菜单:素材名称最多显示 4 个完整字符,超出后追加 `...`;外部素材增加
|
|
9
|
+
垃圾桶删除入口,首次点击后原位置变为红字“确认”,再次点击才执行删除。切换或删除素材时
|
|
10
|
+
子菜单保持展开,并立即更新当前项勾选和删除状态;内置素材及当前已应用素材不显示删除图标。
|
|
11
|
+
|
|
12
|
+
## [1.3.1] - 2026-08-17
|
|
13
|
+
- 修复长期使用后桌宠崩溃、且此后每次启动都在 0.2 秒内再崩(表现为"打不开了"):Claude
|
|
14
|
+
Token 摘要缓存落盘后,下次启动读回时会对同一块缓冲区继续追加新记录,而判断它可不可变
|
|
15
|
+
用的是 `isKindOfClass:NSMutableData`——`NSData` 是类簇,二进制 plist 解析出来的**不可变**
|
|
16
|
+
`__NSCFData` 对这个判断同样返回 YES。于是只读缓冲区被当成可变的原地 `appendBytes`,
|
|
17
|
+
每条记录写出界 48 字节(ASan:`heap-buffer-overflow`,`0 bytes after 6640-byte region`,
|
|
18
|
+
分配点在 `__CFBinaryPlistCreateObjectFiltered`)。越界写破坏的是堆元数据,进程不会当场
|
|
19
|
+
死,而是在之后任意一次内存分配上倒下,所以三份崩溃报告的落点(`EnumerateLines`、
|
|
20
|
+
`SaveTokenSummaryCache`)和真正的写入点毫无关系,只是先撞上的受害者。
|
|
21
|
+
触发要同时满足"缓存已落盘、重启后读回、该会话文件之后又变长",因此新装的机器碰不到,
|
|
22
|
+
攒够会话的老用户则一旦中招每次启动必崩。改为缓存一读回来就把记录换成本进程创建的可变
|
|
23
|
+
副本,只替换记录本身、其余字段原样保留(Codex 侧条目形状不同,整条重建会抹掉
|
|
24
|
+
`start`/`total`/`segments`/`days`);追加处那个失效的可变性判断一并删除,避免再写回去。
|
|
25
|
+
- `cc-pets --clean` 补上 Token 摘要缓存(`*-token-cache-*.plist`、`*-monthly-usage-*.plist`
|
|
26
|
+
及落盘中断留下的 `.tmp`)。它同样只是缓存,丢了只意味着重新解析一遍会话文件,原先却漏在
|
|
27
|
+
清理之外,于是缓存一旦出问题,`clean` 救不回来,只能让用户手动删文件。
|
|
28
|
+
## [1.3.0] - 2026-08-12
|
|
7
29
|
- 修复面板上的剩余额度来回跳(实测 Claude 5 小时窗口 99% → 94% → 又回 99%):多个会话的
|
|
8
30
|
status line 各写各的额度快照到同一个文件,而"响应更早、落盘更晚"在并发下完全正常,
|
|
9
31
|
无条件覆盖会让旧快照盖掉新快照。改为逐窗口按"同一 `resets_at` 内用量只增不减"取舍,
|
|
@@ -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];
|
|
@@ -24,6 +24,22 @@ static BOOL RemoveIfPresent(NSString *path) {
|
|
|
24
24
|
return [NSFileManager.defaultManager removeItemAtPath:path error:nil];
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
// Token 摘要缓存按数据源目录分文件(<provider>-token-cache-<hash>.plist、
|
|
28
|
+
// <provider>-monthly-usage-<hash>.plist),同一台机器可以有多份 CODEX_HOME / 配置目录,
|
|
29
|
+
// 份数和 hash 都不固定,因此按名字标记枚举,而不是拼具体路径。
|
|
30
|
+
// 落盘走临时文件替换,中途崩溃会留下同名的 .tmp,一并清掉。
|
|
31
|
+
static NSUInteger RemoveTokenCaches(NSString *supportDirectory) {
|
|
32
|
+
NSArray<NSString *> *entries =
|
|
33
|
+
[NSFileManager.defaultManager contentsOfDirectoryAtPath:supportDirectory error:nil];
|
|
34
|
+
NSUInteger removed = 0;
|
|
35
|
+
for (NSString *entry in entries) {
|
|
36
|
+
if ([entry rangeOfString:@"-token-cache-"].location == NSNotFound &&
|
|
37
|
+
[entry rangeOfString:@"-monthly-usage-"].location == NSNotFound) continue;
|
|
38
|
+
if (RemoveIfPresent([supportDirectory stringByAppendingPathComponent:entry])) removed += 1;
|
|
39
|
+
}
|
|
40
|
+
return removed;
|
|
41
|
+
}
|
|
42
|
+
|
|
27
43
|
// statusline 写者不依赖桌宠进程存在,所以 RuntimeIsActive() 不能证明额度文件无人使用。
|
|
28
44
|
// 清理额度缓存前必须拿到同一把锁;锁文件本身刻意长期保留,持锁时 unlink 仍会让后来者
|
|
29
45
|
// 创建新 inode,导致新旧两把锁同时存在、互斥失效。
|
|
@@ -136,6 +152,9 @@ int CleanCCPetsData(BOOL purge) {
|
|
|
136
152
|
ClientStateDirectory(), buildCache ?: @""]) {
|
|
137
153
|
if (RemoveIfPresent(path)) removed += 1;
|
|
138
154
|
}
|
|
155
|
+
// Token 摘要缓存也是纯缓存(丢了只是重新解析一遍会话文件),原先漏在清理之外,
|
|
156
|
+
// 于是缓存一旦出问题,cc-pets clean 救不回来,只能让用户手动删文件。
|
|
157
|
+
removed += RemoveTokenCaches(supportDirectory);
|
|
139
158
|
NSString *updateLog = [supportDirectory stringByAppendingPathComponent:@"update.log"];
|
|
140
159
|
if (RemoveIfPresent(updateLog)) removed += 1;
|
|
141
160
|
if (purge) {
|
|
@@ -978,6 +978,29 @@ static NSMutableDictionary *LoadTokenSummaryCache(NSString *path) {
|
|
|
978
978
|
return cache;
|
|
979
979
|
}
|
|
980
980
|
|
|
981
|
+
// NSData 是类簇,可变性判断不能靠 isKindOfClass:二进制 plist 解析出来的**不可变**
|
|
982
|
+
// __NSCFData 对 isKindOfClass:NSMutableData 同样返回 YES。据此把只读缓冲区当成可变的
|
|
983
|
+
// 原地 appendBytes,就会写到它末尾之外——ASan 抓到的正是这一下:
|
|
984
|
+
// heap-buffer-overflow, WRITE of size 48, 0 bytes after 6640-byte region
|
|
985
|
+
// 分配点 __CFBinaryPlistCreateObjectFiltered <- LoadTokenSummaryCache
|
|
986
|
+
// 越界写破坏堆之后进程不会当场死,而是在之后任意一次内存操作上倒下,因此崩溃点看起来
|
|
987
|
+
// 和这里毫无关系(实际报告里落在 EnumerateLines 和 SaveTokenSummaryCache 上)。
|
|
988
|
+
//
|
|
989
|
+
// 可靠的做法只有一条:落盘缓存一读回来就把 records 换成本进程创建的可变副本,
|
|
990
|
+
// 之后 ClaudeRecordsForFile 的原地追加才成立。只替换 records,其余字段原样保留——
|
|
991
|
+
// Codex 侧的条目形状不同(start/total/segments/days…),整条重建会把它们抹掉。
|
|
992
|
+
static void MakeCachedRecordsMutable(NSMutableDictionary *cache) {
|
|
993
|
+
for (NSString *path in cache.allKeys) {
|
|
994
|
+
NSDictionary *entry = cache[path];
|
|
995
|
+
if (![entry isKindOfClass:NSDictionary.class]) continue;
|
|
996
|
+
NSData *records = [entry[@"records"] isKindOfClass:NSData.class] ? entry[@"records"] : nil;
|
|
997
|
+
if (!records) continue;
|
|
998
|
+
NSMutableDictionary *updated = [entry mutableCopy];
|
|
999
|
+
updated[@"records"] = [records mutableCopy];
|
|
1000
|
+
cache[path] = updated;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
|
|
981
1004
|
static void SaveTokenSummaryCache(NSDictionary *cache, NSString *path) {
|
|
982
1005
|
if (!path.length) return;
|
|
983
1006
|
NSData *data = [NSPropertyListSerialization dataWithPropertyList:cache
|
|
@@ -1518,18 +1541,19 @@ static NSData *ClaudeRecordsForFile(NSURL *url, NSMutableDictionary *cache) {
|
|
|
1518
1541
|
unsigned long long size = [attributes[NSFileSize] unsignedLongLongValue];
|
|
1519
1542
|
NSDictionary *cached = cache[url.path];
|
|
1520
1543
|
unsigned long long offset = [cached[@"offset"] unsignedLongLongValue];
|
|
1521
|
-
//
|
|
1544
|
+
// 这里可以直接原地追加:可变性由 MakeCachedRecordsMutable 在缓存读回时统一保证。
|
|
1545
|
+
// 不要退回成 isKindOfClass:NSMutableData 判断——NSData 是类簇,落盘缓存解析出的
|
|
1546
|
+
// 不可变 __NSCFData 对它同样返回 YES,据此原地 appendBytes 会写出界。详见该函数注释。
|
|
1522
1547
|
NSMutableData *records = [cached[@"records"] isKindOfClass:NSData.class]
|
|
1523
|
-
? (
|
|
1524
|
-
? cached[@"records"] : [cached[@"records"] mutableCopy])
|
|
1525
|
-
: nil;
|
|
1548
|
+
? (NSMutableData *)cached[@"records"] : nil;
|
|
1526
1549
|
if (!records || offset > size) {
|
|
1527
1550
|
// 首次读取,或文件被改写导致偏移失效,整篇重来。
|
|
1528
1551
|
offset = 0;
|
|
1529
1552
|
records = [NSMutableData data];
|
|
1530
1553
|
}
|
|
1531
1554
|
if (cached && offset == size) {
|
|
1532
|
-
//
|
|
1555
|
+
// 正常情况下 records 就是缓存里那一份,不必回写;只有上面因偏移失效重建过
|
|
1556
|
+
// (文件被清空到 0 字节)才是新对象,这时写回去,免得下一轮又重建一遍。
|
|
1533
1557
|
if (records != cached[@"records"]) {
|
|
1534
1558
|
cache[url.path] = @{@"offset": @(offset), @"records": records};
|
|
1535
1559
|
}
|
|
@@ -1645,6 +1669,7 @@ static NSArray<NSDictionary *> *ClaudeDailyTokenSeries(NSArray<NSData *> *record
|
|
|
1645
1669
|
_projectsURL = ClaudeProjectsURL();
|
|
1646
1670
|
_recordCachePath = TokenSummaryCachePath(@"claude", _projectsURL.path);
|
|
1647
1671
|
_recordCache = LoadTokenSummaryCache(_recordCachePath);
|
|
1672
|
+
MakeCachedRecordsMutable(_recordCache);
|
|
1648
1673
|
_monthlyArchivePath = MonthlyUsageArchivePath(@"claude", _projectsURL.path);
|
|
1649
1674
|
_monthlyArchive = LoadTokenSummaryCache(_monthlyArchivePath);
|
|
1650
1675
|
}
|
package/Sources/CCPets/PetView.h
CHANGED
|
@@ -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;
|
package/Sources/CCPets/PetView.m
CHANGED
|
@@ -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 = 6.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)
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
if (
|
|
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 {
|
|
445
|
+
if (![name isKindOfClass:NSString.class]) return @"";
|
|
446
|
+
__block NSUInteger count = 0;
|
|
447
|
+
__block NSUInteger end = 0;
|
|
448
|
+
__block BOOL truncated = NO;
|
|
449
|
+
[name enumerateSubstringsInRange:NSMakeRange(0, name.length)
|
|
450
|
+
options:NSStringEnumerationByComposedCharacterSequences
|
|
451
|
+
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
|
|
452
|
+
if (count == 4) {
|
|
453
|
+
truncated = YES;
|
|
454
|
+
*stop = YES;
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
count += 1;
|
|
458
|
+
end = NSMaxRange(substringRange);
|
|
459
|
+
}];
|
|
460
|
+
if (!truncated) return name;
|
|
461
|
+
return [[name substringToIndex:end] stringByAppendingString:@"..."];
|
|
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
|
+
NSString *displayName = [self displayNameForPetName:name];
|
|
467
|
+
BOOL selected = [petID isEqualToString:self.currentPetID];
|
|
468
|
+
BOOL deletable = [petID hasPrefix:@"external:"];
|
|
469
|
+
|
|
470
|
+
NSFont *font = [NSFont menuFontOfSize:13];
|
|
471
|
+
CGFloat nameWidth = ceil([@"桌宠素材..." sizeWithAttributes:@{NSFontAttributeName: font}].width);
|
|
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 =
|
|
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:@"
|
|
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
|
-
|
|
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
|