cc-pets 2.3.0 → 2.5.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 +63 -7
- package/CC_BRIDGE.zh-CN.md +43 -5
- package/CHANGELOG.md +106 -0
- package/CHANGELOG.zh-CN.md +106 -0
- package/CONTRIBUTING.md +23 -1
- package/CONTRIBUTING.zh-CN.md +18 -1
- package/PROVIDER_PROTOCOL.md +5 -1
- package/PROVIDER_PROTOCOL.zh-CN.md +4 -1
- package/README.md +177 -33
- package/README.zh-CN.md +137 -32
- package/Resources/AgentIcons/claude.png +0 -0
- package/Resources/AgentIcons/codex.icns +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.vsix +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.zip +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +187 -35
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentEvents.m +47 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +121 -20
- package/Sources/CCPets/App/CCPetsAppDelegate+Bridge.m +8 -6
- package/Sources/CCPets/App/CCPetsAppDelegate+MenuBar.m +86 -0
- package/Sources/CCPets/App/CCPetsAppDelegate+Private.h +14 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Quota.m +6 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +3 -6
- package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +118 -50
- package/Sources/CCPets/App/CCPetsAppDelegate+Update.m +4 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +4 -0
- package/Sources/CCPets/App/CCPetsAppDelegate.h +12 -2
- package/Sources/CCPets/App/CCPetsAppDelegate.m +4 -2
- package/Sources/CCPets/App/main.m +6 -0
- package/Sources/CCPets/Bridge/CCPetsBridge.m +45 -18
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +175 -68
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +14 -6
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +356 -110
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +0 -2
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +247 -126
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +37 -9
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +385 -80
- package/Sources/CCPets/Integrations/CCPetsEvents.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.m +68 -12
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.h +21 -0
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.m +503 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +1 -1
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.m +367 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.h +8 -1
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +156 -25
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.h +17 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.m +64 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.h +23 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.m +475 -0
- package/Sources/CCPets/Pet/CCPetsPhrasesEditor.m +7 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +11 -3
- package/Sources/CCPets/Pet/PetView.h +7 -0
- package/Sources/CCPets/Pet/PetView.m +12 -1
- package/Sources/CCPets/Support/CCPetsDoctor.m +26 -12
- package/Sources/CCPets/Support/CCPetsPreferences.h +12 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +52 -2
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +2 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +18 -7
- package/Sources/CCPets/UI/CCPetsGlassView.h +26 -4
- package/Sources/CCPets/UI/CCPetsGlassView.m +40 -9
- package/Sources/CCPets/UI/CCPetsMenuBarIcon.h +16 -0
- package/Sources/CCPets/UI/CCPetsMenuBarIcon.m +70 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.h +18 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.m +528 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.h +51 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.m +95 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +6 -1
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +577 -66
- package/Sources/CCPets/UI/CCPetsStatusItem.h +28 -0
- package/Sources/CCPets/UI/CCPetsStatusItem.m +147 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +5 -2
- package/Sources/CCPets/UI/CCPetsUIStyle.m +57 -8
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +4 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +23 -28
- package/Sources/CCPets/UI/CCPetsUsageStore.m +2 -2
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +5 -3
- package/Sources/CCPets/Usage/CCPetsUsage+CodexBackground.m +193 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +1 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +49 -11
- package/Sources/CCPets/Usage/CCPetsUsage.h +3 -2
- package/Sources/CCPets/Usage/CCPetsUsageMonitor.m +15 -17
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +11 -29
- package/Sources/CCPets/Usage/QuotaDashboardView.m +128 -2
- package/THIRD_PARTY_NOTICES.md +7 -3
- package/THIRD_PARTY_NOTICES.zh-CN.md +5 -1
- package/bin/claude-with-pet +2 -0
- package/bin/codex-with-pet +2 -0
- package/package.json +5 -1
- package/plugins/jetbrains/README.md +62 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalBridge.java +379 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalIdentity.java +24 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalStartup.java +12 -0
- package/plugins/jetbrains/src/main/resources/META-INF/plugin.xml +23 -0
- package/plugins/jetbrains/src/main/resources/META-INF/pluginIcon.svg +380 -0
- package/plugins/vscode/README.md +63 -0
- package/plugins/vscode/extension.js +210 -0
- package/plugins/vscode/package.json +18 -0
- package/scripts/build-idea-plugin.sh +101 -0
- package/scripts/build-vscode-extension.sh +112 -0
- package/scripts/build.sh +24 -2
- package/scripts/compile-cached.zsh +133 -0
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
#import "CCPetsMenuBarPanel.h"
|
|
2
|
+
#import "CCPetsStatusItem.h"
|
|
3
|
+
#import "CCPetsMenuBarSummary.h"
|
|
4
|
+
#import "CCPetsGlassView.h"
|
|
5
|
+
#import "CCPetsL10n.h"
|
|
6
|
+
#import "CCPetsUsagePage.h"
|
|
7
|
+
|
|
8
|
+
static const CGFloat PanelWidth = 300;
|
|
9
|
+
static const CGFloat PanelPadding = 6;
|
|
10
|
+
static const CGFloat RowInset = 14;
|
|
11
|
+
static const NSUInteger PanelSessionLimit = 5;
|
|
12
|
+
|
|
13
|
+
typedef void (^MenuBarRowDrawer)(NSRect bounds, BOOL hovered);
|
|
14
|
+
|
|
15
|
+
#pragma mark - 绘制小工具
|
|
16
|
+
|
|
17
|
+
static NSColor *PrimaryInk(void) { return [NSColor colorWithWhite:0.97 alpha:1]; }
|
|
18
|
+
static NSColor *SecondaryInk(void) { return [NSColor colorWithWhite:0.80 alpha:1]; }
|
|
19
|
+
static NSColor *TertiaryInk(void) { return [NSColor colorWithWhite:0.80 alpha:0.62]; }
|
|
20
|
+
|
|
21
|
+
// 与悬停面板同一组品牌色:Codex 青、Claude 紫(原生玻璃上提亮一档)。
|
|
22
|
+
static NSColor *ProviderColor(NSString *provider, BOOL liquid) {
|
|
23
|
+
if ([provider isEqualToString:@"Claude"]) {
|
|
24
|
+
return liquid ? [NSColor colorWithRed:0.80 green:0.66 blue:1.00 alpha:1]
|
|
25
|
+
: [NSColor colorWithRed:0.66 green:0.43 blue:0.94 alpha:1];
|
|
26
|
+
}
|
|
27
|
+
return [NSColor colorWithRed:0.30 green:0.82 blue:0.90 alpha:1];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static NSColor *LevelColor(CCPetsMenuBarLevel level, NSColor *normal) {
|
|
31
|
+
if (level == CCPetsMenuBarLevelCritical) return NSColor.systemRedColor;
|
|
32
|
+
if (level == CCPetsMenuBarLevelWarning) return NSColor.systemOrangeColor;
|
|
33
|
+
return normal;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 单行文字,按 rect 垂直居中,放不下截尾。
|
|
37
|
+
static void DrawText(NSString *text, NSRect rect, CGFloat size, NSFontWeight weight,
|
|
38
|
+
NSColor *color, NSTextAlignment alignment) {
|
|
39
|
+
if (text.length == 0) return;
|
|
40
|
+
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
41
|
+
style.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
42
|
+
style.alignment = alignment;
|
|
43
|
+
NSDictionary *attributes = @{
|
|
44
|
+
NSFontAttributeName: [NSFont monospacedDigitSystemFontOfSize:size weight:weight],
|
|
45
|
+
NSForegroundColorAttributeName: color, NSParagraphStyleAttributeName: style,
|
|
46
|
+
NSShadowAttributeName: CCPetsGlassTextShadow()};
|
|
47
|
+
CGFloat height = ceil([text sizeWithAttributes:attributes].height);
|
|
48
|
+
[text drawInRect:NSMakeRect(NSMinX(rect), NSMidY(rect) - height / 2.0, NSWidth(rect), height)
|
|
49
|
+
withAttributes:attributes];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static CGFloat TextWidth(NSString *text, CGFloat size, NSFontWeight weight) {
|
|
53
|
+
if (text.length == 0) return 0;
|
|
54
|
+
return ceil([text sizeWithAttributes:@{
|
|
55
|
+
NSFontAttributeName: [NSFont monospacedDigitSystemFontOfSize:size weight:weight]}].width);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 剩余额度环:底轨 + 从 12 点钟顺时针的剩余弧。fraction < 0 表示没有数据,只画底轨。
|
|
59
|
+
static void DrawRing(NSRect box, double fraction, NSColor *color) {
|
|
60
|
+
NSRect ring = NSInsetRect(box, 2, 2);
|
|
61
|
+
NSBezierPath *track = [NSBezierPath bezierPathWithOvalInRect:ring];
|
|
62
|
+
track.lineWidth = 3;
|
|
63
|
+
[[NSColor colorWithWhite:1 alpha:0.16] setStroke];
|
|
64
|
+
[track stroke];
|
|
65
|
+
if (fraction <= 0) return;
|
|
66
|
+
NSBezierPath *arc = [NSBezierPath bezierPath];
|
|
67
|
+
// 行视图是翻转坐标:y 向下,顺时针在这里要写成 clockwise:NO。
|
|
68
|
+
[arc appendBezierPathWithArcWithCenter:NSMakePoint(NSMidX(ring), NSMidY(ring))
|
|
69
|
+
radius:NSWidth(ring) / 2.0 startAngle:-90 endAngle:-90 + 360 * MIN(1, fraction) clockwise:NO];
|
|
70
|
+
arc.lineWidth = 3;
|
|
71
|
+
arc.lineCapStyle = NSLineCapStyleRound;
|
|
72
|
+
[color setStroke];
|
|
73
|
+
[arc stroke];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static void DrawDot(NSPoint center, CGFloat diameter, NSColor *color) {
|
|
77
|
+
[color setFill];
|
|
78
|
+
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(center.x - diameter / 2.0,
|
|
79
|
+
center.y - diameter / 2.0, diameter, diameter)] fill];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
static NSString *PercentText(id value) {
|
|
83
|
+
if (![value isKindOfClass:NSNumber.class] || !isfinite([value doubleValue])) return @"--";
|
|
84
|
+
return [NSString stringWithFormat:@"%ld%%", (long)floor(fmax(0, fmin(100, [value doubleValue])))];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static NSString *ResetText(id value) {
|
|
88
|
+
if (![value isKindOfClass:NSNumber.class]) return nil;
|
|
89
|
+
NSDateFormatter *formatter = [NSDateFormatter new];
|
|
90
|
+
formatter.dateFormat = @"M/d HH:mm";
|
|
91
|
+
return [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[value doubleValue]]];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#pragma mark - 视图
|
|
95
|
+
|
|
96
|
+
// 一行:内容交给 drawer 画,有 action 才可点、才有悬停高亮。
|
|
97
|
+
@interface CCPetsMenuBarRow : NSView
|
|
98
|
+
@property(nonatomic, copy) MenuBarRowDrawer drawer;
|
|
99
|
+
@property(nonatomic, copy) void (^action)(void);
|
|
100
|
+
@end
|
|
101
|
+
|
|
102
|
+
@implementation CCPetsMenuBarRow {
|
|
103
|
+
NSTrackingArea *_trackingArea;
|
|
104
|
+
BOOL _hovered;
|
|
105
|
+
}
|
|
106
|
+
- (BOOL)isFlipped { return YES; }
|
|
107
|
+
- (BOOL)acceptsFirstMouse:(NSEvent *)event { return YES; }
|
|
108
|
+
- (void)updateTrackingAreas {
|
|
109
|
+
[super updateTrackingAreas];
|
|
110
|
+
if (_trackingArea) [self removeTrackingArea:_trackingArea];
|
|
111
|
+
_trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
|
|
112
|
+
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect
|
|
113
|
+
owner:self userInfo:nil];
|
|
114
|
+
[self addTrackingArea:_trackingArea];
|
|
115
|
+
}
|
|
116
|
+
- (void)mouseEntered:(NSEvent *)event { _hovered = YES; self.needsDisplay = YES; }
|
|
117
|
+
- (void)mouseExited:(NSEvent *)event { _hovered = NO; self.needsDisplay = YES; }
|
|
118
|
+
- (void)mouseDown:(NSEvent *)event {}
|
|
119
|
+
- (void)mouseUp:(NSEvent *)event {
|
|
120
|
+
if (!self.action) return;
|
|
121
|
+
NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
|
|
122
|
+
if (NSPointInRect(point, self.bounds)) self.action();
|
|
123
|
+
}
|
|
124
|
+
- (void)drawRect:(NSRect)dirtyRect {
|
|
125
|
+
BOOL hovered = _hovered && self.action != nil;
|
|
126
|
+
if (hovered) {
|
|
127
|
+
[[NSColor colorWithWhite:1 alpha:0.14] setFill];
|
|
128
|
+
[[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(self.bounds, 6, 1) xRadius:7 yRadius:7] fill];
|
|
129
|
+
}
|
|
130
|
+
if (self.drawer) self.drawer(self.bounds, hovered);
|
|
131
|
+
}
|
|
132
|
+
@end
|
|
133
|
+
|
|
134
|
+
@interface CCPetsMenuBarDocument : NSView
|
|
135
|
+
@end
|
|
136
|
+
@implementation CCPetsMenuBarDocument
|
|
137
|
+
- (BOOL)isFlipped { return YES; }
|
|
138
|
+
@end
|
|
139
|
+
|
|
140
|
+
@interface CCPetsMenuBarWindow : NSPanel
|
|
141
|
+
@end
|
|
142
|
+
@implementation CCPetsMenuBarWindow
|
|
143
|
+
- (BOOL)canBecomeKeyWindow { return NO; }
|
|
144
|
+
@end
|
|
145
|
+
|
|
146
|
+
#pragma mark - 面板
|
|
147
|
+
|
|
148
|
+
@interface CCPetsMenuBarPanel ()
|
|
149
|
+
@property(nonatomic, weak) CCPetsStatusItemController *owner;
|
|
150
|
+
@property(nonatomic) CCPetsMenuBarWindow *window;
|
|
151
|
+
@property(nonatomic) CCPetsGlassView *glass;
|
|
152
|
+
@property(nonatomic, weak) NSStatusBarButton *button;
|
|
153
|
+
@property(nonatomic) id localMonitor;
|
|
154
|
+
@property(nonatomic) id globalMonitor;
|
|
155
|
+
@end
|
|
156
|
+
|
|
157
|
+
@implementation CCPetsMenuBarPanel
|
|
158
|
+
|
|
159
|
+
- (instancetype)initWithOwner:(CCPetsStatusItemController *)owner {
|
|
160
|
+
if (!(self = [super init])) return nil;
|
|
161
|
+
_owner = owner;
|
|
162
|
+
return self;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
- (BOOL)isShown {
|
|
166
|
+
return self.window.isVisible;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
- (void)dismiss {
|
|
170
|
+
if (self.localMonitor) [NSEvent removeMonitor:self.localMonitor];
|
|
171
|
+
if (self.globalMonitor) [NSEvent removeMonitor:self.globalMonitor];
|
|
172
|
+
self.localMonitor = nil;
|
|
173
|
+
self.globalMonitor = nil;
|
|
174
|
+
[self.window orderOut:nil];
|
|
175
|
+
self.window = nil;
|
|
176
|
+
self.glass = nil;
|
|
177
|
+
self.button.highlighted = NO;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
- (void)showBelowButton:(NSStatusBarButton *)button {
|
|
181
|
+
[self dismiss];
|
|
182
|
+
if (!button.window) return;
|
|
183
|
+
self.button = button;
|
|
184
|
+
CCPetsMenuBarWindow *window = [[CCPetsMenuBarWindow alloc]
|
|
185
|
+
initWithContentRect:NSMakeRect(0, 0, PanelWidth, 100)
|
|
186
|
+
styleMask:NSWindowStyleMaskBorderless | NSWindowStyleMaskNonactivatingPanel
|
|
187
|
+
backing:NSBackingStoreBuffered defer:NO];
|
|
188
|
+
window.opaque = NO;
|
|
189
|
+
window.backgroundColor = NSColor.clearColor;
|
|
190
|
+
window.hasShadow = NO;
|
|
191
|
+
window.level = NSPopUpMenuWindowLevel;
|
|
192
|
+
window.collectionBehavior = NSWindowCollectionBehaviorCanJoinAllSpaces |
|
|
193
|
+
NSWindowCollectionBehaviorFullScreenAuxiliary | NSWindowCollectionBehaviorTransient;
|
|
194
|
+
window.hidesOnDeactivate = NO;
|
|
195
|
+
window.acceptsMouseMovedEvents = YES;
|
|
196
|
+
CCPetsGlassView *glass = [[CCPetsGlassView alloc] initWithFrame:NSMakeRect(0, 0, PanelWidth, 100)
|
|
197
|
+
material:NSVisualEffectMaterialMenu appearance:NSAppearanceNameDarkAqua cornerRadius:12];
|
|
198
|
+
glass.edgeShadeHeight = 10;
|
|
199
|
+
glass.edgeShadeAlpha = 0.6;
|
|
200
|
+
glass.usesWidgetGlass = YES;
|
|
201
|
+
glass.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
|
202
|
+
window.contentView = glass;
|
|
203
|
+
self.window = window;
|
|
204
|
+
self.glass = glass;
|
|
205
|
+
[self layoutContent];
|
|
206
|
+
[window orderFrontRegardless];
|
|
207
|
+
// 按钮的 action 在松开鼠标时触发,AppKit 处理完这次点击会把高亮复位,所以放到下一轮再按下去。
|
|
208
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
209
|
+
if (self.isShown && self.button == button) button.highlighted = YES;
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// 点面板以外就收起。点在菜单栏图标上的那一下留给图标自己的 action 去切换,
|
|
213
|
+
// 否则这里先收起、紧接着 action 又打开,面板会关不掉。
|
|
214
|
+
__weak typeof(self) weakSelf = self;
|
|
215
|
+
NSEventMask mask = NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown | NSEventMaskOtherMouseDown;
|
|
216
|
+
self.globalMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:mask handler:^(NSEvent *event) {
|
|
217
|
+
[weakSelf dismiss];
|
|
218
|
+
}];
|
|
219
|
+
self.localMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:mask | NSEventMaskKeyDown
|
|
220
|
+
handler:^NSEvent *(NSEvent *event) {
|
|
221
|
+
typeof(self) strongSelf = weakSelf;
|
|
222
|
+
if (!strongSelf) return event;
|
|
223
|
+
if (event.type == NSEventTypeKeyDown) {
|
|
224
|
+
if (event.keyCode == 53) { [strongSelf dismiss]; return nil; }
|
|
225
|
+
return event;
|
|
226
|
+
}
|
|
227
|
+
if (event.window != strongSelf.window && event.window != strongSelf.button.window) [strongSelf dismiss];
|
|
228
|
+
return event;
|
|
229
|
+
}];
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
- (void)reload {
|
|
233
|
+
if (self.isShown) [self layoutContent];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 按内容重排并定位:顶边贴在菜单栏图标下方,水平居中对齐图标,夹在屏幕可见区域内。
|
|
237
|
+
- (void)layoutContent {
|
|
238
|
+
NSStatusBarButton *button = self.button;
|
|
239
|
+
if (!self.window || !button.window) return;
|
|
240
|
+
NSArray<CCPetsMenuBarRow *> *rows = [self buildRows];
|
|
241
|
+
CGFloat contentHeight = 0;
|
|
242
|
+
for (CCPetsMenuBarRow *row in rows) contentHeight += NSHeight(row.frame);
|
|
243
|
+
NSRect buttonRect = [button.window convertRectToScreen:[button convertRect:button.bounds toView:nil]];
|
|
244
|
+
NSRect visible = NSInsetRect((button.window.screen ?: NSScreen.mainScreen).visibleFrame, 4, 4);
|
|
245
|
+
CGFloat height = MIN(contentHeight + PanelPadding * 2, NSHeight(visible));
|
|
246
|
+
CGFloat x = MIN(MAX(NSMidX(buttonRect) - PanelWidth / 2.0, NSMinX(visible)), NSMaxX(visible) - PanelWidth);
|
|
247
|
+
CGFloat top = MIN(NSMinY(buttonRect) - 4, NSMaxY(visible));
|
|
248
|
+
[self.window setFrame:NSMakeRect(x, top - height, PanelWidth, height) display:NO];
|
|
249
|
+
|
|
250
|
+
CCPetsMenuBarDocument *document = [[CCPetsMenuBarDocument alloc]
|
|
251
|
+
initWithFrame:NSMakeRect(0, 0, PanelWidth, contentHeight)];
|
|
252
|
+
CGFloat y = 0;
|
|
253
|
+
for (CCPetsMenuBarRow *row in rows) {
|
|
254
|
+
row.frame = NSMakeRect(0, y, PanelWidth, NSHeight(row.frame));
|
|
255
|
+
[document addSubview:row];
|
|
256
|
+
y += NSHeight(row.frame);
|
|
257
|
+
}
|
|
258
|
+
for (NSView *view in self.glass.contentView.subviews.copy) [view removeFromSuperview];
|
|
259
|
+
NSScrollView *scroll = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, PanelPadding,
|
|
260
|
+
PanelWidth, height - PanelPadding * 2)];
|
|
261
|
+
scroll.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
|
262
|
+
scroll.drawsBackground = NO;
|
|
263
|
+
scroll.contentView.drawsBackground = NO;
|
|
264
|
+
scroll.borderType = NSNoBorder;
|
|
265
|
+
scroll.hasVerticalScroller = contentHeight > NSHeight(scroll.frame);
|
|
266
|
+
scroll.autohidesScrollers = YES;
|
|
267
|
+
scroll.scrollerStyle = NSScrollerStyleOverlay;
|
|
268
|
+
scroll.documentView = document;
|
|
269
|
+
[self.glass.contentView addSubview:scroll];
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
#pragma mark - 内容
|
|
273
|
+
|
|
274
|
+
- (CCPetsMenuBarRow *)rowWithHeight:(CGFloat)height drawer:(MenuBarRowDrawer)drawer action:(void (^)(void))action {
|
|
275
|
+
CCPetsMenuBarRow *row = [[CCPetsMenuBarRow alloc] initWithFrame:NSMakeRect(0, 0, PanelWidth, height)];
|
|
276
|
+
row.drawer = drawer;
|
|
277
|
+
row.action = action;
|
|
278
|
+
return row;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
- (CCPetsMenuBarRow *)separator {
|
|
282
|
+
return [self rowWithHeight:11 drawer:^(NSRect bounds, BOOL hovered) {
|
|
283
|
+
[[NSColor colorWithWhite:1 alpha:0.09] setFill];
|
|
284
|
+
NSRectFill(NSMakeRect(RowInset, floor(NSMidY(bounds)), NSWidth(bounds) - RowInset * 2, 1));
|
|
285
|
+
} action:nil];
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// 分组标题:左边小号灰字,右边可选一段补充(额度组放今日用量)。
|
|
289
|
+
- (CCPetsMenuBarRow *)sectionTitle:(NSString *)title trailing:(NSString *)trailing {
|
|
290
|
+
return [self rowWithHeight:22 drawer:^(NSRect bounds, BOOL hovered) {
|
|
291
|
+
NSRect inner = NSInsetRect(bounds, RowInset, 0);
|
|
292
|
+
DrawText(title, inner, 11, NSFontWeightSemibold, TertiaryInk(), NSTextAlignmentLeft);
|
|
293
|
+
DrawText(trailing, inner, 11, NSFontWeightMedium, SecondaryInk(), NSTextAlignmentRight);
|
|
294
|
+
} action:nil];
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
- (NSArray<CCPetsMenuBarRow *> *)buildRows {
|
|
298
|
+
CCPetsStatusItemController *owner = self.owner;
|
|
299
|
+
id<CCPetsWorkbenchSource, CCPetsPetCardSource> source = owner.source;
|
|
300
|
+
if (!source) return @[];
|
|
301
|
+
NSMutableArray<CCPetsMenuBarRow *> *rows = [NSMutableArray array];
|
|
302
|
+
[rows addObject:[self headerRow:[source petCardSnapshot]]];
|
|
303
|
+
[rows addObject:[self separator]];
|
|
304
|
+
[rows addObjectsFromArray:[self sessionRows:[source workbenchSessions]]];
|
|
305
|
+
[rows addObject:[self separator]];
|
|
306
|
+
NSArray<NSDictionary *> *quotas = [source workbenchQuotas];
|
|
307
|
+
NSNumber *today = CCPetsMenuBarTodayTokens(quotas);
|
|
308
|
+
[rows addObject:[self sectionTitle:L(@"Quota") trailing:[NSString stringWithFormat:L(@"Today %@"),
|
|
309
|
+
today ? CCPetsFormatTokens(today.unsignedLongLongValue) : @"--"]]];
|
|
310
|
+
for (NSDictionary *quota in CCPetsMenuBarOrderedQuotas(quotas)) [rows addObject:[self quotaRow:quota]];
|
|
311
|
+
[rows addObject:[self separator]];
|
|
312
|
+
[rows addObjectsFromArray:[self footerRows]];
|
|
313
|
+
return rows;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// 宠物名片:第一帧缩略图、名字、此刻在做什么;有人等审批时右边一枚红色胶囊。
|
|
317
|
+
- (CCPetsMenuBarRow *)headerRow:(NSDictionary *)snapshot {
|
|
318
|
+
NSImage *sheet = [snapshot[@"sheet"] isKindOfClass:NSImage.class] ? snapshot[@"sheet"] : nil;
|
|
319
|
+
NSInteger rowCount = MAX(1, [snapshot[@"rowCount"] integerValue]);
|
|
320
|
+
NSString *name = [snapshot[@"name"] isKindOfClass:NSString.class] ? snapshot[@"name"] : @"CC Pets";
|
|
321
|
+
NSString *status = [snapshot[@"status"] isKindOfClass:NSString.class] ? snapshot[@"status"] : @"";
|
|
322
|
+
NSColor *statusColor = [snapshot[@"statusColor"] isKindOfClass:NSColor.class]
|
|
323
|
+
? snapshot[@"statusColor"] : SecondaryInk();
|
|
324
|
+
NSUInteger approvals = [snapshot[@"approvals"] unsignedIntegerValue];
|
|
325
|
+
return [self rowWithHeight:50 drawer:^(NSRect bounds, BOOL hovered) {
|
|
326
|
+
CGFloat x = RowInset;
|
|
327
|
+
if (sheet) {
|
|
328
|
+
// 精灵图 8 列;第一行第一格是默认待机帧。翻转视图里按图片自身坐标取格子。
|
|
329
|
+
NSSize cell = NSMakeSize(sheet.size.width / 8.0, sheet.size.height / rowCount);
|
|
330
|
+
CGFloat height = 40, width = height * cell.width / MAX(1, cell.height);
|
|
331
|
+
[sheet drawInRect:NSMakeRect(x - 4, NSMidY(bounds) - height / 2.0, width, height)
|
|
332
|
+
fromRect:NSMakeRect(0, sheet.size.height - cell.height, cell.width, cell.height)
|
|
333
|
+
operation:NSCompositingOperationSourceOver fraction:1 respectFlipped:YES hints:nil];
|
|
334
|
+
x += width;
|
|
335
|
+
}
|
|
336
|
+
CGFloat badgeWidth = 0;
|
|
337
|
+
if (approvals > 0) {
|
|
338
|
+
NSString *badge = [NSString stringWithFormat:@"⚠︎ %lu", (unsigned long)approvals];
|
|
339
|
+
badgeWidth = TextWidth(badge, 11, NSFontWeightSemibold) + 14;
|
|
340
|
+
NSRect pill = NSMakeRect(NSMaxX(bounds) - RowInset - badgeWidth, NSMidY(bounds) - 10, badgeWidth, 20);
|
|
341
|
+
[[NSColor.systemRedColor colorWithAlphaComponent:0.85] setFill];
|
|
342
|
+
[[NSBezierPath bezierPathWithRoundedRect:pill xRadius:10 yRadius:10] fill];
|
|
343
|
+
DrawText(badge, pill, 11, NSFontWeightSemibold, NSColor.whiteColor, NSTextAlignmentCenter);
|
|
344
|
+
badgeWidth += 8;
|
|
345
|
+
}
|
|
346
|
+
CGFloat textWidth = NSMaxX(bounds) - RowInset - badgeWidth - x;
|
|
347
|
+
DrawText(name, NSMakeRect(x, NSMidY(bounds) - 17, textWidth, 18), 13, NSFontWeightSemibold,
|
|
348
|
+
PrimaryInk(), NSTextAlignmentLeft);
|
|
349
|
+
DrawDot(NSMakePoint(x + 3.5, NSMidY(bounds) + 9), 7, statusColor);
|
|
350
|
+
DrawText(status, NSMakeRect(x + 12, NSMidY(bounds) + 1, textWidth - 12, 16), 11, NSFontWeightRegular,
|
|
351
|
+
SecondaryInk(), NSTextAlignmentLeft);
|
|
352
|
+
} action:nil];
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
- (NSArray<CCPetsMenuBarRow *> *)sessionRows:(NSArray<NSDictionary *> *)sessions {
|
|
356
|
+
NSMutableArray<CCPetsMenuBarRow *> *rows = [NSMutableArray array];
|
|
357
|
+
[rows addObject:[self sectionTitle:L(@"Sessions") trailing:nil]];
|
|
358
|
+
if (sessions.count == 0) {
|
|
359
|
+
[rows addObject:[self rowWithHeight:26 drawer:^(NSRect bounds, BOOL hovered) {
|
|
360
|
+
DrawText(L(@"No sessions online."), NSInsetRect(bounds, RowInset, 0), 12, NSFontWeightRegular,
|
|
361
|
+
SecondaryInk(), NSTextAlignmentLeft);
|
|
362
|
+
} action:nil]];
|
|
363
|
+
return rows;
|
|
364
|
+
}
|
|
365
|
+
__weak typeof(self) weakSelf = self;
|
|
366
|
+
NSColor *(^stateColor)(NSString *) = self.owner.stateColorProvider;
|
|
367
|
+
NSUInteger shown = MIN(sessions.count, PanelSessionLimit);
|
|
368
|
+
for (NSDictionary *session in [sessions subarrayWithRange:NSMakeRange(0, shown)]) {
|
|
369
|
+
NSString *display = [session[@"displayName"] isKindOfClass:NSString.class] ? session[@"displayName"] : @"";
|
|
370
|
+
NSString *name = display.length > 0 ? display : (session[@"provider"] ?: @"Agent");
|
|
371
|
+
NSString *state = [session[@"state"] isKindOfClass:NSString.class] ? session[@"state"] : @"";
|
|
372
|
+
NSString *status = session[@"status"] ?: @"";
|
|
373
|
+
NSString *terminal = [session[@"terminal"] isKindOfClass:NSString.class] ? session[@"terminal"] : @"";
|
|
374
|
+
NSUInteger waiting = [session[@"pendingMessages"] unsignedIntegerValue];
|
|
375
|
+
NSColor *dotColor = (stateColor ? stateColor(state) : nil) ?: SecondaryInk();
|
|
376
|
+
BOOL approval = [state isEqualToString:@"approval"];
|
|
377
|
+
NSString *sessionID = session[@"id"];
|
|
378
|
+
CCPetsMenuBarRow *row = [self rowWithHeight:28 drawer:^(NSRect bounds, BOOL hovered) {
|
|
379
|
+
DrawDot(NSMakePoint(RowInset + 4, NSMidY(bounds)), 8, dotColor);
|
|
380
|
+
NSString *trailing = waiting > 0
|
|
381
|
+
? [NSString stringWithFormat:@"%@ · ✉︎ %lu", status, (unsigned long)waiting] : status;
|
|
382
|
+
CGFloat trailingWidth = MIN(TextWidth(trailing, 11, NSFontWeightMedium), 130);
|
|
383
|
+
NSRect inner = NSMakeRect(RowInset + 16, NSMinY(bounds), NSWidth(bounds) - RowInset * 2 - 16, NSHeight(bounds));
|
|
384
|
+
DrawText(trailing, inner, 11, NSFontWeightMedium,
|
|
385
|
+
approval ? NSColor.systemOrangeColor : SecondaryInk(), NSTextAlignmentRight);
|
|
386
|
+
DrawText(name, NSMakeRect(NSMinX(inner), NSMinY(inner), NSWidth(inner) - trailingWidth - 10, NSHeight(inner)),
|
|
387
|
+
13, NSFontWeightRegular, PrimaryInk(), NSTextAlignmentLeft);
|
|
388
|
+
} action:^{
|
|
389
|
+
[weakSelf dismiss];
|
|
390
|
+
[weakSelf.owner.source focusWorkbenchSessionWithID:sessionID];
|
|
391
|
+
}];
|
|
392
|
+
row.toolTip = terminal.length > 0 ? [NSString stringWithFormat:@"%@ · %@", name, terminal] : name;
|
|
393
|
+
[rows addObject:row];
|
|
394
|
+
}
|
|
395
|
+
if (sessions.count > shown) {
|
|
396
|
+
NSString *more = [NSString stringWithFormat:L(@"%@ more sessions…"), @(sessions.count - shown)];
|
|
397
|
+
[rows addObject:[self rowWithHeight:24 drawer:^(NSRect bounds, BOOL hovered) {
|
|
398
|
+
DrawText(more, NSInsetRect(bounds, RowInset + 16, 0), 11, NSFontWeightMedium, SecondaryInk(),
|
|
399
|
+
NSTextAlignmentLeft);
|
|
400
|
+
} action:^{
|
|
401
|
+
[weakSelf dismiss];
|
|
402
|
+
[CCPetsMainWindowController presentPage:CCPetsMainWindowPageSessions];
|
|
403
|
+
}]];
|
|
404
|
+
}
|
|
405
|
+
return rows;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// 订阅账号:5 小时额度环 + 大号百分比,第二行本周剩余与重置时间。
|
|
409
|
+
// API 账号:右边是本月 Token,第二行本月调用次数。点击都打开主窗口概览。
|
|
410
|
+
- (CCPetsMenuBarRow *)quotaRow:(NSDictionary *)quota {
|
|
411
|
+
NSString *provider = [quota[@"provider"] isKindOfClass:NSString.class] ? quota[@"provider"] : @"Agent";
|
|
412
|
+
NSString *profileName = [quota[@"profileName"] isKindOfClass:NSString.class] ? quota[@"profileName"] : nil;
|
|
413
|
+
NSString *name = profileName.length > 0 && [profileName caseInsensitiveCompare:provider] != NSOrderedSame
|
|
414
|
+
? [NSString stringWithFormat:@"%@ · %@", provider, profileName] : provider;
|
|
415
|
+
BOOL api = [quota[@"apiMode"] boolValue];
|
|
416
|
+
BOOL limited = [quota[@"state"] isEqual:@"limited"];
|
|
417
|
+
NSDictionary *usage = [quota[@"usage"] isKindOfClass:NSDictionary.class] ? quota[@"usage"] : @{};
|
|
418
|
+
NSDictionary *tokens = [usage[@"tokenUsage"] isKindOfClass:NSDictionary.class] ? usage[@"tokenUsage"] : nil;
|
|
419
|
+
NSDictionary *month = [tokens[@"month"] isKindOfClass:NSDictionary.class] ? tokens[@"month"] : nil;
|
|
420
|
+
NSString *status = [quota[@"status"] isKindOfClass:NSString.class] ? quota[@"status"] : @"";
|
|
421
|
+
BOOL liquid = self.glass.usesLiquidGlass;
|
|
422
|
+
NSColor *brand = ProviderColor(provider, liquid);
|
|
423
|
+
|
|
424
|
+
NSInteger remaining = CCPetsMenuBarLowestFiveHourRemaining(@[quota]);
|
|
425
|
+
NSColor *accent = LevelColor(CCPetsMenuBarLevelForRemaining(remaining), brand);
|
|
426
|
+
NSString *value, *detail;
|
|
427
|
+
NSColor *valueColor = PrimaryInk();
|
|
428
|
+
if (api) {
|
|
429
|
+
value = month ? CCPetsFormatTokens([month[@"total_tokens"] unsignedLongLongValue]) : @"--";
|
|
430
|
+
detail = month ? [NSString stringWithFormat:L(@"This month · %@ calls"),
|
|
431
|
+
[NSNumberFormatter localizedStringFromNumber:@([month[@"request_count"] unsignedLongLongValue])
|
|
432
|
+
numberStyle:NSNumberFormatterDecimalStyle]] : status;
|
|
433
|
+
} else if (limited) {
|
|
434
|
+
value = L(@"Rate limited");
|
|
435
|
+
valueColor = NSColor.systemRedColor;
|
|
436
|
+
NSString *reset = ResetText(quota[@"fiveReset"]);
|
|
437
|
+
detail = reset ? [NSString stringWithFormat:L(@"Resets %@"), reset] : status;
|
|
438
|
+
} else if (remaining >= 0 || quota[@"weekRemaining"]) {
|
|
439
|
+
value = remaining >= 0 ? PercentText(quota[@"fiveRemaining"]) : @"--";
|
|
440
|
+
if (remaining >= 0 && accent != brand) valueColor = accent;
|
|
441
|
+
NSMutableArray<NSString *> *parts = [NSMutableArray arrayWithObject:
|
|
442
|
+
[NSString stringWithFormat:L(@"Week %@"), PercentText(quota[@"weekRemaining"])]];
|
|
443
|
+
NSString *reset = ResetText(quota[@"fiveReset"]);
|
|
444
|
+
if (reset) [parts addObject:[NSString stringWithFormat:L(@"Resets %@"), reset]];
|
|
445
|
+
detail = [parts componentsJoinedByString:@" · "];
|
|
446
|
+
} else {
|
|
447
|
+
value = @"";
|
|
448
|
+
detail = status;
|
|
449
|
+
}
|
|
450
|
+
double fraction = api ? -1 : (limited ? 0 : (remaining >= 0 ? remaining / 100.0 : -1));
|
|
451
|
+
__weak typeof(self) weakSelf = self;
|
|
452
|
+
CCPetsMenuBarRow *row = [self rowWithHeight:44 drawer:^(NSRect bounds, BOOL hovered) {
|
|
453
|
+
NSRect ringBox = NSMakeRect(RowInset, NSMidY(bounds) - 13, 26, 26);
|
|
454
|
+
if (api) {
|
|
455
|
+
// API 账号没有额度窗口:只画一个品牌色实心点,和订阅账号的环对齐。
|
|
456
|
+
DrawDot(NSMakePoint(NSMidX(ringBox), NSMidY(ringBox)), 10, brand);
|
|
457
|
+
} else {
|
|
458
|
+
DrawRing(ringBox, fraction, accent);
|
|
459
|
+
}
|
|
460
|
+
CGFloat x = NSMaxX(ringBox) + 10;
|
|
461
|
+
CGFloat valueWidth = TextWidth(value, 15, NSFontWeightSemibold);
|
|
462
|
+
NSRect valueRect = NSMakeRect(NSMaxX(bounds) - RowInset - valueWidth, NSMinY(bounds), valueWidth, NSHeight(bounds));
|
|
463
|
+
DrawText(value, valueRect, 15, NSFontWeightSemibold, valueColor, NSTextAlignmentRight);
|
|
464
|
+
CGFloat textWidth = NSMinX(valueRect) - 8 - x;
|
|
465
|
+
DrawText(name, NSMakeRect(x, NSMidY(bounds) - 17, textWidth, 17), 13, NSFontWeightMedium,
|
|
466
|
+
PrimaryInk(), NSTextAlignmentLeft);
|
|
467
|
+
DrawText(detail, NSMakeRect(x, NSMidY(bounds) + 1, textWidth, 15), 11, NSFontWeightRegular,
|
|
468
|
+
SecondaryInk(), NSTextAlignmentLeft);
|
|
469
|
+
} action:^{
|
|
470
|
+
[weakSelf dismiss];
|
|
471
|
+
[CCPetsMainWindowController presentPage:CCPetsMainWindowPageOverview];
|
|
472
|
+
}];
|
|
473
|
+
return row;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// 底部:有新版本时的更新入口、显示桌宠开关、打开主菜单。退出在右键菜单里。
|
|
477
|
+
- (NSArray<CCPetsMenuBarRow *> *)footerRows {
|
|
478
|
+
CCPetsStatusItemController *owner = self.owner;
|
|
479
|
+
__weak typeof(self) weakSelf = self;
|
|
480
|
+
NSMutableArray<CCPetsMenuBarRow *> *rows = [NSMutableArray array];
|
|
481
|
+
NSString *pending = owner.pendingUpdateVersionProvider ? owner.pendingUpdateVersionProvider() : nil;
|
|
482
|
+
if (pending.length > 0) {
|
|
483
|
+
NSString *title = [NSString stringWithFormat:L(@"Update to %@…"), pending];
|
|
484
|
+
[rows addObject:[self footerRowWithTitle:title symbol:@"arrow.up.circle.fill"
|
|
485
|
+
tint:[NSColor colorWithRed:0.30 green:0.82 blue:0.90 alpha:1] trailing:nil action:^{
|
|
486
|
+
[weakSelf dismiss];
|
|
487
|
+
if (weakSelf.owner.showPendingUpdate) weakSelf.owner.showPendingUpdate();
|
|
488
|
+
}]];
|
|
489
|
+
}
|
|
490
|
+
BOOL petVisible = owner.petVisibleProvider && owner.petVisibleProvider();
|
|
491
|
+
[rows addObject:[self footerRowWithTitle:L(@"Show Pet") symbol:petVisible ? @"checkmark.circle.fill" : @"circle"
|
|
492
|
+
tint:petVisible ? NSColor.systemGreenColor : SecondaryInk() trailing:nil action:^{
|
|
493
|
+
if (weakSelf.owner.togglePetVisible) weakSelf.owner.togglePetVisible();
|
|
494
|
+
[weakSelf reload];
|
|
495
|
+
}]];
|
|
496
|
+
[rows addObject:[self footerRowWithTitle:L(@"Main menu") symbol:@"macwindow" tint:SecondaryInk()
|
|
497
|
+
trailing:@"⌘," action:^{
|
|
498
|
+
[weakSelf dismiss];
|
|
499
|
+
[CCPetsMainWindowController presentPage:nil];
|
|
500
|
+
}]];
|
|
501
|
+
return rows;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
- (CCPetsMenuBarRow *)footerRowWithTitle:(NSString *)title symbol:(NSString *)symbol tint:(NSColor *)tint
|
|
505
|
+
trailing:(NSString *)trailing action:(void (^)(void))action {
|
|
506
|
+
NSImage *icon = [[NSImage imageWithSystemSymbolName:symbol accessibilityDescription:nil]
|
|
507
|
+
imageWithSymbolConfiguration:[NSImageSymbolConfiguration configurationWithPointSize:12
|
|
508
|
+
weight:NSFontWeightMedium]];
|
|
509
|
+
CCPetsMenuBarRow *row = [self rowWithHeight:28 drawer:^(NSRect bounds, BOOL hovered) {
|
|
510
|
+
if (icon) {
|
|
511
|
+
NSImage *tinted = [NSImage imageWithSize:icon.size flipped:NO drawingHandler:^BOOL(NSRect rect) {
|
|
512
|
+
[icon drawInRect:rect];
|
|
513
|
+
[tint set];
|
|
514
|
+
NSRectFillUsingOperation(rect, NSCompositingOperationSourceAtop);
|
|
515
|
+
return YES;
|
|
516
|
+
}];
|
|
517
|
+
[tinted drawInRect:NSMakeRect(RowInset, NSMidY(bounds) - icon.size.height / 2.0,
|
|
518
|
+
icon.size.width, icon.size.height) fromRect:NSZeroRect
|
|
519
|
+
operation:NSCompositingOperationSourceOver fraction:1 respectFlipped:YES hints:nil];
|
|
520
|
+
}
|
|
521
|
+
NSRect inner = NSMakeRect(RowInset + 22, NSMinY(bounds), NSWidth(bounds) - RowInset * 2 - 22, NSHeight(bounds));
|
|
522
|
+
DrawText(trailing, inner, 11, NSFontWeightRegular, TertiaryInk(), NSTextAlignmentRight);
|
|
523
|
+
DrawText(title, inner, 13, NSFontWeightRegular, PrimaryInk(), NSTextAlignmentLeft);
|
|
524
|
+
} action:action];
|
|
525
|
+
return row;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
@end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// 菜单栏标题要显示什么。只依赖 workbenchQuotas 的快照(见 CCPetsWorkbench.h),不碰 AppKit,
|
|
4
|
+
// 单独成文件是为了让 tests/menu-bar-summary-harness.m 不必链接整套界面代码。
|
|
5
|
+
|
|
6
|
+
typedef NS_ENUM(NSInteger, CCPetsMenuBarLevel) {
|
|
7
|
+
CCPetsMenuBarLevelNormal = 0,
|
|
8
|
+
CCPetsMenuBarLevelWarning, // 剩余 ≤ 20%
|
|
9
|
+
CCPetsMenuBarLevelCritical, // 剩余 ≤ 10%,或已被限流
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// 与低额度台词同一组阈值(20 / 10),菜单栏变色和宠物开口在同一时刻发生。
|
|
13
|
+
extern const NSInteger CCPetsMenuBarWarningPercent;
|
|
14
|
+
extern const NSInteger CCPetsMenuBarCriticalPercent;
|
|
15
|
+
|
|
16
|
+
// 订阅账号里最低的 5 小时剩余百分比(0–100)。API 账号、没有数据的账号不参与;
|
|
17
|
+
// 被限流(state == limited)的账号按 0 算。没有任何可比较的账号时返回 -1。
|
|
18
|
+
NSInteger CCPetsMenuBarLowestFiveHourRemaining(NSArray<NSDictionary *> *quotas);
|
|
19
|
+
|
|
20
|
+
CCPetsMenuBarLevel CCPetsMenuBarLevelForRemaining(NSInteger remaining);
|
|
21
|
+
|
|
22
|
+
// 菜单栏图标旁的文字。键:
|
|
23
|
+
// percent(NSString,如 "62%";不该显示时没有这个键)、level(NSNumber,CCPetsMenuBarLevel)、
|
|
24
|
+
// approvals(NSString,待审批数;为 0 时没有这个键)。
|
|
25
|
+
// 数字只由 showPercent 决定:用户关掉就不显示,哪怕额度已进警告档(低额度在面板里照样着色)。
|
|
26
|
+
// 曾经在 ≤20% 时无视开关强制显示,结果关掉开关后数字不消失,看起来像开关坏了。
|
|
27
|
+
NSDictionary *CCPetsMenuBarTitleParts(NSArray<NSDictionary *> *quotas, NSUInteger approvals, BOOL showPercent);
|
|
28
|
+
|
|
29
|
+
// 菜单栏图标的色调。state 是宠物名片快照里的 Agent 状态(和状态卡同一条),归类与状态卡的
|
|
30
|
+
// statusColorForState: 一致;待机或没有状态时保持原色,颜色只用来提示「有 Agent 在动」或「刚完成 / 失败」。
|
|
31
|
+
// 具体颜色由菜单栏那边取系统色:状态卡的工作蓝是为浅色玻璃调的,放在深色菜单栏上太暗。
|
|
32
|
+
typedef NS_ENUM(NSInteger, CCPetsMenuBarIconTone) {
|
|
33
|
+
CCPetsMenuBarIconToneNone = 0,
|
|
34
|
+
CCPetsMenuBarIconToneWorking,
|
|
35
|
+
CCPetsMenuBarIconToneAttention, // 等待审批、需要关注
|
|
36
|
+
CCPetsMenuBarIconToneCompleted,
|
|
37
|
+
CCPetsMenuBarIconToneFailed,
|
|
38
|
+
};
|
|
39
|
+
CCPetsMenuBarIconTone CCPetsMenuBarIconToneForState(NSString *state);
|
|
40
|
+
|
|
41
|
+
// 面板里账号的排列:订阅账号在前,API 账号在后;同一组内保持 workbenchQuotas 原有次序
|
|
42
|
+
// (平台次序与悬停面板一致)。订阅额度会耗尽、要盯着看,API 用量只是记账。
|
|
43
|
+
NSArray<NSDictionary *> *CCPetsMenuBarOrderedQuotas(NSArray<NSDictionary *> *quotas);
|
|
44
|
+
|
|
45
|
+
// 有会话在等审批时,左键菜单栏图标要直接跳去的会话 id(workbenchSessions 的 id)。
|
|
46
|
+
// 取列表里第一个等审批、且解析到了终端的会话:和面板会话列表同一个次序,处理完一个再点就是下一个。
|
|
47
|
+
// 没有等审批的会话、或都没有可跳的终端时返回 nil,此时照常打开面板。
|
|
48
|
+
NSString *CCPetsMenuBarApprovalSessionID(NSArray<NSDictionary *> *sessions);
|
|
49
|
+
|
|
50
|
+
// 所有账号今日 Token 合计(usage.tokenUsage.today.total_tokens)。都没有数据时返回 nil。
|
|
51
|
+
NSNumber *CCPetsMenuBarTodayTokens(NSArray<NSDictionary *> *quotas);
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#import "CCPetsMenuBarSummary.h"
|
|
2
|
+
#import <math.h>
|
|
3
|
+
|
|
4
|
+
const NSInteger CCPetsMenuBarWarningPercent = 20;
|
|
5
|
+
const NSInteger CCPetsMenuBarCriticalPercent = 10;
|
|
6
|
+
|
|
7
|
+
NSInteger CCPetsMenuBarLowestFiveHourRemaining(NSArray<NSDictionary *> *quotas) {
|
|
8
|
+
NSInteger lowest = -1;
|
|
9
|
+
for (NSDictionary *quota in quotas) {
|
|
10
|
+
if (![quota isKindOfClass:NSDictionary.class] || [quota[@"apiMode"] boolValue]) continue;
|
|
11
|
+
NSInteger remaining;
|
|
12
|
+
if ([quota[@"state"] isEqual:@"limited"]) {
|
|
13
|
+
remaining = 0;
|
|
14
|
+
} else if ([quota[@"fiveRemaining"] isKindOfClass:NSNumber.class]) {
|
|
15
|
+
double value = [quota[@"fiveRemaining"] doubleValue];
|
|
16
|
+
if (!isfinite(value)) continue;
|
|
17
|
+
// 向下取整:剩 19.6% 时显示 20% 却不变色,会让人以为阈值没生效。
|
|
18
|
+
remaining = (NSInteger)floor(fmax(0, fmin(100, value)));
|
|
19
|
+
} else {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (lowest < 0 || remaining < lowest) lowest = remaining;
|
|
23
|
+
}
|
|
24
|
+
return lowest;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
CCPetsMenuBarLevel CCPetsMenuBarLevelForRemaining(NSInteger remaining) {
|
|
28
|
+
if (remaining < 0) return CCPetsMenuBarLevelNormal;
|
|
29
|
+
if (remaining <= CCPetsMenuBarCriticalPercent) return CCPetsMenuBarLevelCritical;
|
|
30
|
+
if (remaining <= CCPetsMenuBarWarningPercent) return CCPetsMenuBarLevelWarning;
|
|
31
|
+
return CCPetsMenuBarLevelNormal;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
NSDictionary *CCPetsMenuBarTitleParts(NSArray<NSDictionary *> *quotas, NSUInteger approvals, BOOL showPercent) {
|
|
35
|
+
NSInteger remaining = CCPetsMenuBarLowestFiveHourRemaining(quotas);
|
|
36
|
+
CCPetsMenuBarLevel level = CCPetsMenuBarLevelForRemaining(remaining);
|
|
37
|
+
NSMutableDictionary *parts = [NSMutableDictionary dictionaryWithObject:@(level) forKey:@"level"];
|
|
38
|
+
if (remaining >= 0 && showPercent) {
|
|
39
|
+
parts[@"percent"] = [NSString stringWithFormat:@"%ld%%", (long)remaining];
|
|
40
|
+
}
|
|
41
|
+
if (approvals > 0) parts[@"approvals"] = [NSString stringWithFormat:@"%lu", (unsigned long)approvals];
|
|
42
|
+
return parts;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
NSArray<NSDictionary *> *CCPetsMenuBarOrderedQuotas(NSArray<NSDictionary *> *quotas) {
|
|
46
|
+
NSMutableArray<NSDictionary *> *subscription = [NSMutableArray array];
|
|
47
|
+
NSMutableArray<NSDictionary *> *api = [NSMutableArray array];
|
|
48
|
+
for (NSDictionary *quota in quotas) {
|
|
49
|
+
if (![quota isKindOfClass:NSDictionary.class]) continue;
|
|
50
|
+
[([quota[@"apiMode"] boolValue] ? api : subscription) addObject:quota];
|
|
51
|
+
}
|
|
52
|
+
return [subscription arrayByAddingObjectsFromArray:api];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
CCPetsMenuBarIconTone CCPetsMenuBarIconToneForState(NSString *state) {
|
|
56
|
+
if (![state isKindOfClass:NSString.class] || state.length == 0 || [state isEqualToString:@"idle"]) {
|
|
57
|
+
return CCPetsMenuBarIconToneNone;
|
|
58
|
+
}
|
|
59
|
+
if ([state isEqualToString:@"completed"] || [state isEqualToString:@"tool_completed"]) {
|
|
60
|
+
return CCPetsMenuBarIconToneCompleted;
|
|
61
|
+
}
|
|
62
|
+
if ([state isEqualToString:@"failed"] || [state isEqualToString:@"tool_failed"]) {
|
|
63
|
+
return CCPetsMenuBarIconToneFailed;
|
|
64
|
+
}
|
|
65
|
+
if ([state isEqualToString:@"approval"] || [state isEqualToString:@"notification"]) {
|
|
66
|
+
return CCPetsMenuBarIconToneAttention;
|
|
67
|
+
}
|
|
68
|
+
return CCPetsMenuBarIconToneWorking;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
NSString *CCPetsMenuBarApprovalSessionID(NSArray<NSDictionary *> *sessions) {
|
|
72
|
+
for (NSDictionary *session in sessions) {
|
|
73
|
+
if (![session isKindOfClass:NSDictionary.class] || ![session[@"state"] isEqual:@"approval"]) continue;
|
|
74
|
+
NSString *sessionID = [session[@"id"] isKindOfClass:NSString.class] ? session[@"id"] : nil;
|
|
75
|
+
NSString *terminal = [session[@"terminal"] isKindOfClass:NSString.class] ? session[@"terminal"] : nil;
|
|
76
|
+
if (sessionID.length > 0 && terminal.length > 0) return sessionID;
|
|
77
|
+
}
|
|
78
|
+
return nil;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
NSNumber *CCPetsMenuBarTodayTokens(NSArray<NSDictionary *> *quotas) {
|
|
82
|
+
BOOL found = NO;
|
|
83
|
+
unsigned long long total = 0;
|
|
84
|
+
for (NSDictionary *quota in quotas) {
|
|
85
|
+
if (![quota isKindOfClass:NSDictionary.class]) continue;
|
|
86
|
+
NSDictionary *usage = [quota[@"usage"] isKindOfClass:NSDictionary.class] ? quota[@"usage"] : nil;
|
|
87
|
+
NSDictionary *tokens = [usage[@"tokenUsage"] isKindOfClass:NSDictionary.class] ? usage[@"tokenUsage"] : nil;
|
|
88
|
+
NSDictionary *today = [tokens[@"today"] isKindOfClass:NSDictionary.class] ? tokens[@"today"] : nil;
|
|
89
|
+
NSNumber *value = [today[@"total_tokens"] isKindOfClass:NSNumber.class] ? today[@"total_tokens"] : nil;
|
|
90
|
+
if (!value || value.doubleValue < 0) continue;
|
|
91
|
+
found = YES;
|
|
92
|
+
total += value.unsignedLongLongValue;
|
|
93
|
+
}
|
|
94
|
+
return found ? @(total) : nil;
|
|
95
|
+
}
|