cc-pets 1.0.4 → 1.0.5
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 +14 -0
- package/README.md +30 -3
- package/Sources/CCPets/CCPetsAppDelegate.h +4 -0
- package/Sources/CCPets/CCPetsAppDelegate.m +53 -0
- package/Sources/CCPets/CCPetsPaths.h +1 -0
- package/Sources/CCPets/CCPetsPaths.m +2 -0
- package/Sources/CCPets/CCPetsPreviews.m +20 -2
- package/Sources/CCPets/CCPetsUsage.h +8 -0
- package/Sources/CCPets/CCPetsUsage.m +681 -22
- package/Sources/CCPets/CCPetsUsageMonitor.m +12 -8
- package/Sources/CCPets/QuotaDashboardView.h +8 -0
- package/Sources/CCPets/QuotaDashboardView.m +222 -142
- package/package.json +1 -1
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
@interface CCPetsUsageMonitor ()
|
|
9
9
|
@property CodexUsageReader *codexReader;
|
|
10
|
+
@property ClaudeUsageReader *claudeReader;
|
|
10
11
|
@property(readwrite) NSDictionary *codexUsage;
|
|
11
12
|
@property(readwrite) NSDictionary *claudeUsage;
|
|
12
13
|
@property FSEventStreamRef codexStream;
|
|
@@ -53,23 +54,25 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
53
54
|
- (instancetype)init {
|
|
54
55
|
if ((self = [super init])) {
|
|
55
56
|
_codexReader = [CodexUsageReader new];
|
|
57
|
+
_claudeReader = [ClaudeUsageReader new];
|
|
56
58
|
}
|
|
57
59
|
return self;
|
|
58
60
|
}
|
|
59
61
|
- (void)publishChange {
|
|
60
62
|
if (self.changeHandler) self.changeHandler(self.codexUsage, self.claudeUsage);
|
|
61
63
|
}
|
|
62
|
-
|
|
64
|
+
// changed=YES 表示额度文件确实变了(VNODE 事件),跳过 stat 比较;
|
|
65
|
+
// aggregate=YES 表示这是手动刷新/面板打开/兜底轮询,Token 聚合要绕过节流立即重算。
|
|
66
|
+
- (NSDictionary *)readClaudeUsageChanged:(BOOL)changed forcingAggregation:(BOOL)aggregate {
|
|
63
67
|
NSString *path = ClaudeUsagePath();
|
|
64
68
|
NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path error:nil];
|
|
65
69
|
unsigned long long size = [attributes[NSFileSize] unsignedLongLongValue];
|
|
66
70
|
NSTimeInterval modifiedAt = [attributes[NSFileModificationDate] timeIntervalSince1970];
|
|
67
|
-
|
|
68
|
-
return self.claudeUsage;
|
|
69
|
-
}
|
|
71
|
+
BOOL unchanged = size == self.claudeSize && modifiedAt == self.claudeModifiedAt;
|
|
70
72
|
self.claudeSize = size;
|
|
71
73
|
self.claudeModifiedAt = modifiedAt;
|
|
72
|
-
|
|
74
|
+
if (!changed && !aggregate && unchanged && self.claudeUsage) return self.claudeUsage;
|
|
75
|
+
return aggregate ? [self.claudeReader refreshWithFullAggregation] : [self.claudeReader refresh];
|
|
73
76
|
}
|
|
74
77
|
- (void)startCodexStream {
|
|
75
78
|
if (self.codexStream) return;
|
|
@@ -118,7 +121,7 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
118
121
|
dispatch_source_t current = strongSelf.claudeSource;
|
|
119
122
|
if (!current) return;
|
|
120
123
|
unsigned long flags = dispatch_source_get_data(current);
|
|
121
|
-
strongSelf.claudeUsage = [strongSelf
|
|
124
|
+
strongSelf.claudeUsage = [strongSelf readClaudeUsageChanged:YES forcingAggregation:NO];
|
|
122
125
|
[strongSelf publishChange];
|
|
123
126
|
if (flags & (DISPATCH_VNODE_DELETE | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE)) {
|
|
124
127
|
strongSelf.claudeSource = nil;
|
|
@@ -138,8 +141,9 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
138
141
|
[self startClaudeSource];
|
|
139
142
|
}
|
|
140
143
|
- (void)refreshNow {
|
|
141
|
-
|
|
142
|
-
self.
|
|
144
|
+
// 手动刷新、面板打开和 120 秒兜底都走全量路径,Token 聚合的节流只约束 FSEvents 热路径。
|
|
145
|
+
self.codexUsage = [self.codexReader refreshWithFullDiscovery];
|
|
146
|
+
self.claudeUsage = [self readClaudeUsageChanged:NO forcingAggregation:YES];
|
|
143
147
|
if (!self.codexStream) [self startCodexStream];
|
|
144
148
|
if (!self.claudeSource) [self startClaudeSource];
|
|
145
149
|
[self publishChange];
|
|
@@ -13,6 +13,14 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName);
|
|
|
13
13
|
@property NSArray<NSNumber *> *codexHistory;
|
|
14
14
|
@property NSArray<NSNumber *> *claudeHistory;
|
|
15
15
|
@property BOOL historyEnabled;
|
|
16
|
+
@property NSInteger activeAgentCount;
|
|
17
|
+
// 仍在运行的客户端所属的 provider("Codex" / "Claude")。卡片的在线徽章据此判定,
|
|
18
|
+
// 而不是看有没有额度数据——额度会一直缓存着,拿它当在线信号会恒亮。
|
|
19
|
+
@property NSSet<NSString *> *liveProviders;
|
|
20
|
+
// 老版本包装脚本写出的 pid 文件没有 provider 名。有这种客户端时无法断言某一家离线。
|
|
21
|
+
@property BOOL hasUnlabeledClient;
|
|
22
|
+
@property NSTimeInterval lastUpdatedAt;
|
|
23
|
+
- (BOOL)isProviderOnline:(NSString *)name;
|
|
16
24
|
@property NSImage *codexLogo;
|
|
17
25
|
@property NSImage *claudeLogo;
|
|
18
26
|
@property BOOL refreshHovered;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#import "QuotaDashboardView.h"
|
|
2
2
|
|
|
3
|
-
const CGFloat QuotaLogicalWidth =
|
|
4
|
-
const CGFloat QuotaLogicalHeight =
|
|
5
|
-
const CGFloat QuotaScale = 0.
|
|
3
|
+
const CGFloat QuotaLogicalWidth = 760;
|
|
4
|
+
const CGFloat QuotaLogicalHeight = 590;
|
|
5
|
+
const CGFloat QuotaScale = 0.58;
|
|
6
6
|
|
|
7
7
|
NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
8
8
|
NSURL *appURL = [NSWorkspace.sharedWorkspace URLForApplicationWithBundleIdentifier:bundleIdentifier];
|
|
@@ -92,19 +92,68 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
92
92
|
id value = usage[key];
|
|
93
93
|
return [value isKindOfClass:NSDictionary.class] ? value : nil;
|
|
94
94
|
}
|
|
95
|
-
- (
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return
|
|
95
|
+
- (NSDictionary *)tokenTotals:(NSDictionary *)usage key:(NSString *)key {
|
|
96
|
+
NSDictionary *tokenUsage = [usage[@"tokenUsage"] isKindOfClass:NSDictionary.class]
|
|
97
|
+
? usage[@"tokenUsage"] : nil;
|
|
98
|
+
return [tokenUsage[key] isKindOfClass:NSDictionary.class] ? tokenUsage[key] : nil;
|
|
99
99
|
}
|
|
100
|
-
- (NSString *)
|
|
101
|
-
NSNumber *
|
|
102
|
-
|
|
100
|
+
- (NSString *)formattedTokenCount:(NSDictionary *)totals {
|
|
101
|
+
NSNumber *number = [totals[@"total_tokens"] isKindOfClass:NSNumber.class]
|
|
102
|
+
? totals[@"total_tokens"] : nil;
|
|
103
|
+
if (!number) return @"--";
|
|
104
|
+
double value = number.doubleValue;
|
|
105
|
+
if (value >= 1000000000) return [NSString stringWithFormat:@"%.2fB", value / 1000000000.0];
|
|
106
|
+
if (value >= 1000000) return [NSString stringWithFormat:@"%.2fM", value / 1000000.0];
|
|
107
|
+
if (value >= 1000) return [NSString stringWithFormat:@"%.1fK", value / 1000.0];
|
|
108
|
+
return [NSString stringWithFormat:@"%.0f", value];
|
|
109
|
+
}
|
|
110
|
+
// 两个窗口共用。resets_at 是整分对齐的,带秒只会多占宽度。
|
|
111
|
+
- (NSString *)resetText:(NSDictionary *)quota {
|
|
112
|
+
NSNumber *reset = [quota[@"resets_at"] isKindOfClass:NSNumber.class] ? quota[@"resets_at"] : nil;
|
|
113
|
+
if (!reset) return @"--";
|
|
103
114
|
NSDateFormatter *formatter = [NSDateFormatter new];
|
|
104
115
|
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
|
|
105
116
|
formatter.timeZone = NSTimeZone.localTimeZone;
|
|
106
|
-
formatter.dateFormat = @"M/d HH:mm
|
|
107
|
-
return [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:
|
|
117
|
+
formatter.dateFormat = @"M/d HH:mm";
|
|
118
|
+
return [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:reset.doubleValue]];
|
|
119
|
+
}
|
|
120
|
+
// 在线 = 这一家还有活着的客户端,和有没有额度数据无关:额度会一直缓存着,
|
|
121
|
+
// 拿它当在线信号会恒亮。身份不明的老客户端无法归属到某一家,保守地都算在线。
|
|
122
|
+
- (BOOL)isProviderOnline:(NSString *)name {
|
|
123
|
+
return [self.liveProviders containsObject:name] || self.hasUnlabeledClient;
|
|
124
|
+
}
|
|
125
|
+
- (NSColor *)primaryColor { return [NSColor colorWithWhite:0.97 alpha:1]; }
|
|
126
|
+
- (NSColor *)secondaryColor { return [NSColor colorWithWhite:0.68 alpha:1]; }
|
|
127
|
+
- (NSColor *)accentColor { return [NSColor colorWithRed:0.30 green:0.82 blue:0.90 alpha:1]; }
|
|
128
|
+
// 有 Agent 在线才用绿色,否则整块转灰,避免"暂无 Agent 在线"配一个绿点。
|
|
129
|
+
- (NSColor *)agentStatusColor {
|
|
130
|
+
return self.activeAgentCount > 0 ? NSColor.systemGreenColor : self.secondaryColor;
|
|
131
|
+
}
|
|
132
|
+
// 汇总条展示两个 Provider 的合计,任一侧缺数据时按 0 计入,两侧都没有才显示 `--`。
|
|
133
|
+
// 只能传自然日口径的 key(today / recentWeek):两家的订阅窗口起止并不一致,
|
|
134
|
+
// 按各自 resets_at 算出来的数不在同一区间上,相加得不到任何有意义的量。
|
|
135
|
+
- (NSDictionary *)combinedTokens:(NSString *)key {
|
|
136
|
+
NSDictionary *codex = [self tokenTotals:self.codexUsage key:key];
|
|
137
|
+
NSDictionary *claude = [self tokenTotals:self.claudeUsage key:key];
|
|
138
|
+
NSNumber *codexTotal = [codex[@"total_tokens"] isKindOfClass:NSNumber.class]
|
|
139
|
+
? codex[@"total_tokens"] : nil;
|
|
140
|
+
NSNumber *claudeTotal = [claude[@"total_tokens"] isKindOfClass:NSNumber.class]
|
|
141
|
+
? claude[@"total_tokens"] : nil;
|
|
142
|
+
if (!codexTotal && !claudeTotal) return @{};
|
|
143
|
+
return @{@"total_tokens": @(codexTotal.unsignedLongLongValue +
|
|
144
|
+
claudeTotal.unsignedLongLongValue)};
|
|
145
|
+
}
|
|
146
|
+
- (NSNumber *)usedForQuota:(NSDictionary *)quota usedKey:(NSString *)usedKey {
|
|
147
|
+
NSNumber *used = [quota[usedKey] isKindOfClass:NSNumber.class] ? quota[usedKey] : nil;
|
|
148
|
+
return used ? @(fmax(0, fmin(100, used.doubleValue))) : nil;
|
|
149
|
+
}
|
|
150
|
+
- (NSString *)refreshAgeText {
|
|
151
|
+
if (self.lastUpdatedAt <= 0) return @"等待数据";
|
|
152
|
+
NSTimeInterval age = fmax(0, NSDate.date.timeIntervalSince1970 - self.lastUpdatedAt);
|
|
153
|
+
if (age < 5) return @"刚刚";
|
|
154
|
+
if (age < 60) return [NSString stringWithFormat:@"%.0f 秒前", age];
|
|
155
|
+
if (age < 3600) return [NSString stringWithFormat:@"%.0f 分钟前", floor(age / 60)];
|
|
156
|
+
return [NSString stringWithFormat:@"%.0f 小时前", floor(age / 3600)];
|
|
108
157
|
}
|
|
109
158
|
- (NSDictionary *)textAttributesWithSize:(CGFloat)size color:(NSColor *)color weight:(NSFontWeight)weight {
|
|
110
159
|
return @{NSFontAttributeName: [NSFont systemFontOfSize:size weight:weight],
|
|
@@ -121,131 +170,122 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
121
170
|
[color setFill];
|
|
122
171
|
[[NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius] fill];
|
|
123
172
|
}
|
|
124
|
-
- (void)
|
|
173
|
+
- (void)drawUsageBars:(NSArray<NSNumber *> *)series inRect:(NSRect)rect color:(NSColor *)color {
|
|
174
|
+
// 柱高按 0–100% 绝对映射,低用量时上半截会空着。补一层底框把空白说明成刻度,
|
|
175
|
+
// 否则看起来像没画完。
|
|
176
|
+
[self fillRoundedRect:rect radius:4 color:[NSColor colorWithWhite:1 alpha:0.05]];
|
|
125
177
|
if (!self.historyEnabled || series.count == 0) return;
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
CGFloat
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
CGFloat
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
else [line lineToPoint:point];
|
|
136
|
-
if (series.count == 1) {
|
|
137
|
-
[[color colorWithAlphaComponent:0.9] setFill];
|
|
138
|
-
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(point.x - 2, point.y - 2, 4, 4)] fill];
|
|
139
|
-
}
|
|
178
|
+
NSUInteger visibleCount = MIN((NSUInteger)14, series.count);
|
|
179
|
+
CGFloat gap = 4;
|
|
180
|
+
CGFloat width = (NSWidth(rect) - gap * (visibleCount - 1)) / visibleCount;
|
|
181
|
+
for (NSUInteger index = 0; index < visibleCount; index++) {
|
|
182
|
+
NSNumber *remaining = series[series.count - visibleCount + index];
|
|
183
|
+
CGFloat used = 100.0 - fmax(0, fmin(100, remaining.doubleValue));
|
|
184
|
+
CGFloat height = fmax(3, NSHeight(rect) * used / 100.0);
|
|
185
|
+
NSRect bar = NSMakeRect(NSMinX(rect) + index * (width + gap), NSMinY(rect), width, height);
|
|
186
|
+
[self fillRoundedRect:bar radius:2 color:[color colorWithAlphaComponent:0.85]];
|
|
140
187
|
}
|
|
141
|
-
line.lineWidth = 2;
|
|
142
|
-
line.lineJoinStyle = NSLineJoinStyleRound;
|
|
143
|
-
line.lineCapStyle = NSLineCapStyleRound;
|
|
144
|
-
[[color colorWithAlphaComponent:0.9] setStroke];
|
|
145
|
-
[line stroke];
|
|
146
188
|
}
|
|
147
189
|
- (void)drawProvider:(NSString *)name usage:(NSDictionary *)usage usedKey:(NSString *)usedKey
|
|
148
190
|
color:(NSColor *)color cardRect:(NSRect)card {
|
|
149
|
-
NSColor *primary =
|
|
150
|
-
NSColor *secondary =
|
|
151
|
-
NSColor *track = [NSColor colorWithWhite:0.28 alpha:0.55];
|
|
191
|
+
NSColor *primary = self.primaryColor;
|
|
192
|
+
NSColor *secondary = self.secondaryColor;
|
|
152
193
|
NSDictionary *five = [self quota:usage key:@"fiveHour"];
|
|
153
194
|
NSDictionary *week = [self quota:usage key:@"week"];
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
195
|
+
NSDictionary *fiveTokens = [self tokenTotals:usage key:@"fiveHour"];
|
|
196
|
+
NSDictionary *weekTokens = [self tokenTotals:usage key:@"week"];
|
|
197
|
+
NSNumber *fiveUsed = [self usedForQuota:five usedKey:usedKey];
|
|
198
|
+
NSNumber *weekUsed = [self usedForQuota:week usedKey:usedKey];
|
|
199
|
+
BOOL hasData = fiveUsed || weekUsed || fiveTokens || weekTokens;
|
|
200
|
+
BOOL online = [self isProviderOnline:name];
|
|
157
201
|
|
|
158
|
-
NSBezierPath *cardPath = [NSBezierPath bezierPathWithRoundedRect:card xRadius:
|
|
202
|
+
NSBezierPath *cardPath = [NSBezierPath bezierPathWithRoundedRect:card xRadius:18 yRadius:18];
|
|
159
203
|
NSGradient *cardGlass = [[NSGradient alloc]
|
|
160
|
-
initWithStartingColor:[NSColor colorWithWhite:1 alpha:0.
|
|
161
|
-
endingColor:[
|
|
162
|
-
[cardGlass drawInBezierPath:cardPath angle:-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
[[NSColor
|
|
170
|
-
|
|
171
|
-
|
|
204
|
+
initWithStartingColor:[NSColor colorWithWhite:1 alpha:0.085]
|
|
205
|
+
endingColor:[color colorWithAlphaComponent:0.025]];
|
|
206
|
+
[cardGlass drawInBezierPath:cardPath angle:-25];
|
|
207
|
+
[[NSColor colorWithWhite:1 alpha:0.18] setStroke];
|
|
208
|
+
cardPath.lineWidth = 1;
|
|
209
|
+
[cardPath stroke];
|
|
210
|
+
|
|
211
|
+
NSArray<NSNumber *> *separators = @[@(NSMinX(card) + 190), @(NSMinX(card) + 370),
|
|
212
|
+
@(NSMinX(card) + 540)];
|
|
213
|
+
[[NSColor colorWithWhite:1 alpha:0.10] setStroke];
|
|
214
|
+
for (NSNumber *value in separators) {
|
|
215
|
+
[NSBezierPath strokeLineFromPoint:NSMakePoint(value.doubleValue, NSMinY(card) + 14)
|
|
216
|
+
toPoint:NSMakePoint(value.doubleValue, NSMaxY(card) - 14)];
|
|
217
|
+
}
|
|
172
218
|
|
|
173
|
-
NSRect icon = NSMakeRect(NSMinX(card) +
|
|
219
|
+
NSRect icon = NSMakeRect(NSMinX(card) + 28, NSMinY(card) + 53, 54, 54);
|
|
174
220
|
NSImage *logo = [name isEqualToString:@"Codex"] ? self.codexLogo : self.claudeLogo;
|
|
175
221
|
if (logo) {
|
|
176
222
|
[NSGraphicsContext saveGraphicsState];
|
|
177
|
-
[[NSBezierPath bezierPathWithRoundedRect:icon xRadius:
|
|
223
|
+
[[NSBezierPath bezierPathWithRoundedRect:icon xRadius:14 yRadius:14] addClip];
|
|
178
224
|
[logo drawInRect:icon fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1.0
|
|
179
225
|
respectFlipped:YES hints:@{NSImageHintInterpolation: @(NSImageInterpolationHigh)}];
|
|
180
226
|
[NSGraphicsContext restoreGraphicsState];
|
|
181
227
|
} else {
|
|
182
|
-
[self fillRoundedRect:icon radius:
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
withAttributes:[self centeredTextAttributesWithSize:20 color:NSColor.whiteColor weight:NSFontWeightBold]];
|
|
228
|
+
[self fillRoundedRect:icon radius:14 color:[color colorWithAlphaComponent:0.7]];
|
|
229
|
+
[name drawInRect:NSInsetRect(icon, 4, 17)
|
|
230
|
+
withAttributes:[self centeredTextAttributesWithSize:13 color:NSColor.whiteColor weight:NSFontWeightBold]];
|
|
186
231
|
}
|
|
232
|
+
[name drawInRect:NSMakeRect(NSMinX(card) + 94, NSMinY(card) + 84, 88, 30)
|
|
233
|
+
withAttributes:[self textAttributesWithSize:22 color:primary weight:NSFontWeightBold]];
|
|
234
|
+
// 三态:没有任何额度数据 → 等待数据;有数据但客户端已退出 → 离线;两者都有 → 在线。
|
|
235
|
+
NSString *statusText = !hasData ? @"等待数据" : (online ? @"● 在线" : @"离线");
|
|
236
|
+
NSColor *statusColor = online && hasData ? NSColor.systemGreenColor : secondary;
|
|
237
|
+
NSDictionary *statusAttributes = [self textAttributesWithSize:11 color:statusColor
|
|
238
|
+
weight:NSFontWeightMedium];
|
|
239
|
+
CGFloat statusWidth = ceil([statusText sizeWithAttributes:statusAttributes].width) + 18;
|
|
240
|
+
NSRect status = NSMakeRect(NSMinX(card) + 94, NSMinY(card) + 55, statusWidth, 24);
|
|
241
|
+
[self fillRoundedRect:status radius:12 color:[statusColor colorWithAlphaComponent:0.13]];
|
|
242
|
+
[statusText drawInRect:NSInsetRect(status, 9, 4) withAttributes:statusAttributes];
|
|
187
243
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
[
|
|
244
|
+
// 层级:官方剩余额度是订阅限额的权威值,占主位;本机 Token 是参考量,降一级并用
|
|
245
|
+
// 强调色区分;重置时间最小。反过来会让最抢眼的数字恰好是最不该照着做决定的那个。
|
|
246
|
+
NSArray<NSDictionary *> *windows = @[
|
|
247
|
+
@{@"x": @(NSMinX(card) + 214), @"title": @"5 小时", @"quota": five ?: @{},
|
|
248
|
+
@"tokens": fiveTokens ?: @{}, @"used": fiveUsed ?: NSNull.null},
|
|
249
|
+
@{@"x": @(NSMinX(card) + 394), @"title": @"7 天", @"quota": week ?: @{},
|
|
250
|
+
@"tokens": weekTokens ?: @{}, @"used": weekUsed ?: NSNull.null}
|
|
251
|
+
];
|
|
252
|
+
for (NSDictionary *window in windows) {
|
|
253
|
+
CGFloat x = [window[@"x"] doubleValue];
|
|
254
|
+
[window[@"title"] drawInRect:NSMakeRect(x, NSMinY(card) + 114, 130, 20)
|
|
255
|
+
withAttributes:[self textAttributesWithSize:13 color:secondary weight:NSFontWeightRegular]];
|
|
256
|
+
NSNumber *used = [window[@"used"] isKindOfClass:NSNumber.class] ? window[@"used"] : nil;
|
|
257
|
+
NSString *official = used ? [NSString stringWithFormat:@"%.0f%%",
|
|
258
|
+
100.0 - used.doubleValue] : @"--";
|
|
259
|
+
[official drawInRect:NSMakeRect(x, NSMinY(card) + 78, 145, 34)
|
|
260
|
+
withAttributes:[self textAttributesWithSize:25 color:primary weight:NSFontWeightBold]];
|
|
261
|
+
[@"剩余额度" drawInRect:NSMakeRect(x, NSMinY(card) + 60, 145, 18)
|
|
262
|
+
withAttributes:[self textAttributesWithSize:11 color:secondary weight:NSFontWeightRegular]];
|
|
263
|
+
NSString *tokenText = [NSString stringWithFormat:@"Token %@",
|
|
264
|
+
[self formattedTokenCount:window[@"tokens"]]];
|
|
265
|
+
[tokenText drawInRect:NSMakeRect(x, NSMinY(card) + 36, 145, 22)
|
|
266
|
+
withAttributes:[self textAttributesWithSize:15 color:color weight:NSFontWeightSemibold]];
|
|
267
|
+
// 整列都是 `--` 时说明这个窗口当前根本没有额度数据,直接说清楚,
|
|
268
|
+
// 比堆三个 `--` 让人以为是渲染坏了要好。
|
|
269
|
+
NSString *resetValue = [self resetText:window[@"quota"]];
|
|
270
|
+
NSString *reset = [resetValue isEqualToString:@"--"]
|
|
271
|
+
? @"当前无活跃窗口" : [NSString stringWithFormat:@"重置于 %@", resetValue];
|
|
272
|
+
[reset drawInRect:NSMakeRect(x, NSMinY(card) + 16, 145, 18)
|
|
273
|
+
withAttributes:[self textAttributesWithSize:11 color:secondary weight:NSFontWeightRegular]];
|
|
208
274
|
}
|
|
209
275
|
|
|
210
|
-
CGFloat
|
|
211
|
-
[
|
|
212
|
-
|
|
213
|
-
toPoint:NSMakePoint(separatorOne, NSMaxY(card) - 20)];
|
|
214
|
-
CGFloat ringX = separatorOne + 38;
|
|
215
|
-
NSPoint center = NSMakePoint(ringX, NSMinY(card) + 51);
|
|
216
|
-
NSBezierPath *ringTrack = [NSBezierPath bezierPath];
|
|
217
|
-
[ringTrack appendBezierPathWithArcWithCenter:center radius:24 startAngle:0 endAngle:360];
|
|
218
|
-
ringTrack.lineWidth = 7;
|
|
219
|
-
[track setStroke];
|
|
220
|
-
[ringTrack stroke];
|
|
221
|
-
if (weekRemaining) {
|
|
222
|
-
NSBezierPath *ring = [NSBezierPath bezierPath];
|
|
223
|
-
[ring appendBezierPathWithArcWithCenter:center radius:24 startAngle:90
|
|
224
|
-
endAngle:90 - 360 * weekRemaining.doubleValue / 100.0 clockwise:YES];
|
|
225
|
-
ring.lineWidth = 7;
|
|
226
|
-
ring.lineCapStyle = NSLineCapStyleRound;
|
|
227
|
-
[color setStroke];
|
|
228
|
-
[ring stroke];
|
|
229
|
-
}
|
|
230
|
-
NSString *weekText = weekRemaining ? [NSString stringWithFormat:@"%.0f%%", weekRemaining.doubleValue] : @"--";
|
|
231
|
-
[weekText drawInRect:NSMakeRect(ringX - 19, NSMinY(card) + 42, 38, 18)
|
|
232
|
-
withAttributes:[self centeredTextAttributesWithSize:13 color:primary weight:NSFontWeightSemibold]];
|
|
233
|
-
[@"周剩余" drawInRect:NSMakeRect(separatorOne + 68, NSMinY(card) + 66, 55, 20)
|
|
234
|
-
withAttributes:[self textAttributesWithSize:12 color:secondary weight:NSFontWeightRegular]];
|
|
235
|
-
|
|
236
|
-
CGFloat separatorTwo = NSMinX(card) + 414;
|
|
237
|
-
[[NSColor colorWithWhite:1 alpha:0.12] setStroke];
|
|
238
|
-
[NSBezierPath strokeLineFromPoint:NSMakePoint(separatorTwo, NSMinY(card) + 20)
|
|
239
|
-
toPoint:NSMakePoint(separatorTwo, NSMaxY(card) - 20)];
|
|
240
|
-
[@"周重置" drawInRect:NSMakeRect(separatorTwo + 16, NSMinY(card) + 66, 80, 20)
|
|
241
|
-
withAttributes:[self textAttributesWithSize:12 color:secondary weight:NSFontWeightRegular]];
|
|
242
|
-
[[self weeklyReset:week] drawInRect:NSMakeRect(separatorTwo + 16, NSMinY(card) + 39,
|
|
243
|
-
NSMaxX(card) - separatorTwo - 28, 24)
|
|
244
|
-
withAttributes:[self textAttributesWithSize:15 color:primary weight:NSFontWeightMedium]];
|
|
276
|
+
CGFloat trendX = NSMinX(card) + 564;
|
|
277
|
+
[@"过去 7 天额度趋势" drawInRect:NSMakeRect(trendX, NSMinY(card) + 114, 145, 20)
|
|
278
|
+
withAttributes:[self textAttributesWithSize:13 color:secondary weight:NSFontWeightRegular]];
|
|
245
279
|
NSArray<NSNumber *> *history = [name isEqualToString:@"Codex"] ? self.codexHistory : self.claudeHistory;
|
|
246
|
-
[self
|
|
247
|
-
|
|
248
|
-
|
|
280
|
+
[self drawUsageBars:history inRect:NSMakeRect(trendX, NSMinY(card) + 42, 135, 64) color:color];
|
|
281
|
+
NSString *trendDetail = @"等待历史数据";
|
|
282
|
+
if (self.historyEnabled && history.count > 0) {
|
|
283
|
+
double peak = 0;
|
|
284
|
+
for (NSNumber *remaining in history) peak = fmax(peak, 100.0 - remaining.doubleValue);
|
|
285
|
+
trendDetail = [NSString stringWithFormat:@"峰值额度已用 %.0f%%", peak];
|
|
286
|
+
}
|
|
287
|
+
[trendDetail drawInRect:NSMakeRect(trendX, NSMinY(card) + 18, 145, 18)
|
|
288
|
+
withAttributes:[self textAttributesWithSize:11 color:secondary weight:NSFontWeightRegular]];
|
|
249
289
|
}
|
|
250
290
|
- (void)drawRect:(NSRect)dirtyRect {
|
|
251
291
|
[NSGraphicsContext saveGraphicsState];
|
|
@@ -254,10 +294,10 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
254
294
|
[scale concat];
|
|
255
295
|
NSRect logicalBounds = NSMakeRect(0, 0, QuotaLogicalWidth, QuotaLogicalHeight);
|
|
256
296
|
NSRect outer = NSInsetRect(logicalBounds, 1, 1);
|
|
257
|
-
NSBezierPath *background = [NSBezierPath bezierPathWithRoundedRect:outer xRadius:
|
|
297
|
+
NSBezierPath *background = [NSBezierPath bezierPathWithRoundedRect:outer xRadius:24 yRadius:24];
|
|
258
298
|
NSGradient *gradient = [[NSGradient alloc]
|
|
259
|
-
initWithStartingColor:[NSColor colorWithRed:0.
|
|
260
|
-
endingColor:[NSColor colorWithRed:0.
|
|
299
|
+
initWithStartingColor:[NSColor colorWithRed:0.025 green:0.055 blue:0.095 alpha:0.88]
|
|
300
|
+
endingColor:[NSColor colorWithRed:0.07 green:0.12 blue:0.19 alpha:0.80]];
|
|
261
301
|
[gradient drawInBezierPath:background angle:-20];
|
|
262
302
|
|
|
263
303
|
[NSGraphicsContext saveGraphicsState];
|
|
@@ -265,36 +305,40 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
265
305
|
NSGradient *topGlow = [[NSGradient alloc]
|
|
266
306
|
initWithStartingColor:[NSColor colorWithWhite:1 alpha:0.34]
|
|
267
307
|
endingColor:[NSColor colorWithWhite:1 alpha:0.0]];
|
|
268
|
-
[topGlow drawFromCenter:NSMakePoint(
|
|
269
|
-
toCenter:NSMakePoint(
|
|
308
|
+
[topGlow drawFromCenter:NSMakePoint(700, 650) radius:0
|
|
309
|
+
toCenter:NSMakePoint(700, 650) radius:250 options:0];
|
|
270
310
|
NSGradient *cyanGlow = [[NSGradient alloc]
|
|
271
311
|
initWithStartingColor:[NSColor colorWithRed:0.18 green:0.72 blue:1 alpha:0.11]
|
|
272
312
|
endingColor:[NSColor colorWithRed:0.18 green:0.88 blue:1 alpha:0.0]];
|
|
273
|
-
[cyanGlow drawFromCenter:NSMakePoint(
|
|
274
|
-
toCenter:NSMakePoint(
|
|
313
|
+
[cyanGlow drawFromCenter:NSMakePoint(660, 250) radius:0
|
|
314
|
+
toCenter:NSMakePoint(660, 250) radius:260 options:0];
|
|
275
315
|
[NSGraphicsContext restoreGraphicsState];
|
|
276
316
|
|
|
277
|
-
[[NSColor colorWithWhite:1 alpha:0.
|
|
317
|
+
[[NSColor colorWithWhite:1 alpha:0.34] setStroke];
|
|
278
318
|
background.lineWidth = 1.5;
|
|
279
319
|
[background stroke];
|
|
280
320
|
NSBezierPath *innerGlassBorder = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(outer, 2, 2)
|
|
281
|
-
xRadius:
|
|
321
|
+
xRadius:22 yRadius:22];
|
|
282
322
|
[[NSColor colorWithRed:0.58 green:0.82 blue:1 alpha:0.22] setStroke];
|
|
283
323
|
innerGlassBorder.lineWidth = 0.8;
|
|
284
324
|
[innerGlassBorder stroke];
|
|
325
|
+
NSColor *primary = self.primaryColor;
|
|
326
|
+
NSColor *secondary = self.secondaryColor;
|
|
327
|
+
NSColor *cyan = self.accentColor;
|
|
328
|
+
NSColor *agentColor = self.agentStatusColor;
|
|
329
|
+
[@"Agent Usage" drawInRect:NSMakeRect(28, QuotaLogicalHeight - 65, 260, 42)
|
|
330
|
+
withAttributes:[self textAttributesWithSize:30 color:primary weight:NSFontWeightBold]];
|
|
331
|
+
[@"实时使用统计" drawInRect:NSMakeRect(30, QuotaLogicalHeight - 94, 130, 22)
|
|
332
|
+
withAttributes:[self textAttributesWithSize:14 color:secondary weight:NSFontWeightRegular]];
|
|
333
|
+
[agentColor setFill];
|
|
334
|
+
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(174, QuotaLogicalHeight - 88, 9, 9)] fill];
|
|
335
|
+
NSString *agentText = self.activeAgentCount > 0
|
|
336
|
+
? [NSString stringWithFormat:@"%ld 个 Agent 在线", (long)self.activeAgentCount]
|
|
337
|
+
: @"暂无 Agent 在线";
|
|
338
|
+
[agentText drawInRect:NSMakeRect(190, QuotaLogicalHeight - 95, 160, 22)
|
|
339
|
+
withAttributes:[self textAttributesWithSize:14 color:agentColor weight:NSFontWeightMedium]];
|
|
285
340
|
|
|
286
|
-
|
|
287
|
-
[@"Agent 余量" drawInRect:NSMakeRect(24, QuotaLogicalHeight - 53, 180, 30)
|
|
288
|
-
withAttributes:[self textAttributesWithSize:23 color:[NSColor colorWithWhite:0.97 alpha:1] weight:NSFontWeightBold]];
|
|
289
|
-
[@"实时配额状态" drawInRect:NSMakeRect(25, QuotaLogicalHeight - 76, 130, 20)
|
|
290
|
-
withAttributes:[self textAttributesWithSize:12 color:[NSColor colorWithWhite:0.63 alpha:1] weight:NSFontWeightRegular]];
|
|
291
341
|
BOOL recentlyRefreshed = self.refreshedUntil > NSDate.date.timeIntervalSince1970;
|
|
292
|
-
NSRect live = NSMakeRect(QuotaLogicalWidth - 145, QuotaLogicalHeight - 51, 78, 28);
|
|
293
|
-
[self fillRoundedRect:live radius:14 color:[NSColor colorWithWhite:1 alpha:0.055]];
|
|
294
|
-
[[NSColor systemGreenColor] setFill];
|
|
295
|
-
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(NSMinX(live) + 12, NSMinY(live) + 10, 8, 8)] fill];
|
|
296
|
-
[recentlyRefreshed ? @"已刷新" : @"实时更新" drawInRect:NSMakeRect(NSMinX(live) + 26, NSMinY(live) + 6, 50, 18)
|
|
297
|
-
withAttributes:[self textAttributesWithSize:11 color:[NSColor colorWithWhite:0.87 alpha:1] weight:NSFontWeightMedium]];
|
|
298
342
|
CGFloat buttonAlpha = self.refreshPressed ? 0.18 : (self.refreshHovered ? 0.12 : 0.06);
|
|
299
343
|
NSRect logicalRefreshRect = [self logicalRefreshRect];
|
|
300
344
|
NSRect refreshRect = self.refreshPressed ? NSInsetRect(logicalRefreshRect, 1.5, 1.5) : logicalRefreshRect;
|
|
@@ -302,16 +346,52 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
302
346
|
[recentlyRefreshed ? @"✓" : @"↻" drawInRect:NSInsetRect(refreshRect, 8, 4)
|
|
303
347
|
withAttributes:[self textAttributesWithSize:19 color:[NSColor colorWithWhite:0.9 alpha:1] weight:NSFontWeightRegular]];
|
|
304
348
|
|
|
349
|
+
NSRect summary = NSMakeRect(20, 372, QuotaLogicalWidth - 40, 86);
|
|
350
|
+
NSBezierPath *summaryPath = [NSBezierPath bezierPathWithRoundedRect:summary xRadius:18 yRadius:18];
|
|
351
|
+
[[NSColor colorWithWhite:1 alpha:0.065] setFill];
|
|
352
|
+
[summaryPath fill];
|
|
353
|
+
[[NSColor colorWithWhite:1 alpha:0.16] setStroke];
|
|
354
|
+
[summaryPath stroke];
|
|
355
|
+
|
|
356
|
+
// 在线 Agent 数已经在标题旁展示过,这里换成 5 小时合计,和第二格的 7 天形成对照。
|
|
357
|
+
// 三格等宽由 summary 推导,改宽度时分隔线和内容不会各走各的。
|
|
358
|
+
NSArray<NSDictionary *> *cells = @[
|
|
359
|
+
@{@"icon": @"◔", @"tint": cyan, @"value": [self formattedTokenCount:
|
|
360
|
+
[self combinedTokens:@"today"]], @"label": @"今日 Token"},
|
|
361
|
+
@{@"icon": @"◉", @"tint": cyan, @"value": [self formattedTokenCount:
|
|
362
|
+
[self combinedTokens:@"recentWeek"]], @"label": @"最近 7 天"},
|
|
363
|
+
@{@"icon": @"◷", @"tint": NSColor.systemPurpleColor, @"value": [self refreshAgeText],
|
|
364
|
+
@"label": @"数据刷新"}
|
|
365
|
+
];
|
|
366
|
+
CGFloat cellWidth = NSWidth(summary) / cells.count;
|
|
367
|
+
[[NSColor colorWithWhite:1 alpha:0.10] setStroke];
|
|
368
|
+
for (NSUInteger index = 1; index < cells.count; index++) {
|
|
369
|
+
CGFloat x = NSMinX(summary) + cellWidth * index;
|
|
370
|
+
[NSBezierPath strokeLineFromPoint:NSMakePoint(x, NSMinY(summary) + 16)
|
|
371
|
+
toPoint:NSMakePoint(x, NSMaxY(summary) - 16)];
|
|
372
|
+
}
|
|
373
|
+
for (NSUInteger index = 0; index < cells.count; index++) {
|
|
374
|
+
NSDictionary *cell = cells[index];
|
|
375
|
+
CGFloat x = NSMinX(summary) + cellWidth * index;
|
|
376
|
+
[cell[@"icon"] drawInRect:NSMakeRect(x + 26, NSMinY(summary) + 23, 42, 42)
|
|
377
|
+
withAttributes:[self textAttributesWithSize:30 color:cell[@"tint"]
|
|
378
|
+
weight:NSFontWeightRegular]];
|
|
379
|
+
[cell[@"value"] drawInRect:NSMakeRect(x + 74, NSMinY(summary) + 40, 150, 28)
|
|
380
|
+
withAttributes:[self textAttributesWithSize:22 color:primary weight:NSFontWeightBold]];
|
|
381
|
+
[cell[@"label"] drawInRect:NSMakeRect(x + 74, NSMinY(summary) + 17, 150, 20)
|
|
382
|
+
withAttributes:[self textAttributesWithSize:12 color:secondary
|
|
383
|
+
weight:NSFontWeightRegular]];
|
|
384
|
+
}
|
|
385
|
+
|
|
305
386
|
[self drawProvider:@"Codex" usage:self.codexUsage usedKey:@"used_percent"
|
|
306
|
-
color:
|
|
307
|
-
cardRect:NSMakeRect(18, 174, QuotaLogicalWidth - 36, 104)];
|
|
387
|
+
color:cyan cardRect:NSMakeRect(20, 208, QuotaLogicalWidth - 40, 152)];
|
|
308
388
|
[self drawProvider:@"Claude" usage:self.claudeUsage usedKey:@"used_percentage"
|
|
309
389
|
color:[NSColor colorWithRed:0.66 green:0.43 blue:0.94 alpha:1]
|
|
310
|
-
cardRect:NSMakeRect(
|
|
311
|
-
NSString *footer =
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
[footer drawInRect:NSMakeRect(
|
|
390
|
+
cardRect:NSMakeRect(20, 44, QuotaLogicalWidth - 40, 152)];
|
|
391
|
+
NSString *footer = @"ⓘ Token 为本机统计;官方订阅额度以服务端百分比为准,数据可能存在短暂延迟";
|
|
392
|
+
// 卡片底边在 y=44,原来 footer 顶到 44 完全没有间隙。下移到与卡片留约 8pt,
|
|
393
|
+
// 同时底部留白 16pt,和顶部标题的留白大致对称。
|
|
394
|
+
[footer drawInRect:NSMakeRect(28, 16, 650, 20)
|
|
315
395
|
withAttributes:[self textAttributesWithSize:11 color:[NSColor colorWithWhite:0.58 alpha:1] weight:NSFontWeightRegular]];
|
|
316
396
|
[NSGraphicsContext restoreGraphicsState];
|
|
317
397
|
}
|