cc-pets 1.0.1 → 1.0.4
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 +5 -0
- package/README.md +12 -2
- package/Sources/CCPets/CCPetsAppDelegate.h +54 -0
- package/Sources/CCPets/CCPetsAppDelegate.m +1158 -0
- package/Sources/CCPets/CCPetsCleanup.h +7 -0
- package/Sources/CCPets/CCPetsCleanup.m +126 -0
- package/Sources/CCPets/CCPetsEvents.h +10 -0
- package/Sources/CCPets/CCPetsEvents.m +232 -0
- package/Sources/CCPets/CCPetsImageLoader.h +4 -0
- package/Sources/CCPets/CCPetsImageLoader.m +36 -0
- package/Sources/CCPets/CCPetsPaths.h +23 -0
- package/Sources/CCPets/CCPetsPaths.m +48 -0
- package/Sources/CCPets/CCPetsPreviews.h +6 -0
- package/Sources/CCPets/CCPetsPreviews.m +159 -0
- package/Sources/CCPets/CCPetsQuotaHistory.h +7 -0
- package/Sources/CCPets/CCPetsQuotaHistory.m +83 -0
- package/Sources/CCPets/CCPetsUsage.h +19 -0
- package/Sources/CCPets/CCPetsUsage.m +289 -0
- package/Sources/CCPets/CCPetsUsageMonitor.h +10 -0
- package/Sources/CCPets/CCPetsUsageMonitor.m +163 -0
- package/Sources/CCPets/CCPetsVersion.h +10 -0
- package/Sources/CCPets/CCPetsVersion.m +62 -0
- package/Sources/CCPets/MenuToggleSwitch.h +5 -0
- package/Sources/CCPets/MenuToggleSwitch.m +47 -0
- package/Sources/CCPets/PetView.h +35 -0
- package/Sources/CCPets/PetView.m +511 -0
- package/Sources/CCPets/QuotaDashboardView.h +23 -0
- package/Sources/CCPets/QuotaDashboardView.m +318 -0
- package/Sources/CCPets/main.m +11 -2528
- package/bin/cc-pets +5 -0
- package/bin/claude-with-pet +2 -1
- package/bin/codex-with-pet +3 -1
- package/package.json +2 -3
- package/scripts/build.sh +20 -1
- package/scripts/install-git-hooks.sh +47 -0
- package/scripts/uninstall-app.sh +9 -0
- package/scripts/uninstall-shell-integration.sh +50 -2
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#import "MenuToggleSwitch.h"
|
|
2
|
+
|
|
3
|
+
@implementation MenuToggleSwitch
|
|
4
|
+
- (instancetype)initWithFrame:(NSRect)frameRect {
|
|
5
|
+
if ((self = [super initWithFrame:frameRect])) {
|
|
6
|
+
self.bordered = NO;
|
|
7
|
+
self.title = @"";
|
|
8
|
+
self.buttonType = NSButtonTypeToggle;
|
|
9
|
+
self.focusRingType = NSFocusRingTypeNone;
|
|
10
|
+
[self setAccessibilityRole:NSAccessibilityCheckBoxRole];
|
|
11
|
+
}
|
|
12
|
+
return self;
|
|
13
|
+
}
|
|
14
|
+
- (void)setState:(NSControlStateValue)value {
|
|
15
|
+
[super setState:value];
|
|
16
|
+
self.needsDisplay = YES;
|
|
17
|
+
}
|
|
18
|
+
- (void)drawRect:(NSRect)dirtyRect {
|
|
19
|
+
NSRect track = NSInsetRect(self.bounds, 1, 2);
|
|
20
|
+
BOOL enabled = self.state == NSControlStateValueOn;
|
|
21
|
+
NSColor *trackColor = enabled
|
|
22
|
+
? [NSColor.controlAccentColor colorWithAlphaComponent:self.highlighted ? 0.72 : 0.96]
|
|
23
|
+
: [NSColor colorWithWhite:self.highlighted ? 0.38 : 0.46 alpha:0.82];
|
|
24
|
+
[trackColor setFill];
|
|
25
|
+
NSBezierPath *trackPath = [NSBezierPath bezierPathWithRoundedRect:track
|
|
26
|
+
xRadius:NSHeight(track) / 2.0 yRadius:NSHeight(track) / 2.0];
|
|
27
|
+
[trackPath fill];
|
|
28
|
+
[[NSColor colorWithWhite:0 alpha:enabled ? 0.08 : 0.16] setStroke];
|
|
29
|
+
trackPath.lineWidth = 0.8;
|
|
30
|
+
[trackPath stroke];
|
|
31
|
+
|
|
32
|
+
CGFloat diameter = NSHeight(track) - 4;
|
|
33
|
+
CGFloat knobX = enabled
|
|
34
|
+
? NSMaxX(track) - diameter - 2
|
|
35
|
+
: NSMinX(track) + 2;
|
|
36
|
+
NSRect knob = NSMakeRect(knobX, NSMidY(track) - diameter / 2.0, diameter, diameter);
|
|
37
|
+
[NSGraphicsContext saveGraphicsState];
|
|
38
|
+
NSShadow *shadow = [NSShadow new];
|
|
39
|
+
shadow.shadowColor = [NSColor colorWithWhite:0 alpha:0.24];
|
|
40
|
+
shadow.shadowBlurRadius = 1.5;
|
|
41
|
+
shadow.shadowOffset = NSMakeSize(0, -0.5);
|
|
42
|
+
[shadow set];
|
|
43
|
+
[NSColor.whiteColor setFill];
|
|
44
|
+
[[NSBezierPath bezierPathWithOvalInRect:knob] fill];
|
|
45
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
46
|
+
}
|
|
47
|
+
@end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
|
|
3
|
+
@interface PetView : NSView <NSMenuDelegate>
|
|
4
|
+
@property NSImage *sheet;
|
|
5
|
+
@property NSInteger spriteRowCount;
|
|
6
|
+
@property(nonatomic) NSInteger lookDirection;
|
|
7
|
+
@property NSInteger frameIndex;
|
|
8
|
+
@property NSInteger rowIndex;
|
|
9
|
+
@property NSArray<NSNumber *> *frames;
|
|
10
|
+
@property NSUInteger sequenceIndex;
|
|
11
|
+
@property BOOL oneShot;
|
|
12
|
+
@property BOOL draggingPet;
|
|
13
|
+
@property BOOL agentActive;
|
|
14
|
+
@property NSPoint dragStartMouse;
|
|
15
|
+
@property NSPoint dragStartWindowOrigin;
|
|
16
|
+
@property NSString *currentPetID;
|
|
17
|
+
@property id globalMouseMonitor;
|
|
18
|
+
@property id localMouseMonitor;
|
|
19
|
+
@property(copy) void (^pocketHoverChanged)(BOOL hovering);
|
|
20
|
+
@property(copy) void (^positionChanged)(void);
|
|
21
|
+
@property(copy) void (^dragStateChanged)(BOOL dragging);
|
|
22
|
+
@property(copy) NSArray<NSDictionary *> *(^petOptionsRequested)(void);
|
|
23
|
+
@property(copy) void (^switchPetRequested)(NSString *petID);
|
|
24
|
+
@property NSCache<NSString *, NSImage *> *petMenuPreviewCache;
|
|
25
|
+
@property NSPanel *petMenuPreviewPanel;
|
|
26
|
+
@property NSImageView *petMenuPreviewImageView;
|
|
27
|
+
@property NSTimer *frameTimer;
|
|
28
|
+
@property(nonatomic) BOOL animationSuspended;
|
|
29
|
+
@property BOOL lowPowerMode;
|
|
30
|
+
@property BOOL reduceMotion;
|
|
31
|
+
- (instancetype)initWithFrame:(NSRect)frame sheet:(NSImage *)sheet rowCount:(NSInteger)rowCount;
|
|
32
|
+
- (void)applySheet:(NSImage *)sheet petID:(NSString *)petID rowCount:(NSInteger)rowCount;
|
|
33
|
+
- (void)handleAgentEvent:(NSString *)event tool:(NSString *)tool failed:(BOOL)failed;
|
|
34
|
+
- (void)playRow:(NSInteger)row throughFrame:(NSInteger)last oneShot:(BOOL)oneShot;
|
|
35
|
+
@end
|
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
#import "PetView.h"
|
|
2
|
+
#import "MenuToggleSwitch.h"
|
|
3
|
+
#import "CCPetsPaths.h"
|
|
4
|
+
#import "CCPetsImageLoader.h"
|
|
5
|
+
|
|
6
|
+
@implementation PetView
|
|
7
|
+
- (instancetype)initWithFrame:(NSRect)frame sheet:(NSImage *)sheet rowCount:(NSInteger)rowCount {
|
|
8
|
+
if ((self = [super initWithFrame:frame])) {
|
|
9
|
+
_sheet = sheet;
|
|
10
|
+
_spriteRowCount = rowCount;
|
|
11
|
+
_lookDirection = -1;
|
|
12
|
+
_frames = @[@0, @1, @2, @3, @4, @5];
|
|
13
|
+
_petMenuPreviewCache = [NSCache new];
|
|
14
|
+
_petMenuPreviewCache.countLimit = 24;
|
|
15
|
+
_petMenuPreviewCache.totalCostLimit = 4 * 1024 * 1024;
|
|
16
|
+
[NSNotificationCenter.defaultCenter addObserver:self
|
|
17
|
+
selector:@selector(powerStateDidChange:)
|
|
18
|
+
name:NSProcessInfoPowerStateDidChangeNotification object:nil];
|
|
19
|
+
[NSWorkspace.sharedWorkspace.notificationCenter addObserver:self
|
|
20
|
+
selector:@selector(powerStateDidChange:)
|
|
21
|
+
name:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification object:nil];
|
|
22
|
+
[self updatePowerPreferences];
|
|
23
|
+
[self updateFrameTimer];
|
|
24
|
+
NSTrackingArea *tracking = [[NSTrackingArea alloc] initWithRect:NSZeroRect
|
|
25
|
+
options:NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited |
|
|
26
|
+
NSTrackingActiveAlways | NSTrackingInVisibleRect
|
|
27
|
+
owner:self userInfo:nil];
|
|
28
|
+
[self addTrackingArea:tracking];
|
|
29
|
+
[self updateLookTracking];
|
|
30
|
+
}
|
|
31
|
+
return self;
|
|
32
|
+
}
|
|
33
|
+
// 只碰 ivar,不走属性 setter:setter 会调用 updateFrameTimer,在 dealloc 期间
|
|
34
|
+
// 重新排一个定时器出来。
|
|
35
|
+
- (void)dealloc {
|
|
36
|
+
[_frameTimer invalidate];
|
|
37
|
+
if (_globalMouseMonitor) [NSEvent removeMonitor:_globalMouseMonitor];
|
|
38
|
+
if (_localMouseMonitor) [NSEvent removeMonitor:_localMouseMonitor];
|
|
39
|
+
[NSNotificationCenter.defaultCenter removeObserver:self];
|
|
40
|
+
[NSWorkspace.sharedWorkspace.notificationCenter removeObserver:self];
|
|
41
|
+
}
|
|
42
|
+
- (void)powerStateDidChange:(NSNotification *)notification {
|
|
43
|
+
[self updatePowerPreferences];
|
|
44
|
+
}
|
|
45
|
+
- (void)updatePowerPreferences {
|
|
46
|
+
BOOL lowPower = NSProcessInfo.processInfo.lowPowerModeEnabled;
|
|
47
|
+
BOOL reduceMotion = NSWorkspace.sharedWorkspace.accessibilityDisplayShouldReduceMotion;
|
|
48
|
+
if (self.lowPowerMode == lowPower && self.reduceMotion == reduceMotion) return;
|
|
49
|
+
self.lowPowerMode = lowPower;
|
|
50
|
+
self.reduceMotion = reduceMotion;
|
|
51
|
+
[self.frameTimer invalidate];
|
|
52
|
+
self.frameTimer = nil;
|
|
53
|
+
[self updateFrameTimer];
|
|
54
|
+
}
|
|
55
|
+
// 动画帧只在“可见”且“不处于十六向跟随”时推进。跟随模式显示的是静态定格,
|
|
56
|
+
// nextFrame: 进来只会立刻 return,定时器纯属空转。
|
|
57
|
+
- (void)updateFrameTimer {
|
|
58
|
+
BOOL shouldRun = !self.animationSuspended && self.lookDirection < 0;
|
|
59
|
+
if (shouldRun == self.frameTimer.isValid) return;
|
|
60
|
+
if (!shouldRun) {
|
|
61
|
+
[self.frameTimer invalidate];
|
|
62
|
+
self.frameTimer = nil;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
// block 版定时器 + weak self,避免 NSTimer 持有 target 造成 PetView 永不释放。
|
|
66
|
+
__weak PetView *weakSelf = self;
|
|
67
|
+
NSTimeInterval interval = PetFrameInterval;
|
|
68
|
+
if (self.lowPowerMode) interval *= 1.5;
|
|
69
|
+
if (self.reduceMotion) interval *= 2.0;
|
|
70
|
+
self.frameTimer = [NSTimer scheduledTimerWithTimeInterval:interval repeats:YES
|
|
71
|
+
block:^(NSTimer *timer) { [weakSelf nextFrame:timer]; }];
|
|
72
|
+
self.frameTimer.tolerance = interval * 0.25;
|
|
73
|
+
}
|
|
74
|
+
- (void)setLookDirection:(NSInteger)value {
|
|
75
|
+
if (_lookDirection == value) return;
|
|
76
|
+
_lookDirection = value;
|
|
77
|
+
[self updateFrameTimer];
|
|
78
|
+
}
|
|
79
|
+
- (void)setAnimationSuspended:(BOOL)suspended {
|
|
80
|
+
if (_animationSuspended == suspended) return;
|
|
81
|
+
_animationSuspended = suspended;
|
|
82
|
+
[self updateFrameTimer];
|
|
83
|
+
if (!suspended) self.needsDisplay = YES;
|
|
84
|
+
}
|
|
85
|
+
- (void)startLookTracking {
|
|
86
|
+
if (self.spriteRowCount != 11 || self.globalMouseMonitor || self.localMouseMonitor) return;
|
|
87
|
+
__weak PetView *weakSelf = self;
|
|
88
|
+
self.globalMouseMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskMouseMoved
|
|
89
|
+
handler:^(NSEvent *event) {
|
|
90
|
+
[weakSelf updateLookForScreenPoint:NSEvent.mouseLocation];
|
|
91
|
+
}];
|
|
92
|
+
self.localMouseMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskMouseMoved
|
|
93
|
+
handler:^NSEvent *(NSEvent *event) {
|
|
94
|
+
[weakSelf updateLookForScreenPoint:NSEvent.mouseLocation];
|
|
95
|
+
return event;
|
|
96
|
+
}];
|
|
97
|
+
}
|
|
98
|
+
- (void)stopLookTracking {
|
|
99
|
+
if (self.globalMouseMonitor) {
|
|
100
|
+
[NSEvent removeMonitor:self.globalMouseMonitor];
|
|
101
|
+
self.globalMouseMonitor = nil;
|
|
102
|
+
}
|
|
103
|
+
if (self.localMouseMonitor) {
|
|
104
|
+
[NSEvent removeMonitor:self.localMouseMonitor];
|
|
105
|
+
self.localMouseMonitor = nil;
|
|
106
|
+
}
|
|
107
|
+
self.lookDirection = -1;
|
|
108
|
+
}
|
|
109
|
+
- (void)updateLookTracking {
|
|
110
|
+
if (self.spriteRowCount == 11) [self startLookTracking];
|
|
111
|
+
else [self stopLookTracking];
|
|
112
|
+
}
|
|
113
|
+
- (void)updateLookForScreenPoint:(NSPoint)screenPoint {
|
|
114
|
+
if (self.spriteRowCount != 11 || self.draggingPet || self.oneShot || self.agentActive || !self.window) return;
|
|
115
|
+
|
|
116
|
+
NSPoint windowPoint = [self.window convertPointFromScreen:screenPoint];
|
|
117
|
+
NSPoint localPoint = [self convertPoint:windowPoint fromView:nil];
|
|
118
|
+
if (NSPointInRect(localPoint, self.bounds)) return;
|
|
119
|
+
|
|
120
|
+
NSPoint center = [self convertPoint:NSMakePoint(NSMidX(self.bounds), NSMidY(self.bounds)) toView:nil];
|
|
121
|
+
center = [self.window convertPointToScreen:center];
|
|
122
|
+
CGFloat dx = screenPoint.x - center.x;
|
|
123
|
+
CGFloat dy = screenPoint.y - center.y;
|
|
124
|
+
if (hypot(dx, dy) <= 1.0) return;
|
|
125
|
+
|
|
126
|
+
CGFloat degrees = fmod(atan2(dx, dy) * 180.0 / M_PI + 360.0, 360.0);
|
|
127
|
+
NSInteger direction = ((NSInteger)llround(degrees / 22.5)) % 16;
|
|
128
|
+
if (direction == self.lookDirection) return;
|
|
129
|
+
|
|
130
|
+
self.lookDirection = direction;
|
|
131
|
+
self.rowIndex = 9 + direction / 8;
|
|
132
|
+
self.frameIndex = direction % 8;
|
|
133
|
+
self.needsDisplay = YES;
|
|
134
|
+
}
|
|
135
|
+
- (NSArray<NSNumber *> *)framesFromZeroThrough:(NSInteger)last {
|
|
136
|
+
NSMutableArray<NSNumber *> *result = [NSMutableArray arrayWithCapacity:last + 1];
|
|
137
|
+
for (NSInteger index = 0; index <= last; index++) [result addObject:@(index)];
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
- (void)playRow:(NSInteger)row throughFrame:(NSInteger)last oneShot:(BOOL)oneShot {
|
|
141
|
+
if (!oneShot && !self.oneShot && self.rowIndex == row) return;
|
|
142
|
+
self.lookDirection = -1;
|
|
143
|
+
self.rowIndex = row;
|
|
144
|
+
self.frames = [self framesFromZeroThrough:last];
|
|
145
|
+
self.sequenceIndex = 0;
|
|
146
|
+
self.frameIndex = 0;
|
|
147
|
+
self.oneShot = oneShot;
|
|
148
|
+
self.needsDisplay = YES;
|
|
149
|
+
}
|
|
150
|
+
- (BOOL)isPocketPoint:(NSPoint)point {
|
|
151
|
+
CGFloat centerX = NSMidX(self.bounds);
|
|
152
|
+
CGFloat centerY = NSHeight(self.bounds) * 0.32;
|
|
153
|
+
CGFloat dx = (point.x - centerX) / (NSWidth(self.bounds) * 0.22);
|
|
154
|
+
CGFloat dy = (point.y - centerY) / (NSHeight(self.bounds) * 0.16);
|
|
155
|
+
return dx * dx + dy * dy <= 1.0;
|
|
156
|
+
}
|
|
157
|
+
- (void)updateHoverForPoint:(NSPoint)point {
|
|
158
|
+
BOOL overPocket = [self isPocketPoint:point];
|
|
159
|
+
if (self.pocketHoverChanged) self.pocketHoverChanged(overPocket);
|
|
160
|
+
if (overPocket) {
|
|
161
|
+
[NSCursor.pointingHandCursor set];
|
|
162
|
+
[self playRow:5 throughFrame:7 oneShot:NO];
|
|
163
|
+
} else if (point.y >= NSHeight(self.bounds) * 0.58) {
|
|
164
|
+
[NSCursor.pointingHandCursor set];
|
|
165
|
+
[self playRow:4 throughFrame:4 oneShot:NO];
|
|
166
|
+
} else if (point.y <= NSHeight(self.bounds) * 0.20) {
|
|
167
|
+
[NSCursor.pointingHandCursor set];
|
|
168
|
+
[self playRow:6 throughFrame:5 oneShot:NO];
|
|
169
|
+
} else if (point.x >= NSWidth(self.bounds) * 0.66) {
|
|
170
|
+
[NSCursor.arrowCursor set];
|
|
171
|
+
[self playRow:3 throughFrame:3 oneShot:NO];
|
|
172
|
+
} else if (point.x <= NSWidth(self.bounds) * 0.34) {
|
|
173
|
+
[NSCursor.arrowCursor set];
|
|
174
|
+
[self playRow:8 throughFrame:5 oneShot:NO];
|
|
175
|
+
} else {
|
|
176
|
+
[NSCursor.arrowCursor set];
|
|
177
|
+
[self playRow:0 throughFrame:5 oneShot:NO];
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
- (void)mouseMoved:(NSEvent *)event {
|
|
181
|
+
if (self.draggingPet || self.oneShot) return;
|
|
182
|
+
NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
|
|
183
|
+
if (self.agentActive) {
|
|
184
|
+
BOOL overPocket = [self isPocketPoint:point];
|
|
185
|
+
if (self.pocketHoverChanged) self.pocketHoverChanged(overPocket);
|
|
186
|
+
if (overPocket) [NSCursor.pointingHandCursor set];
|
|
187
|
+
else [NSCursor.arrowCursor set];
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
[self updateHoverForPoint:point];
|
|
191
|
+
}
|
|
192
|
+
- (void)mouseExited:(NSEvent *)event {
|
|
193
|
+
if (self.draggingPet) return;
|
|
194
|
+
if (self.pocketHoverChanged) self.pocketHoverChanged(NO);
|
|
195
|
+
[NSCursor.arrowCursor set];
|
|
196
|
+
if (self.agentActive) return;
|
|
197
|
+
[self playRow:0 throughFrame:5 oneShot:NO];
|
|
198
|
+
}
|
|
199
|
+
- (void)nextFrame:(NSTimer *)timer {
|
|
200
|
+
if (self.lookDirection >= 0) return;
|
|
201
|
+
self.sequenceIndex += 1;
|
|
202
|
+
if (self.sequenceIndex >= self.frames.count) {
|
|
203
|
+
if (self.oneShot) {
|
|
204
|
+
self.rowIndex = 0;
|
|
205
|
+
self.frames = [self framesFromZeroThrough:5];
|
|
206
|
+
self.oneShot = NO;
|
|
207
|
+
self.agentActive = NO;
|
|
208
|
+
}
|
|
209
|
+
self.sequenceIndex = 0;
|
|
210
|
+
}
|
|
211
|
+
self.frameIndex = self.frames[self.sequenceIndex].integerValue;
|
|
212
|
+
self.needsDisplay = YES;
|
|
213
|
+
}
|
|
214
|
+
- (void)handleAgentEvent:(NSString *)event tool:(NSString *)tool failed:(BOOL)failed {
|
|
215
|
+
if ([event isEqualToString:@"SessionStart"]) {
|
|
216
|
+
self.agentActive = YES;
|
|
217
|
+
[self playRow:4 throughFrame:4 oneShot:YES];
|
|
218
|
+
} else if ([event isEqualToString:@"UserPromptSubmit"]) {
|
|
219
|
+
self.agentActive = YES;
|
|
220
|
+
[self playRow:5 throughFrame:7 oneShot:NO];
|
|
221
|
+
} else if ([event isEqualToString:@"PermissionRequest"]) {
|
|
222
|
+
self.agentActive = YES;
|
|
223
|
+
[self playRow:4 throughFrame:4 oneShot:NO];
|
|
224
|
+
} else if ([event isEqualToString:@"AutoReviewRequest"]) {
|
|
225
|
+
self.agentActive = YES;
|
|
226
|
+
[self playRow:8 throughFrame:5 oneShot:NO];
|
|
227
|
+
} else if ([event isEqualToString:@"PreToolUse"]) {
|
|
228
|
+
self.agentActive = YES;
|
|
229
|
+
NSString *lower = tool.lowercaseString;
|
|
230
|
+
if ([lower containsString:@"bash"] || [lower containsString:@"exec"]) {
|
|
231
|
+
[self playRow:1 throughFrame:7 oneShot:NO];
|
|
232
|
+
} else if ([lower containsString:@"patch"] || [lower containsString:@"edit"] || [lower containsString:@"write"]) {
|
|
233
|
+
[self playRow:7 throughFrame:5 oneShot:NO];
|
|
234
|
+
} else if ([lower containsString:@"read"] || [lower containsString:@"search"] || [lower containsString:@"find"] ||
|
|
235
|
+
[lower containsString:@"grep"] || [lower containsString:@"glob"] || [lower containsString:@"web"] ||
|
|
236
|
+
[lower hasPrefix:@"mcp__"]) {
|
|
237
|
+
[self playRow:3 throughFrame:3 oneShot:NO];
|
|
238
|
+
} else if ([lower isEqualToString:@"task"] || [lower containsString:@"agent"]) {
|
|
239
|
+
[self playRow:6 throughFrame:5 oneShot:NO];
|
|
240
|
+
} else {
|
|
241
|
+
[self playRow:8 throughFrame:5 oneShot:NO];
|
|
242
|
+
}
|
|
243
|
+
} else if ([event isEqualToString:@"SubagentStart"] || [event isEqualToString:@"TaskCreated"]) {
|
|
244
|
+
self.agentActive = YES;
|
|
245
|
+
[self playRow:6 throughFrame:5 oneShot:NO];
|
|
246
|
+
} else if ([event isEqualToString:@"PostToolUse"] || [event isEqualToString:@"PostToolUseFailure"]) {
|
|
247
|
+
self.agentActive = YES;
|
|
248
|
+
[self playRow:failed ? 5 : 7 throughFrame:failed ? 7 : 5 oneShot:YES];
|
|
249
|
+
} else if ([event isEqualToString:@"StopFailure"]) {
|
|
250
|
+
self.agentActive = YES;
|
|
251
|
+
[self playRow:5 throughFrame:7 oneShot:YES];
|
|
252
|
+
} else if ([event isEqualToString:@"Notification"]) {
|
|
253
|
+
self.agentActive = YES;
|
|
254
|
+
[self playRow:4 throughFrame:4 oneShot:YES];
|
|
255
|
+
} else if ([event isEqualToString:@"Stop"] || [event isEqualToString:@"SubagentStop"] ||
|
|
256
|
+
[event isEqualToString:@"TaskCompleted"] || [event isEqualToString:@"SessionEnd"]) {
|
|
257
|
+
self.agentActive = YES;
|
|
258
|
+
[self playRow:3 throughFrame:3 oneShot:YES];
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
- (void)mouseDown:(NSEvent *)event {
|
|
262
|
+
self.agentActive = NO;
|
|
263
|
+
self.dragStartMouse = NSEvent.mouseLocation;
|
|
264
|
+
self.dragStartWindowOrigin = self.window.frame.origin;
|
|
265
|
+
self.draggingPet = NO;
|
|
266
|
+
}
|
|
267
|
+
- (void)mouseDragged:(NSEvent *)event {
|
|
268
|
+
NSPoint current = NSEvent.mouseLocation;
|
|
269
|
+
CGFloat horizontal = current.x - self.dragStartMouse.x;
|
|
270
|
+
CGFloat vertical = current.y - self.dragStartMouse.y;
|
|
271
|
+
if (!self.draggingPet && hypot(horizontal, vertical) >= 3) {
|
|
272
|
+
self.draggingPet = YES;
|
|
273
|
+
if (self.dragStateChanged) self.dragStateChanged(YES);
|
|
274
|
+
}
|
|
275
|
+
if (self.draggingPet) {
|
|
276
|
+
if (self.pocketHoverChanged) self.pocketHoverChanged(NO);
|
|
277
|
+
[self playRow:horizontal >= 0 ? 1 : 2 throughFrame:7 oneShot:NO];
|
|
278
|
+
}
|
|
279
|
+
NSPoint origin = NSMakePoint(
|
|
280
|
+
self.dragStartWindowOrigin.x + horizontal,
|
|
281
|
+
self.dragStartWindowOrigin.y + vertical
|
|
282
|
+
);
|
|
283
|
+
[self.window setFrameOrigin:origin];
|
|
284
|
+
if (self.positionChanged) self.positionChanged();
|
|
285
|
+
}
|
|
286
|
+
- (void)mouseUp:(NSEvent *)event {
|
|
287
|
+
if (self.draggingPet) {
|
|
288
|
+
self.draggingPet = NO;
|
|
289
|
+
if (self.dragStateChanged) self.dragStateChanged(NO);
|
|
290
|
+
[self playRow:0 throughFrame:5 oneShot:NO];
|
|
291
|
+
} else {
|
|
292
|
+
// 第 8 行未标注的动作作为点击反馈,完整利用剩余素材。
|
|
293
|
+
[self playRow:7 throughFrame:5 oneShot:YES];
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
- (void)drawRect:(NSRect)dirtyRect {
|
|
297
|
+
CGFloat cellWidth = self.sheet.size.width / 8.0;
|
|
298
|
+
CGFloat cellHeight = self.sheet.size.height / self.spriteRowCount;
|
|
299
|
+
NSRect source = NSMakeRect(self.frameIndex * cellWidth,
|
|
300
|
+
self.sheet.size.height - (self.rowIndex + 1) * cellHeight, cellWidth, cellHeight);
|
|
301
|
+
[self.sheet drawInRect:NSInsetRect(self.bounds, 2, 0) fromRect:source
|
|
302
|
+
operation:NSCompositingOperationSourceOver fraction:1.0
|
|
303
|
+
respectFlipped:NO hints:@{NSImageHintInterpolation: @(NSImageInterpolationNone)}];
|
|
304
|
+
}
|
|
305
|
+
- (void)applySheet:(NSImage *)sheet petID:(NSString *)petID rowCount:(NSInteger)rowCount {
|
|
306
|
+
self.sheet = sheet;
|
|
307
|
+
self.spriteRowCount = rowCount;
|
|
308
|
+
self.lookDirection = -1;
|
|
309
|
+
self.currentPetID = petID;
|
|
310
|
+
self.rowIndex = 0;
|
|
311
|
+
self.frameIndex = 0;
|
|
312
|
+
self.sequenceIndex = 0;
|
|
313
|
+
self.frames = [self framesFromZeroThrough:5];
|
|
314
|
+
self.oneShot = NO;
|
|
315
|
+
[self updateLookTracking];
|
|
316
|
+
self.needsDisplay = YES;
|
|
317
|
+
}
|
|
318
|
+
- (void)selectPet:(NSMenuItem *)sender {
|
|
319
|
+
NSDictionary *pet = [sender.representedObject isKindOfClass:NSDictionary.class]
|
|
320
|
+
? sender.representedObject : nil;
|
|
321
|
+
NSString *petID = pet[@"id"];
|
|
322
|
+
if (petID.length > 0 && self.switchPetRequested) self.switchPetRequested(petID);
|
|
323
|
+
}
|
|
324
|
+
- (NSImage *)menuPreviewForPet:(NSDictionary *)pet {
|
|
325
|
+
NSString *path = [pet[@"path"] isKindOfClass:NSString.class] ? pet[@"path"] : nil;
|
|
326
|
+
NSInteger rowCount = [pet[@"spriteRowCount"] integerValue];
|
|
327
|
+
if (path.length == 0 || rowCount <= 0) return nil;
|
|
328
|
+
|
|
329
|
+
NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path error:nil];
|
|
330
|
+
NSTimeInterval modifiedAt = [attributes[NSFileModificationDate] timeIntervalSince1970];
|
|
331
|
+
unsigned long long fileSize = [attributes[NSFileSize] unsignedLongLongValue];
|
|
332
|
+
NSString *cacheKey = [NSString stringWithFormat:@"%@:%ld:%.6f:%llu",
|
|
333
|
+
path, (long)rowCount, modifiedAt, fileSize];
|
|
334
|
+
NSImage *cached = [self.petMenuPreviewCache objectForKey:cacheKey];
|
|
335
|
+
if (cached) return cached;
|
|
336
|
+
|
|
337
|
+
NSImage *sheet = LoadPetSpriteImage(path, NSMakeSize(92, 76), rowCount);
|
|
338
|
+
if (!sheet || sheet.size.width <= 0 || sheet.size.height <= 0) return nil;
|
|
339
|
+
|
|
340
|
+
CGFloat cellWidth = sheet.size.width / 8.0;
|
|
341
|
+
CGFloat cellHeight = sheet.size.height / rowCount;
|
|
342
|
+
NSRect firstFrame = NSMakeRect(0, sheet.size.height - cellHeight, cellWidth, cellHeight);
|
|
343
|
+
CGFloat previewHeight = 76.0;
|
|
344
|
+
CGFloat previewWidth = round(previewHeight * cellWidth / cellHeight);
|
|
345
|
+
NSImage *preview = [[NSImage alloc] initWithSize:NSMakeSize(previewWidth, previewHeight)];
|
|
346
|
+
[preview lockFocus];
|
|
347
|
+
[sheet drawInRect:NSMakeRect(0, 0, previewWidth, previewHeight) fromRect:firstFrame
|
|
348
|
+
operation:NSCompositingOperationSourceOver fraction:1.0
|
|
349
|
+
respectFlipped:NO hints:@{NSImageHintInterpolation: @(NSImageInterpolationNone)}];
|
|
350
|
+
[preview unlockFocus];
|
|
351
|
+
NSUInteger cost = (NSUInteger)ceil(previewWidth * previewHeight * 4.0);
|
|
352
|
+
[self.petMenuPreviewCache setObject:preview forKey:cacheKey cost:cost];
|
|
353
|
+
return preview;
|
|
354
|
+
}
|
|
355
|
+
- (void)preparePetMenuPreviewPanel {
|
|
356
|
+
if (self.petMenuPreviewPanel) return;
|
|
357
|
+
|
|
358
|
+
NSRect panelFrame = NSMakeRect(0, 0, 92, 100);
|
|
359
|
+
self.petMenuPreviewPanel = [[NSPanel alloc] initWithContentRect:panelFrame
|
|
360
|
+
styleMask:NSWindowStyleMaskBorderless | NSWindowStyleMaskNonactivatingPanel
|
|
361
|
+
backing:NSBackingStoreBuffered defer:NO];
|
|
362
|
+
self.petMenuPreviewPanel.opaque = NO;
|
|
363
|
+
self.petMenuPreviewPanel.backgroundColor = NSColor.clearColor;
|
|
364
|
+
self.petMenuPreviewPanel.hasShadow = YES;
|
|
365
|
+
self.petMenuPreviewPanel.ignoresMouseEvents = YES;
|
|
366
|
+
self.petMenuPreviewPanel.level = NSPopUpMenuWindowLevel + 1;
|
|
367
|
+
self.petMenuPreviewPanel.collectionBehavior = NSWindowCollectionBehaviorTransient |
|
|
368
|
+
NSWindowCollectionBehaviorMoveToActiveSpace;
|
|
369
|
+
|
|
370
|
+
NSVisualEffectView *background = [[NSVisualEffectView alloc] initWithFrame:panelFrame];
|
|
371
|
+
background.material = NSVisualEffectMaterialMenu;
|
|
372
|
+
background.blendingMode = NSVisualEffectBlendingModeBehindWindow;
|
|
373
|
+
background.state = NSVisualEffectStateActive;
|
|
374
|
+
background.wantsLayer = YES;
|
|
375
|
+
background.layer.cornerRadius = 10.0;
|
|
376
|
+
background.layer.masksToBounds = YES;
|
|
377
|
+
|
|
378
|
+
self.petMenuPreviewImageView = [[NSImageView alloc] initWithFrame:NSInsetRect(panelFrame, 8, 8)];
|
|
379
|
+
self.petMenuPreviewImageView.imageScaling = NSImageScaleProportionallyUpOrDown;
|
|
380
|
+
self.petMenuPreviewImageView.animates = NO;
|
|
381
|
+
[background addSubview:self.petMenuPreviewImageView];
|
|
382
|
+
self.petMenuPreviewPanel.contentView = background;
|
|
383
|
+
}
|
|
384
|
+
- (void)showMenuPreviewForPet:(NSDictionary *)pet item:(NSMenuItem *)item menu:(NSMenu *)menu {
|
|
385
|
+
NSImage *preview = [self menuPreviewForPet:pet];
|
|
386
|
+
if (!preview) {
|
|
387
|
+
[self.petMenuPreviewPanel orderOut:nil];
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
[self preparePetMenuPreviewPanel];
|
|
391
|
+
self.petMenuPreviewImageView.image = preview;
|
|
392
|
+
|
|
393
|
+
NSRect itemFrame = item.accessibilityFrame;
|
|
394
|
+
NSRect menuFrame = menu.accessibilityFrame;
|
|
395
|
+
if (NSIsEmptyRect(menuFrame)) {
|
|
396
|
+
NSPoint mouse = NSEvent.mouseLocation;
|
|
397
|
+
menuFrame = NSMakeRect(mouse.x - menu.size.width / 2.0,
|
|
398
|
+
mouse.y - menu.size.height / 2.0, menu.size.width, menu.size.height);
|
|
399
|
+
}
|
|
400
|
+
if (NSIsEmptyRect(itemFrame)) {
|
|
401
|
+
NSPoint mouse = NSEvent.mouseLocation;
|
|
402
|
+
itemFrame = NSMakeRect(menuFrame.origin.x, mouse.y - 11.0, menuFrame.size.width, 22.0);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// 子菜单可能根据屏幕空间展开到主菜单左侧。预览应放在整组菜单外侧,
|
|
406
|
+
// 不能只按当前子菜单定位,否则会覆盖它右边的主菜单。
|
|
407
|
+
NSRect menuGroupFrame = menuFrame;
|
|
408
|
+
for (NSMenu *ancestor = menu.supermenu; ancestor; ancestor = ancestor.supermenu) {
|
|
409
|
+
NSRect ancestorFrame = ancestor.accessibilityFrame;
|
|
410
|
+
if (!NSIsEmptyRect(ancestorFrame)) menuGroupFrame = NSUnionRect(menuGroupFrame, ancestorFrame);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
NSPoint menuCenter = NSMakePoint(NSMidX(menuGroupFrame), NSMidY(menuGroupFrame));
|
|
414
|
+
NSScreen *targetScreen = NSScreen.mainScreen ?: NSScreen.screens.firstObject;
|
|
415
|
+
for (NSScreen *screen in NSScreen.screens) {
|
|
416
|
+
if (NSPointInRect(menuCenter, screen.frame)) {
|
|
417
|
+
targetScreen = screen;
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
NSRect screenFrame = targetScreen.visibleFrame;
|
|
422
|
+
NSSize previewSize = self.petMenuPreviewPanel.frame.size;
|
|
423
|
+
CGFloat x = NSMaxX(menuGroupFrame) + 8.0;
|
|
424
|
+
if (x + previewSize.width > NSMaxX(screenFrame)) {
|
|
425
|
+
x = NSMinX(menuGroupFrame) - previewSize.width - 8.0;
|
|
426
|
+
}
|
|
427
|
+
CGFloat y = NSMidY(itemFrame) - previewSize.height / 2.0;
|
|
428
|
+
y = MIN(MAX(y, NSMinY(screenFrame)), NSMaxY(screenFrame) - previewSize.height);
|
|
429
|
+
[self.petMenuPreviewPanel setFrameOrigin:NSMakePoint(x, y)];
|
|
430
|
+
[self.petMenuPreviewPanel orderFront:nil];
|
|
431
|
+
}
|
|
432
|
+
- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item {
|
|
433
|
+
NSDictionary *pet = [item.representedObject isKindOfClass:NSDictionary.class]
|
|
434
|
+
? item.representedObject : nil;
|
|
435
|
+
if (pet) [self showMenuPreviewForPet:pet item:item menu:menu];
|
|
436
|
+
else [self.petMenuPreviewPanel orderOut:nil];
|
|
437
|
+
}
|
|
438
|
+
- (void)menuDidClose:(NSMenu *)menu {
|
|
439
|
+
[self.petMenuPreviewPanel orderOut:nil];
|
|
440
|
+
}
|
|
441
|
+
- (NSMenuItem *)addPersistentSwitchToMenu:(NSMenu *)menu
|
|
442
|
+
title:(NSString *)title
|
|
443
|
+
checked:(BOOL)checked
|
|
444
|
+
action:(SEL)action
|
|
445
|
+
tag:(NSInteger)tag {
|
|
446
|
+
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
|
|
447
|
+
NSView *row = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 194, 28)];
|
|
448
|
+
NSTextField *label = [NSTextField labelWithString:title];
|
|
449
|
+
label.frame = NSMakeRect(12, 5, 132, 18);
|
|
450
|
+
label.font = [NSFont menuFontOfSize:13];
|
|
451
|
+
label.textColor = NSColor.labelColor;
|
|
452
|
+
label.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
453
|
+
[row addSubview:label];
|
|
454
|
+
MenuToggleSwitch *toggle = [[MenuToggleSwitch alloc] initWithFrame:NSMakeRect(150, 3, 36, 22)];
|
|
455
|
+
toggle.target = NSApp.delegate;
|
|
456
|
+
toggle.action = action;
|
|
457
|
+
toggle.state = checked ? NSControlStateValueOn : NSControlStateValueOff;
|
|
458
|
+
toggle.tag = tag;
|
|
459
|
+
[toggle setAccessibilityLabel:title];
|
|
460
|
+
[row addSubview:toggle];
|
|
461
|
+
item.view = row;
|
|
462
|
+
[menu addItem:item];
|
|
463
|
+
return item;
|
|
464
|
+
}
|
|
465
|
+
- (void)rightMouseDown:(NSEvent *)event {
|
|
466
|
+
NSArray<NSDictionary *> *availablePets = self.petOptionsRequested ? self.petOptionsRequested() : @[];
|
|
467
|
+
NSMenu *menu = [NSMenu new];
|
|
468
|
+
NSMenuItem *switchItem = [menu addItemWithTitle:@"切换桌宠" action:nil keyEquivalent:@""];
|
|
469
|
+
NSMenu *switchMenu = [NSMenu new];
|
|
470
|
+
switchMenu.delegate = self;
|
|
471
|
+
for (NSDictionary *pet in availablePets) {
|
|
472
|
+
NSString *petID = pet[@"id"];
|
|
473
|
+
NSMenuItem *item = [switchMenu addItemWithTitle:pet[@"name"]
|
|
474
|
+
action:@selector(selectPet:) keyEquivalent:@""];
|
|
475
|
+
item.target = self;
|
|
476
|
+
item.representedObject = pet;
|
|
477
|
+
item.state = [petID isEqualToString:self.currentPetID] ? NSControlStateValueOn : NSControlStateValueOff;
|
|
478
|
+
}
|
|
479
|
+
switchItem.submenu = switchMenu;
|
|
480
|
+
[menu addItem:NSMenuItem.separatorItem];
|
|
481
|
+
NSMenuItem *refreshItem = [menu addItemWithTitle:@"刷新用量" action:@selector(refreshUsage:) keyEquivalent:@"r"];
|
|
482
|
+
refreshItem.target = NSApp.delegate;
|
|
483
|
+
[self addPersistentSwitchToMenu:menu
|
|
484
|
+
title:@"记录本地额度历史"
|
|
485
|
+
checked:[NSUserDefaults.standardUserDefaults boolForKey:HistoryEnabledKey]
|
|
486
|
+
action:@selector(toggleQuotaHistory:)
|
|
487
|
+
tag:0];
|
|
488
|
+
NSMenuItem *notificationItem = [menu addItemWithTitle:@"系统通知" action:nil keyEquivalent:@""];
|
|
489
|
+
NSMenu *notificationMenu = [NSMenu new];
|
|
490
|
+
NSArray<NSDictionary *> *notificationOptions = @[
|
|
491
|
+
@{@"title": @"任务完成", @"key": NotificationCompletionKey, @"tag": @1},
|
|
492
|
+
@{@"title": @"任务失败", @"key": NotificationFailureKey, @"tag": @2},
|
|
493
|
+
@{@"title": @"等待审批", @"key": NotificationApprovalKey, @"tag": @3}
|
|
494
|
+
];
|
|
495
|
+
for (NSDictionary *option in notificationOptions) {
|
|
496
|
+
[self addPersistentSwitchToMenu:notificationMenu
|
|
497
|
+
title:option[@"title"]
|
|
498
|
+
checked:[NSUserDefaults.standardUserDefaults boolForKey:option[@"key"]]
|
|
499
|
+
action:@selector(toggleNotification:)
|
|
500
|
+
tag:[option[@"tag"] integerValue]];
|
|
501
|
+
}
|
|
502
|
+
notificationItem.submenu = notificationMenu;
|
|
503
|
+
NSMenuItem *updateItem = [menu addItemWithTitle:@"检查更新…" action:@selector(checkForUpdates:) keyEquivalent:@""];
|
|
504
|
+
updateItem.target = NSApp.delegate;
|
|
505
|
+
[menu addItem:NSMenuItem.separatorItem];
|
|
506
|
+
NSMenuItem *quitItem = [menu addItemWithTitle:@"退出桌宠" action:@selector(terminate:) keyEquivalent:@"q"];
|
|
507
|
+
quitItem.target = NSApp;
|
|
508
|
+
[NSMenu popUpContextMenu:menu withEvent:event forView:self];
|
|
509
|
+
[self.petMenuPreviewPanel orderOut:nil];
|
|
510
|
+
}
|
|
511
|
+
@end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
|
|
3
|
+
// 额度面板按逻辑尺寸绘制后整体缩放,保证离屏渲染和实时显示是同一套代码。
|
|
4
|
+
extern const CGFloat QuotaLogicalWidth;
|
|
5
|
+
extern const CGFloat QuotaLogicalHeight;
|
|
6
|
+
extern const CGFloat QuotaScale;
|
|
7
|
+
|
|
8
|
+
NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName);
|
|
9
|
+
|
|
10
|
+
@interface QuotaDashboardView : NSView
|
|
11
|
+
@property NSDictionary *codexUsage;
|
|
12
|
+
@property NSDictionary *claudeUsage;
|
|
13
|
+
@property NSArray<NSNumber *> *codexHistory;
|
|
14
|
+
@property NSArray<NSNumber *> *claudeHistory;
|
|
15
|
+
@property BOOL historyEnabled;
|
|
16
|
+
@property NSImage *codexLogo;
|
|
17
|
+
@property NSImage *claudeLogo;
|
|
18
|
+
@property BOOL refreshHovered;
|
|
19
|
+
@property BOOL refreshPressed;
|
|
20
|
+
@property NSTimeInterval refreshedUntil;
|
|
21
|
+
@property(copy) void (^hoverChanged)(BOOL hovering);
|
|
22
|
+
@property(copy) void (^refreshRequested)(void);
|
|
23
|
+
@end
|