cc-pets 2.2.0 → 2.3.0
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/CC_BRIDGE.md +5 -2
- package/CC_BRIDGE.zh-CN.md +4 -2
- package/CHANGELOG.md +85 -0
- package/CHANGELOG.zh-CN.md +85 -0
- package/CONTRIBUTING.md +28 -11
- package/CONTRIBUTING.zh-CN.md +23 -11
- package/README.md +127 -27
- package/README.zh-CN.md +104 -30
- package/Resources/zh-Hans.lproj/Localizable.strings +573 -17
- package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
- package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +89 -31
- package/Sources/CCPets/{CCPetsAppDelegate+Bridge.m → App/CCPetsAppDelegate+Bridge.m} +35 -24
- package/Sources/CCPets/{CCPetsAppDelegate+Pets.m → App/CCPetsAppDelegate+Pets.m} +18 -4
- package/Sources/CCPets/{CCPetsAppDelegate+Private.h → App/CCPetsAppDelegate+Private.h} +90 -21
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +238 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Quota.m → App/CCPetsAppDelegate+Quota.m} +47 -32
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +269 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +13 -7
- package/Sources/CCPets/{CCPetsAppDelegate+Update.m → App/CCPetsAppDelegate+Update.m} +71 -47
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
- package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +16 -2
- package/Sources/CCPets/{CCPetsAppDelegate.m → App/CCPetsAppDelegate.m} +110 -12
- package/Sources/CCPets/App/CCPetsOpenCommand.h +13 -0
- package/Sources/CCPets/App/CCPetsOpenCommand.m +99 -0
- package/Sources/CCPets/{main.m → App/main.m} +68 -1
- package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
- package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +32 -17
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +792 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +60 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +558 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +501 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +103 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +779 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.h +75 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.m +342 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.h +15 -0
- package/Sources/CCPets/{CCPetsEvents.m → Integrations/CCPetsEvents.m} +23 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.h +51 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
- package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +42 -12
- package/Sources/CCPets/Pet/CCPetsImageLoader.h +5 -0
- package/Sources/CCPets/{CCPetsImageLoader.m → Pet/CCPetsImageLoader.m} +10 -2
- package/Sources/CCPets/Pet/CCPetsPetSize.h +10 -0
- package/Sources/CCPets/Pet/CCPetsPetSize.m +21 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.h +12 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.m +476 -0
- package/Sources/CCPets/{CCPetsPhrases.h → Pet/CCPetsPhrases.h} +18 -2
- package/Sources/CCPets/{CCPetsPhrases.m → Pet/CCPetsPhrases.m} +53 -22
- package/Sources/CCPets/{CCPetsPhrasesEditor.m → Pet/CCPetsPhrasesEditor.m} +42 -2
- package/Sources/CCPets/Pet/PetView+Interaction.m +207 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +598 -0
- package/Sources/CCPets/Pet/PetView+MicroBehavior.m +234 -0
- package/Sources/CCPets/Pet/PetView+Private.h +81 -0
- package/Sources/CCPets/{PetView.h → Pet/PetView.h} +3 -5
- package/Sources/CCPets/Pet/PetView.m +635 -0
- package/Sources/CCPets/Support/CCPetsDoctor.h +25 -0
- package/Sources/CCPets/Support/CCPetsDoctor.m +632 -0
- package/Sources/CCPets/Support/CCPetsJSON.h +27 -0
- package/Sources/CCPets/Support/CCPetsJSON.m +397 -0
- package/Sources/CCPets/{CCPetsL10n.h → Support/CCPetsL10n.h} +3 -1
- package/Sources/CCPets/{CCPetsL10n.m → Support/CCPetsL10n.m} +30 -1
- package/Sources/CCPets/Support/CCPetsPreferences.h +31 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +85 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +42 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +268 -0
- package/Sources/CCPets/{CCPetsGlassView.h → UI/CCPetsGlassView.h} +5 -1
- package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +25 -7
- package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
- package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +62 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +917 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +132 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +816 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +284 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +717 -0
- package/Sources/CCPets/UI/CCPetsUsageShared.h +53 -0
- package/Sources/CCPets/UI/CCPetsUsageStore.m +120 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.h +30 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.m +201 -0
- package/Sources/CCPets/UI/CCPetsWorkbench.h +40 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.h +13 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +783 -0
- package/Sources/CCPets/{MenuChoiceRow.m → UI/MenuChoiceRow.m} +3 -0
- package/Sources/CCPets/UI/MenuPetSizeView.h +5 -0
- package/Sources/CCPets/UI/MenuPetSizeView.m +73 -0
- package/Sources/CCPets/{MenuToggleSwitch.m → UI/MenuToggleSwitch.m} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.h → Usage/CCPetsCodexRateLimits.h} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.m → Usage/CCPetsCodexRateLimits.m} +10 -3
- package/Sources/CCPets/{CCPetsQuotaHistory.h → Usage/CCPetsQuotaHistory.h} +3 -0
- package/Sources/CCPets/{CCPetsQuotaHistory.m → Usage/CCPetsQuotaHistory.m} +58 -0
- package/Sources/CCPets/Usage/CCPetsUsage+ClaudeReader.m +436 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexReader.m +392 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexSessions.m +484 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +96 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +285 -0
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
- package/Sources/CCPets/Usage/CCPetsUsage.h +57 -0
- package/Sources/CCPets/Usage/CCPetsUsage.m +225 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.h +10 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.m +163 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.h → Usage/CCPetsUsageMonitor.h} +3 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.m → Usage/CCPetsUsageMonitor.m} +163 -6
- package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +328 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +103 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +616 -0
- package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
- package/Sources/CCPets/Usage/QuotaDashboardView.m +587 -0
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/THIRD_PARTY_NOTICES.zh-CN.md +1 -1
- package/bin/cc-pets +8 -6
- package/bin/claude-with-pet +10 -2
- package/bin/codex-with-pet +13 -5
- package/package.json +1 -1
- package/scripts/build.sh +7 -3
- package/scripts/i18n.zsh +9 -52
- package/scripts/install-app.sh +4 -0
- package/scripts/install-shell-integration.sh +17 -10
- package/scripts/uninstall-shell-integration.sh +6 -1
- package/Sources/CCPets/CCPetsAppDelegate+Settings.m +0 -226
- package/Sources/CCPets/CCPetsEvents.h +0 -10
- package/Sources/CCPets/CCPetsImageLoader.h +0 -4
- package/Sources/CCPets/CCPetsUsage.h +0 -36
- package/Sources/CCPets/CCPetsUsage.m +0 -2069
- package/Sources/CCPets/PetView.m +0 -1806
- package/Sources/CCPets/QuotaDashboardView.m +0 -1145
- package/scripts/bridge/cli.mjs +0 -237
- package/scripts/bridge/core.mjs +0 -228
- package/scripts/bridge/hook.mjs +0 -150
- package/scripts/bridge/install.mjs +0 -311
- package/scripts/bridge/mcp-server.mjs +0 -308
- package/scripts/bridge/options.mjs +0 -87
- package/scripts/bridge/process.mjs +0 -151
- package/scripts/bridge/reservations.mjs +0 -248
- package/scripts/bridge/store.mjs +0 -433
- package/scripts/bridge/watch.mjs +0 -69
- package/scripts/check-l10n.mjs +0 -168
- package/scripts/configure-updater.mjs +0 -43
- package/scripts/detect-cli.mjs +0 -68
- package/scripts/doctor.mjs +0 -470
- package/scripts/i18n.mjs +0 -122
- package/scripts/install-claude-hooks.mjs +0 -253
- package/scripts/install-codex-hooks.mjs +0 -78
- package/scripts/integration-markers.mjs +0 -51
- package/scripts/locales/zh-Hans.json +0 -366
- package/scripts/pet-store.mjs +0 -415
- package/scripts/uninstall-integrations.mjs +0 -205
- /package/Sources/CCPets/{CCPetsVersion.h → App/CCPetsVersion.h} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.m → App/CCPetsVersion.m} +0 -0
- /package/Sources/CCPets/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +0 -0
- /package/Sources/CCPets/{CCPetsPhrasesEditor.h → Pet/CCPetsPhrasesEditor.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.h → Pet/CCPetsPreviews.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.m → Pet/CCPetsPreviews.m} +0 -0
- /package/Sources/CCPets/Resources/{/351/273/230/350/256/244.webp" → ByteMochi.webp} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.h → Support/CCPetsCleanup.h} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.m → Support/CCPetsCleanup.m} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.h → Support/CCPetsPaths.h} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.m → Support/CCPetsPaths.m} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.h → Support/CCPetsSystemMonitor.h} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.m → Support/CCPetsSystemMonitor.m} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.h → UI/CCPetsGlassMenu.h} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.m → UI/CCPetsGlassMenu.m} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.h → UI/MenuChoiceRow.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.h → UI/MenuHintView.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.m → UI/MenuHintView.m} +0 -0
- /package/Sources/CCPets/{MenuToggleSwitch.h → UI/MenuToggleSwitch.h} +0 -0
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
#import "CCPetsUsage.h"
|
|
3
3
|
#import "CCPetsCodexRateLimits.h"
|
|
4
4
|
#import "CCPetsPaths.h"
|
|
5
|
+
#import "CCPetsProfiles.h"
|
|
6
|
+
#import "CCPetsUsageMode.h"
|
|
5
7
|
#import <CoreServices/CoreServices.h>
|
|
6
8
|
#import <fcntl.h>
|
|
7
9
|
#import <unistd.h>
|
|
@@ -20,6 +22,13 @@
|
|
|
20
22
|
@property ClaudeUsageReader *claudeReader;
|
|
21
23
|
@property(readwrite) NSDictionary *codexUsage;
|
|
22
24
|
@property(readwrite) NSDictionary *claudeUsage;
|
|
25
|
+
@property(readwrite) NSDictionary<NSString *, NSDictionary *> *profileUsages;
|
|
26
|
+
@property(readwrite) NSArray<NSDictionary *> *profiles;
|
|
27
|
+
@property NSMutableDictionary<NSString *, ClaudeUsageReader *> *claudeProfileReaders;
|
|
28
|
+
@property NSMutableDictionary<NSString *, CodexUsageReader *> *codexProfileReaders;
|
|
29
|
+
@property NSMutableDictionary<NSString *, CCPetsCodexRateLimitsReader *> *codexProfileRateReaders;
|
|
30
|
+
@property NSMutableDictionary<NSString *, NSDictionary *> *codexProfileLiveUsages;
|
|
31
|
+
@property NSMutableDictionary<NSString *, NSString *> *profileReaderHomes;
|
|
23
32
|
@property NSDictionary *codexLiveUsage;
|
|
24
33
|
@property NSTimeInterval codexLiveUsageUpdatedAt;
|
|
25
34
|
@property FSEventStreamRef codexStream;
|
|
@@ -38,6 +47,15 @@
|
|
|
38
47
|
// 失败时不能让一次旧的受限结论一直挂着。
|
|
39
48
|
static const NSTimeInterval CodexLiveUsageTTL = 5 * 60.0;
|
|
40
49
|
|
|
50
|
+
static BOOL HasRegisteredDefaultProfile(NSString *provider) {
|
|
51
|
+
NSString *target = ([provider isEqualToString:CCPetsProfileProviderClaude]
|
|
52
|
+
? DefaultClaudeConfigDirectory() : DefaultCodexHomeDirectory()).stringByStandardizingPath;
|
|
53
|
+
for (NSDictionary *profile in CCPetsProfilesForProvider(provider)) {
|
|
54
|
+
if ([[profile[@"home"] stringByStandardizingPath] isEqualToString:target]) return YES;
|
|
55
|
+
}
|
|
56
|
+
return NO;
|
|
57
|
+
}
|
|
58
|
+
|
|
41
59
|
static NSDictionary *LoadCodexLiveUsage(void) {
|
|
42
60
|
NSData *data = [NSData dataWithContentsOfFile:CodexLiveUsagePath()];
|
|
43
61
|
id value = data.length > 0 ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
|
@@ -111,6 +129,11 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
111
129
|
_codexReader = [CodexUsageReader new];
|
|
112
130
|
_codexRateLimitsReader = [CCPetsCodexRateLimitsReader new];
|
|
113
131
|
_claudeReader = [ClaudeUsageReader new];
|
|
132
|
+
_claudeProfileReaders = [NSMutableDictionary dictionary];
|
|
133
|
+
_codexProfileReaders = [NSMutableDictionary dictionary];
|
|
134
|
+
_codexProfileRateReaders = [NSMutableDictionary dictionary];
|
|
135
|
+
_codexProfileLiveUsages = [NSMutableDictionary dictionary];
|
|
136
|
+
_profileReaderHomes = [NSMutableDictionary dictionary];
|
|
114
137
|
// 桌宠是唯一的常驻读取者,摘要缓存由它维护并落盘。
|
|
115
138
|
_codexReader.persistsCache = YES;
|
|
116
139
|
_claudeReader.persistsCache = YES;
|
|
@@ -149,12 +172,128 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
149
172
|
}
|
|
150
173
|
// 回调交给主线程:下游全是 UI(面板重排、气泡定位、系统通知)。
|
|
151
174
|
- (void)publishChange {
|
|
175
|
+
[self syncDefaultProfileUsages];
|
|
152
176
|
void (^handler)(NSDictionary *, NSDictionary *) = self.changeHandler;
|
|
153
177
|
if (!handler) return;
|
|
154
178
|
NSDictionary *codex = self.codexUsage;
|
|
155
179
|
NSDictionary *claude = self.claudeUsage;
|
|
156
180
|
dispatch_async(dispatch_get_main_queue(), ^{ handler(codex, claude); });
|
|
157
181
|
}
|
|
182
|
+
|
|
183
|
+
// 默认账号的卡片读的是 profileUsages,但 FSEvents、statusline 监听和 App Server 回调
|
|
184
|
+
// 只更新 codexUsage / claudeUsage。每次发布前把默认账号那一格对齐到最新值,
|
|
185
|
+
// 否则卡片要等下一轮全量刷新(最长 120 秒)才动。
|
|
186
|
+
- (void)syncDefaultProfileUsages {
|
|
187
|
+
if (self.profiles.count == 0) return;
|
|
188
|
+
NSString *defaultClaude = DefaultClaudeConfigDirectory().stringByStandardizingPath;
|
|
189
|
+
NSString *defaultCodex = DefaultCodexHomeDirectory().stringByStandardizingPath;
|
|
190
|
+
NSMutableDictionary<NSString *, NSDictionary *> *usages =
|
|
191
|
+
[self.profileUsages mutableCopy] ?: [NSMutableDictionary dictionary];
|
|
192
|
+
BOOL changed = NO;
|
|
193
|
+
for (NSDictionary *profile in self.profiles) {
|
|
194
|
+
NSString *profileID = profile[@"id"], *provider = profile[@"provider"];
|
|
195
|
+
NSString *home = [profile[@"home"] stringByStandardizingPath];
|
|
196
|
+
if (profileID.length == 0 || home.length == 0) continue;
|
|
197
|
+
NSDictionary *usage = nil;
|
|
198
|
+
if ([provider isEqualToString:CCPetsProfileProviderClaude] && [home isEqualToString:defaultClaude]) {
|
|
199
|
+
usage = self.claudeUsage;
|
|
200
|
+
} else if ([provider isEqualToString:CCPetsProfileProviderCodex] && [home isEqualToString:defaultCodex]) {
|
|
201
|
+
usage = self.codexUsage;
|
|
202
|
+
} else continue;
|
|
203
|
+
if (usages[profileID] == usage || [usages[profileID] isEqual:usage]) continue;
|
|
204
|
+
usages[profileID] = usage;
|
|
205
|
+
changed = YES;
|
|
206
|
+
}
|
|
207
|
+
if (changed) self.profileUsages = usages;
|
|
208
|
+
}
|
|
209
|
+
- (void)refreshProfileUsages {
|
|
210
|
+
NSArray<NSDictionary *> *profiles = CCPetsProfiles();
|
|
211
|
+
NSMutableDictionary<NSString *, NSDictionary *> *usages = [NSMutableDictionary dictionary];
|
|
212
|
+
NSMutableSet<NSString *> *liveIDs = [NSMutableSet set];
|
|
213
|
+
NSString *defaultClaude = DefaultClaudeConfigDirectory().stringByStandardizingPath;
|
|
214
|
+
NSString *defaultCodex = DefaultCodexHomeDirectory().stringByStandardizingPath;
|
|
215
|
+
for (NSDictionary *profile in profiles) {
|
|
216
|
+
NSString *profileID = profile[@"id"], *provider = profile[@"provider"];
|
|
217
|
+
NSString *home = [profile[@"home"] stringByStandardizingPath];
|
|
218
|
+
if (profileID.length == 0 || home.length == 0) continue;
|
|
219
|
+
[liveIDs addObject:profileID];
|
|
220
|
+
NSString *readerHome = self.profileReaderHomes[profileID];
|
|
221
|
+
if (readerHome.length > 0 && ![readerHome isEqualToString:home]) {
|
|
222
|
+
[self.claudeProfileReaders removeObjectForKey:profileID];
|
|
223
|
+
[self.codexProfileReaders removeObjectForKey:profileID];
|
|
224
|
+
[self.codexProfileRateReaders[profileID] stop];
|
|
225
|
+
[self.codexProfileRateReaders removeObjectForKey:profileID];
|
|
226
|
+
[self.codexProfileLiveUsages removeObjectForKey:profileID];
|
|
227
|
+
}
|
|
228
|
+
self.profileReaderHomes[profileID] = home;
|
|
229
|
+
NSDictionary *usage = nil;
|
|
230
|
+
if ([provider isEqualToString:CCPetsProfileProviderClaude]) {
|
|
231
|
+
if ([home isEqualToString:defaultClaude]) usage = self.claudeUsage;
|
|
232
|
+
else {
|
|
233
|
+
ClaudeUsageReader *reader = self.claudeProfileReaders[profileID];
|
|
234
|
+
if (!reader) {
|
|
235
|
+
reader = [[ClaudeUsageReader alloc] initWithConfigDirectory:home];
|
|
236
|
+
reader.persistsCache = YES;
|
|
237
|
+
self.claudeProfileReaders[profileID] = reader;
|
|
238
|
+
}
|
|
239
|
+
usage = [reader refreshWithFullAggregation];
|
|
240
|
+
}
|
|
241
|
+
} else if ([provider isEqualToString:CCPetsProfileProviderCodex]) {
|
|
242
|
+
if ([home isEqualToString:defaultCodex]) usage = self.codexUsage;
|
|
243
|
+
else {
|
|
244
|
+
CodexUsageReader *reader = self.codexProfileReaders[profileID];
|
|
245
|
+
if (!reader) {
|
|
246
|
+
reader = [[CodexUsageReader alloc] initWithCodexHome:home];
|
|
247
|
+
reader.persistsCache = YES;
|
|
248
|
+
self.codexProfileReaders[profileID] = reader;
|
|
249
|
+
}
|
|
250
|
+
usage = [reader refreshWithFullDiscovery];
|
|
251
|
+
NSDictionary *live = self.codexProfileLiveUsages[profileID];
|
|
252
|
+
if (live) usage = UsageByRemovingExpiredQuotaWindows(
|
|
253
|
+
CodexUsageByApplyingLiveUsage(usage, live));
|
|
254
|
+
CCPetsCodexRateLimitsReader *rateReader = self.codexProfileRateReaders[profileID];
|
|
255
|
+
if (!rateReader) {
|
|
256
|
+
rateReader = [[CCPetsCodexRateLimitsReader alloc] initWithCodexHome:home];
|
|
257
|
+
__weak typeof(self) weakSelf = self;
|
|
258
|
+
NSString *capturedID = [profileID copy];
|
|
259
|
+
rateReader.liveUsageHandler = ^(NSDictionary *liveUsage) {
|
|
260
|
+
dispatch_async(weakSelf.queue, ^{
|
|
261
|
+
typeof(self) strongSelf = weakSelf;
|
|
262
|
+
if (!strongSelf || !strongSelf.started) return;
|
|
263
|
+
strongSelf.codexProfileLiveUsages[capturedID] = liveUsage;
|
|
264
|
+
NSMutableDictionary *updated = [strongSelf.profileUsages mutableCopy] ?: [NSMutableDictionary dictionary];
|
|
265
|
+
NSDictionary *session = strongSelf.codexProfileReaders[capturedID].usage;
|
|
266
|
+
if (session) updated[capturedID] = UsageByRemovingExpiredQuotaWindows(
|
|
267
|
+
CodexUsageByApplyingLiveUsage(session, liveUsage));
|
|
268
|
+
strongSelf.profileUsages = updated;
|
|
269
|
+
[strongSelf publishChange];
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
self.codexProfileRateReaders[profileID] = rateReader;
|
|
273
|
+
}
|
|
274
|
+
[rateReader refresh];
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (usage) usages[profileID] = usage;
|
|
278
|
+
}
|
|
279
|
+
for (NSString *profileID in self.claudeProfileReaders.allKeys.copy) {
|
|
280
|
+
if (![liveIDs containsObject:profileID]) [self.claudeProfileReaders removeObjectForKey:profileID];
|
|
281
|
+
}
|
|
282
|
+
for (NSString *profileID in self.codexProfileReaders.allKeys.copy) {
|
|
283
|
+
if (![liveIDs containsObject:profileID]) {
|
|
284
|
+
[self.codexProfileReaders removeObjectForKey:profileID];
|
|
285
|
+
[self.codexProfileRateReaders[profileID] stop];
|
|
286
|
+
[self.codexProfileRateReaders removeObjectForKey:profileID];
|
|
287
|
+
[self.codexProfileLiveUsages removeObjectForKey:profileID];
|
|
288
|
+
[self.profileReaderHomes removeObjectForKey:profileID];
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
for (NSString *profileID in self.profileReaderHomes.allKeys.copy) {
|
|
292
|
+
if (![liveIDs containsObject:profileID]) [self.profileReaderHomes removeObjectForKey:profileID];
|
|
293
|
+
}
|
|
294
|
+
self.profiles = profiles;
|
|
295
|
+
self.profileUsages = usages;
|
|
296
|
+
}
|
|
158
297
|
// changed=YES 表示额度文件确实变了(VNODE 事件),跳过 stat 比较;
|
|
159
298
|
// aggregate=YES 表示这是手动刷新/面板打开/兜底轮询,Token 聚合要绕过节流立即重算。
|
|
160
299
|
- (NSDictionary *)readClaudeUsageChanged:(BOOL)changed forcingAggregation:(BOOL)aggregate {
|
|
@@ -240,13 +379,28 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
240
379
|
}
|
|
241
380
|
- (void)refreshOnQueue {
|
|
242
381
|
if (!self.started) return;
|
|
382
|
+
CCPetsRefreshProfileAuthenticationModes(CCPetsProfiles());
|
|
243
383
|
// 手动刷新、面板打开和 120 秒兜底都走全量路径,Token 聚合的节流只约束 FSEvents 热路径。
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
if (
|
|
249
|
-
|
|
384
|
+
BOOL hasDefaultCodex = HasRegisteredDefaultProfile(CCPetsProfileProviderCodex);
|
|
385
|
+
BOOL hasDefaultClaude = HasRegisteredDefaultProfile(CCPetsProfileProviderClaude);
|
|
386
|
+
self.codexUsage = hasDefaultCodex ? [self usageByApplyingLiveCodexUsage:
|
|
387
|
+
[self.codexReader refreshWithFullDiscovery]] : nil;
|
|
388
|
+
if (hasDefaultCodex) [self.codexRateLimitsReader refresh];
|
|
389
|
+
self.claudeUsage = hasDefaultClaude
|
|
390
|
+
? [self readClaudeUsageChanged:NO forcingAggregation:YES] : nil;
|
|
391
|
+
[self refreshProfileUsages];
|
|
392
|
+
if (hasDefaultCodex && !self.codexStream) [self startCodexStream];
|
|
393
|
+
if (hasDefaultClaude && !self.claudeSource) [self startClaudeSource];
|
|
394
|
+
if (!hasDefaultCodex && self.codexStream) {
|
|
395
|
+
FSEventStreamStop(self.codexStream);
|
|
396
|
+
FSEventStreamInvalidate(self.codexStream);
|
|
397
|
+
FSEventStreamRelease(self.codexStream);
|
|
398
|
+
self.codexStream = NULL;
|
|
399
|
+
}
|
|
400
|
+
if (!hasDefaultClaude && self.claudeSource) {
|
|
401
|
+
dispatch_source_cancel(self.claudeSource);
|
|
402
|
+
self.claudeSource = nil;
|
|
403
|
+
}
|
|
250
404
|
[self publishChange];
|
|
251
405
|
}
|
|
252
406
|
// 同步收尾:退出时要保证事件源在进程消失之前真的停掉,而它们只能在读取队列上安全拆除。
|
|
@@ -259,6 +413,9 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
259
413
|
}
|
|
260
414
|
- (void)teardownSources {
|
|
261
415
|
[self.codexRateLimitsReader stop];
|
|
416
|
+
for (CCPetsCodexRateLimitsReader *reader in self.codexProfileRateReaders.allValues) {
|
|
417
|
+
[reader stop];
|
|
418
|
+
}
|
|
262
419
|
if (self.codexStream) {
|
|
263
420
|
FSEventStreamStop(self.codexStream);
|
|
264
421
|
FSEventStreamInvalidate(self.codexStream);
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
#import "QuotaDashboardView+Private.h"
|
|
2
|
+
|
|
3
|
+
@implementation QuotaDashboardView (Primitives)
|
|
4
|
+
- (NSColor *)primaryColor {
|
|
5
|
+
return [NSColor colorWithWhite:0.97 alpha:1];
|
|
6
|
+
}
|
|
7
|
+
- (NSColor *)secondaryColor {
|
|
8
|
+
// 没有深色底托着时,0.68 的灰在亮壁纸上会发虚。
|
|
9
|
+
return [NSColor colorWithWhite:self.usesLiquidGlass ? 0.84 : 0.68 alpha:1];
|
|
10
|
+
}
|
|
11
|
+
// 原生玻璃背后只有一层压暗,原来的紫在上面偏暗,提亮到和青色相近的明度。
|
|
12
|
+
- (NSColor *)claudeColor {
|
|
13
|
+
return self.usesLiquidGlass ? [NSColor colorWithRed:0.80 green:0.66 blue:1.00 alpha:1]
|
|
14
|
+
: [NSColor colorWithRed:0.66 green:0.43 blue:0.94 alpha:1];
|
|
15
|
+
}
|
|
16
|
+
- (NSColor *)accentColor { return [NSColor colorWithRed:0.30 green:0.82 blue:0.90 alpha:1]; }
|
|
17
|
+
// 有 Agent 在线才用绿色,否则整块转灰,避免"暂无 Agent 在线"配一个绿点。
|
|
18
|
+
- (NSColor *)agentStatusColor {
|
|
19
|
+
return self.activeAgentCount > 0 ? NSColor.systemGreenColor : self.secondaryColor;
|
|
20
|
+
}
|
|
21
|
+
- (void)drawSystemMetricsEndingAtX:(CGFloat)rightEdge y:(CGFloat)y {
|
|
22
|
+
NSMutableArray<NSDictionary *> *metrics = [NSMutableArray array];
|
|
23
|
+
if (self.systemCPUEnabled) {
|
|
24
|
+
NSString *value = self.systemCPUPercent
|
|
25
|
+
? [NSString stringWithFormat:@"%.0f%%", self.systemCPUPercent.doubleValue] : @"--";
|
|
26
|
+
[metrics addObject:@{@"symbol": @"cpu", @"label": [NSString stringWithFormat:@"CPU %@", value],
|
|
27
|
+
@"color": [NSColor colorWithRed:245.0 / 255.0 green:166.0 / 255.0
|
|
28
|
+
blue:35.0 / 255.0 alpha:1]}];
|
|
29
|
+
}
|
|
30
|
+
if (self.systemTemperatureEnabled) {
|
|
31
|
+
NSString *value = self.systemTemperatureCelsius
|
|
32
|
+
? [NSString stringWithFormat:@"%.0f°C", self.systemTemperatureCelsius.doubleValue] : @"--°C";
|
|
33
|
+
double temperature = self.systemTemperatureCelsius.doubleValue;
|
|
34
|
+
NSColor *color = temperature >= 90 ? NSColor.systemRedColor :
|
|
35
|
+
(temperature >= 80 ? [NSColor colorWithRed:1.00 green:0.35 blue:0.16 alpha:1] :
|
|
36
|
+
[NSColor colorWithRed:1.00 green:107.0 / 255.0 blue:74.0 / 255.0 alpha:1]);
|
|
37
|
+
[metrics addObject:@{@"symbol": @"thermometer.medium", @"label": value,
|
|
38
|
+
@"color": color}];
|
|
39
|
+
}
|
|
40
|
+
if (self.systemMemoryEnabled) {
|
|
41
|
+
NSString *value = self.systemMemoryPercent
|
|
42
|
+
? [NSString stringWithFormat:@"%.0f%%", self.systemMemoryPercent.doubleValue] : @"--";
|
|
43
|
+
[metrics addObject:@{@"symbol": @"memorychip", @"label": [NSString stringWithFormat:@"MEM %@", value],
|
|
44
|
+
@"color": [NSColor colorWithRed:32.0 / 255.0 green:214.0 / 255.0
|
|
45
|
+
blue:165.0 / 255.0 alpha:1]}];
|
|
46
|
+
}
|
|
47
|
+
NSFont *font = [NSFont systemFontOfSize:13 weight:NSFontWeightSemibold];
|
|
48
|
+
NSMutableAttributedString *statusLine = [NSMutableAttributedString new];
|
|
49
|
+
for (NSUInteger index = 0; index < metrics.count; index++) {
|
|
50
|
+
NSDictionary *metric = metrics[index];
|
|
51
|
+
NSString *label = metric[@"label"];
|
|
52
|
+
NSColor *color = metric[@"color"];
|
|
53
|
+
NSDictionary *attributes = @{NSFontAttributeName: font,
|
|
54
|
+
NSForegroundColorAttributeName: [self textColor:color]};
|
|
55
|
+
NSImage *icon = [NSImage imageWithSystemSymbolName:metric[@"symbol"]
|
|
56
|
+
accessibilityDescription:label];
|
|
57
|
+
NSImageSymbolConfiguration *sizeConfiguration =
|
|
58
|
+
[NSImageSymbolConfiguration configurationWithPointSize:
|
|
59
|
+
[metric[@"symbol"] isEqualToString:@"thermometer.medium"] ? 16 : 14
|
|
60
|
+
weight:NSFontWeightSemibold];
|
|
61
|
+
NSImageSymbolConfiguration *colorConfiguration =
|
|
62
|
+
[NSImageSymbolConfiguration configurationWithHierarchicalColor:metric[@"color"]];
|
|
63
|
+
icon = [icon imageWithSymbolConfiguration:
|
|
64
|
+
[sizeConfiguration configurationByApplyingConfiguration:colorConfiguration]];
|
|
65
|
+
NSTextAttachment *attachment = [NSTextAttachment new];
|
|
66
|
+
attachment.image = icon;
|
|
67
|
+
CGFloat iconSize = [metric[@"symbol"] isEqualToString:@"thermometer.medium"] ? 18 : 16;
|
|
68
|
+
attachment.bounds = NSMakeRect(0, (font.capHeight - iconSize) / 2.0,
|
|
69
|
+
iconSize, iconSize);
|
|
70
|
+
[statusLine appendAttributedString:[NSAttributedString
|
|
71
|
+
attributedStringWithAttachment:attachment]];
|
|
72
|
+
[statusLine appendAttributedString:[[NSAttributedString alloc]
|
|
73
|
+
initWithString:@" " attributes:attributes]];
|
|
74
|
+
[statusLine appendAttributedString:[[NSAttributedString alloc]
|
|
75
|
+
initWithString:label attributes:attributes]];
|
|
76
|
+
if (index + 1 < metrics.count) {
|
|
77
|
+
NSDictionary *separatorAttributes = @{NSFontAttributeName:
|
|
78
|
+
[NSFont systemFontOfSize:12 weight:NSFontWeightRegular],
|
|
79
|
+
NSForegroundColorAttributeName: [NSColor colorWithWhite:0.42 alpha:0.7]};
|
|
80
|
+
[statusLine appendAttributedString:[[NSAttributedString alloc]
|
|
81
|
+
initWithString:@" │ " attributes:separatorAttributes]];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
CGFloat width = ceil(statusLine.size.width);
|
|
85
|
+
[statusLine drawInRect:NSMakeRect(rightEdge - width, y, width + 1, 20)];
|
|
86
|
+
}
|
|
87
|
+
- (NSString *)formattedTokenSplitForUsage:(NSDictionary *)usage key:(NSString *)key
|
|
88
|
+
wantsOutput:(BOOL)wantsOutput {
|
|
89
|
+
return [self formattedTokenSplit:[self tokenTotals:usage key:key] ?: @{}
|
|
90
|
+
wantsOutput:wantsOutput];
|
|
91
|
+
}
|
|
92
|
+
- (NSNumber *)usedForQuota:(NSDictionary *)quota usedKey:(NSString *)usedKey {
|
|
93
|
+
NSNumber *used = [quota[usedKey] isKindOfClass:NSNumber.class] ? quota[usedKey] : nil;
|
|
94
|
+
return used ? @(fmax(0, fmin(100, used.doubleValue))) : nil;
|
|
95
|
+
}
|
|
96
|
+
- (NSString *)refreshAgeText {
|
|
97
|
+
if (self.lastUpdatedAt <= 0) return L(@"Waiting for data");
|
|
98
|
+
NSTimeInterval age = fmax(0, NSDate.date.timeIntervalSince1970 - self.lastUpdatedAt);
|
|
99
|
+
if (age < 5) return L(@"just now");
|
|
100
|
+
if (age < 60) return [NSString stringWithFormat:L(@"%.0fs ago"), age];
|
|
101
|
+
if (age < 3600) return [NSString stringWithFormat:L(@"%.0fm ago"), floor(age / 60)];
|
|
102
|
+
return [NSString stringWithFormat:L(@"%.0fh ago"), floor(age / 3600)];
|
|
103
|
+
}
|
|
104
|
+
// 两个主题共用字号、字重和等宽数字,切换主题时保持文字大小一致。
|
|
105
|
+
// 小字统一抬到逻辑 14(屏幕约 8.1pt),这是 440pt 宽面板在不改版式的前提下放得下的上限;
|
|
106
|
+
// 再往上要放大面板或删行。数字用等宽字形,跳动时不左右晃;汉字不受影响。
|
|
107
|
+
- (NSFont *)fontWithSize:(CGFloat)size weight:(NSFontWeight)weight {
|
|
108
|
+
if (size < 14) size = 14;
|
|
109
|
+
else if (size <= 16) size += 1;
|
|
110
|
+
if (weight < NSFontWeightMedium) weight = NSFontWeightMedium;
|
|
111
|
+
else if (weight < NSFontWeightSemibold) weight = NSFontWeightSemibold;
|
|
112
|
+
else if (weight < NSFontWeightBold) weight = NSFontWeightBold;
|
|
113
|
+
return [NSFont monospacedDigitSystemFontOfSize:size weight:weight];
|
|
114
|
+
}
|
|
115
|
+
// 局部衬底:只垫在卡片和汇总区下面,面板边缘和卡片间隙保持通透。衬底本身不能带
|
|
116
|
+
// 文字投影,否则卡片边缘会多一圈黑晕。
|
|
117
|
+
// 原生玻璃下在文字前画一个彩色小圆点,替代原来的彩色字;经典主题照旧画彩色字。
|
|
118
|
+
- (void)drawText:(NSString *)text inRect:(NSRect)rect size:(CGFloat)size
|
|
119
|
+
weight:(NSFontWeight)weight color:(NSColor *)color dotColor:(NSColor *)dotColor {
|
|
120
|
+
if (!self.usesLiquidGlass) {
|
|
121
|
+
NSMutableDictionary *attributes = [[self textAttributesWithSize:size color:color
|
|
122
|
+
weight:weight] mutableCopy];
|
|
123
|
+
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
124
|
+
style.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
125
|
+
attributes[NSParagraphStyleAttributeName] = style;
|
|
126
|
+
[text drawInRect:rect withAttributes:[self attributes:attributes fittingText:text
|
|
127
|
+
inWidth:NSWidth(rect)]];
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
NSMutableDictionary *attributes = [[self textAttributesWithSize:size color:color
|
|
131
|
+
weight:weight] mutableCopy];
|
|
132
|
+
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
133
|
+
style.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
134
|
+
attributes[NSParagraphStyleAttributeName] = style;
|
|
135
|
+
NSFont *font = attributes[NSFontAttributeName];
|
|
136
|
+
NSRect textRect = rect;
|
|
137
|
+
if (dotColor) {
|
|
138
|
+
CGFloat diameter = 9;
|
|
139
|
+
// drawInRect: 从矩形顶部开始排版,圆点对齐到首行小写字母的中线。
|
|
140
|
+
CGFloat baselineMid = NSMaxY(rect) - font.ascender + font.xHeight / 2.0;
|
|
141
|
+
[NSGraphicsContext saveGraphicsState];
|
|
142
|
+
[[NSShadow new] set];
|
|
143
|
+
[dotColor setFill];
|
|
144
|
+
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(NSMinX(rect), baselineMid - diameter / 2.0,
|
|
145
|
+
diameter, diameter)] fill];
|
|
146
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
147
|
+
textRect.origin.x += diameter + 6;
|
|
148
|
+
textRect.size.width -= diameter + 6;
|
|
149
|
+
}
|
|
150
|
+
[text drawInRect:textRect withAttributes:[self attributes:attributes fittingText:text
|
|
151
|
+
inWidth:NSWidth(textRect)]];
|
|
152
|
+
}
|
|
153
|
+
// 译文长短差得多,固定宽度的格子迟早有放不下的语言。放不下时先把字号往下收,最多收 2pt
|
|
154
|
+
// (liquid 下即从 14 收到 12)——比截成"…"好认;还放不下才交给截断。放得下就原样返回,
|
|
155
|
+
// 中文和多数英文不受影响。
|
|
156
|
+
- (NSDictionary *)attributes:(NSDictionary *)attributes fittingText:(NSString *)text
|
|
157
|
+
inWidth:(CGFloat)width {
|
|
158
|
+
NSFont *font = attributes[NSFontAttributeName];
|
|
159
|
+
if (!font || text.length == 0 || [text sizeWithAttributes:attributes].width <= width) {
|
|
160
|
+
return attributes;
|
|
161
|
+
}
|
|
162
|
+
NSMutableDictionary *fitted = [attributes mutableCopy];
|
|
163
|
+
for (CGFloat size = font.pointSize - 0.5; size >= font.pointSize - 2; size -= 0.5) {
|
|
164
|
+
fitted[NSFontAttributeName] = [NSFont fontWithDescriptor:font.fontDescriptor size:size];
|
|
165
|
+
if ([text sizeWithAttributes:fitted].width <= width) break;
|
|
166
|
+
}
|
|
167
|
+
return fitted;
|
|
168
|
+
}
|
|
169
|
+
// 风险提示只保留文字,和 Max / Now 使用同一条左边界。
|
|
170
|
+
- (void)drawTip:(NSString *)text inRect:(NSRect)rect color:(NSColor *)color {
|
|
171
|
+
if (text.length == 0) return;
|
|
172
|
+
[self drawText:text inRect:rect size:11 weight:NSFontWeightMedium color:color dotColor:nil];
|
|
173
|
+
}
|
|
174
|
+
- (void)fillLiquidScrim:(NSBezierPath *)path {
|
|
175
|
+
[NSGraphicsContext saveGraphicsState];
|
|
176
|
+
[[NSShadow new] set];
|
|
177
|
+
[[NSColor colorWithWhite:0 alpha:self.cardScrimAlpha] setFill];
|
|
178
|
+
[path fill];
|
|
179
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
180
|
+
}
|
|
181
|
+
- (NSColor *)textColor:(NSColor *)color {
|
|
182
|
+
if (!self.usesLiquidGlass) return color;
|
|
183
|
+
NSColor *rgb = [color colorUsingColorSpace:NSColorSpace.sRGBColorSpace];
|
|
184
|
+
if (!rgb) return color;
|
|
185
|
+
CGFloat r, g, b, a;
|
|
186
|
+
[rgb getRed:&r green:&g blue:&b alpha:&a];
|
|
187
|
+
CGFloat saturation = MAX(r, MAX(g, b)) - MIN(r, MIN(g, b));
|
|
188
|
+
if (saturation < 0.05) return [NSColor colorWithWhite:MAX(MAX(r, MAX(g, b)), 0.84) alpha:a];
|
|
189
|
+
// 彩色字在任意亮度的背景上对比都不稳定,原生玻璃下文字一律用白色;颜色的语义改由
|
|
190
|
+
// 字前的小圆点、图标和曲线承担(见 drawText:inRect:size:weight:color:dotColor:)。
|
|
191
|
+
return [NSColor colorWithWhite:0.96 alpha:a];
|
|
192
|
+
}
|
|
193
|
+
- (NSDictionary *)textAttributesWithSize:(CGFloat)size color:(NSColor *)color weight:(NSFontWeight)weight {
|
|
194
|
+
return @{NSFontAttributeName: [self fontWithSize:size weight:weight],
|
|
195
|
+
NSForegroundColorAttributeName: [self textColor:color]};
|
|
196
|
+
}
|
|
197
|
+
- (NSDictionary *)rightAlignedTextAttributesWithSize:(CGFloat)size color:(NSColor *)color
|
|
198
|
+
weight:(NSFontWeight)weight {
|
|
199
|
+
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
200
|
+
style.alignment = NSTextAlignmentRight;
|
|
201
|
+
return @{NSFontAttributeName: [self fontWithSize:size weight:weight],
|
|
202
|
+
NSForegroundColorAttributeName: [self textColor:color],
|
|
203
|
+
NSParagraphStyleAttributeName: style};
|
|
204
|
+
}
|
|
205
|
+
- (NSDictionary *)centeredTextAttributesWithSize:(CGFloat)size color:(NSColor *)color weight:(NSFontWeight)weight {
|
|
206
|
+
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
207
|
+
style.alignment = NSTextAlignmentCenter;
|
|
208
|
+
return @{NSFontAttributeName: [self fontWithSize:size weight:weight],
|
|
209
|
+
NSForegroundColorAttributeName: [self textColor:color],
|
|
210
|
+
NSParagraphStyleAttributeName: style};
|
|
211
|
+
}
|
|
212
|
+
- (void)fillRoundedRect:(NSRect)rect radius:(CGFloat)radius color:(NSColor *)color {
|
|
213
|
+
[color setFill];
|
|
214
|
+
[[NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius] fill];
|
|
215
|
+
}
|
|
216
|
+
- (NSArray<NSDictionary *> *)curvePointsFromHistory:(NSArray<NSDictionary *> *)history
|
|
217
|
+
currentUsed:(NSNumber *)currentUsed {
|
|
218
|
+
// 历史只能补充当前快照,不能替代当前快照。当前窗口还没有采集到额度时继续画旧历史,
|
|
219
|
+
// 会让“等待数据”的卡片同时出现一条悬在中间的曲线;此时只保留图表底部基线。
|
|
220
|
+
if (!currentUsed) return @[];
|
|
221
|
+
|
|
222
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
223
|
+
NSTimeInterval cutoff = now - 7 * 24 * 60 * 60;
|
|
224
|
+
NSMutableArray<NSDictionary *> *points = [NSMutableArray array];
|
|
225
|
+
for (NSDictionary *sample in history) {
|
|
226
|
+
if (![sample isKindOfClass:NSDictionary.class]) continue;
|
|
227
|
+
NSNumber *timestamp = [sample[@"timestamp"] isKindOfClass:NSNumber.class]
|
|
228
|
+
? sample[@"timestamp"] : nil;
|
|
229
|
+
NSNumber *remaining = [sample[@"remaining"] isKindOfClass:NSNumber.class]
|
|
230
|
+
? sample[@"remaining"] : nil;
|
|
231
|
+
if (!timestamp || !remaining || timestamp.doubleValue < cutoff) continue;
|
|
232
|
+
double used = 100.0 - fmax(0, fmin(100, remaining.doubleValue));
|
|
233
|
+
[points addObject:@{@"timestamp": timestamp, @"used": @(used)}];
|
|
234
|
+
}
|
|
235
|
+
// 终点必须和卡片上的当前官方额度同源;历史落盘最多会慢 15 分钟,不能直接拿它
|
|
236
|
+
// 冒充“当前”,否则同一张卡里会出现两个不同百分比。
|
|
237
|
+
[points addObject:@{@"timestamp": @(now), @"used": currentUsed}];
|
|
238
|
+
return points;
|
|
239
|
+
}
|
|
240
|
+
- (void)appendCurveFromPoint:(NSPoint)from toPoint:(NSPoint)to path:(NSBezierPath *)path {
|
|
241
|
+
CGFloat controlOffset = (to.x - from.x) / 3.0;
|
|
242
|
+
[path curveToPoint:to
|
|
243
|
+
controlPoint1:NSMakePoint(from.x + controlOffset, from.y)
|
|
244
|
+
controlPoint2:NSMakePoint(to.x - controlOffset, to.y)];
|
|
245
|
+
}
|
|
246
|
+
- (void)drawCurveSegment:(NSArray<NSValue *> *)segment inRect:(NSRect)rect color:(NSColor *)color {
|
|
247
|
+
if (segment.count == 0) return;
|
|
248
|
+
NSBezierPath *line = [NSBezierPath bezierPath];
|
|
249
|
+
NSPoint first = segment.firstObject.pointValue;
|
|
250
|
+
[line moveToPoint:first];
|
|
251
|
+
NSPoint previous = first;
|
|
252
|
+
for (NSUInteger index = 1; index < segment.count; index++) {
|
|
253
|
+
NSPoint point = segment[index].pointValue;
|
|
254
|
+
[self appendCurveFromPoint:previous toPoint:point path:line];
|
|
255
|
+
previous = point;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
NSBezierPath *fill = [line copy];
|
|
259
|
+
[fill lineToPoint:NSMakePoint(previous.x, NSMinY(rect))];
|
|
260
|
+
[fill lineToPoint:NSMakePoint(first.x, NSMinY(rect))];
|
|
261
|
+
[fill closePath];
|
|
262
|
+
[NSGraphicsContext saveGraphicsState];
|
|
263
|
+
[fill addClip];
|
|
264
|
+
NSGradient *area = [[NSGradient alloc]
|
|
265
|
+
initWithStartingColor:[color colorWithAlphaComponent:0.34]
|
|
266
|
+
endingColor:[color colorWithAlphaComponent:0.025]];
|
|
267
|
+
[area drawFromPoint:NSMakePoint(NSMidX(rect), NSMaxY(rect))
|
|
268
|
+
toPoint:NSMakePoint(NSMidX(rect), NSMinY(rect)) options:0];
|
|
269
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
270
|
+
|
|
271
|
+
[[color colorWithAlphaComponent:0.95] setStroke];
|
|
272
|
+
line.lineWidth = 2;
|
|
273
|
+
[line stroke];
|
|
274
|
+
}
|
|
275
|
+
- (void)drawUsageCurve:(NSArray<NSDictionary *> *)points inRect:(NSRect)rect
|
|
276
|
+
color:(NSColor *)color {
|
|
277
|
+
[[NSColor colorWithWhite:1 alpha:0.08] setStroke];
|
|
278
|
+
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(rect), NSMinY(rect))
|
|
279
|
+
toPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))];
|
|
280
|
+
if (points.count == 0) return;
|
|
281
|
+
|
|
282
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
283
|
+
NSTimeInterval cutoff = now - 7 * 24 * 60 * 60;
|
|
284
|
+
// 曲线始终连成一段:采样缺口只代表桌宠当时没在运行,不代表没有消耗,
|
|
285
|
+
// 缺口两端直接插值比断开更接近真实轨迹;窗口重置带来的回落本身也是真实变化。
|
|
286
|
+
NSMutableArray<NSValue *> *allPoints = [NSMutableArray arrayWithCapacity:points.count];
|
|
287
|
+
for (NSDictionary *sample in points) {
|
|
288
|
+
NSTimeInterval timestamp = [sample[@"timestamp"] doubleValue];
|
|
289
|
+
double used = fmax(0, fmin(100, [sample[@"used"] doubleValue]));
|
|
290
|
+
CGFloat x = NSMinX(rect) + NSWidth(rect) * (timestamp - cutoff) / (now - cutoff);
|
|
291
|
+
CGFloat y = NSMinY(rect) + NSHeight(rect) * used / 100.0;
|
|
292
|
+
[allPoints addObject:[NSValue valueWithPoint:NSMakePoint(
|
|
293
|
+
fmax(NSMinX(rect), fmin(NSMaxX(rect), x)), y)]];
|
|
294
|
+
}
|
|
295
|
+
[self drawCurveSegment:allPoints inRect:rect color:color];
|
|
296
|
+
}
|
|
297
|
+
- (void)drawMonthlyTokenCurve:(NSArray<NSDictionary *> *)series inRect:(NSRect)rect
|
|
298
|
+
color:(NSColor *)color {
|
|
299
|
+
[[NSColor colorWithWhite:1 alpha:0.08] setStroke];
|
|
300
|
+
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(rect), NSMinY(rect))
|
|
301
|
+
toPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))];
|
|
302
|
+
if (series.count == 0) return;
|
|
303
|
+
// 横轴铺满整个自然月,曲线只画到今天:把月末还没到的日子当成 0 画出去,看起来会像
|
|
304
|
+
// 用量突然归零。留白本身就是"这个月还剩多少天"的刻度。
|
|
305
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
306
|
+
double peak = 0;
|
|
307
|
+
NSUInteger elapsed = 0;
|
|
308
|
+
for (NSDictionary *day in series) {
|
|
309
|
+
if ([day[@"timestamp"] doubleValue] > now) break;
|
|
310
|
+
NSDictionary *totals = [day[@"totals"] isKindOfClass:NSDictionary.class]
|
|
311
|
+
? day[@"totals"] : nil;
|
|
312
|
+
peak = fmax(peak, [totals[@"total_tokens"] doubleValue]);
|
|
313
|
+
elapsed += 1;
|
|
314
|
+
}
|
|
315
|
+
if (peak <= 0 || elapsed == 0) return;
|
|
316
|
+
NSMutableArray<NSValue *> *points = [NSMutableArray arrayWithCapacity:elapsed];
|
|
317
|
+
for (NSUInteger index = 0; index < elapsed; index++) {
|
|
318
|
+
NSDictionary *totals = [series[index][@"totals"] isKindOfClass:NSDictionary.class]
|
|
319
|
+
? series[index][@"totals"] : nil;
|
|
320
|
+
double value = [totals[@"total_tokens"] doubleValue];
|
|
321
|
+
CGFloat progress = series.count > 1 ? (CGFloat)index / (series.count - 1) : 1;
|
|
322
|
+
[points addObject:[NSValue valueWithPoint:NSMakePoint(
|
|
323
|
+
NSMinX(rect) + NSWidth(rect) * progress,
|
|
324
|
+
NSMinY(rect) + NSHeight(rect) * value / peak)]];
|
|
325
|
+
}
|
|
326
|
+
[self drawCurveSegment:points inRect:rect color:color];
|
|
327
|
+
}
|
|
328
|
+
@end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// QuotaDashboardView 各个 category 共用的布局常量、私有属性和跨文件调用的方法。
|
|
2
|
+
#import "QuotaDashboardView.h"
|
|
3
|
+
#import "CCPetsL10n.h"
|
|
4
|
+
#import "CCPetsUsageMode.h"
|
|
5
|
+
|
|
6
|
+
// 面板高度按"检测到几家 CLI"变化,所以纵向布局不再是一堆绝对坐标,而是自上而下的分段:
|
|
7
|
+
// 标题区 → 汇总条 → N × 额度卡,块之间统一留 QuotaBlockGap。
|
|
8
|
+
// QuotaLogicalHeight 保留为两家都装时的高度(590),既是历史默认值,也是预览图尺寸。
|
|
9
|
+
static const CGFloat QuotaHeaderHeight = 106;
|
|
10
|
+
static const CGFloat QuotaSummaryHeight = 112;
|
|
11
|
+
static const CGFloat QuotaBaseCardHeight = 152;
|
|
12
|
+
static const CGFloat QuotaBlockGap = 12;
|
|
13
|
+
// 原脚注区占用 44px;保留 12px 外边距,其余空间平均分给可见卡片。
|
|
14
|
+
static const CGFloat QuotaReclaimedFooterHeight = 32;
|
|
15
|
+
static const CGFloat QuotaBottomPadding = 12;
|
|
16
|
+
static const CGFloat QuotaCarouselDotGap = 18;
|
|
17
|
+
static const CGFloat QuotaCarouselDotDiameter = 8;
|
|
18
|
+
// 两家都没装时的精简态:只留标题区、三行说明和脚注,汇总条和额度卡整块去掉——
|
|
19
|
+
// 没有 provider 的"今日用量/近 7 天"是两行 0,比不显示更让人以为是坏了。
|
|
20
|
+
static const CGFloat QuotaEmptyNoticeHeight = 76;
|
|
21
|
+
|
|
22
|
+
static inline CGFloat QuotaCardHeightForProviderCount(NSUInteger count) {
|
|
23
|
+
return count > 0 ? QuotaBaseCardHeight + QuotaReclaimedFooterHeight / count
|
|
24
|
+
: QuotaBaseCardHeight;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@interface QuotaDashboardView ()
|
|
28
|
+
@property NSMutableDictionary<NSString *, NSString *> *selectedProfileIDs;
|
|
29
|
+
@property(copy) NSString *activeCarouselProvider;
|
|
30
|
+
@property NSTimeInterval lastCarouselSwitchAt;
|
|
31
|
+
@property(copy) NSString *animatingCarouselProvider;
|
|
32
|
+
@property NSTimeInterval carouselAnimationStartedAt;
|
|
33
|
+
@property NSInteger carouselAnimationDirection;
|
|
34
|
+
@property(copy) NSString *dragCarouselProvider;
|
|
35
|
+
@property NSPoint dragCarouselStartPoint;
|
|
36
|
+
@property BOOL dragCarouselDidSwitch;
|
|
37
|
+
@property(copy) NSString *scrollCarouselProvider;
|
|
38
|
+
@property CGFloat scrollCarouselAccumulator;
|
|
39
|
+
@property BOOL scrollCarouselDidSwitch;
|
|
40
|
+
|
|
41
|
+
// 主文件里实现、绘图 category 也要用的方法。
|
|
42
|
+
- (NSArray<NSDictionary *> *)profilesForProvider:(NSString *)name;
|
|
43
|
+
- (NSDictionary *)summaryUsageForProvider:(NSString *)name;
|
|
44
|
+
- (NSString *)displayNameForProvider:(NSString *)name;
|
|
45
|
+
- (NSUInteger)selectedIndexForProvider:(NSString *)name;
|
|
46
|
+
- (NSRect)logicalSummaryRect;
|
|
47
|
+
- (NSRect)logicalSummaryCellRectAtIndex:(NSUInteger)index;
|
|
48
|
+
- (NSDictionary *)quota:(NSDictionary *)usage key:(NSString *)key;
|
|
49
|
+
- (NSDictionary *)tokenTotals:(NSDictionary *)usage key:(NSString *)key;
|
|
50
|
+
- (NSString *)formattedTokenValue:(double)value;
|
|
51
|
+
- (NSString *)formattedTokenCount:(NSDictionary *)totals;
|
|
52
|
+
- (NSString *)formattedRequestCount:(NSDictionary *)totals;
|
|
53
|
+
- (NSDictionary *)comparisonForCurrent:(double)current previous:(double)previous
|
|
54
|
+
color:(NSColor *)color;
|
|
55
|
+
- (NSString *)formattedTokenSplit:(NSDictionary *)totals wantsOutput:(BOOL)wantsOutput;
|
|
56
|
+
- (NSString *)resetText:(NSDictionary *)quota showDate:(BOOL)showDate;
|
|
57
|
+
- (NSString *)recentWeekUsageLabel;
|
|
58
|
+
- (NSDictionary *)accountStateForProvider:(NSString *)name profile:(NSDictionary *)profile usage:(NSDictionary *)usage;
|
|
59
|
+
@end
|
|
60
|
+
|
|
61
|
+
// 绘图基础:配色、文字排版、系统指标、圆角块和用量曲线。
|
|
62
|
+
@interface QuotaDashboardView (Primitives)
|
|
63
|
+
- (NSColor *)primaryColor;
|
|
64
|
+
- (NSColor *)secondaryColor;
|
|
65
|
+
- (NSColor *)claudeColor;
|
|
66
|
+
- (NSColor *)accentColor;
|
|
67
|
+
- (NSColor *)agentStatusColor;
|
|
68
|
+
- (void)drawSystemMetricsEndingAtX:(CGFloat)rightEdge y:(CGFloat)y;
|
|
69
|
+
- (NSString *)formattedTokenSplitForUsage:(NSDictionary *)usage key:(NSString *)key
|
|
70
|
+
wantsOutput:(BOOL)wantsOutput;
|
|
71
|
+
- (NSNumber *)usedForQuota:(NSDictionary *)quota usedKey:(NSString *)usedKey;
|
|
72
|
+
- (NSString *)refreshAgeText;
|
|
73
|
+
- (NSFont *)fontWithSize:(CGFloat)size weight:(NSFontWeight)weight;
|
|
74
|
+
- (void)drawText:(NSString *)text inRect:(NSRect)rect size:(CGFloat)size
|
|
75
|
+
weight:(NSFontWeight)weight color:(NSColor *)color dotColor:(NSColor *)dotColor;
|
|
76
|
+
- (NSDictionary *)attributes:(NSDictionary *)attributes fittingText:(NSString *)text
|
|
77
|
+
inWidth:(CGFloat)width;
|
|
78
|
+
- (void)drawTip:(NSString *)text inRect:(NSRect)rect color:(NSColor *)color;
|
|
79
|
+
- (void)fillLiquidScrim:(NSBezierPath *)path;
|
|
80
|
+
- (NSColor *)textColor:(NSColor *)color;
|
|
81
|
+
- (NSDictionary *)textAttributesWithSize:(CGFloat)size color:(NSColor *)color weight:(NSFontWeight)weight;
|
|
82
|
+
- (NSDictionary *)rightAlignedTextAttributesWithSize:(CGFloat)size color:(NSColor *)color
|
|
83
|
+
weight:(NSFontWeight)weight;
|
|
84
|
+
- (NSDictionary *)centeredTextAttributesWithSize:(CGFloat)size color:(NSColor *)color weight:(NSFontWeight)weight;
|
|
85
|
+
- (void)fillRoundedRect:(NSRect)rect radius:(CGFloat)radius color:(NSColor *)color;
|
|
86
|
+
- (NSArray<NSDictionary *> *)curvePointsFromHistory:(NSArray<NSDictionary *> *)history
|
|
87
|
+
currentUsed:(NSNumber *)currentUsed;
|
|
88
|
+
- (void)appendCurveFromPoint:(NSPoint)from toPoint:(NSPoint)to path:(NSBezierPath *)path;
|
|
89
|
+
- (void)drawCurveSegment:(NSArray<NSValue *> *)segment inRect:(NSRect)rect color:(NSColor *)color;
|
|
90
|
+
- (void)drawUsageCurve:(NSArray<NSDictionary *> *)points inRect:(NSRect)rect
|
|
91
|
+
color:(NSColor *)color;
|
|
92
|
+
- (void)drawMonthlyTokenCurve:(NSArray<NSDictionary *> *)series inRect:(NSRect)rect
|
|
93
|
+
color:(NSColor *)color;
|
|
94
|
+
@end
|
|
95
|
+
|
|
96
|
+
// 卡片、汇总条和整块面板的绘制。
|
|
97
|
+
@interface QuotaDashboardView (Rendering)
|
|
98
|
+
- (void)drawProvider:(NSString *)name displayName:(NSString *)displayName
|
|
99
|
+
usage:(NSDictionary *)usage usedKey:(NSString *)usedKey color:(NSColor *)color
|
|
100
|
+
cardRect:(NSRect)card profileCount:(NSUInteger)profileCount selectedIndex:(NSUInteger)selectedIndex;
|
|
101
|
+
- (void)drawSummaryForProviders:(NSArray<NSString *> *)providers primary:(NSColor *)primary
|
|
102
|
+
secondary:(NSColor *)secondary accent:(NSColor *)cyan;
|
|
103
|
+
@end
|