cc-pets 2.1.0 → 2.1.1

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.
@@ -0,0 +1,196 @@
1
+ #import "CCPetsGlassMenu.h"
2
+ #import "CCPetsGlassView.h"
3
+
4
+ static const CGFloat GlassMenuRowHeight = 24;
5
+ static const CGFloat GlassMenuSeparatorHeight = 11;
6
+ static const CGFloat GlassMenuPadding = 6;
7
+ static const CGFloat GlassMenuTextInset = 30;
8
+
9
+ static NSShadow *GlassMenuTextShadow(void) {
10
+ NSShadow *shadow = [NSShadow new];
11
+ shadow.shadowColor = [NSColor colorWithWhite:0 alpha:0.45];
12
+ shadow.shadowBlurRadius = 2;
13
+ shadow.shadowOffset = NSMakeSize(0, -0.5);
14
+ return shadow;
15
+ }
16
+
17
+ @interface CCPetsGlassMenuRow : NSView
18
+ @property(nonatomic, strong) NSMenuItem *item;
19
+ @end
20
+
21
+ @implementation CCPetsGlassMenuRow {
22
+ NSTrackingArea *_trackingArea;
23
+ BOOL _hovered;
24
+ }
25
+ - (BOOL)selectable { return self.item.isEnabled && self.item.action != nil; }
26
+ - (BOOL)acceptsFirstMouse:(NSEvent *)event { return YES; }
27
+ // 这是独立的面板窗口,不在菜单跟踪循环里,进出事件是可靠的。
28
+ - (void)updateTrackingAreas {
29
+ [super updateTrackingAreas];
30
+ if (_trackingArea) [self removeTrackingArea:_trackingArea];
31
+ _trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
32
+ options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect
33
+ owner:self userInfo:nil];
34
+ [self addTrackingArea:_trackingArea];
35
+ }
36
+ - (void)mouseEntered:(NSEvent *)event { _hovered = YES; self.needsDisplay = YES; }
37
+ - (void)mouseExited:(NSEvent *)event { _hovered = NO; self.needsDisplay = YES; }
38
+ - (void)mouseDown:(NSEvent *)event {}
39
+ - (void)mouseUp:(NSEvent *)event {
40
+ if (![self selectable]) return;
41
+ NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
42
+ if (!NSPointInRect(point, self.bounds)) return;
43
+ NSMenuItem *item = self.item;
44
+ [CCPetsGlassMenu dismiss];
45
+ [NSApp sendAction:item.action to:item.target from:item];
46
+ }
47
+ - (void)drawRect:(NSRect)dirtyRect {
48
+ NSMenuItem *item = self.item;
49
+ if (item.isSeparatorItem) {
50
+ [[NSColor colorWithWhite:1 alpha:0.18] setFill];
51
+ NSRectFill(NSMakeRect(12, floor(NSMidY(self.bounds)), NSWidth(self.bounds) - 24, 1));
52
+ return;
53
+ }
54
+ BOOL highlighted = _hovered && [self selectable];
55
+ if (highlighted) {
56
+ [NSColor.selectedContentBackgroundColor setFill];
57
+ [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(self.bounds, 5, 0)
58
+ xRadius:6 yRadius:6] fill];
59
+ }
60
+ // 标题行(不可点)用浅一档的白,和系统菜单的分组标题一致。
61
+ NSColor *color = [self selectable] ? [NSColor colorWithWhite:1 alpha:0.96]
62
+ : [NSColor colorWithWhite:1 alpha:0.72];
63
+ NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
64
+ style.lineBreakMode = NSLineBreakByTruncatingTail;
65
+ NSDictionary *attributes = @{NSFontAttributeName: [NSFont menuFontOfSize:13],
66
+ NSForegroundColorAttributeName: color, NSParagraphStyleAttributeName: style,
67
+ NSShadowAttributeName: highlighted ? [NSShadow new] : GlassMenuTextShadow()};
68
+ CGFloat textHeight = ceil([item.title sizeWithAttributes:attributes].height);
69
+ NSRect textRect = NSMakeRect(GlassMenuTextInset, (NSHeight(self.bounds) - textHeight) / 2.0,
70
+ NSWidth(self.bounds) - GlassMenuTextInset - 14, textHeight);
71
+ [item.title drawInRect:textRect withAttributes:attributes];
72
+ if (item.state == NSControlStateValueOn) {
73
+ [@"✓" drawAtPoint:NSMakePoint(13, NSMinY(textRect)) withAttributes:attributes];
74
+ }
75
+ }
76
+ @end
77
+
78
+ // 翻转坐标,行从上往下排,滚动视图默认停在顶部。
79
+ @interface CCPetsGlassMenuDocument : NSView
80
+ @end
81
+ @implementation CCPetsGlassMenuDocument
82
+ - (BOOL)isFlipped { return YES; }
83
+ @end
84
+
85
+ @interface CCPetsGlassMenuPanel : NSPanel
86
+ @end
87
+ @implementation CCPetsGlassMenuPanel
88
+ - (BOOL)canBecomeKeyWindow { return NO; }
89
+ @end
90
+
91
+ static CCPetsGlassMenuPanel *ActivePanel;
92
+ static id LocalMonitor;
93
+ static id GlobalMonitor;
94
+
95
+ @implementation CCPetsGlassMenu
96
+ + (void)dismiss {
97
+ if (LocalMonitor) [NSEvent removeMonitor:LocalMonitor];
98
+ if (GlobalMonitor) [NSEvent removeMonitor:GlobalMonitor];
99
+ LocalMonitor = nil;
100
+ GlobalMonitor = nil;
101
+ [ActivePanel orderOut:nil];
102
+ ActivePanel = nil;
103
+ }
104
+
105
+ + (void)showMenu:(NSMenu *)menu belowView:(NSView *)anchor alignRightTo:(NSView *)alignView {
106
+ [self dismiss];
107
+ NSWindow *anchorWindow = anchor.window;
108
+ if (!anchorWindow) return;
109
+ NSDictionary *measure = @{NSFontAttributeName: [NSFont menuFontOfSize:13]};
110
+ CGFloat textWidth = 0, contentHeight = 0;
111
+ for (NSMenuItem *item in menu.itemArray) {
112
+ if (item.isHidden) continue;
113
+ contentHeight += item.isSeparatorItem ? GlassMenuSeparatorHeight : GlassMenuRowHeight;
114
+ textWidth = MAX(textWidth, ceil([item.title sizeWithAttributes:measure].width));
115
+ }
116
+ CGFloat width = MIN(MAX(textWidth + GlassMenuTextInset + 20, 220), 560);
117
+
118
+ NSRect anchorRect = [anchorWindow convertRectToScreen:[anchor convertRect:anchor.bounds toView:nil]];
119
+ NSRect alignRect = [anchorWindow convertRectToScreen:[alignView convertRect:alignView.bounds toView:nil]];
120
+ NSRect visible = NSInsetRect((anchorWindow.screen ?: NSScreen.mainScreen).visibleFrame, 4, 4);
121
+ // 会话、消息多的时候不能无限长:限高,超出部分在面板内滚动。
122
+ CGFloat height = MIN(contentHeight + GlassMenuPadding * 2, MIN(420, NSHeight(visible)));
123
+ CGFloat x = NSMaxX(alignRect) - width;
124
+ x = MIN(MAX(x, NSMinX(visible)), NSMaxX(visible) - width);
125
+ // 优先放在状态卡下方,放不下再放上方;两边都放不下时夹在屏幕可见区域内(会盖住状态卡)。
126
+ CGFloat below = NSMinY(anchorRect) - 6 - height;
127
+ CGFloat above = NSMaxY(anchorRect) + 6;
128
+ CGFloat y = below >= NSMinY(visible) ? below
129
+ : (above + height <= NSMaxY(visible) ? above : below);
130
+ y = MIN(MAX(y, NSMinY(visible)), NSMaxY(visible) - height);
131
+
132
+ CCPetsGlassMenuPanel *panel = [[CCPetsGlassMenuPanel alloc]
133
+ initWithContentRect:NSMakeRect(x, y, width, height)
134
+ styleMask:NSWindowStyleMaskBorderless | NSWindowStyleMaskNonactivatingPanel
135
+ backing:NSBackingStoreBuffered defer:NO];
136
+ panel.opaque = NO;
137
+ panel.backgroundColor = NSColor.clearColor;
138
+ panel.hasShadow = NO;
139
+ panel.level = anchorWindow.level + 1;
140
+ panel.collectionBehavior = NSWindowCollectionBehaviorCanJoinAllSpaces |
141
+ NSWindowCollectionBehaviorFullScreenAuxiliary;
142
+ panel.hidesOnDeactivate = NO;
143
+ panel.acceptsMouseMovedEvents = YES;
144
+
145
+ CCPetsGlassView *glass = [[CCPetsGlassView alloc] initWithFrame:NSMakeRect(0, 0, width, height)
146
+ material:NSVisualEffectMaterialMenu appearance:NSAppearanceNameDarkAqua cornerRadius:12];
147
+ glass.edgeShadeHeight = 10;
148
+ glass.edgeShadeAlpha = 0.6;
149
+ glass.usesWidgetGlass = YES;
150
+ CCPetsGlassMenuDocument *document = [[CCPetsGlassMenuDocument alloc]
151
+ initWithFrame:NSMakeRect(0, 0, width, contentHeight)];
152
+ CGFloat rowY = 0;
153
+ for (NSMenuItem *item in menu.itemArray) {
154
+ if (item.isHidden) continue;
155
+ CGFloat rowHeight = item.isSeparatorItem ? GlassMenuSeparatorHeight : GlassMenuRowHeight;
156
+ CCPetsGlassMenuRow *row = [[CCPetsGlassMenuRow alloc]
157
+ initWithFrame:NSMakeRect(0, rowY, width, rowHeight)];
158
+ row.item = item;
159
+ if (item.toolTip.length > 0) row.toolTip = item.toolTip;
160
+ [document addSubview:row];
161
+ rowY += rowHeight;
162
+ }
163
+ NSScrollView *scroll = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, GlassMenuPadding,
164
+ width, height - GlassMenuPadding * 2)];
165
+ scroll.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
166
+ scroll.drawsBackground = NO;
167
+ scroll.contentView.drawsBackground = NO;
168
+ scroll.borderType = NSNoBorder;
169
+ scroll.hasVerticalScroller = contentHeight > NSHeight(scroll.frame);
170
+ scroll.autohidesScrollers = YES;
171
+ scroll.scrollerStyle = NSScrollerStyleOverlay;
172
+ scroll.documentView = document;
173
+ [glass.contentView addSubview:scroll];
174
+ panel.contentView = glass;
175
+ [panel orderFrontRegardless];
176
+ ActivePanel = panel;
177
+
178
+ // 点面板以外的任何地方就收起,和系统菜单一致。桌宠是非激活 App,大部分点击落在别的
179
+ // App 上,要靠全局监听;落在本 App 其他窗口(宠物、状态卡)上的走本地监听。
180
+ NSEventMask mask = NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown | NSEventMaskOtherMouseDown;
181
+ GlobalMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:mask handler:^(NSEvent *event) {
182
+ [CCPetsGlassMenu dismiss];
183
+ }];
184
+ // 键盘:面板刻意不抢焦点(抢了会把前台终端的焦点夺走),所以只有本 App 恰好在前台时
185
+ // Esc 才生效,也不支持方向键选择。这是替代系统菜单的已知取舍,不用全局键盘监听来补。
186
+ LocalMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:mask | NSEventMaskKeyDown
187
+ handler:^NSEvent *(NSEvent *event) {
188
+ if (event.type == NSEventTypeKeyDown) {
189
+ if (event.keyCode == 53) { [CCPetsGlassMenu dismiss]; return nil; }
190
+ return event;
191
+ }
192
+ if (event.window != ActivePanel) [CCPetsGlassMenu dismiss];
193
+ return event;
194
+ }];
195
+ }
196
+ @end
@@ -0,0 +1,31 @@
1
+ #import <Cocoa/Cocoa.h>
2
+
3
+ extern NSString *const CCPetsPanelThemeKey;
4
+ NSString *CCPetsPanelTheme(void);
5
+ // 桌面小组件式玻璃背后压暗层的档位:0 / 15 / 25 / 45(百分比),默认 25。
6
+ extern NSString *const CCPetsGlassDimKey;
7
+ NSInteger CCPetsGlassDimLevel(void);
8
+ NSArray<NSNumber *> *CCPetsGlassDimLevels(void);
9
+ // 额度卡片衬底的不透明度,跟随压暗档位:卡片区域同时受背后压暗和衬底两层影响,
10
+ // 衬底固定的话选「通透」时卡片仍然很深。
11
+ CGFloat CCPetsGlassCardScrimAlpha(void);
12
+ // 运行系统和构建 SDK 都支持 NSGlassEffectView 时才为 YES。
13
+ BOOL CCPetsLiquidGlassAvailable(void);
14
+
15
+ // 固定的内容容器使主题切换不影响文字、按钮和悬停状态。
16
+ @interface CCPetsGlassView : NSView
17
+ @property(nonatomic, readonly) NSView *contentView;
18
+ @property(nonatomic, readonly) BOOL usesLiquidGlass;
19
+ @property(nonatomic) CGFloat cornerRadius;
20
+ // 桌面小组件式玻璃:背景清晰、边缘折射,并在玻璃背后垫一层压暗托住白字。只适合白字面板。
21
+ @property(nonatomic) BOOL usesWidgetGlass;
22
+ // 上下沿压高光暗带的高度和最深处不透明度,默认 12pt / 0.6(额度面板实测值)。
23
+ // 胶囊形的状态卡、气泡比较矮,要在设置 usesWidgetGlass 之前调小。
24
+ @property(nonatomic) CGFloat edgeShadeHeight;
25
+ @property(nonatomic) CGFloat edgeShadeAlpha;
26
+ - (instancetype)initWithFrame:(NSRect)frame material:(NSVisualEffectMaterial)material
27
+ appearance:(NSAppearanceName)appearance cornerRadius:(CGFloat)cornerRadius;
28
+ - (void)applyTheme;
29
+ // 只按当前档位更新压暗层透明度,不重建玻璃。
30
+ - (void)applyDimLevel;
31
+ @end
@@ -0,0 +1,246 @@
1
+ #import "CCPetsGlassView.h"
2
+ #import <QuartzCore/QuartzCore.h>
3
+
4
+ NSString *const CCPetsPanelThemeKey = @"CCPetsPanelTheme";
5
+
6
+ NSString *CCPetsPanelTheme(void) {
7
+ NSString *theme = [NSUserDefaults.standardUserDefaults stringForKey:CCPetsPanelThemeKey];
8
+ // 早期版本有过 "system" 选项,行为与 liquid 完全一致,读到时按 liquid 处理。
9
+ if ([theme isEqualToString:@"system"]) return @"liquid";
10
+ return [theme isEqualToString:@"liquid"] ? theme : @"classic";
11
+ }
12
+
13
+ NSString *const CCPetsGlassDimKey = @"CCPetsGlassDim";
14
+
15
+ NSArray<NSNumber *> *CCPetsGlassDimLevels(void) { return @[@0, @15, @25, @45]; }
16
+
17
+ // 档位来自对照实测:0 最通透但亮背景下小字看不清;25 在暗、亮壁纸上都基本可读;
18
+ // 45 最清楚但明显偏暗。没设置过或值不在档位里时用 25。
19
+ NSInteger CCPetsGlassDimLevel(void) {
20
+ id stored = [NSUserDefaults.standardUserDefaults objectForKey:CCPetsGlassDimKey];
21
+ if (![stored isKindOfClass:NSNumber.class]) return 25;
22
+ return [CCPetsGlassDimLevels() containsObject:stored] ? [stored integerValue] : 25;
23
+ }
24
+
25
+ CGFloat CCPetsGlassCardScrimAlpha(void) {
26
+ switch (CCPetsGlassDimLevel()) {
27
+ case 0: return 0.06;
28
+ case 15: return 0.10;
29
+ case 45: return 0.16;
30
+ default: return 0.12;
31
+ }
32
+ }
33
+
34
+ BOOL CCPetsLiquidGlassAvailable(void) {
35
+ #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 260000
36
+ if (@available(macOS 26.0, *)) return YES;
37
+ #endif
38
+ return NO;
39
+ }
40
+
41
+ // 上下沿各一条从边缘向内渐隐的暗带,压 variant 11 自带的上下高光线。
42
+ @interface CCPetsEdgeShadeView : NSView
43
+ @property(nonatomic) CGFloat bandHeight;
44
+ @property(nonatomic) CGFloat bandAlpha;
45
+ @end
46
+
47
+ @implementation CCPetsEdgeShadeView
48
+ - (instancetype)initWithFrame:(NSRect)frame {
49
+ if ((self = [super initWithFrame:frame])) {
50
+ self.wantsLayer = YES;
51
+ self.layer.masksToBounds = YES;
52
+ self.layer.cornerCurve = kCACornerCurveContinuous;
53
+ for (int index = 0; index < 2; index++) [self.layer addSublayer:[CAGradientLayer layer]];
54
+ }
55
+ return self;
56
+ }
57
+ - (NSView *)hitTest:(NSPoint)point { return nil; }
58
+ - (void)setFrameSize:(NSSize)size {
59
+ [super setFrameSize:size];
60
+ [self updateBands];
61
+ }
62
+ - (void)updateBands {
63
+ [CATransaction begin];
64
+ [CATransaction setDisableActions:YES];
65
+ id dark = (id)[NSColor colorWithWhite:0 alpha:self.bandAlpha].CGColor;
66
+ id clear = (id)[NSColor colorWithWhite:0 alpha:0].CGColor;
67
+ NSArray<CAGradientLayer *> *bands = (NSArray<CAGradientLayer *> *)self.layer.sublayers;
68
+ // 底带暗色贴下沿,顶带暗色贴上沿;CAGradientLayer 的颜色顺序沿 layer 坐标 y 递增。
69
+ bands[0].frame = CGRectMake(0, 0, NSWidth(self.bounds), self.bandHeight);
70
+ bands[0].colors = @[dark, clear];
71
+ bands[1].frame = CGRectMake(0, NSHeight(self.bounds) - self.bandHeight,
72
+ NSWidth(self.bounds), self.bandHeight);
73
+ bands[1].colors = @[clear, dark];
74
+ [CATransaction commit];
75
+ }
76
+ @end
77
+
78
+ @interface CCPetsGlassView ()
79
+ @property(nonatomic) NSView *contentView;
80
+ @property(nonatomic) NSView *effectView;
81
+ @property(nonatomic) NSView *backdropDimView;
82
+ @property(nonatomic) CCPetsEdgeShadeView *edgeShadeView;
83
+ @property(nonatomic) BOOL experimentalVariantApplied;
84
+ @property(nonatomic) BOOL usesLiquidGlass;
85
+ @property(nonatomic) NSVisualEffectMaterial fallbackMaterial;
86
+ @end
87
+
88
+ @implementation CCPetsGlassView
89
+ - (instancetype)initWithFrame:(NSRect)frame material:(NSVisualEffectMaterial)material
90
+ appearance:(NSAppearanceName)appearance cornerRadius:(CGFloat)cornerRadius {
91
+ self = [super initWithFrame:frame];
92
+ if (self) {
93
+ _fallbackMaterial = material;
94
+ _cornerRadius = cornerRadius;
95
+ _edgeShadeHeight = 12;
96
+ _edgeShadeAlpha = 0.6;
97
+ self.appearance = [NSAppearance appearanceNamed:appearance];
98
+ _contentView = [[NSView alloc] initWithFrame:self.bounds];
99
+ _contentView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
100
+ [self applyTheme];
101
+ }
102
+ return self;
103
+ }
104
+
105
+ - (void)applyTheme {
106
+ #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 260000
107
+ if (@available(macOS 26.0, *)) {
108
+ if ([self.effectView isKindOfClass:NSGlassEffectView.class]) {
109
+ ((NSGlassEffectView *)self.effectView).contentView = nil;
110
+ }
111
+ }
112
+ #endif
113
+ [self.contentView removeFromSuperview];
114
+ // NSGlassEffectView 接管 contentView 时会把它改成 Auto Layout(关掉自动转换约束)。
115
+ // 移到别的父视图后若不恢复,布局引擎会按固有尺寸把它压成 0 宽,里面的滚动视图随之
116
+ // 变成 0×0、内容全被裁掉(实测)。重建时统一恢复成按 autoresizingMask 布局。
117
+ self.contentView.translatesAutoresizingMaskIntoConstraints = YES;
118
+ [self.effectView removeFromSuperview];
119
+ [self.backdropDimView removeFromSuperview];
120
+ self.backdropDimView = nil;
121
+ [self.edgeShadeView removeFromSuperview];
122
+ self.edgeShadeView = nil;
123
+ self.experimentalVariantApplied = NO;
124
+ self.usesLiquidGlass = NO;
125
+ self.contentView.frame = self.bounds;
126
+ // 编译期和运行期双重保护,仍能用旧 SDK 构建并在 macOS 13 上运行。
127
+ #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 260000
128
+ if (@available(macOS 26.0, *)) {
129
+ if ([CCPetsPanelTheme() isEqualToString:@"liquid"]) {
130
+ NSGlassEffectView *glass = [[NSGlassEffectView alloc] initWithFrame:self.bounds];
131
+ glass.style = self.usesWidgetGlass
132
+ ? NSGlassEffectViewStyleClear : NSGlassEffectViewStyleRegular;
133
+ if (self.usesWidgetGlass) glass = [self applyWidgetGlass:glass];
134
+ glass.cornerRadius = self.cornerRadius;
135
+ // 暗带要盖在玻璃上、又不能盖住文字,所以私有 variant 生效时内容改放到玻璃
136
+ // 外面最上层;玻璃只拿一个空的 contentView。
137
+ glass.contentView = self.experimentalVariantApplied
138
+ ? [[NSView alloc] initWithFrame:self.bounds] : self.contentView;
139
+ self.effectView = glass;
140
+ self.usesLiquidGlass = YES;
141
+ }
142
+ }
143
+ #endif
144
+ if (!self.usesLiquidGlass) {
145
+ NSVisualEffectView *blur = [[NSVisualEffectView alloc] initWithFrame:self.bounds];
146
+ blur.material = self.fallbackMaterial;
147
+ blur.blendingMode = NSVisualEffectBlendingModeBehindWindow;
148
+ blur.state = NSVisualEffectStateActive;
149
+ blur.wantsLayer = YES;
150
+ blur.layer.cornerRadius = self.cornerRadius;
151
+ blur.layer.cornerCurve = kCACornerCurveContinuous;
152
+ blur.layer.masksToBounds = YES;
153
+ if (self.fallbackMaterial == NSVisualEffectMaterialPopover) {
154
+ blur.layer.borderWidth = 1;
155
+ blur.layer.borderColor = [NSColor colorWithWhite:1 alpha:0.48].CGColor;
156
+ }
157
+ [blur addSubview:self.contentView];
158
+ self.effectView = blur;
159
+ }
160
+ self.effectView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
161
+ [self addSubview:self.effectView];
162
+ if (self.experimentalVariantApplied) {
163
+ CCPetsEdgeShadeView *shade = [[CCPetsEdgeShadeView alloc] initWithFrame:self.bounds];
164
+ shade.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
165
+ shade.layer.cornerRadius = self.cornerRadius;
166
+ // 默认 12pt / 0.6 是额度面板的对照实测值:再深会在亮背景上压出一条明显的黑带,
167
+ // 再浅压不住深色背景上的亮线。
168
+ shade.bandHeight = self.edgeShadeHeight;
169
+ shade.bandAlpha = self.edgeShadeAlpha;
170
+ [shade updateBands];
171
+ [self addSubview:shade];
172
+ self.edgeShadeView = shade;
173
+ [self addSubview:self.contentView];
174
+ self.contentView.frame = self.bounds;
175
+ }
176
+ }
177
+
178
+ #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 260000
179
+ // macOS 27 上实测:公开的 Clear/Regular 在 App 未激活、窗口不是 key 时会被换成磨砂的
180
+ // 非激活样式;桌宠永远是 nonactivating 面板,在这个系统上公开样式基本是一块奶白。未公开的
181
+ // _variant 不跟随 key 状态,能保持清透和折射。扫过 0–35:只有 11 有厚玻璃的边缘折射,
182
+ // 但它上下沿自带很亮的高光线,深色背景下扎眼,由 CCPetsEdgeShadeView 压下去;14 没有亮线,
183
+ // 但折射也几乎没了,看起来不像玻璃。它是实现细节而不是
184
+ // API:只在验证过的系统主版本上启用,并可用 defaults 关掉,其余情况一律用公开 Clear。
185
+ static BOOL CCPetsExperimentalGlassVariantEnabled(void) {
186
+ if ([NSUserDefaults.standardUserDefaults boolForKey:@"CCPetsDisableExperimentalGlass"]) return NO;
187
+ return NSProcessInfo.processInfo.operatingSystemVersion.majorVersion == 27;
188
+ }
189
+ //
190
+ // 可读性不能靠 tintColor 或 contentView 里的深色底:前者对这个 variant 无效,后者会被
191
+ // 玻璃的 vibrant 混合吃掉。有效的做法是把压暗层放在玻璃背后,让玻璃采样到的背景本身
192
+ // 变暗,折射和高光都还在。
193
+ //
194
+ // 压暗层在私有 variant 失败、被关闭或系统版本不符时同样保留:此时公开 Clear 在非 key
195
+ // 窗口里是磨砂,没有这层垫底白字会更难读。这是刻意的设计,不是遗留。
196
+ - (NSGlassEffectView *)applyWidgetGlass:(NSGlassEffectView *)glass API_AVAILABLE(macos(26.0)) {
197
+ if (CCPetsExperimentalGlassVariantEnabled() &&
198
+ [glass respondsToSelector:NSSelectorFromString(@"set_variant:")]) {
199
+ @try {
200
+ [glass setValue:@11 forKey:@"_variant"];
201
+ self.experimentalVariantApplied = YES;
202
+ } @catch (NSException *exception) {
203
+ // 赋值抛到一半时内部状态不可信,换一个全新的公开 Clear 实例。
204
+ glass = [[NSGlassEffectView alloc] initWithFrame:self.bounds];
205
+ glass.style = NSGlassEffectViewStyleClear;
206
+ }
207
+ }
208
+ NSView *dim = [[NSView alloc] initWithFrame:self.bounds];
209
+ dim.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
210
+ dim.wantsLayer = YES;
211
+ dim.layer.cornerRadius = self.cornerRadius;
212
+ dim.layer.cornerCurve = kCACornerCurveContinuous;
213
+ [self addSubview:dim];
214
+ self.backdropDimView = dim;
215
+ [self applyDimLevel];
216
+ return glass;
217
+ }
218
+ #endif
219
+
220
+ - (void)applyDimLevel {
221
+ CGFloat alpha = CCPetsGlassDimLevel() / 100.0;
222
+ self.backdropDimView.layer.backgroundColor = [NSColor colorWithWhite:0 alpha:alpha].CGColor;
223
+ self.backdropDimView.hidden = alpha <= 0;
224
+ }
225
+
226
+ - (void)setUsesWidgetGlass:(BOOL)usesWidgetGlass {
227
+ if (_usesWidgetGlass == usesWidgetGlass) return;
228
+ _usesWidgetGlass = usesWidgetGlass;
229
+ [self applyTheme];
230
+ }
231
+
232
+ - (void)setCornerRadius:(CGFloat)cornerRadius {
233
+ _cornerRadius = cornerRadius;
234
+ self.backdropDimView.layer.cornerRadius = cornerRadius;
235
+ self.edgeShadeView.layer.cornerRadius = cornerRadius;
236
+ #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 260000
237
+ if (@available(macOS 26.0, *)) {
238
+ if (self.usesLiquidGlass) {
239
+ ((NSGlassEffectView *)self.effectView).cornerRadius = cornerRadius;
240
+ return;
241
+ }
242
+ }
243
+ #endif
244
+ self.effectView.layer.cornerRadius = cornerRadius;
245
+ }
246
+ @end
@@ -8,6 +8,13 @@ NSString *ClaudeUsagePath(void);
8
8
  // 额度文件的写锁。挂在单独的文件上:数据文件本身由 rename 整体替换,
9
9
  // 锁在它上面的话两个写者会锁到不同的 inode,等于没锁。
10
10
  NSString *ClaudeUsageLockPath(void);
11
+ // 最近一次 Codex App Server 实时额度。桌宠随最后一个客户端退出,重启后先用它,
12
+ // 不从会话日志里可能早已过时的快照起步。
13
+ NSString *CodexLiveUsagePath(void);
14
+ // codex-with-pet 每次启动登记的终端,以及 Codex 会话(thread)与它的配对。
15
+ // 读改写必须在锁里:多个 hook 可能同时第一次见到各自的新会话。
16
+ NSString *CodexLaunchRegistryPath(void);
17
+ NSString *CodexLaunchRegistryLockPath(void);
11
18
  // 桌宠端用:只看主账号,不受继承来的 CLAUDE_CONFIG_DIR 影响。
12
19
  NSString *DefaultClaudeConfigDirectory(void);
13
20
  NSString *CurrentClaudeConfigDirectory(void);
@@ -107,6 +107,20 @@ NSString *ClaudeUsageLockPath(void) {
107
107
  return [ClaudeUsagePath() stringByAppendingPathExtension:@"lock"];
108
108
  }
109
109
 
110
+ NSString *CodexLiveUsagePath(void) {
111
+ NSString *name = [NSString stringWithFormat:@"cc-pets-%u-codex-live-usage.json", getuid()];
112
+ return [PetStateDirectory() stringByAppendingPathComponent:name];
113
+ }
114
+
115
+ NSString *CodexLaunchRegistryPath(void) {
116
+ NSString *name = [NSString stringWithFormat:@"cc-pets-%u-codex-launches.json", getuid()];
117
+ return [PetStateDirectory() stringByAppendingPathComponent:name];
118
+ }
119
+
120
+ NSString *CodexLaunchRegistryLockPath(void) {
121
+ return [CodexLaunchRegistryPath() stringByAppendingPathExtension:@"lock"];
122
+ }
123
+
110
124
  NSString *ApplicationSupportDirectory(void) {
111
125
  NSString *override = NSProcessInfo.processInfo.environment[@"CC_PETS_APPLICATION_SUPPORT_DIR"];
112
126
  if (override.length > 0) return override.stringByStandardizingPath;
@@ -2,10 +2,25 @@
2
2
 
3
3
  // 包装脚本在 Agent 启动时固定下来的终端身份。Hook 子进程继承这些环境变量,
4
4
  // 因此任务结束时即使用户已经切到别的应用,也不会把目标错记成当前前台窗口。
5
+ // 由 Codex 共享服务(app-server --managed-daemon)拉起的 hook 返回空目标,见实现处注释。
5
6
  NSDictionary *TerminalFocusTargetFromEnvironment(void);
7
+ BOOL TerminalFocusRunsUnderSharedAgentHost(void);
8
+
9
+ // hook 用:普通情况同 TerminalFocusTargetFromEnvironment;Codex 共享服务下按会话 ID 找
10
+ // codex-with-pet 登记的终端,找不到返回空目标。
11
+ NSDictionary *TerminalFocusTargetForHook(NSDictionary *payload, NSString *provider);
12
+ // cc-pets --codex-launch:codex-with-pet 在 exec 真正的 codex 之前登记自己的终端。
13
+ int RegisterCodexLaunch(void);
14
+ // 以下两个供测试直接驱动。
15
+ BOOL RegisterCodexLaunchForProcess(pid_t pid, NSString *cwd, NSDictionary *terminal,
16
+ NSTimeInterval launchedAt);
17
+ NSDictionary *CodexLaunchTerminalForThread(NSString *thread, NSDictionary *payload);
6
18
 
7
19
  // Terminal / iTerm2 按 TTY 精确选中 tab/session;其他终端在无扩展模式下激活应用。
8
20
  BOOL ActivateTerminalFocusTarget(NSDictionary *target);
9
21
 
22
+ // 回跳目标的候选 bundle ID,按优先级排列,已去重去空。
23
+ NSArray<NSString *> *TerminalFocusBundleCandidates(NSDictionary *target);
24
+
10
25
  // 供包装脚本在 exec Agent 之前捕获承载终端的应用。
11
26
  NSString *FrontmostApplicationBundleIdentifier(void);