cc-pets 2.2.0 → 2.2.1

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.
@@ -201,10 +201,10 @@ static void TrimUpdateLog(NSString *path) {
201
201
  [NSURL URLWithString:[@"https://github.com/" stringByAppendingString:CCPetsRepositorySlug]]];
202
202
  }
203
203
  }
204
- // 版本号以 npm Registry 为准(自动更新装的就是它),更新说明取同版本 tag 的 GitHub Release
205
- // 描述。Release 没写、没建、或 GitHub 请求失败都只是没有说明,不影响提示更新本身。
206
- - (void)fetchLatestReleaseWithCompletion:(void (^)(NSString *version, NSArray<NSString *> *highlights,
207
- BOOL highlightsTruncated, NSString *errorMessage))completion {
204
+ // 版本号以 npm Registry 为准(自动更新装的就是它),更新说明取同版本 tag 里的
205
+ // release notes 文件。说明没写、或请求全部失败都只是没有说明,不影响提示更新本身。
206
+ - (void)fetchLatestReleaseWithCompletion:(void (^)(NSString *version, NSString *notes,
207
+ NSString *errorMessage))completion {
208
208
  NSURL *url = [NSURL URLWithString:@"https://registry.npmjs.org/cc-pets/latest"];
209
209
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
210
210
  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
@@ -221,35 +221,59 @@ static void TrimUpdateLog(NSString *path) {
221
221
  NSComparisonResult comparison = CompareStableVersions(@CC_PETS_VERSION, latestVersion, &valid);
222
222
  if (error || httpResponse.statusCode != 200 || !valid) {
223
223
  NSString *message = error.localizedDescription ?: L(@"The npm registry returned invalid version info. Please try again later.");
224
- dispatch_async(dispatch_get_main_queue(), ^{ completion(nil, nil, NO, message); });
224
+ dispatch_async(dispatch_get_main_queue(), ^{ completion(nil, nil, message); });
225
225
  return;
226
226
  }
227
227
  if (comparison != NSOrderedAscending) {
228
- dispatch_async(dispatch_get_main_queue(), ^{ completion(latestVersion, nil, NO, nil); });
228
+ dispatch_async(dispatch_get_main_queue(), ^{ completion(latestVersion, nil, nil); });
229
229
  return;
230
230
  }
231
- NSString *releaseURL = [NSString stringWithFormat:
232
- @"https://api.github.com/repos/%@/releases/tags/v%@", CCPetsRepositorySlug, latestVersion];
233
- NSMutableURLRequest *releaseRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:releaseURL]
234
- cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
235
- [releaseRequest setValue:@"application/vnd.github+json" forHTTPHeaderField:@"Accept"];
236
- [releaseRequest setValue:@"cc-pets/" CC_PETS_VERSION forHTTPHeaderField:@"User-Agent"];
237
- NSURLSessionDataTask *releaseTask = [NSURLSession.sharedSession dataTaskWithRequest:releaseRequest
238
- completionHandler:^(NSData *releaseData, NSURLResponse *releaseResponse, NSError *releaseError) {
239
- NSInteger status = [releaseResponse isKindOfClass:NSHTTPURLResponse.class]
240
- ? ((NSHTTPURLResponse *)releaseResponse).statusCode : 0;
241
- NSDictionary *release = !releaseError && status == 200 && releaseData
242
- ? [NSJSONSerialization JSONObjectWithData:releaseData options:0 error:nil] : nil;
243
- NSString *body = [release isKindOfClass:NSDictionary.class] &&
244
- [release[@"body"] isKindOfClass:NSString.class] ? release[@"body"] : nil;
245
- BOOL truncated = NO;
246
- NSArray<NSString *> *highlights = ReleaseNoteHighlightsForLanguage(body,
247
- CCPetsCurrentLanguage(), UpdateHighlightLimit, UpdateHighlightMaxLength, &truncated);
248
- dispatch_async(dispatch_get_main_queue(), ^{
249
- completion(latestVersion, highlights, truncated, nil);
250
- });
231
+ [self fetchReleaseNotesForVersion:latestVersion sourceIndex:0 completion:^(NSString *body) {
232
+ dispatch_async(dispatch_get_main_queue(), ^{ completion(latestVersion, body, nil); });
251
233
  }];
252
- [releaseTask resume];
234
+ }];
235
+ [task resume];
236
+ }
237
+ // 更新说明按顺序尝试:tag 里的 docs/release-notes-vX.Y.Z.md(raw.githubusercontent.com,
238
+ // 再退到 jsDelivr),最后才是 GitHub Release API。未认证的 API 按出口 IP 每小时只给 60 次,
239
+ // 走共享代理时很容易被别人用光,所以只当兜底。全部失败时 body 为 nil。
240
+ - (void)fetchReleaseNotesForVersion:(NSString *)version sourceIndex:(NSUInteger)index
241
+ completion:(void (^)(NSString *body))completion {
242
+ NSString *notesPath = [NSString stringWithFormat:@"v%@/docs/release-notes-v%@.md", version, version];
243
+ NSArray<NSString *> *sources = @[
244
+ [NSString stringWithFormat:@"https://raw.githubusercontent.com/%@/%@", CCPetsRepositorySlug, notesPath],
245
+ [NSString stringWithFormat:@"https://cdn.jsdelivr.net/gh/%@@%@", CCPetsRepositorySlug, notesPath],
246
+ [NSString stringWithFormat:@"https://api.github.com/repos/%@/releases/tags/v%@",
247
+ CCPetsRepositorySlug, version],
248
+ ];
249
+ if (index >= sources.count) {
250
+ completion(nil);
251
+ return;
252
+ }
253
+ BOOL fromAPI = index == sources.count - 1;
254
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:sources[index]]
255
+ cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
256
+ if (fromAPI) [request setValue:@"application/vnd.github+json" forHTTPHeaderField:@"Accept"];
257
+ [request setValue:@"cc-pets/" CC_PETS_VERSION forHTTPHeaderField:@"User-Agent"];
258
+ NSURLSessionDataTask *task = [NSURLSession.sharedSession dataTaskWithRequest:request
259
+ completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
260
+ NSInteger status = [response isKindOfClass:NSHTTPURLResponse.class]
261
+ ? ((NSHTTPURLResponse *)response).statusCode : 0;
262
+ NSString *body = nil;
263
+ if (!error && status == 200 && data) {
264
+ if (fromAPI) {
265
+ NSDictionary *release = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
266
+ body = [release isKindOfClass:NSDictionary.class] &&
267
+ [release[@"body"] isKindOfClass:NSString.class] ? release[@"body"] : nil;
268
+ } else {
269
+ body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
270
+ }
271
+ }
272
+ if (body.length > 0) {
273
+ completion(body);
274
+ return;
275
+ }
276
+ [self fetchReleaseNotesForVersion:version sourceIndex:index + 1 completion:completion];
253
277
  }];
254
278
  [task resume];
255
279
  }
@@ -257,8 +281,7 @@ static void TrimUpdateLog(NSString *path) {
257
281
  if (self.checkingForUpdate || self.updating) return;
258
282
  self.checkingForUpdate = YES;
259
283
  __weak typeof(self) weakSelf = self;
260
- [self fetchLatestReleaseWithCompletion:^(NSString *version, NSArray<NSString *> *highlights,
261
- BOOL truncated, NSString *errorMessage) {
284
+ [self fetchLatestReleaseWithCompletion:^(NSString *version, NSString *notes, NSString *errorMessage) {
262
285
  typeof(self) strongSelf = weakSelf;
263
286
  if (!strongSelf) return;
264
287
  strongSelf.checkingForUpdate = NO;
@@ -273,7 +296,7 @@ static void TrimUpdateLog(NSString *path) {
273
296
  message:[NSString stringWithFormat:L(@"Version %@"), @CC_PETS_VERSION]];
274
297
  return;
275
298
  }
276
- [strongSelf rememberPendingUpdate:version highlights:highlights truncated:truncated];
299
+ [strongSelf rememberPendingUpdate:version notes:notes];
277
300
  [strongSelf showUpdateDialog];
278
301
  }];
279
302
  }
@@ -290,8 +313,7 @@ static void TrimUpdateLog(NSString *path) {
290
313
  self.lastSilentUpdateCheckAt = now;
291
314
  self.checkingForUpdate = YES;
292
315
  __weak typeof(self) weakSelf = self;
293
- [self fetchLatestReleaseWithCompletion:^(NSString *version, NSArray<NSString *> *highlights,
294
- BOOL truncated, NSString *errorMessage) {
316
+ [self fetchLatestReleaseWithCompletion:^(NSString *version, NSString *notes, NSString *errorMessage) {
295
317
  typeof(self) strongSelf = weakSelf;
296
318
  if (!strongSelf) return;
297
319
  strongSelf.checkingForUpdate = NO;
@@ -301,17 +323,16 @@ static void TrimUpdateLog(NSString *path) {
301
323
  [strongSelf clearPendingUpdate];
302
324
  return;
303
325
  }
304
- [strongSelf rememberPendingUpdate:version highlights:highlights truncated:truncated];
326
+ [strongSelf rememberPendingUpdate:version notes:notes];
305
327
  [strongSelf showUpdateBubble];
306
328
  }];
307
329
  }
308
- - (void)rememberPendingUpdate:(NSString *)version highlights:(NSArray<NSString *> *)highlights
309
- truncated:(BOOL)truncated {
330
+ // 存的是说明原文,弹窗时再按当时的界面语言解析:切换语言后不用重新联网,要点也跟着换。
331
+ - (void)rememberPendingUpdate:(NSString *)version notes:(NSString *)notes {
310
332
  // 「稍后」只针对当时那个版本;又出了更新的版本就重新提醒。
311
333
  if (![version isEqualToString:self.pendingUpdateVersion]) self.updateReminderSnoozed = NO;
312
334
  self.pendingUpdateVersion = version;
313
- self.pendingUpdateHighlights = highlights ?: @[];
314
- self.pendingUpdateHighlightsTruncated = truncated;
335
+ self.pendingUpdateNotes = notes;
315
336
  [self refreshUpdateBadge];
316
337
  }
317
338
  - (void)refreshUpdateBadge {
@@ -322,19 +343,19 @@ static void TrimUpdateLog(NSString *path) {
322
343
  }
323
344
  - (void)clearPendingUpdate {
324
345
  self.pendingUpdateVersion = nil;
325
- self.pendingUpdateHighlights = nil;
326
- self.pendingUpdateHighlightsTruncated = NO;
346
+ self.pendingUpdateNotes = nil;
327
347
  [self refreshUpdateBadge];
328
348
  if (self.updateBubbleVisible) [self hideSpeechBubble];
329
349
  }
330
350
  // 更新要点放进 accessoryView 而不是 informativeText:后者是一整段纯文本,列表项折行后
331
351
  // 第二行会顶到「•」下面,几条长说明挤成一坨。这里用悬挂缩进让折行对齐到文字起点。
332
- - (NSView *)updateHighlightsAccessoryView {
333
- if (self.pendingUpdateHighlights.count == 0) return nil;
352
+ - (NSView *)updateHighlightsAccessoryView:(NSArray<NSString *> *)highlights truncated:(BOOL)truncated {
353
+ if (highlights.count == 0) return nil;
334
354
  const CGFloat width = 300;
335
355
  NSFont *bodyFont = [NSFont systemFontOfSize:12];
336
356
  NSString *bullet = @"•\t";
337
- CGFloat indent = ceil([bullet sizeWithAttributes:@{NSFontAttributeName: bodyFont}].width) + 4;
357
+ // 只量「•」本身:连 \t 一起量会按默认 28pt 制表位展开,符号和文字之间空出一大截。
358
+ CGFloat indent = ceil([@"•" sizeWithAttributes:@{NSFontAttributeName: bodyFont}].width) + 6;
338
359
  NSMutableParagraphStyle *itemStyle = [NSMutableParagraphStyle new];
339
360
  itemStyle.tabStops = @[[[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft
340
361
  location:indent options:@{}]];
@@ -351,14 +372,14 @@ static void TrimUpdateLog(NSString *path) {
351
372
  NSDictionary *itemAttributes = @{NSFontAttributeName: bodyFont,
352
373
  NSForegroundColorAttributeName: NSColor.secondaryLabelColor,
353
374
  NSParagraphStyleAttributeName: itemStyle};
354
- [self.pendingUpdateHighlights enumerateObjectsUsingBlock:^(NSString *item, NSUInteger index, BOOL *stop) {
375
+ [highlights enumerateObjectsUsingBlock:^(NSString *item, NSUInteger index, BOOL *stop) {
355
376
  NSString *line = [NSString stringWithFormat:@"%@%@%@", bullet, item,
356
- index + 1 < self.pendingUpdateHighlights.count || self.pendingUpdateHighlightsTruncated
377
+ index + 1 < highlights.count || truncated
357
378
  ? @"\n" : @""];
358
379
  [text appendAttributedString:[[NSAttributedString alloc] initWithString:line
359
380
  attributes:itemAttributes]];
360
381
  }];
361
- if (self.pendingUpdateHighlightsTruncated) {
382
+ if (truncated) {
362
383
  [text appendAttributedString:[[NSAttributedString alloc] initWithString:L(@"More changes in the full GitHub release notes")
363
384
  attributes:@{NSFontAttributeName: [NSFont systemFontOfSize:11],
364
385
  NSForegroundColorAttributeName: NSColor.tertiaryLabelColor,
@@ -379,10 +400,13 @@ static void TrimUpdateLog(NSString *path) {
379
400
  NSAlert *alert = [NSAlert new];
380
401
  alert.messageText = [NSString stringWithFormat:L(@"CC Pets %@ Is Available"), version];
381
402
  alert.informativeText = [NSString stringWithFormat:L(@"Current version %@"), @CC_PETS_VERSION];
382
- alert.accessoryView = [self updateHighlightsAccessoryView];
403
+ BOOL truncated = NO;
404
+ NSArray<NSString *> *highlights = ReleaseNoteHighlightsForLanguage(self.pendingUpdateNotes,
405
+ CCPetsCurrentLanguage(), UpdateHighlightLimit, UpdateHighlightMaxLength, &truncated);
406
+ alert.accessoryView = [self updateHighlightsAccessoryView:highlights truncated:truncated];
383
407
  [alert addButtonWithTitle:L(@"Update Now")];
384
408
  [alert addButtonWithTitle:L(@"Later")];
385
- if (self.pendingUpdateHighlights.count > 0) [alert addButtonWithTitle:L(@"Release Notes")];
409
+ if (highlights.count > 0) [alert addButtonWithTitle:L(@"Release Notes")];
386
410
  NSModalResponse response = [alert runModal];
387
411
  if (response == NSAlertFirstButtonReturn) {
388
412
  [self startUpdateToVersion:version];
@@ -93,8 +93,7 @@
93
93
  @property BOOL checkingForUpdate;
94
94
  @property BOOL updating;
95
95
  @property(copy) NSString *pendingUpdateVersion;
96
- @property(copy) NSArray<NSString *> *pendingUpdateHighlights;
97
- @property BOOL pendingUpdateHighlightsTruncated;
96
+ @property(copy) NSString *pendingUpdateNotes;
98
97
  @property NSTimeInterval lastSilentUpdateCheckAt;
99
98
  @property BOOL updateBubbleVisible;
100
99
  @property BOOL updateBubbleDeferred;
@@ -159,7 +159,7 @@
159
159
  NSDictionary *selectedOption = [self petOptionWithID:selected inOptions:petOptions] ?: petOptions.firstObject;
160
160
  selected = selectedOption[@"id"];
161
161
  NSString *spritePath = selectedOption[@"path"];
162
- NSImage *image = LoadPetSpriteImage(spritePath, NSMakeSize(140, 150),
162
+ NSImage *image = LoadPetSpriteImage(spritePath, CCPetsPetDecodeCellSize(),
163
163
  [selectedOption[@"spriteRowCount"] integerValue] ?: 9);
164
164
  if (!image) {
165
165
  fprintf(stderr, "%s\n", [NSString stringWithFormat:L(@"Can't read the pet sprite: %@"), spritePath].UTF8String);
@@ -396,6 +396,8 @@
396
396
 
397
397
  NSScreen *screen = NSScreen.mainScreen;
398
398
  NSRect visible = screen.visibleFrame;
399
+ [self applyPetSizePreference];
400
+ size = self.panel.frame.size;
399
401
  [self.panel setFrameOrigin:NSMakePoint(NSMaxX(visible) - size.width - 24, NSMinY(visible) + 18)];
400
402
  [self.panel orderFrontRegardless];
401
403
  self.usageMonitor = [CCPetsUsageMonitor new];
@@ -1,4 +1,5 @@
1
1
  #import <Cocoa/Cocoa.h>
2
2
 
3
- // 按桌宠实际绘制尺寸解码精灵图,避免让超大外部素材长期占用完整 RGBA 内存。
3
+ // cellSize 使用逻辑点;按至少 2x 及当前屏幕最高倍率解码,保留 Retina 细节,
4
+ // 同时限制超大外部素材的 RGBA 内存占用。无需降采样时保留原图。
4
5
  NSImage *LoadPetSpriteImage(NSString *path, NSSize cellSize, NSInteger rowCount);
@@ -10,8 +10,16 @@ NSImage *LoadPetSpriteImage(NSString *path, NSSize cellSize, NSInteger rowCount)
10
10
  CGImageSourceCopyPropertiesAtIndex(source, 0, NULL));
11
11
  CGFloat sourceWidth = [properties[(NSString *)kCGImagePropertyPixelWidth] doubleValue];
12
12
  CGFloat sourceHeight = [properties[(NSString *)kCGImagePropertyPixelHeight] doubleValue];
13
- CGFloat desiredWidth = MAX(1.0, cellSize.width) * 8.0;
14
- CGFloat desiredHeight = MAX(1.0, cellSize.height) * rowCount;
13
+ // cellSize 是逻辑点,ImageIO 的缩略尺寸是物理像素。按 1x 解码再画入
14
+ // Retina 的 2x 帧缓存,只会放大已经丢掉细节的缩略图。
15
+ // 加载发生在窗口创建前,取所有屏幕的最高倍率,并至少保留 2x,避免
16
+ // 从普通屏移到 Retina 时源图不足。标准 Codex 精灵图因此直接保留原图。
17
+ CGFloat backingScale = 2.0;
18
+ for (NSScreen *screen in NSScreen.screens) {
19
+ backingScale = MAX(backingScale, screen.backingScaleFactor);
20
+ }
21
+ CGFloat desiredWidth = MAX(1.0, cellSize.width) * backingScale * 8.0;
22
+ CGFloat desiredHeight = MAX(1.0, cellSize.height) * backingScale * rowCount;
15
23
  CGFloat scale = MIN(1.0, MAX(desiredWidth / MAX(1.0, sourceWidth),
16
24
  desiredHeight / MAX(1.0, sourceHeight)));
17
25
  if (scale >= 0.98) {
@@ -0,0 +1,10 @@
1
+ #import <Cocoa/Cocoa.h>
2
+
3
+ extern NSString *const CCPetsPetScaleKey;
4
+ extern const CGFloat CCPetsPetMinimumScale;
5
+ extern const CGFloat CCPetsPetMaximumScale;
6
+ CGFloat CCPetsPetScalePreference(void);
7
+ // 状态卡与消息气泡随宠物缩小,放大时最多使用原尺寸。
8
+ CGFloat CCPetsBubbleScalePreference(void);
9
+ // 按滑动条的最大尺寸预解码,连续缩放时无需重新读文件。
10
+ NSSize CCPetsPetDecodeCellSize(void);
@@ -0,0 +1,21 @@
1
+ #import "CCPetsPetSize.h"
2
+ #import <math.h>
3
+
4
+ NSString *const CCPetsPetScaleKey = @"CCPetsPetScale";
5
+ const CGFloat CCPetsPetMinimumScale = 0.5;
6
+ const CGFloat CCPetsPetMaximumScale = 2.0;
7
+
8
+ CGFloat CCPetsPetScalePreference(void) {
9
+ id value = [NSUserDefaults.standardUserDefaults objectForKey:CCPetsPetScaleKey];
10
+ if (![value isKindOfClass:NSNumber.class]) return 1.0;
11
+ double scale = [value doubleValue];
12
+ return isfinite(scale) ? MAX(CCPetsPetMinimumScale, MIN(CCPetsPetMaximumScale, scale)) : 1.0;
13
+ }
14
+
15
+ NSSize CCPetsPetDecodeCellSize(void) {
16
+ return NSMakeSize(140 * CCPetsPetMaximumScale, 150 * CCPetsPetMaximumScale);
17
+ }
18
+
19
+ CGFloat CCPetsBubbleScalePreference(void) {
20
+ return MIN(CCPetsPetScalePreference(), 1.0);
21
+ }
@@ -72,6 +72,13 @@ NSString *PetPhrasesKeyForPetID(NSString *petID);
72
72
  // 某只宠物的专属词库路径。petID 为空返回 nil。
73
73
  NSString *PetPhrasesFilePathForPetID(NSString *petID);
74
74
 
75
+ // 全局台词来源偏好,切换宠物和重启后仍保留。未设置时保持原来的专属优先行为。
76
+ // 专属模式按小节回落到通用词库;默认模式只读通用词库,不改写任何台词文件。
77
+ extern NSString *const PetPhrasesSourcePet;
78
+ extern NSString *const PetPhrasesSourceDefault;
79
+ NSString *PetPhrasesSource(void);
80
+ void PetPhrasesSetSource(NSString *source);
81
+
75
82
  // 当前是哪只宠物。取词层本来是纯函数,这里是唯一的外部状态——桌宠启动和每次切换
76
83
  // 宠物时由 AppDelegate 推进来。做成 setter 而不是让 PetPhraseForTag 多带一个参数:
77
84
  // 说话的调用点散在十几处,每处都传一遍 petID 只会让人漏传,而漏传的表现是"专属台词
@@ -1,6 +1,21 @@
1
1
  #import "CCPetsPhrases.h"
2
2
  #import "CCPetsL10n.h"
3
3
 
4
+ NSString *const PetPhrasesSourcePet = @"pet";
5
+ NSString *const PetPhrasesSourceDefault = @"default";
6
+ static NSString *const PetPhrasesSourceKey = @"CCPetsPhrasesSource";
7
+
8
+ NSString *PetPhrasesSource(void) {
9
+ NSString *source = [NSUserDefaults.standardUserDefaults stringForKey:PetPhrasesSourceKey];
10
+ return [source isEqualToString:PetPhrasesSourceDefault] ?
11
+ PetPhrasesSourceDefault : PetPhrasesSourcePet;
12
+ }
13
+
14
+ void PetPhrasesSetSource(NSString *source) {
15
+ if (![@[PetPhrasesSourcePet, PetPhrasesSourceDefault] containsObject:source]) return;
16
+ [NSUserDefaults.standardUserDefaults setObject:source forKey:PetPhrasesSourceKey];
17
+ }
18
+
4
19
  NSString *const PetPhraseTagDone = @"done";
5
20
  NSString *const PetPhraseTagFail = @"fail";
6
21
  NSString *const PetPhraseTagQuotaLow = @"quota_low";
@@ -228,26 +243,23 @@ NSArray<NSString *> *PetPhraseAllTags(void) {
228
243
  }
229
244
 
230
245
  NSString *PetPhraseTagDescription(NSString *tag) {
231
- static NSDictionary<NSString *, NSString *> *map;
232
- static dispatch_once_t once;
233
- dispatch_once(&once, ^{
234
- map = @{
235
- PetPhraseTagIdle: L(@"idle"), PetPhraseTagDone: L(@"Task done"),
236
- PetPhraseTagFail: L(@"repeated failures"), PetPhraseTagWake: L(@"back after a long break"),
237
- PetPhraseTagLateNight: L(@"working late at night"), PetPhraseTagLongSession: L(@"long session"),
238
- PetPhraseTagQuotaLow: L(@"quota running low"),
239
- PetPhraseTagClickHeart: L(@"happy after a few clicks"),
240
- PetPhraseTagClickAnnoyed: L(@"annoyed after too many clicks"),
241
- PetPhraseTagStateStarting: L(@"Starting"), PetPhraseTagStateIdle: L(@"Idle"),
242
- PetPhraseTagStateThinking: L(@"Thinking"), PetPhraseTagStateTool: L(@"using a tool"),
243
- PetPhraseTagStateToolBash: L(@"Running a command"), PetPhraseTagStateToolEdit: L(@"Editing files"),
244
- PetPhraseTagStateToolRead: L(@"Looking things up"), PetPhraseTagStateToolDone: L(@"step done"),
245
- PetPhraseTagStateToolFailed: L(@"Tool failed"), PetPhraseTagStateSubagent: L(@"Subagent working"),
246
- PetPhraseTagStateApproval: L(@"Awaiting approval"), PetPhraseTagStateAutoReview: L(@"Auto-reviewing"),
247
- PetPhraseTagStateCompleted: L(@"Task completed"), PetPhraseTagStateFailed: L(@"Task failed"),
248
- PetPhraseTagStateNotification: L(@"Needs attention"),
249
- };
250
- });
246
+ // 翻译跟随当前语言,不能用 dispatch_once 固定成首次调用时的语言。
247
+ NSDictionary<NSString *, NSString *> *map = @{
248
+ PetPhraseTagIdle: L(@"idle"), PetPhraseTagDone: L(@"Task done"),
249
+ PetPhraseTagFail: L(@"repeated failures"), PetPhraseTagWake: L(@"back after a long break"),
250
+ PetPhraseTagLateNight: L(@"working late at night"), PetPhraseTagLongSession: L(@"long session"),
251
+ PetPhraseTagQuotaLow: L(@"quota running low"),
252
+ PetPhraseTagClickHeart: L(@"happy after a few clicks"),
253
+ PetPhraseTagClickAnnoyed: L(@"annoyed after too many clicks"),
254
+ PetPhraseTagStateStarting: L(@"Starting"), PetPhraseTagStateIdle: L(@"Idle"),
255
+ PetPhraseTagStateThinking: L(@"Thinking"), PetPhraseTagStateTool: L(@"using a tool"),
256
+ PetPhraseTagStateToolBash: L(@"Running a command"), PetPhraseTagStateToolEdit: L(@"Editing files"),
257
+ PetPhraseTagStateToolRead: L(@"Looking things up"), PetPhraseTagStateToolDone: L(@"step done"),
258
+ PetPhraseTagStateToolFailed: L(@"Tool failed"), PetPhraseTagStateSubagent: L(@"Subagent working"),
259
+ PetPhraseTagStateApproval: L(@"Awaiting approval"), PetPhraseTagStateAutoReview: L(@"Auto-reviewing"),
260
+ PetPhraseTagStateCompleted: L(@"Task completed"), PetPhraseTagStateFailed: L(@"Task failed"),
261
+ PetPhraseTagStateNotification: L(@"Needs attention"),
262
+ };
251
263
  return map[tag] ?: tag;
252
264
  }
253
265
 
@@ -460,7 +472,9 @@ static NSString *FillSlots(NSString *template, NSDictionary<NSString *, NSString
460
472
  // 标签却不写台词,是用户在明确表达"这只宠物在这个情境闭嘴",那时必须返回空,
461
473
  // 落回通用的话这个表达就没有任何写法能实现了。
462
474
  static NSArray<NSString *> *CandidatesForTag(NSString *tag) {
463
- NSArray *entries = LoadPetPhrases()[tag];
475
+ // 默认模式跳过专属词库,保留用户编辑过的通用台词。
476
+ NSArray *entries = [PetPhrasesSource() isEqualToString:PetPhrasesSourcePet] ?
477
+ LoadPetPhrases()[tag] : nil;
464
478
  if (entries == nil) entries = LoadUserPhrases()[tag];
465
479
  if (![entries isKindOfClass:NSArray.class]) return @[];
466
480
  NSMutableArray<NSString *> *result = [NSMutableArray array];
@@ -8,6 +8,7 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
8
8
  @property NSTextView *textView;
9
9
  @property NSPopUpButton *tagPicker;
10
10
  @property NSTextField *statusLabel;
11
+ @property NSButton *speakButton;
11
12
  @property NSButton *saveButton;
12
13
  @property NSButton *saveAndCloseButton;
13
14
  @property NSButton *revertButton;
@@ -42,6 +43,7 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
42
43
  PetPhrasesEditorController *controller = [self shared];
43
44
  [controller buildWindowIfNeeded];
44
45
  [controller syncScopeToCurrentPet];
46
+ [controller refreshLocalizedUI];
45
47
  [controller reloadFromDisk];
46
48
  [NSApp activateIgnoringOtherApps:YES];
47
49
  [controller.window makeKeyAndOrderFront:nil];
@@ -111,6 +113,7 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
111
113
  NSButton *speak = [NSButton buttonWithTitle:L(@"Try a Line") target:self
112
114
  action:@selector(speakSample:)];
113
115
  speak.translatesAutoresizingMaskIntoConstraints = NO;
116
+ self.speakButton = speak;
114
117
 
115
118
  NSButton *save = [NSButton buttonWithTitle:L(@"Save") target:self action:@selector(save:)];
116
119
  // ⌘S 而不是回车:这是个多行文本编辑器,回车要用来换行。
@@ -182,6 +185,35 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
182
185
  ]];
183
186
 
184
187
  self.window = window;
188
+ [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(languageDidChange:)
189
+ name:CCPetsLanguageDidChangeNotification object:nil];
190
+ }
191
+
192
+ // 只更新界面文案,保留编辑内容、撤销记录和当前编辑的宠物。
193
+ - (void)refreshLocalizedUI {
194
+ self.window.title = L(@"Pet Lines");
195
+ self.speakButton.title = L(@"Try a Line");
196
+ self.saveButton.title = L(@"Save");
197
+ self.saveAndCloseButton.title = L(@"Save & Close");
198
+ self.fillButton.title = L(@"Insert Template");
199
+ [self.scopeControl setLabel:L(@"Shared") forSegment:0];
200
+ [self.scopeControl setLabel:self.scopePetID.length > 0 ?
201
+ [NSString stringWithFormat:L(@"This Pet · %@"),
202
+ [self displayNameForPetID:self.scopePetID]] : L(@"This Pet") forSegment:1];
203
+ [self syncRevertButtonTitle];
204
+ // 更新原有菜单项,不改变用户选中的情境。
205
+ for (NSMenuItem *item in self.tagPicker.itemArray) {
206
+ NSString *tag = item.representedObject;
207
+ item.title = [NSString stringWithFormat:L(@"%1$@ (%2$@)"),
208
+ PetPhraseTagDescription(tag), tag];
209
+ }
210
+ }
211
+
212
+ - (void)languageDidChange:(NSNotification *)notification {
213
+ [self refreshLocalizedUI];
214
+ // 通用词库可能已随语言切换换成新默认内容;有草稿时只刷新界面,不能重载覆盖它。
215
+ if (![self isDirty]) [self reloadFromDisk];
216
+ else [self showCurrentFileStatus];
185
217
  }
186
218
 
187
219
  #pragma mark - 编辑范围
@@ -277,7 +309,12 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
277
309
  self.loadedText = text;
278
310
  self.loadedStamp = [NSFileManager.defaultManager attributesOfItemAtPath:path
279
311
  error:nil][NSFileModificationDate];
280
- if (self.petScope && text.length == 0) {
312
+ [self showCurrentFileStatus];
313
+ }
314
+
315
+ - (void)showCurrentFileStatus {
316
+ NSString *path = [self currentFilePath];
317
+ if (self.petScope && self.textView.string.length == 0) {
281
318
  [self showStatus:[NSString stringWithFormat:
282
319
  L(@"%@ · Still empty, so this pet uses the shared lines. Only write the sections you want to change, or click “Insert Template” at the top right to see what's possible."), path]
283
320
  warning:NO];
@@ -6,6 +6,9 @@
6
6
  target:(id)target action:(SEL)action width:(CGFloat)width {
7
7
  // item 要有 action 才会被菜单高亮。鼠标点击由行视图自己处理(菜单不关);
8
8
  // 用键盘选中后按回车走的是这个 action,行为与点击一致,菜单照常关闭。
9
+ // 所有设置单选行统一按文案撑宽,避免英文标题被截断。
10
+ width = MAX(width, ceil([title sizeWithAttributes:
11
+ @{NSFontAttributeName: [NSFont menuFontOfSize:13]}].width) + 42);
9
12
  NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:@selector(chooseFromMenuItem:)
10
13
  keyEquivalent:@""];
11
14
  MenuChoiceRowView *row = [[MenuChoiceRowView alloc] initWithFrame:NSMakeRect(0, 0, width, 24)];
@@ -0,0 +1,5 @@
1
+ #import <Cocoa/Cocoa.h>
2
+
3
+ @interface MenuPetSizeView : NSView
4
+ - (instancetype)initWithScale:(CGFloat)scale target:(id)target action:(SEL)action;
5
+ @end
@@ -0,0 +1,73 @@
1
+ #import "MenuPetSizeView.h"
2
+ #import "CCPetsPetSize.h"
3
+ #import "CCPetsL10n.h"
4
+
5
+ @interface MenuPetSizeView ()
6
+ @property NSSlider *slider;
7
+ @property NSTextField *percentage;
8
+ @property(weak) id target;
9
+ @property SEL action;
10
+ @end
11
+
12
+ @implementation MenuPetSizeView
13
+ - (instancetype)initWithScale:(CGFloat)scale target:(id)target action:(SEL)action {
14
+ CGFloat resetWidth = ceil([L(@"Reset Size") sizeWithAttributes:
15
+ @{NSFontAttributeName: [NSFont menuFontOfSize:12]}].width) + 24;
16
+ CGFloat width = MAX(220, resetWidth + 94);
17
+ self = [super initWithFrame:NSMakeRect(0, 0, width, 92)];
18
+ if (!self) return nil;
19
+ self.autoresizingMask = NSViewWidthSizable;
20
+ self.target = target;
21
+ self.action = action;
22
+
23
+ self.percentage = [NSTextField labelWithString:@""];
24
+ self.percentage.frame = NSMakeRect(14, 65, width - 28, 18);
25
+ self.percentage.font = [NSFont monospacedDigitSystemFontOfSize:13 weight:NSFontWeightMedium];
26
+ self.percentage.alignment = NSTextAlignmentCenter;
27
+ self.percentage.autoresizingMask = NSViewWidthSizable;
28
+ [self addSubview:self.percentage];
29
+
30
+ self.slider = [NSSlider sliderWithValue:scale minValue:CCPetsPetMinimumScale
31
+ maxValue:CCPetsPetMaximumScale target:self action:@selector(sliderChanged:)];
32
+ self.slider.frame = NSMakeRect(14, 37, width - 28, 24);
33
+ self.slider.continuous = YES;
34
+ self.slider.autoresizingMask = NSViewWidthSizable;
35
+ self.slider.accessibilityLabel = L(@"Pet Size");
36
+ [self addSubview:self.slider];
37
+
38
+ NSTextField *minimum = [NSTextField labelWithString:@"50%"];
39
+ minimum.frame = NSMakeRect(14, 10, 44, 16);
40
+ minimum.font = [NSFont menuFontOfSize:11];
41
+ minimum.textColor = NSColor.secondaryLabelColor;
42
+ [self addSubview:minimum];
43
+ NSTextField *maximum = [NSTextField labelWithString:@"200%"];
44
+ maximum.frame = NSMakeRect(width - 58, 10, 44, 16);
45
+ maximum.font = minimum.font;
46
+ maximum.textColor = minimum.textColor;
47
+ maximum.alignment = NSTextAlignmentRight;
48
+ maximum.autoresizingMask = NSViewMinXMargin;
49
+ [self addSubview:maximum];
50
+
51
+ NSButton *reset = [NSButton buttonWithTitle:L(@"Reset Size") target:self action:@selector(resetSize:)];
52
+ reset.bezelStyle = NSBezelStyleRounded;
53
+ reset.font = [NSFont menuFontOfSize:12];
54
+ reset.frame = NSMakeRect((width - resetWidth) / 2, 5, resetWidth, 26);
55
+ reset.autoresizingMask = NSViewMinXMargin | NSViewMaxXMargin;
56
+ [self addSubview:reset];
57
+ [self updatePercentage];
58
+ return self;
59
+ }
60
+ - (void)updatePercentage {
61
+ self.percentage.stringValue = [NSString stringWithFormat:@"%.0f%%", self.slider.doubleValue * 100];
62
+ }
63
+ - (void)sliderChanged:(NSSlider *)sender {
64
+ [NSApp sendAction:self.action to:self.target from:sender];
65
+ [self updatePercentage];
66
+ }
67
+ - (void)resetSize:(id)sender {
68
+ self.slider.doubleValue = 1.0;
69
+ [self sliderChanged:self.slider];
70
+ }
71
+ // 消费行内空白处的点击,菜单保持打开以便继续调整。
72
+ - (void)mouseDown:(NSEvent *)event {}
73
+ @end