跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

macos-setup-view.m

117 行 · 5.4 KB历史下载
1#import "macos-setup-view.h"
2#include <math.h>
3
4@implementation CageGuestSetupView {
5 NSWindow *_window;
6 VZVirtualMachineView *_view;
7 BOOL (^_authorized)(void);
8}
9
10- (instancetype)initWithMachine:(VZVirtualMachine *)machine
11 size:(NSSize)size
12 setupAuthorized:(BOOL (^)(void))setupAuthorized {
13 if (!NSThread.isMainThread || !machine || !setupAuthorized || !setupAuthorized() ||
14 !isfinite(size.width) || !isfinite(size.height) || size.width <= 0 || size.height <= 0) return nil;
15 self = [super init];
16 if (self) {
17 _authorized = [setupAuthorized copy];
18 NSRect rect = NSMakeRect(0, 0, size.width, size.height);
19 _window = [[NSWindow alloc] initWithContentRect:rect styleMask:NSWindowStyleMaskBorderless
20 backing:NSBackingStoreBuffered defer:NO];
21 _window.excludedFromWindowsMenu = YES;
22 _view = [[VZVirtualMachineView alloc] initWithFrame:rect];
23 _view.virtualMachine = machine;
24 _window.contentView = _view;
25 [_window makeFirstResponder:_view];
26 }
27 return self;
28}
29
30- (BOOL)canUseView {
31 if (!NSThread.isMainThread) return NO;
32 if (!_view || !_authorized || !_authorized()) {
33 [self close];
34 return NO;
35 }
36 return YES;
37}
38
39- (NSData *)snapshotType:(NSBitmapImageFileType)type properties:(NSDictionary *)properties {
40 if (![self canUseView]) return nil;
41 NSBitmapImageRep *rep = [_view bitmapImageRepForCachingDisplayInRect:_view.bounds];
42 if (!rep) return nil;
43 [_view cacheDisplayInRect:_view.bounds toBitmapImageRep:rep];
44 NSData *data = [rep representationUsingType:type properties:properties];
45 return [self canUseView] ? data : nil;
46}
47
48- (NSData *)snapshotPNG { return [self snapshotType:NSBitmapImageFileTypePNG properties:@{}]; }
49- (NSData *)snapshotJPEG {
50 return [self snapshotType:NSBitmapImageFileTypeJPEG properties:@{NSImageCompressionFactor: @0.92}];
51}
52
53- (BOOL)sendKey:(NSEvent *)event {
54 if (![self canUseView]) return NO;
55 NSEventType kind = event.type;
56 if (kind != NSEventTypeKeyDown && kind != NSEventTypeKeyUp && kind != NSEventTypeFlagsChanged) return NO;
57 BOOL flagsOnly = kind == NSEventTypeFlagsChanged;
58 NSEvent *target = [NSEvent keyEventWithType:kind location:NSZeroPoint modifierFlags:event.modifierFlags
59 timestamp:NSProcessInfo.processInfo.systemUptime windowNumber:_window.windowNumber
60 context:nil characters:flagsOnly ? @"" : event.characters ?: @""
61 charactersIgnoringModifiers:flagsOnly ? @"" : event.charactersIgnoringModifiers ?: @""
62 isARepeat:!flagsOnly && event.isARepeat keyCode:event.keyCode];
63 if (kind == NSEventTypeKeyDown) [_view keyDown:target];
64 else if (kind == NSEventTypeKeyUp) [_view keyUp:target];
65 else [_view flagsChanged:target];
66 return YES;
67}
68
69- (BOOL)clickAtX:(CGFloat)x y:(CGFloat)y {
70 if (![self canUseView]) return NO;
71 NSRect rect = _view.bounds;
72 if (!isfinite(x) || !isfinite(y) || x < 0 || y < 0 || x >= NSWidth(rect) || y >= NSHeight(rect)) return NO;
73 NSPoint point = [_view convertPoint:NSMakePoint(x, NSHeight(rect) - y) toView:nil];
74 for (NSNumber *type in @[@(NSEventTypeLeftMouseDown), @(NSEventTypeLeftMouseUp)]) {
75 if (![self canUseView]) return NO;
76 NSEvent *event = [NSEvent mouseEventWithType:type.unsignedIntegerValue location:point modifierFlags:0
77 timestamp:NSProcessInfo.processInfo.systemUptime windowNumber:_window.windowNumber
78 context:nil eventNumber:1 clickCount:1 pressure:1];
79 if (event.type == NSEventTypeLeftMouseDown) [_view mouseDown:event];
80 else [_view mouseUp:event];
81 }
82 return YES;
83}
84
85- (BOOL)scrollAtX:(CGFloat)x y:(CGFloat)y deltaX:(CGFloat)deltaX deltaY:(CGFloat)deltaY {
86 if (![self canUseView]) return NO;
87 NSRect rect = _view.bounds;
88 if (!isfinite(x) || !isfinite(y) || !isfinite(deltaX) || !isfinite(deltaY) ||
89 x < 0 || y < 0 || x >= NSWidth(rect) || y >= NSHeight(rect) ||
90 fabs(deltaX) > 4096 || fabs(deltaY) > 4096) return NO;
91 NSPoint point = [_view convertPoint:NSMakePoint(x, NSHeight(rect) - y) toView:nil];
92 NSEvent *move = [NSEvent mouseEventWithType:NSEventTypeMouseMoved location:point modifierFlags:0
93 timestamp:NSProcessInfo.processInfo.systemUptime windowNumber:_window.windowNumber
94 context:nil eventNumber:0 clickCount:0 pressure:0];
95 [_view mouseMoved:move];
96 CGEventRef wheel = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, (int32_t)deltaY, (int32_t)deltaX);
97 if (!wheel) return NO;
98 // A new CG event inherits the host pointer location. Retarget the hidden
99 // window at the screen origin before the VZ view maps it into the guest.
100 NSPoint screen = [_window convertPointToScreen:point];
101 CGEventSetLocation(wheel, CGPointMake(screen.x, NSMaxY(NSScreen.screens.firstObject.frame) - screen.y));
102 NSEvent *event = [NSEvent eventWithCGEvent:wheel];
103 CFRelease(wheel);
104 if (!event || ![self canUseView]) return NO;
105 [_view scrollWheel:event];
106 return YES;
107}
108
109- (void)close {
110 NSAssert(NSThread.isMainThread, @"Close the setup view on the AppKit main thread.");
111 _authorized = nil;
112 _view.virtualMachine = nil;
113 _window.contentView = nil;
114 _view = nil;
115 _window = nil;
116}
117@end