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