cc-pets 0.2.16 → 0.2.17

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.
@@ -324,7 +324,7 @@ static NSDictionary *LatestUsage(void) {
324
324
  }
325
325
  @end
326
326
 
327
- @interface PetView : NSView
327
+ @interface PetView : NSView <NSMenuDelegate>
328
328
  @property NSImage *sheet;
329
329
  @property NSInteger spriteRowCount;
330
330
  @property NSInteger lookDirection;
@@ -345,6 +345,9 @@ static NSDictionary *LatestUsage(void) {
345
345
  @property(copy) void (^dragStateChanged)(BOOL dragging);
346
346
  @property(copy) NSArray<NSDictionary *> *(^petOptionsRequested)(void);
347
347
  @property(copy) void (^switchPetRequested)(NSString *petID);
348
+ @property NSMutableDictionary<NSString *, NSImage *> *petMenuPreviewCache;
349
+ @property NSPanel *petMenuPreviewPanel;
350
+ @property NSImageView *petMenuPreviewImageView;
348
351
  @end
349
352
 
350
353
  @implementation PetView
@@ -354,6 +357,7 @@ static NSDictionary *LatestUsage(void) {
354
357
  _spriteRowCount = rowCount;
355
358
  _lookDirection = -1;
356
359
  _frames = @[@0, @1, @2, @3, @4, @5];
360
+ _petMenuPreviewCache = [NSMutableDictionary dictionary];
357
361
  [NSTimer scheduledTimerWithTimeInterval:0.22 target:self
358
362
  selector:@selector(nextFrame:) userInfo:nil repeats:YES];
359
363
  NSTrackingArea *tracking = [[NSTrackingArea alloc] initWithRect:NSZeroRect
@@ -599,20 +603,135 @@ static NSDictionary *LatestUsage(void) {
599
603
  self.needsDisplay = YES;
600
604
  }
601
605
  - (void)selectPet:(NSMenuItem *)sender {
602
- NSString *petID = sender.representedObject;
606
+ NSDictionary *pet = [sender.representedObject isKindOfClass:NSDictionary.class]
607
+ ? sender.representedObject : nil;
608
+ NSString *petID = pet[@"id"];
603
609
  if (petID.length > 0 && self.switchPetRequested) self.switchPetRequested(petID);
604
610
  }
611
+ - (NSImage *)menuPreviewForPet:(NSDictionary *)pet {
612
+ NSString *path = [pet[@"path"] isKindOfClass:NSString.class] ? pet[@"path"] : nil;
613
+ NSInteger rowCount = [pet[@"spriteRowCount"] integerValue];
614
+ if (path.length == 0 || rowCount <= 0) return nil;
615
+
616
+ NSString *cacheKey = [NSString stringWithFormat:@"%@:%ld", path, (long)rowCount];
617
+ NSImage *cached = self.petMenuPreviewCache[cacheKey];
618
+ if (cached) return cached;
619
+
620
+ NSImage *sheet = [[NSImage alloc] initWithContentsOfFile:path];
621
+ if (!sheet || sheet.size.width <= 0 || sheet.size.height <= 0) return nil;
622
+
623
+ CGFloat cellWidth = sheet.size.width / 8.0;
624
+ CGFloat cellHeight = sheet.size.height / rowCount;
625
+ NSRect firstFrame = NSMakeRect(0, sheet.size.height - cellHeight, cellWidth, cellHeight);
626
+ CGFloat previewHeight = 76.0;
627
+ CGFloat previewWidth = round(previewHeight * cellWidth / cellHeight);
628
+ NSImage *preview = [[NSImage alloc] initWithSize:NSMakeSize(previewWidth, previewHeight)];
629
+ [preview lockFocus];
630
+ [sheet drawInRect:NSMakeRect(0, 0, previewWidth, previewHeight) fromRect:firstFrame
631
+ operation:NSCompositingOperationSourceOver fraction:1.0
632
+ respectFlipped:NO hints:@{NSImageHintInterpolation: @(NSImageInterpolationNone)}];
633
+ [preview unlockFocus];
634
+ self.petMenuPreviewCache[cacheKey] = preview;
635
+ return preview;
636
+ }
637
+ - (void)preparePetMenuPreviewPanel {
638
+ if (self.petMenuPreviewPanel) return;
639
+
640
+ NSRect panelFrame = NSMakeRect(0, 0, 92, 100);
641
+ self.petMenuPreviewPanel = [[NSPanel alloc] initWithContentRect:panelFrame
642
+ styleMask:NSWindowStyleMaskBorderless | NSWindowStyleMaskNonactivatingPanel
643
+ backing:NSBackingStoreBuffered defer:NO];
644
+ self.petMenuPreviewPanel.opaque = NO;
645
+ self.petMenuPreviewPanel.backgroundColor = NSColor.clearColor;
646
+ self.petMenuPreviewPanel.hasShadow = YES;
647
+ self.petMenuPreviewPanel.ignoresMouseEvents = YES;
648
+ self.petMenuPreviewPanel.level = NSPopUpMenuWindowLevel + 1;
649
+ self.petMenuPreviewPanel.collectionBehavior = NSWindowCollectionBehaviorTransient |
650
+ NSWindowCollectionBehaviorMoveToActiveSpace;
651
+
652
+ NSVisualEffectView *background = [[NSVisualEffectView alloc] initWithFrame:panelFrame];
653
+ background.material = NSVisualEffectMaterialMenu;
654
+ background.blendingMode = NSVisualEffectBlendingModeBehindWindow;
655
+ background.state = NSVisualEffectStateActive;
656
+ background.wantsLayer = YES;
657
+ background.layer.cornerRadius = 10.0;
658
+ background.layer.masksToBounds = YES;
659
+
660
+ self.petMenuPreviewImageView = [[NSImageView alloc] initWithFrame:NSInsetRect(panelFrame, 8, 8)];
661
+ self.petMenuPreviewImageView.imageScaling = NSImageScaleProportionallyUpOrDown;
662
+ self.petMenuPreviewImageView.animates = NO;
663
+ [background addSubview:self.petMenuPreviewImageView];
664
+ self.petMenuPreviewPanel.contentView = background;
665
+ }
666
+ - (void)showMenuPreviewForPet:(NSDictionary *)pet item:(NSMenuItem *)item menu:(NSMenu *)menu {
667
+ NSImage *preview = [self menuPreviewForPet:pet];
668
+ if (!preview) {
669
+ [self.petMenuPreviewPanel orderOut:nil];
670
+ return;
671
+ }
672
+ [self preparePetMenuPreviewPanel];
673
+ self.petMenuPreviewImageView.image = preview;
674
+
675
+ NSRect itemFrame = item.accessibilityFrame;
676
+ NSRect menuFrame = menu.accessibilityFrame;
677
+ if (NSIsEmptyRect(menuFrame)) {
678
+ NSPoint mouse = NSEvent.mouseLocation;
679
+ menuFrame = NSMakeRect(mouse.x - menu.size.width / 2.0,
680
+ mouse.y - menu.size.height / 2.0, menu.size.width, menu.size.height);
681
+ }
682
+ if (NSIsEmptyRect(itemFrame)) {
683
+ NSPoint mouse = NSEvent.mouseLocation;
684
+ itemFrame = NSMakeRect(menuFrame.origin.x, mouse.y - 11.0, menuFrame.size.width, 22.0);
685
+ }
686
+
687
+ // 子菜单可能根据屏幕空间展开到主菜单左侧。预览应放在整组菜单外侧,
688
+ // 不能只按当前子菜单定位,否则会覆盖它右边的主菜单。
689
+ NSRect menuGroupFrame = menuFrame;
690
+ for (NSMenu *ancestor = menu.supermenu; ancestor; ancestor = ancestor.supermenu) {
691
+ NSRect ancestorFrame = ancestor.accessibilityFrame;
692
+ if (!NSIsEmptyRect(ancestorFrame)) menuGroupFrame = NSUnionRect(menuGroupFrame, ancestorFrame);
693
+ }
694
+
695
+ NSPoint menuCenter = NSMakePoint(NSMidX(menuGroupFrame), NSMidY(menuGroupFrame));
696
+ NSScreen *targetScreen = NSScreen.mainScreen ?: NSScreen.screens.firstObject;
697
+ for (NSScreen *screen in NSScreen.screens) {
698
+ if (NSPointInRect(menuCenter, screen.frame)) {
699
+ targetScreen = screen;
700
+ break;
701
+ }
702
+ }
703
+ NSRect screenFrame = targetScreen.visibleFrame;
704
+ NSSize previewSize = self.petMenuPreviewPanel.frame.size;
705
+ CGFloat x = NSMaxX(menuGroupFrame) + 8.0;
706
+ if (x + previewSize.width > NSMaxX(screenFrame)) {
707
+ x = NSMinX(menuGroupFrame) - previewSize.width - 8.0;
708
+ }
709
+ CGFloat y = NSMidY(itemFrame) - previewSize.height / 2.0;
710
+ y = MIN(MAX(y, NSMinY(screenFrame)), NSMaxY(screenFrame) - previewSize.height);
711
+ [self.petMenuPreviewPanel setFrameOrigin:NSMakePoint(x, y)];
712
+ [self.petMenuPreviewPanel orderFront:nil];
713
+ }
714
+ - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item {
715
+ NSDictionary *pet = [item.representedObject isKindOfClass:NSDictionary.class]
716
+ ? item.representedObject : nil;
717
+ if (pet) [self showMenuPreviewForPet:pet item:item menu:menu];
718
+ else [self.petMenuPreviewPanel orderOut:nil];
719
+ }
720
+ - (void)menuDidClose:(NSMenu *)menu {
721
+ [self.petMenuPreviewPanel orderOut:nil];
722
+ }
605
723
  - (void)rightMouseDown:(NSEvent *)event {
606
724
  NSArray<NSDictionary *> *availablePets = self.petOptionsRequested ? self.petOptionsRequested() : @[];
607
725
  NSMenu *menu = [NSMenu new];
608
726
  NSMenuItem *switchItem = [menu addItemWithTitle:@"切换桌宠" action:nil keyEquivalent:@""];
609
727
  NSMenu *switchMenu = [NSMenu new];
728
+ switchMenu.delegate = self;
610
729
  for (NSDictionary *pet in availablePets) {
611
730
  NSString *petID = pet[@"id"];
612
731
  NSMenuItem *item = [switchMenu addItemWithTitle:pet[@"name"]
613
732
  action:@selector(selectPet:) keyEquivalent:@""];
614
733
  item.target = self;
615
- item.representedObject = petID;
734
+ item.representedObject = pet;
616
735
  item.state = [petID isEqualToString:self.currentPetID] ? NSControlStateValueOn : NSControlStateValueOff;
617
736
  }
618
737
  switchItem.submenu = switchMenu;
@@ -625,6 +744,7 @@ static NSDictionary *LatestUsage(void) {
625
744
  NSMenuItem *quitItem = [menu addItemWithTitle:@"退出桌宠" action:@selector(terminate:) keyEquivalent:@"q"];
626
745
  quitItem.target = NSApp;
627
746
  [NSMenu popUpContextMenu:menu withEvent:event forView:self];
747
+ [self.petMenuPreviewPanel orderOut:nil];
628
748
  }
629
749
  @end
630
750
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cc-pets",
3
- "version": "0.2.16",
3
+ "version": "0.2.17",
4
4
  "description": "A native macOS desktop pet for Codex CLI and Claude Code",
5
5
  "author": "wangxinyusqn",
6
6
  "license": "MIT",