dsh-gufa-code 1.3.4 → 1.3.6
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/README.md +3 -2
- package/client.js +53 -9
- package/desktop/float/GufaFloat.m +315 -167
- package/desktop/float/bin/gufa-float-macos-arm64 +0 -0
- package/desktop/float//345/217/244/346/263/225/347/274/226/347/250/213/346/202/254/346/265/256/345/261/202.app/Contents/MacOS/gufa-float +0 -0
- package/host/dev.js +28 -13
- package/package.json +1 -1
- package/src/client/00-core.js +3 -0
- package/src/client/90-app.js +15 -9
- package/src/client/99-main.js +35 -0
package/README.md
CHANGED
|
@@ -69,9 +69,10 @@ dsh plugin add dsh-gufa-code
|
|
|
69
69
|
|
|
70
70
|
### 摸鱼模式 / 桌面悬浮层(实验功能,macOS arm64)
|
|
71
71
|
|
|
72
|
-
- **进入摸鱼**:IDE 标题栏点「显示器」图标 → 弹出
|
|
72
|
+
- **进入摸鱼**:IDE 标题栏点「显示器」图标 → 弹出 **运行中 App 列表**(图标 + App 名,和投屏列表类似)→ 选中一个 App,古法编程会:
|
|
73
73
|
- 把该 App 切到前台(主窗口自动被盖住,等于「隐藏主窗口」);
|
|
74
|
-
- 以**无边框、半透明(0.55
|
|
74
|
+
- 以**无边框、半透明(0.55)、永远置顶**的独立悬浮窗出现,并**自动吸附**到该 App 当前窗口:位置、大小一致,窗口移动/缩放时实时跟随。
|
|
75
|
+
- 读取窗口位置/大小走 macOS **「辅助功能」**权限(不是屏幕录制):系统设置 → 隐私与安全性 → 辅助功能 → 勾选 **DeepSeek Harness**;改完重启一次 DSH 生效。
|
|
75
76
|
- **悬浮层交互**:
|
|
76
77
|
- 顶部 22px 拖动条移动窗口(右键 = 摸鱼菜单),窗口大小/位置会记住;
|
|
77
78
|
- 全局快捷键 **⌃⌥⌘G**:切换点击穿透(穿透后鼠标直接操作下面的 App,再按一次切回来);
|
package/client.js
CHANGED
|
@@ -1071,6 +1071,9 @@ html[data-gufa-float] [data-gufa-dock]{
|
|
|
1071
1071
|
}
|
|
1072
1072
|
html[data-gufa-float] [data-gufa-dock] [data-conversation-scroll],
|
|
1073
1073
|
html[data-gufa-float] [data-gufa-dock] [data-conversation-content]{background:transparent!important}
|
|
1074
|
+
/* 悬浮层启动瞬间:先隐藏经典界面,避免闪一下「另一个 DSH」 */
|
|
1075
|
+
html[data-gufa-booting] body{background:transparent!important}
|
|
1076
|
+
html[data-gufa-booting] #root{visibility:hidden!important}
|
|
1074
1077
|
/* 经典 DSH 界面在悬浮窗里整体隐藏(保留古法覆盖层与右侧停靠会话),否则半透明下会透出「另一个 DSH」 */
|
|
1075
1078
|
html[data-gufa-float] #root{visibility:hidden!important}
|
|
1076
1079
|
html[data-gufa-float] #root .gufa-overlay-root,
|
|
@@ -10266,9 +10269,11 @@ G.GufaApp = function GufaApp(props) {
|
|
|
10266
10269
|
setFloatPicker({ status: 'loading', windows: [], error: '' })
|
|
10267
10270
|
try {
|
|
10268
10271
|
const value = await G.api('float.windows')
|
|
10269
|
-
if (value?.ok === true &&
|
|
10270
|
-
setFloatPicker({ status: '
|
|
10271
|
-
} else if (value?.ok ===
|
|
10272
|
+
if (value?.ok === true && value?.accessibility !== true) {
|
|
10273
|
+
setFloatPicker({ status: 'permission', windows: [], error: '' })
|
|
10274
|
+
} else if (value?.ok === true && Array.isArray(value.apps)) {
|
|
10275
|
+
setFloatPicker({ status: 'ready', windows: value.apps, error: '' })
|
|
10276
|
+
} else if (value?.ok === false) {
|
|
10272
10277
|
setFloatPicker({ status: 'permission', windows: [], error: '' })
|
|
10273
10278
|
} else {
|
|
10274
10279
|
setFloatPicker({ status: 'error', windows: [], error: '没有读到可吸附的窗口' })
|
|
@@ -10284,7 +10289,11 @@ G.GufaApp = function GufaApp(props) {
|
|
|
10284
10289
|
setFloatBusy(true)
|
|
10285
10290
|
try {
|
|
10286
10291
|
const value = await G.api('float.start', {
|
|
10287
|
-
follow: {
|
|
10292
|
+
follow: {
|
|
10293
|
+
pid: Number(target.pid) || 0,
|
|
10294
|
+
bundleId: typeof target.bundleId === 'string' ? target.bundleId : '',
|
|
10295
|
+
title: '',
|
|
10296
|
+
},
|
|
10288
10297
|
})
|
|
10289
10298
|
setFloatRunning(value?.running === true)
|
|
10290
10299
|
setFloatPicker(null)
|
|
@@ -11579,8 +11588,8 @@ G.GufaApp = function GufaApp(props) {
|
|
|
11579
11588
|
? h(
|
|
11580
11589
|
'div',
|
|
11581
11590
|
{ className: 'gufa-float-permission' },
|
|
11582
|
-
h('div', { className: 'gufa-float-permission-title' }, '
|
|
11583
|
-
h('div', { className: 'gufa-hint' }, '
|
|
11591
|
+
h('div', { className: 'gufa-float-permission-title' }, '需要「辅助功能」权限'),
|
|
11592
|
+
h('div', { className: 'gufa-hint' }, '摸鱼模式需要「辅助功能」权限才能读取/跟随其他 App 的窗口位置和大小(只读窗口信息,不录屏)。请在「系统设置 → 隐私与安全性 → 辅助功能」里打开 **DeepSeek Harness** 的开关(改完可能需要重启一次 DSH),然后点「重试」。'),
|
|
11584
11593
|
h(
|
|
11585
11594
|
'div',
|
|
11586
11595
|
{ className: 'gufa-row', style: { gap: 8, marginTop: 12, flexWrap: 'wrap' } },
|
|
@@ -11613,10 +11622,10 @@ G.GufaApp = function GufaApp(props) {
|
|
|
11613
11622
|
h(
|
|
11614
11623
|
'span',
|
|
11615
11624
|
{ className: 'gufa-float-meta' },
|
|
11616
|
-
h('span', { className: 'gufa-float-owner' }, win.
|
|
11617
|
-
h('span', { className: 'gufa-float-title' }, win.
|
|
11625
|
+
h('span', { className: 'gufa-float-owner' }, win.name !== undefined && win.name !== '' ? win.name : '未知 App'),
|
|
11626
|
+
h('span', { className: 'gufa-float-title' }, win.bundleId !== undefined ? win.bundleId : ''),
|
|
11618
11627
|
),
|
|
11619
|
-
h('span', { className: 'gufa-float-size' },
|
|
11628
|
+
win.frontmost === true ? h('span', { className: 'gufa-float-size' }, '当前') : null,
|
|
11620
11629
|
),
|
|
11621
11630
|
),
|
|
11622
11631
|
),
|
|
@@ -12214,6 +12223,41 @@ const GUFA_PLUGIN = {
|
|
|
12214
12223
|
apply(ctx) {
|
|
12215
12224
|
G.ctx = ctx
|
|
12216
12225
|
|
|
12226
|
+
/* ── 悬浮层模式(?gufa-float=1 或 helper 注入的 localStorage 标记)──
|
|
12227
|
+
* 尽早打标记并隐藏经典 DSH 界面,避免先闪出一个「另一个 DSH」;
|
|
12228
|
+
* 同时重试进入 IDE 模式,直到覆盖层挂载完成。 */
|
|
12229
|
+
try {
|
|
12230
|
+
let floatMode = false
|
|
12231
|
+
try {
|
|
12232
|
+
floatMode = /[?&]gufa-float=1/.test(String(location.search || '')) || localStorage.getItem('gufa-code:float') === '1'
|
|
12233
|
+
} catch {
|
|
12234
|
+
floatMode = false
|
|
12235
|
+
}
|
|
12236
|
+
if (floatMode) {
|
|
12237
|
+
document.documentElement.setAttribute('data-gufa-float', '1')
|
|
12238
|
+
document.documentElement.setAttribute('data-gufa-booting', '1')
|
|
12239
|
+
let attempts = 0
|
|
12240
|
+
const timer = window.setInterval(() => {
|
|
12241
|
+
attempts += 1
|
|
12242
|
+
try {
|
|
12243
|
+
if (typeof G.mode?.get === 'function' && G.mode.get() !== true) G.mode.set(true)
|
|
12244
|
+
} catch {
|
|
12245
|
+
/* ignore */
|
|
12246
|
+
}
|
|
12247
|
+
if (document.querySelector('.gufa-overlay-root') !== null || attempts > 40) {
|
|
12248
|
+
document.documentElement.removeAttribute('data-gufa-booting')
|
|
12249
|
+
window.clearInterval(timer)
|
|
12250
|
+
}
|
|
12251
|
+
}, 250)
|
|
12252
|
+
ctx.effect(() => () => {
|
|
12253
|
+
window.clearInterval(timer)
|
|
12254
|
+
document.documentElement.removeAttribute('data-gufa-booting')
|
|
12255
|
+
}, 'gufa-code: float boot')
|
|
12256
|
+
}
|
|
12257
|
+
} catch (error) {
|
|
12258
|
+
console.error('[gufa-code] 悬浮模式初始化失败', error)
|
|
12259
|
+
}
|
|
12260
|
+
|
|
12217
12261
|
/* HMR 重载时如果仍处于 IDE 模式,重新打上标记(卸载清理会移除它) */
|
|
12218
12262
|
try {
|
|
12219
12263
|
if (typeof G.mode?.get === 'function' && G.mode.get() === true) document.documentElement.setAttribute('data-gufa-ide', '')
|
|
@@ -12,14 +12,42 @@
|
|
|
12
12
|
#import <AppKit/AppKit.h>
|
|
13
13
|
#import <WebKit/WebKit.h>
|
|
14
14
|
#import <CoreGraphics/CoreGraphics.h>
|
|
15
|
+
#import <ApplicationServices/ApplicationServices.h>
|
|
15
16
|
#import <Carbon/Carbon.h>
|
|
16
17
|
#import <signal.h>
|
|
17
18
|
|
|
18
19
|
static const EventHotKeyID GufaToggleHotKey = {'gufa', 1};
|
|
19
20
|
|
|
21
|
+
/* 前置声明(实现位于文件后半部分) */
|
|
22
|
+
static NSString *GufaOutputPath = nil;
|
|
23
|
+
/** 悬浮层诊断日志(/tmp/gufa-float-debug.log)。 */
|
|
24
|
+
static void GufaLog(NSString *format, ...) {
|
|
25
|
+
va_list args;
|
|
26
|
+
va_start(args, format);
|
|
27
|
+
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
|
|
28
|
+
va_end(args);
|
|
29
|
+
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
|
|
30
|
+
NSString *line = [NSString stringWithFormat:@"[%@] %@\n", [formatter stringFromDate:[NSDate date]], message];
|
|
31
|
+
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:@"/tmp/gufa-float-debug.log"];
|
|
32
|
+
if (handle == nil) {
|
|
33
|
+
[line writeToFile:@"/tmp/gufa-float-debug.log" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
|
|
34
|
+
} else {
|
|
35
|
+
[handle seekToEndOfFile];
|
|
36
|
+
[handle writeData:[line dataUsingEncoding:NSUTF8StringEncoding]];
|
|
37
|
+
[handle closeFile];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
static void GufaEmitJSON(NSDictionary *out);
|
|
41
|
+
static BOOL GufaTrustedForAX(void);
|
|
42
|
+
static NSArray<NSDictionary *> *GufaAXWindows(void);
|
|
43
|
+
static AXUIElementRef GufaCopyAXWindow(pid_t pid, NSString *title);
|
|
44
|
+
static BOOL GufaRectFromAX(AXUIElementRef element, NSRect *out);
|
|
45
|
+
static void GufaActivateBundle(NSString *bundleId);
|
|
46
|
+
static NSArray<NSDictionary *> *GufaAXApps(void);
|
|
47
|
+
static AXUIElementRef GufaCopyAppWindow(pid_t pid);
|
|
48
|
+
|
|
20
49
|
/* SIGTERM 时先把主窗口(DeepSeek Harness)唤回前台,再退出,实现「退出摸鱼」 */
|
|
21
50
|
static void GufaInstallTermHandler(void);
|
|
22
|
-
static BOOL GufaRowsHaveForeignWindows(NSArray<NSDictionary *> *rows);
|
|
23
51
|
|
|
24
52
|
@class GufaAppDelegate;
|
|
25
53
|
|
|
@@ -85,14 +113,16 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
|
|
|
85
113
|
@property (nonatomic, strong) NSDictionary *options;
|
|
86
114
|
@property (nonatomic, assign) EventHotKeyRef hotKeyRef;
|
|
87
115
|
@property (nonatomic, assign) BOOL following;
|
|
88
|
-
@property (nonatomic, assign)
|
|
89
|
-
@property (nonatomic,
|
|
116
|
+
@property (nonatomic, assign) pid_t followPid;
|
|
117
|
+
@property (nonatomic, copy) NSString *followTitle;
|
|
118
|
+
@property (nonatomic, assign) AXUIElementRef followWindowRef;
|
|
90
119
|
@property (nonatomic, strong) NSTimer *followTimer;
|
|
91
120
|
@property (nonatomic, strong) NSMutableArray<NSDictionary *> *windowCandidates;
|
|
92
121
|
- (void)loadWebContent;
|
|
93
122
|
- (void)registerToggleHotKey;
|
|
94
123
|
- (void)activateTargetApp;
|
|
95
124
|
- (void)beginningFollowFromOptions;
|
|
125
|
+
- (void)findFollowWindowWithRetries:(NSInteger)attempt;
|
|
96
126
|
- (void)exitMoyu:(id)sender;
|
|
97
127
|
- (void)restoreMainWindow;
|
|
98
128
|
- (void)toggleClickThrough:(id)sender;
|
|
@@ -128,25 +158,52 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
|
|
|
128
158
|
[self loadWebContent];
|
|
129
159
|
}
|
|
130
160
|
|
|
131
|
-
/** 吸附目标 App
|
|
161
|
+
/** 吸附目标 App:把它切到前台(主窗口自然被盖住 + 切到它所在的 Space)。 */
|
|
132
162
|
- (void)activateTargetApp {
|
|
133
163
|
NSDictionary *follow = self.options[@"follow"];
|
|
134
|
-
|
|
164
|
+
if (![follow isKindOfClass:[NSDictionary class]]) return;
|
|
165
|
+
NSString *bundleId = [follow[@"bundleId"] isKindOfClass:[NSString class]] ? follow[@"bundleId"] : @"";
|
|
166
|
+
if (bundleId.length > 0) {
|
|
167
|
+
GufaActivateBundle(bundleId);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
NSNumber *pid = follow[@"pid"];
|
|
135
171
|
if (![pid isKindOfClass:[NSNumber class]]) return;
|
|
136
172
|
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:(pid_t)pid.intValue];
|
|
137
|
-
if (app
|
|
138
|
-
[app activateWithOptions:NSApplicationActivateAllWindows];
|
|
173
|
+
if (app != nil) GufaActivateBundle(app.bundleIdentifier);
|
|
139
174
|
}
|
|
140
175
|
|
|
141
|
-
/**
|
|
176
|
+
/** 启动就进入「吸附跟随」模式(摸鱼列表选好 App 后直接启动)。 */
|
|
142
177
|
- (void)beginningFollowFromOptions {
|
|
143
178
|
NSDictionary *follow = self.options[@"follow"];
|
|
144
179
|
if (![follow isKindOfClass:[NSDictionary class]]) return;
|
|
145
|
-
NSNumber *number = follow[@"windowNumber"];
|
|
146
180
|
NSNumber *pid = follow[@"pid"];
|
|
147
|
-
if (![
|
|
148
|
-
self.
|
|
149
|
-
self.
|
|
181
|
+
if (![pid isKindOfClass:[NSNumber class]]) return;
|
|
182
|
+
self.followPid = (pid_t)pid.intValue;
|
|
183
|
+
self.followTitle = [follow[@"title"] isKindOfClass:[NSString class]] ? follow[@"title"] : @"";
|
|
184
|
+
GufaLog(@"follow-start pid=%d bundle=%@ axTrusted=%d", self.followPid, follow[@"bundleId"] ?: @"", GufaTrustedForAX() ? 1 : 0);
|
|
185
|
+
[self findFollowWindowWithRetries:0];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/* 激活 App 后窗口可能还在切 Space,轮询最多 5 秒找它的窗口。 */
|
|
189
|
+
- (void)findFollowWindowWithRetries:(NSInteger)attempt {
|
|
190
|
+
AXUIElementRef window = GufaCopyAppWindow(self.followPid);
|
|
191
|
+
if (window == NULL) {
|
|
192
|
+
if (attempt >= 20) {
|
|
193
|
+
GufaLog(@"follow-window-not-found pid=%d attempts=%ld axTrusted=%d", self.followPid, (long)attempt, GufaTrustedForAX() ? 1 : 0);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
__weak typeof(self) weakSelf = self;
|
|
197
|
+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
|
198
|
+
[weakSelf findFollowWindowWithRetries:attempt + 1];
|
|
199
|
+
});
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
NSRect rect = NSZeroRect;
|
|
203
|
+
GufaRectFromAX(window, &rect);
|
|
204
|
+
GufaLog(@"follow-window-found pid=%d rect=%.0f,%.0f %.0fx%.0f attempt=%ld", self.followPid, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, (long)attempt);
|
|
205
|
+
if (self.followWindowRef != NULL) CFRelease(self.followWindowRef);
|
|
206
|
+
self.followWindowRef = window;
|
|
150
207
|
self.following = YES;
|
|
151
208
|
[self startFollowTimer];
|
|
152
209
|
[self applyFollowFrame];
|
|
@@ -232,7 +289,7 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
|
|
|
232
289
|
self.statusItem.button.title = @"古法";
|
|
233
290
|
self.statusItem.button.toolTip = @"古法编程悬浮层";
|
|
234
291
|
NSMenu *menu = [[NSMenu alloc] init];
|
|
235
|
-
[menu addItemWithTitle:@"跟随 App
|
|
292
|
+
[menu addItemWithTitle:@"跟随 App 窗口…(需辅助功能权限)" action:@selector(chooseFollowWindow:) keyEquivalent:@""].target = self;
|
|
236
293
|
[menu addItemWithTitle:@"停止跟随" action:@selector(stopFollow:) keyEquivalent:@""].target = self;
|
|
237
294
|
[menu addItem:[NSMenuItem separatorItem]];
|
|
238
295
|
[menu addItemWithTitle:@"切换点击穿透(⌃⌥⌘G)" action:@selector(toggleClickThrough:) keyEquivalent:@""].target = self;
|
|
@@ -275,87 +332,53 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
|
|
|
275
332
|
[[NSUserDefaults standardUserDefaults] setObject:NSStringFromRect(self.window.frame) forKey:self.frameKey];
|
|
276
333
|
}
|
|
277
334
|
|
|
278
|
-
/* ──
|
|
279
|
-
|
|
280
|
-
/** 枚举屏幕上的普通窗口(需要屏幕录制权限才能看到别的 App)。 */
|
|
281
|
-
- (NSArray<NSDictionary *> *)visibleWindows {
|
|
282
|
-
NSMutableArray<NSDictionary *> *rows = [NSMutableArray array];
|
|
283
|
-
CFArrayRef list = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
|
|
284
|
-
if (list == NULL) return rows;
|
|
285
|
-
NSInteger count = CFArrayGetCount(list);
|
|
286
|
-
pid_t selfPid = NSProcessInfo.processInfo.processIdentifier;
|
|
287
|
-
for (NSInteger i = 0; i < count && rows.count < 60; i++) {
|
|
288
|
-
NSDictionary *info = (__bridge NSDictionary *)CFArrayGetValueAtIndex(list, i);
|
|
289
|
-
NSNumber *layer = info[(id)kCGWindowLayer];
|
|
290
|
-
if (![layer isKindOfClass:[NSNumber class]] || layer.intValue != 0) continue;
|
|
291
|
-
NSNumber *pid = info[(id)kCGWindowOwnerPID];
|
|
292
|
-
if (![pid isKindOfClass:[NSNumber class]] || pid.intValue == selfPid) continue;
|
|
293
|
-
NSNumber *number = info[(id)kCGWindowNumber];
|
|
294
|
-
NSDictionary *bounds = info[(id)kCGWindowBounds];
|
|
295
|
-
if (number == nil || bounds == nil) continue;
|
|
296
|
-
CGRect rect = CGRectZero;
|
|
297
|
-
if (!CGRectMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)bounds, &rect)) continue;
|
|
298
|
-
if (rect.size.width < 220 || rect.size.height < 160) continue;
|
|
299
|
-
NSString *owner = info[(id)kCGWindowOwnerName];
|
|
300
|
-
NSString *title = info[(id)kCGWindowName];
|
|
301
|
-
NSString *label = [NSString stringWithFormat:@"%@%@ · %.0f×%.0f",
|
|
302
|
-
owner != nil ? owner : @"?",
|
|
303
|
-
(title.length > 0 ? [NSString stringWithFormat:@" — %@", title] : @""),
|
|
304
|
-
rect.size.width, rect.size.height];
|
|
305
|
-
[rows addObject:@{
|
|
306
|
-
@"number": number,
|
|
307
|
-
@"pid": pid,
|
|
308
|
-
@"owner": owner != nil ? owner : @"?",
|
|
309
|
-
@"title": title != nil ? title : @"",
|
|
310
|
-
@"label": label,
|
|
311
|
-
@"rect": NSStringFromRect(NSRectFromCGRect(rect)),
|
|
312
|
-
}];
|
|
313
|
-
}
|
|
314
|
-
CFRelease(list);
|
|
315
|
-
return rows;
|
|
316
|
-
}
|
|
335
|
+
/* ── 跟随选中的 App 窗口(Accessibility API) ─────────────────────── */
|
|
317
336
|
|
|
318
337
|
- (void)showPermissionGuide {
|
|
319
338
|
NSAlert *alert = [[NSAlert alloc] init];
|
|
320
|
-
alert.messageText = @"
|
|
321
|
-
alert.informativeText = @"
|
|
339
|
+
alert.messageText = @"需要「辅助功能」权限";
|
|
340
|
+
alert.informativeText = @"摸鱼列表要读取其他 App 窗口的位置和大小,需要「辅助功能」权限:\n\n系统设置 → 隐私与安全性 → 辅助功能 → 打开 DeepSeek Harness(以及「古法编程悬浮层」)的开关;改完后点「重试」。\n\n(只读取窗口尺寸/位置,不录屏、不采集画面。)";
|
|
322
341
|
[alert addButtonWithTitle:@"打开系统设置"];
|
|
323
342
|
[alert addButtonWithTitle:@"取消"];
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture"]];
|
|
343
|
+
if ([alert runModal] == NSAlertFirstButtonReturn) {
|
|
344
|
+
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"]];
|
|
327
345
|
}
|
|
328
346
|
}
|
|
329
347
|
|
|
330
348
|
- (void)chooseFollowWindow:(id)sender {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
349
|
+
if (!GufaTrustedForAX()) {
|
|
350
|
+
[self showPermissionGuide];
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
NSArray<NSDictionary *> *rows = GufaAXApps();
|
|
354
|
+
if (rows.count == 0) {
|
|
334
355
|
[self showPermissionGuide];
|
|
335
356
|
return;
|
|
336
357
|
}
|
|
337
|
-
self.windowCandidates = [rows mutableCopy];
|
|
338
|
-
|
|
339
358
|
NSAlert *alert = [[NSAlert alloc] init];
|
|
340
|
-
alert.messageText = @"
|
|
341
|
-
alert.informativeText = @"
|
|
359
|
+
alert.messageText = @"选择要吸附的 App";
|
|
360
|
+
alert.informativeText = @"悬浮层会切到该 App 的窗口并保持相同位置/大小,窗口移动时自动跟随。";
|
|
342
361
|
NSPopUpButton *popup = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 420, 26) pullsDown:NO];
|
|
343
|
-
for (NSDictionary *row in
|
|
344
|
-
[popup addItemWithTitle:row[@"label"]];
|
|
345
|
-
}
|
|
362
|
+
for (NSDictionary *row in rows) [popup addItemWithTitle:row[@"name"] ?: @"?"];
|
|
346
363
|
alert.accessoryView = popup;
|
|
347
|
-
[alert addButtonWithTitle:@"
|
|
364
|
+
[alert addButtonWithTitle:@"开始摸鱼"];
|
|
348
365
|
[alert addButtonWithTitle:@"取消"];
|
|
349
366
|
if ([alert runModal] != NSAlertFirstButtonReturn) return;
|
|
350
367
|
NSInteger index = popup.indexOfSelectedItem;
|
|
351
|
-
if (index < 0 || index >= (NSInteger)
|
|
352
|
-
NSDictionary *row =
|
|
353
|
-
self.
|
|
354
|
-
self.
|
|
355
|
-
self.
|
|
356
|
-
[
|
|
357
|
-
|
|
358
|
-
|
|
368
|
+
if (index < 0 || index >= (NSInteger)rows.count) return;
|
|
369
|
+
NSDictionary *row = rows[(NSUInteger)index];
|
|
370
|
+
self.followPid = (pid_t)[row[@"pid"] intValue];
|
|
371
|
+
self.followTitle = @"";
|
|
372
|
+
if (self.followWindowRef != NULL) { CFRelease(self.followWindowRef); self.followWindowRef = NULL; }
|
|
373
|
+
GufaActivateBundle(row[@"bundleId"]);
|
|
374
|
+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
|
375
|
+
self.followWindowRef = GufaCopyAppWindow(self.followPid);
|
|
376
|
+
if (self.followWindowRef == NULL) return;
|
|
377
|
+
self.following = YES;
|
|
378
|
+
[self startFollowTimer];
|
|
379
|
+
[self applyFollowFrame];
|
|
380
|
+
self.statusItem.button.title = @"古法·摸鱼";
|
|
381
|
+
});
|
|
359
382
|
}
|
|
360
383
|
|
|
361
384
|
- (void)stopFollow:(id)sender {
|
|
@@ -379,29 +402,25 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
|
|
|
379
402
|
self.followTimer = nil;
|
|
380
403
|
}
|
|
381
404
|
self.following = NO;
|
|
405
|
+
if (self.followWindowRef != NULL) {
|
|
406
|
+
CFRelease(self.followWindowRef);
|
|
407
|
+
self.followWindowRef = NULL;
|
|
408
|
+
}
|
|
382
409
|
}
|
|
383
410
|
|
|
384
|
-
/**
|
|
411
|
+
/** 读取被跟随窗口当前 bounds,把悬浮窗贴上去(主屏坐标换算;多屏后续再完善)。 */
|
|
385
412
|
- (void)applyFollowFrame {
|
|
386
|
-
if (!self.following
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
NSNumber *number = info[(id)kCGWindowNumber];
|
|
395
|
-
if (![number isKindOfClass:[NSNumber class]] || number.intValue != self.followWindowNumber) continue;
|
|
396
|
-
NSNumber *pid = info[(id)kCGWindowOwnerPID];
|
|
397
|
-
if (![pid isKindOfClass:[NSNumber class]] || pid.intValue != self.followOwnerPID) continue;
|
|
398
|
-
NSDictionary *bounds = info[(id)kCGWindowBounds];
|
|
399
|
-
if (bounds != nil && CGRectMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)bounds, &target)) found = YES;
|
|
400
|
-
break;
|
|
413
|
+
if (!self.following) return;
|
|
414
|
+
if (self.followWindowRef == NULL) {
|
|
415
|
+
self.followWindowRef = GufaCopyAXWindow(self.followPid, self.followTitle);
|
|
416
|
+
if (self.followWindowRef == NULL) {
|
|
417
|
+
[self stopFollowTimer];
|
|
418
|
+
self.statusItem.button.title = @"古法";
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
401
421
|
}
|
|
402
|
-
|
|
403
|
-
if (!
|
|
404
|
-
/* 目标窗口关闭:停止跟随,保持当前位置 */
|
|
422
|
+
NSRect target = NSZeroRect;
|
|
423
|
+
if (!GufaRectFromAX(self.followWindowRef, &target)) {
|
|
405
424
|
[self stopFollowTimer];
|
|
406
425
|
self.statusItem.button.title = @"古法";
|
|
407
426
|
return;
|
|
@@ -409,7 +428,6 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
|
|
|
409
428
|
CGFloat screenHeight = NSScreen.mainScreen.frame.size.height;
|
|
410
429
|
NSRect frame = NSMakeRect(target.origin.x, screenHeight - target.origin.y - target.size.height, target.size.width, target.size.height);
|
|
411
430
|
[self.window setFrame:frame display:YES animate:NO];
|
|
412
|
-
[self saveFrame];
|
|
413
431
|
}
|
|
414
432
|
|
|
415
433
|
/* 全局快捷键 ⌃⌥⌘G(四键,避免冲突):穿透后窗口收不到鼠标事件,靠它切回来(Carbon 热键不需要辅助功能权限) */
|
|
@@ -445,10 +463,7 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
|
|
|
445
463
|
}
|
|
446
464
|
|
|
447
465
|
- (void)restoreMainWindow {
|
|
448
|
-
|
|
449
|
-
for (NSRunningApplication *app in apps) {
|
|
450
|
-
[app activateWithOptions:NSApplicationActivateAllWindows];
|
|
451
|
-
}
|
|
466
|
+
GufaActivateBundle(@"com.deepseek.dsh");
|
|
452
467
|
}
|
|
453
468
|
|
|
454
469
|
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender { return YES; }
|
|
@@ -460,10 +475,7 @@ static void GufaInstallTermHandler(void) {
|
|
|
460
475
|
signal(SIGTERM, SIG_IGN);
|
|
461
476
|
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue());
|
|
462
477
|
dispatch_source_set_event_handler(source, ^{
|
|
463
|
-
|
|
464
|
-
for (NSRunningApplication *app in apps) {
|
|
465
|
-
[app activateWithOptions:NSApplicationActivateAllWindows];
|
|
466
|
-
}
|
|
478
|
+
GufaActivateBundle(@"com.deepseek.dsh");
|
|
467
479
|
[NSApp terminate:nil];
|
|
468
480
|
});
|
|
469
481
|
dispatch_resume(source);
|
|
@@ -477,60 +489,170 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
|
|
|
477
489
|
return noErr;
|
|
478
490
|
}
|
|
479
491
|
|
|
480
|
-
/* ──
|
|
492
|
+
/* ── 窗口枚举:用 Accessibility(辅助功能)API ─────────────────────
|
|
493
|
+
* 不用 CGWindowList(它需要「屏幕录制」,ad-hoc 签名的 helper 拿不到授权);
|
|
494
|
+
* 辅助功能权限按 responsible app(DeepSeek Harness)继承,用户只需在
|
|
495
|
+
* 「隐私与安全性 → 辅助功能」里勾选 DeepSeek Harness 即可。
|
|
496
|
+
*/
|
|
497
|
+
|
|
498
|
+
static BOOL GufaStringFromAX(AXUIElementRef element, CFStringRef attribute, NSString **out) {
|
|
499
|
+
CFTypeRef value = NULL;
|
|
500
|
+
if (AXUIElementCopyAttributeValue(element, attribute, &value) != kAXErrorSuccess || value == NULL) return NO;
|
|
501
|
+
if (CFGetTypeID(value) != CFStringGetTypeID()) { CFRelease(value); return NO; }
|
|
502
|
+
if (out != NULL) *out = (__bridge_transfer NSString *)value;
|
|
503
|
+
else CFRelease(value);
|
|
504
|
+
return YES;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
static BOOL GufaRectFromAX(AXUIElementRef element, NSRect *out) {
|
|
508
|
+
CFTypeRef posRef = NULL;
|
|
509
|
+
CFTypeRef sizeRef = NULL;
|
|
510
|
+
CGPoint pos = CGPointZero;
|
|
511
|
+
CGSize size = CGSizeZero;
|
|
512
|
+
BOOL ok = NO;
|
|
513
|
+
if (AXUIElementCopyAttributeValue(element, (CFStringRef)CFSTR("AXPosition"), &posRef) == kAXErrorSuccess && posRef != NULL) {
|
|
514
|
+
AXValueGetValue((AXValueRef)posRef, kAXValueCGPointType, &pos);
|
|
515
|
+
CFRelease(posRef);
|
|
516
|
+
}
|
|
517
|
+
if (AXUIElementCopyAttributeValue(element, (CFStringRef)CFSTR("AXSize"), &sizeRef) == kAXErrorSuccess && sizeRef != NULL) {
|
|
518
|
+
AXValueGetValue((AXValueRef)sizeRef, kAXValueCGSizeType, &size);
|
|
519
|
+
CFRelease(sizeRef);
|
|
520
|
+
ok = YES;
|
|
521
|
+
}
|
|
522
|
+
if (out != NULL) *out = NSMakeRect(pos.x, pos.y, size.width, size.height);
|
|
523
|
+
return ok;
|
|
524
|
+
}
|
|
481
525
|
|
|
482
|
-
|
|
526
|
+
/** 枚举所有可见窗口,返回 [{pid, owner, title, rect, icon}]。 */
|
|
527
|
+
static NSArray<NSDictionary *> *GufaAXWindows(void) {
|
|
483
528
|
NSMutableArray<NSDictionary *> *rows = [NSMutableArray array];
|
|
484
|
-
CFArrayRef list = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
|
|
485
|
-
if (list == NULL) return rows;
|
|
486
|
-
NSInteger count = CFArrayGetCount(list);
|
|
487
|
-
pid_t selfPid = NSProcessInfo.processInfo.processIdentifier;
|
|
488
529
|
NSMutableDictionary<NSNumber *, NSData *> *iconCache = [NSMutableDictionary dictionary];
|
|
489
|
-
for (
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
if (
|
|
493
|
-
|
|
494
|
-
if (
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
530
|
+
for (NSRunningApplication *app in [NSWorkspace sharedWorkspace].runningApplications) {
|
|
531
|
+
if (app.activationPolicy != NSApplicationActivationPolicyRegular) continue;
|
|
532
|
+
AXUIElementRef appElement = AXUIElementCreateApplication(app.processIdentifier);
|
|
533
|
+
if (appElement == NULL) continue;
|
|
534
|
+
CFTypeRef winsRef = NULL;
|
|
535
|
+
if (AXUIElementCopyAttributeValue(appElement, (CFStringRef)CFSTR("AXWindows"), &winsRef) == kAXErrorSuccess && winsRef != NULL) {
|
|
536
|
+
NSArray *wins = (__bridge_transfer NSArray *)winsRef;
|
|
537
|
+
for (id win in wins) {
|
|
538
|
+
AXUIElementRef window = (__bridge AXUIElementRef)win;
|
|
539
|
+
NSRect rect = NSZeroRect;
|
|
540
|
+
if (!GufaRectFromAX(window, &rect)) continue;
|
|
541
|
+
if (rect.size.width < 200 || rect.size.height < 140) continue;
|
|
542
|
+
NSString *title = @"";
|
|
543
|
+
GufaStringFromAX(window, (CFStringRef)CFSTR("AXTitle"), &title);
|
|
544
|
+
NSNumber *pidKey = @(app.processIdentifier);
|
|
545
|
+
NSData *iconData = iconCache[pidKey];
|
|
546
|
+
if (iconData == nil) {
|
|
547
|
+
iconData = NSData.data;
|
|
548
|
+
NSImage *icon = app.icon;
|
|
549
|
+
if (icon != nil) {
|
|
550
|
+
NSImage *resized = [[NSImage alloc] initWithSize:NSMakeSize(32, 32)];
|
|
551
|
+
[resized lockFocus];
|
|
552
|
+
[icon drawInRect:NSMakeRect(0, 0, 32, 32) fromRect:NSZeroRect operation:NSCompositingOperationCopy fraction:1.0 respectFlipped:YES hints:nil];
|
|
553
|
+
[resized unlockFocus];
|
|
554
|
+
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithData:resized.TIFFRepresentation];
|
|
555
|
+
NSData *png = [rep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
|
|
556
|
+
if (png != nil) iconData = png;
|
|
557
|
+
}
|
|
558
|
+
iconCache[pidKey] = iconData;
|
|
559
|
+
}
|
|
560
|
+
[rows addObject:@{
|
|
561
|
+
@"pid": pidKey,
|
|
562
|
+
@"owner": app.localizedName ?: @"",
|
|
563
|
+
@"title": title,
|
|
564
|
+
@"x": @(rect.origin.x),
|
|
565
|
+
@"y": @(rect.origin.y),
|
|
566
|
+
@"width": @(rect.size.width),
|
|
567
|
+
@"height": @(rect.size.height),
|
|
568
|
+
@"icon": [iconData base64EncodedStringWithOptions:0] ?: @"",
|
|
569
|
+
}];
|
|
516
570
|
}
|
|
517
|
-
|
|
571
|
+
}
|
|
572
|
+
CFRelease(appElement);
|
|
573
|
+
}
|
|
574
|
+
return rows;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/** 枚举运行中的普通 App(不依赖任何权限),按「最前 → 后台」排序。 */
|
|
578
|
+
static NSArray<NSDictionary *> *GufaAXApps(void) {
|
|
579
|
+
NSMutableArray<NSDictionary *> *rows = [NSMutableArray array];
|
|
580
|
+
NSRunningApplication *front = [NSWorkspace sharedWorkspace].frontmostApplication;
|
|
581
|
+
CGFloat iconScale = 32.0;
|
|
582
|
+
for (NSRunningApplication *app in [NSWorkspace sharedWorkspace].runningApplications) {
|
|
583
|
+
if (app.activationPolicy != NSApplicationActivationPolicyRegular) continue;
|
|
584
|
+
if (app.bundleIdentifier.length == 0) continue;
|
|
585
|
+
NSData *iconData = NSData.data;
|
|
586
|
+
NSImage *icon = app.icon;
|
|
587
|
+
if (icon != nil) {
|
|
588
|
+
NSImage *resized = [[NSImage alloc] initWithSize:NSMakeSize(iconScale, iconScale)];
|
|
589
|
+
[resized lockFocus];
|
|
590
|
+
[icon drawInRect:NSMakeRect(0, 0, iconScale, iconScale) fromRect:NSZeroRect operation:NSCompositingOperationCopy fraction:1.0 respectFlipped:YES hints:nil];
|
|
591
|
+
[resized unlockFocus];
|
|
592
|
+
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithData:resized.TIFFRepresentation];
|
|
593
|
+
NSData *png = [rep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
|
|
594
|
+
if (png != nil) iconData = png;
|
|
518
595
|
}
|
|
519
596
|
[rows addObject:@{
|
|
520
|
-
@"
|
|
521
|
-
@"
|
|
522
|
-
@"
|
|
523
|
-
@"
|
|
524
|
-
@"width": @(rect.size.width),
|
|
525
|
-
@"height": @(rect.size.height),
|
|
597
|
+
@"pid": @(app.processIdentifier),
|
|
598
|
+
@"bundleId": app.bundleIdentifier,
|
|
599
|
+
@"name": app.localizedName ?: @"",
|
|
600
|
+
@"frontmost": @(front != nil && front.processIdentifier == app.processIdentifier),
|
|
526
601
|
@"icon": [iconData base64EncodedStringWithOptions:0] ?: @"",
|
|
527
602
|
}];
|
|
528
603
|
}
|
|
529
|
-
|
|
604
|
+
[rows sortUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
|
|
605
|
+
BOOL fa = [a[@"frontmost"] boolValue];
|
|
606
|
+
BOOL fb = [b[@"frontmost"] boolValue];
|
|
607
|
+
if (fa != fb) return fa ? NSOrderedAscending : NSOrderedDescending;
|
|
608
|
+
return [a[@"name"] localizedCompare:b[@"name"]];
|
|
609
|
+
}];
|
|
530
610
|
return rows;
|
|
531
611
|
}
|
|
532
612
|
|
|
533
|
-
|
|
613
|
+
/** 找某个 App 的窗口:优先 focused/main,其次最大的窗口。 */
|
|
614
|
+
static AXUIElementRef GufaCopyAppWindow(pid_t pid) {
|
|
615
|
+
AXUIElementRef appElement = AXUIElementCreateApplication(pid);
|
|
616
|
+
if (appElement == NULL) return NULL;
|
|
617
|
+
CFTypeRef windowRef = NULL;
|
|
618
|
+
AXUIElementRef found = NULL;
|
|
619
|
+
if (AXUIElementCopyAttributeValue(appElement, (CFStringRef)CFSTR("AXFocusedWindow"), &windowRef) == kAXErrorSuccess && windowRef != NULL) {
|
|
620
|
+
found = (AXUIElementRef)CFRetain(windowRef);
|
|
621
|
+
CFRelease(windowRef);
|
|
622
|
+
}
|
|
623
|
+
if (found == NULL && AXUIElementCopyAttributeValue(appElement, (CFStringRef)CFSTR("AXMainWindow"), &windowRef) == kAXErrorSuccess && windowRef != NULL) {
|
|
624
|
+
found = (AXUIElementRef)CFRetain(windowRef);
|
|
625
|
+
CFRelease(windowRef);
|
|
626
|
+
}
|
|
627
|
+
if (found == NULL) {
|
|
628
|
+
CFTypeRef winsRef = NULL;
|
|
629
|
+
if (AXUIElementCopyAttributeValue(appElement, (CFStringRef)CFSTR("AXWindows"), &winsRef) == kAXErrorSuccess && winsRef != NULL) {
|
|
630
|
+
NSArray *wins = (__bridge NSArray *)winsRef;
|
|
631
|
+
CGFloat bestArea = 0;
|
|
632
|
+
for (id win in wins) {
|
|
633
|
+
AXUIElementRef window = (__bridge AXUIElementRef)win;
|
|
634
|
+
NSRect rect = NSZeroRect;
|
|
635
|
+
if (!GufaRectFromAX(window, &rect)) continue;
|
|
636
|
+
CGFloat area = rect.size.width * rect.size.height;
|
|
637
|
+
if (area > bestArea && rect.size.width >= 200 && rect.size.height >= 140) {
|
|
638
|
+
if (found != NULL) CFRelease(found);
|
|
639
|
+
found = (AXUIElementRef)CFRetain(window);
|
|
640
|
+
bestArea = area;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
CFRelease(winsRef);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
CFRelease(appElement);
|
|
647
|
+
return found;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
static BOOL GufaTrustedForAX(void) {
|
|
651
|
+
if (AXIsProcessTrusted()) return YES;
|
|
652
|
+
NSDictionary *options = @{(__bridge NSString *)kAXTrustedCheckOptionPrompt: @YES};
|
|
653
|
+
AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options);
|
|
654
|
+
return NO;
|
|
655
|
+
}
|
|
534
656
|
|
|
535
657
|
static void GufaEmitJSON(NSDictionary *out) {
|
|
536
658
|
NSData *json = [NSJSONSerialization dataWithJSONObject:out options:0 error:NULL];
|
|
@@ -543,33 +665,59 @@ static void GufaEmitJSON(NSDictionary *out) {
|
|
|
543
665
|
fputc('\n', stdout);
|
|
544
666
|
}
|
|
545
667
|
|
|
546
|
-
static
|
|
547
|
-
|
|
668
|
+
static int GufaListWindowsMode(void) {
|
|
669
|
+
NSArray<NSDictionary *> *rows = GufaAXApps();
|
|
670
|
+
GufaEmitJSON(@{@"ok": @YES, @"apps": rows, @"accessibility": @(GufaTrustedForAX())});
|
|
671
|
+
return 0;
|
|
548
672
|
}
|
|
549
673
|
|
|
550
|
-
/**
|
|
551
|
-
static
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
674
|
+
/** 激活目标 App(LaunchServices open -b,能切到它的 Space)。 */
|
|
675
|
+
static void GufaActivateBundle(NSString *bundleId) {
|
|
676
|
+
if (bundleId.length == 0) return;
|
|
677
|
+
NSTask *task = [[NSTask alloc] init];
|
|
678
|
+
task.executableURL = [NSURL fileURLWithPath:@"/usr/bin/open"];
|
|
679
|
+
task.arguments = @[@"-b", bundleId];
|
|
680
|
+
@try {
|
|
681
|
+
[task launchAndReturnError:NULL];
|
|
682
|
+
} @catch (NSException *exception) {
|
|
683
|
+
/* ignore */
|
|
559
684
|
}
|
|
560
|
-
return NO;
|
|
561
685
|
}
|
|
562
686
|
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
687
|
+
/** 按 pid + title 查找对应 AX window(follow 用)。 */
|
|
688
|
+
static AXUIElementRef GufaCopyAXWindow(pid_t pid, NSString *title) {
|
|
689
|
+
AXUIElementRef appElement = AXUIElementCreateApplication(pid);
|
|
690
|
+
if (appElement == NULL) return NULL;
|
|
691
|
+
CFTypeRef winsRef = NULL;
|
|
692
|
+
AXUIElementRef found = NULL;
|
|
693
|
+
if (AXUIElementCopyAttributeValue(appElement, (CFStringRef)CFSTR("AXWindows"), &winsRef) == kAXErrorSuccess && winsRef != NULL) {
|
|
694
|
+
NSArray *wins = (__bridge NSArray *)winsRef;
|
|
695
|
+
for (id win in wins) {
|
|
696
|
+
AXUIElementRef window = (__bridge AXUIElementRef)win;
|
|
697
|
+
NSRect rect = NSZeroRect;
|
|
698
|
+
if (!GufaRectFromAX(window, &rect)) continue;
|
|
699
|
+
NSString *candidateTitle = @"";
|
|
700
|
+
GufaStringFromAX(window, (CFStringRef)CFSTR("AXTitle"), &candidateTitle);
|
|
701
|
+
if (title.length > 0 && ![candidateTitle isEqualToString:title]) continue;
|
|
702
|
+
if (rect.size.width < 200 || rect.size.height < 140) continue;
|
|
703
|
+
found = (AXUIElementRef)CFRetain(window);
|
|
704
|
+
break;
|
|
705
|
+
}
|
|
706
|
+
if (found == NULL && title.length == 0) {
|
|
707
|
+
/* 没标题时退化为第一个够大的窗口 */
|
|
708
|
+
for (id win in wins) {
|
|
709
|
+
AXUIElementRef window = (__bridge AXUIElementRef)win;
|
|
710
|
+
NSRect rect = NSZeroRect;
|
|
711
|
+
if (!GufaRectFromAX(window, &rect)) continue;
|
|
712
|
+
if (rect.size.width < 200 || rect.size.height < 140) continue;
|
|
713
|
+
found = (AXUIElementRef)CFRetain(window);
|
|
714
|
+
break;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
CFRelease(winsRef);
|
|
570
718
|
}
|
|
571
|
-
|
|
572
|
-
return
|
|
719
|
+
CFRelease(appElement);
|
|
720
|
+
return found;
|
|
573
721
|
}
|
|
574
722
|
|
|
575
723
|
int main(int argc, const char *argv[]) {
|
|
Binary file
|
|
Binary file
|
package/host/dev.js
CHANGED
|
@@ -59,15 +59,22 @@ export function register(router, env) {
|
|
|
59
59
|
}
|
|
60
60
|
return null
|
|
61
61
|
}
|
|
62
|
-
const
|
|
62
|
+
const floatProcessPatterns = ['gufa-float-macos-', '古法编程悬浮层.app/Contents/MacOS/gufa-float']
|
|
63
63
|
|
|
64
64
|
const listFloatPids = async () => {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
const pids = new Set()
|
|
66
|
+
for (const pattern of floatProcessPatterns) {
|
|
67
|
+
try {
|
|
68
|
+
const { stdout } = await runFile('pgrep', ['-f', pattern], { timeout: 3000 })
|
|
69
|
+
for (const line of String(stdout ?? '').split('\n')) {
|
|
70
|
+
const pid = Number(line.trim())
|
|
71
|
+
if (Number.isFinite(pid) && pid > 0) pids.add(pid)
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
/* 没匹配到 */
|
|
75
|
+
}
|
|
70
76
|
}
|
|
77
|
+
return [...pids]
|
|
71
78
|
}
|
|
72
79
|
const floatRunningAsync = async () => (await listFloatPids()).length > 0
|
|
73
80
|
|
|
@@ -176,7 +183,7 @@ export function register(router, env) {
|
|
|
176
183
|
router.method('float.openPrivacy', async () => {
|
|
177
184
|
if (process.platform !== 'darwin') return { ok: false }
|
|
178
185
|
try {
|
|
179
|
-
spawn('open', ['x-apple.systempreferences:com.apple.preference.security?
|
|
186
|
+
spawn('open', ['x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility'], { detached: true, stdio: 'ignore' }).unref()
|
|
180
187
|
return { ok: true }
|
|
181
188
|
} catch {
|
|
182
189
|
return { ok: false }
|
|
@@ -187,9 +194,13 @@ export function register(router, env) {
|
|
|
187
194
|
if (process.platform !== 'darwin') throw new ApiError('unsupported', '摸鱼模式目前只支持 macOS', 400)
|
|
188
195
|
if (appBundlePath() === null) throw new ApiError('missing-helper', '悬浮层 App 未构建,请先运行 scripts/build-float-macos.sh', 500)
|
|
189
196
|
const follow = body !== null && typeof body === 'object' && body.follow !== null && typeof body.follow === 'object'
|
|
190
|
-
? {
|
|
197
|
+
? {
|
|
198
|
+
pid: Number(body.follow.pid) || 0,
|
|
199
|
+
bundleId: typeof body.follow.bundleId === 'string' ? body.follow.bundleId : '',
|
|
200
|
+
title: typeof body.follow.title === 'string' ? body.follow.title : '',
|
|
201
|
+
}
|
|
191
202
|
: null
|
|
192
|
-
if (follow !== null && follow.
|
|
203
|
+
if (follow !== null && follow.pid > 0) {
|
|
193
204
|
await killFloat()
|
|
194
205
|
} else if (await floatRunningAsync()) {
|
|
195
206
|
return { running: true, pid: (await listFloatPids())[0] ?? null }
|
|
@@ -201,12 +212,16 @@ export function register(router, env) {
|
|
|
201
212
|
}
|
|
202
213
|
const base = `${webServer.protocol}//127.0.0.1:${String(webServer.port)}`
|
|
203
214
|
const authUrl = connection.authenticatedUrl(`${base}/`)
|
|
204
|
-
const payload = JSON.stringify({ authUrl, appUrl: `${base}/`, follow: follow !== null && follow.
|
|
205
|
-
|
|
215
|
+
const payload = JSON.stringify({ authUrl, appUrl: `${base}/`, follow: follow !== null && follow.pid > 0 ? follow : undefined })
|
|
216
|
+
const binary = floatBinaryPath()
|
|
217
|
+
if (binary === null) throw new ApiError('missing-helper', '悬浮层程序未构建', 500)
|
|
218
|
+
const child = spawn(binary, [payload], { detached: true, stdio: 'ignore' })
|
|
219
|
+
child.unref()
|
|
220
|
+
floatState.child = child
|
|
221
|
+
floatState.startedAt = Date.now()
|
|
206
222
|
await new Promise((resolve) => setTimeout(resolve, 300))
|
|
207
223
|
const pids = await listFloatPids()
|
|
208
|
-
|
|
209
|
-
return { running: pids.length > 0, pid: pids[0] ?? null }
|
|
224
|
+
return { running: pids.length > 0 || child.exitCode === null, pid: child.pid ?? pids[0] ?? null }
|
|
210
225
|
})
|
|
211
226
|
|
|
212
227
|
router.method('float.stop', async () => {
|
package/package.json
CHANGED
package/src/client/00-core.js
CHANGED
|
@@ -1063,6 +1063,9 @@ html[data-gufa-float] [data-gufa-dock]{
|
|
|
1063
1063
|
}
|
|
1064
1064
|
html[data-gufa-float] [data-gufa-dock] [data-conversation-scroll],
|
|
1065
1065
|
html[data-gufa-float] [data-gufa-dock] [data-conversation-content]{background:transparent!important}
|
|
1066
|
+
/* 悬浮层启动瞬间:先隐藏经典界面,避免闪一下「另一个 DSH」 */
|
|
1067
|
+
html[data-gufa-booting] body{background:transparent!important}
|
|
1068
|
+
html[data-gufa-booting] #root{visibility:hidden!important}
|
|
1066
1069
|
/* 经典 DSH 界面在悬浮窗里整体隐藏(保留古法覆盖层与右侧停靠会话),否则半透明下会透出「另一个 DSH」 */
|
|
1067
1070
|
html[data-gufa-float] #root{visibility:hidden!important}
|
|
1068
1071
|
html[data-gufa-float] #root .gufa-overlay-root,
|
package/src/client/90-app.js
CHANGED
|
@@ -852,9 +852,11 @@ G.GufaApp = function GufaApp(props) {
|
|
|
852
852
|
setFloatPicker({ status: 'loading', windows: [], error: '' })
|
|
853
853
|
try {
|
|
854
854
|
const value = await G.api('float.windows')
|
|
855
|
-
if (value?.ok === true &&
|
|
856
|
-
setFloatPicker({ status: '
|
|
857
|
-
} else if (value?.ok ===
|
|
855
|
+
if (value?.ok === true && value?.accessibility !== true) {
|
|
856
|
+
setFloatPicker({ status: 'permission', windows: [], error: '' })
|
|
857
|
+
} else if (value?.ok === true && Array.isArray(value.apps)) {
|
|
858
|
+
setFloatPicker({ status: 'ready', windows: value.apps, error: '' })
|
|
859
|
+
} else if (value?.ok === false) {
|
|
858
860
|
setFloatPicker({ status: 'permission', windows: [], error: '' })
|
|
859
861
|
} else {
|
|
860
862
|
setFloatPicker({ status: 'error', windows: [], error: '没有读到可吸附的窗口' })
|
|
@@ -870,7 +872,11 @@ G.GufaApp = function GufaApp(props) {
|
|
|
870
872
|
setFloatBusy(true)
|
|
871
873
|
try {
|
|
872
874
|
const value = await G.api('float.start', {
|
|
873
|
-
follow: {
|
|
875
|
+
follow: {
|
|
876
|
+
pid: Number(target.pid) || 0,
|
|
877
|
+
bundleId: typeof target.bundleId === 'string' ? target.bundleId : '',
|
|
878
|
+
title: '',
|
|
879
|
+
},
|
|
874
880
|
})
|
|
875
881
|
setFloatRunning(value?.running === true)
|
|
876
882
|
setFloatPicker(null)
|
|
@@ -2165,8 +2171,8 @@ G.GufaApp = function GufaApp(props) {
|
|
|
2165
2171
|
? h(
|
|
2166
2172
|
'div',
|
|
2167
2173
|
{ className: 'gufa-float-permission' },
|
|
2168
|
-
h('div', { className: 'gufa-float-permission-title' }, '
|
|
2169
|
-
h('div', { className: 'gufa-hint' }, '
|
|
2174
|
+
h('div', { className: 'gufa-float-permission-title' }, '需要「辅助功能」权限'),
|
|
2175
|
+
h('div', { className: 'gufa-hint' }, '摸鱼模式需要「辅助功能」权限才能读取/跟随其他 App 的窗口位置和大小(只读窗口信息,不录屏)。请在「系统设置 → 隐私与安全性 → 辅助功能」里打开 **DeepSeek Harness** 的开关(改完可能需要重启一次 DSH),然后点「重试」。'),
|
|
2170
2176
|
h(
|
|
2171
2177
|
'div',
|
|
2172
2178
|
{ className: 'gufa-row', style: { gap: 8, marginTop: 12, flexWrap: 'wrap' } },
|
|
@@ -2199,10 +2205,10 @@ G.GufaApp = function GufaApp(props) {
|
|
|
2199
2205
|
h(
|
|
2200
2206
|
'span',
|
|
2201
2207
|
{ className: 'gufa-float-meta' },
|
|
2202
|
-
h('span', { className: 'gufa-float-owner' }, win.
|
|
2203
|
-
h('span', { className: 'gufa-float-title' }, win.
|
|
2208
|
+
h('span', { className: 'gufa-float-owner' }, win.name !== undefined && win.name !== '' ? win.name : '未知 App'),
|
|
2209
|
+
h('span', { className: 'gufa-float-title' }, win.bundleId !== undefined ? win.bundleId : ''),
|
|
2204
2210
|
),
|
|
2205
|
-
h('span', { className: 'gufa-float-size' },
|
|
2211
|
+
win.frontmost === true ? h('span', { className: 'gufa-float-size' }, '当前') : null,
|
|
2206
2212
|
),
|
|
2207
2213
|
),
|
|
2208
2214
|
),
|
package/src/client/99-main.js
CHANGED
|
@@ -218,6 +218,41 @@ const GUFA_PLUGIN = {
|
|
|
218
218
|
apply(ctx) {
|
|
219
219
|
G.ctx = ctx
|
|
220
220
|
|
|
221
|
+
/* ── 悬浮层模式(?gufa-float=1 或 helper 注入的 localStorage 标记)──
|
|
222
|
+
* 尽早打标记并隐藏经典 DSH 界面,避免先闪出一个「另一个 DSH」;
|
|
223
|
+
* 同时重试进入 IDE 模式,直到覆盖层挂载完成。 */
|
|
224
|
+
try {
|
|
225
|
+
let floatMode = false
|
|
226
|
+
try {
|
|
227
|
+
floatMode = /[?&]gufa-float=1/.test(String(location.search || '')) || localStorage.getItem('gufa-code:float') === '1'
|
|
228
|
+
} catch {
|
|
229
|
+
floatMode = false
|
|
230
|
+
}
|
|
231
|
+
if (floatMode) {
|
|
232
|
+
document.documentElement.setAttribute('data-gufa-float', '1')
|
|
233
|
+
document.documentElement.setAttribute('data-gufa-booting', '1')
|
|
234
|
+
let attempts = 0
|
|
235
|
+
const timer = window.setInterval(() => {
|
|
236
|
+
attempts += 1
|
|
237
|
+
try {
|
|
238
|
+
if (typeof G.mode?.get === 'function' && G.mode.get() !== true) G.mode.set(true)
|
|
239
|
+
} catch {
|
|
240
|
+
/* ignore */
|
|
241
|
+
}
|
|
242
|
+
if (document.querySelector('.gufa-overlay-root') !== null || attempts > 40) {
|
|
243
|
+
document.documentElement.removeAttribute('data-gufa-booting')
|
|
244
|
+
window.clearInterval(timer)
|
|
245
|
+
}
|
|
246
|
+
}, 250)
|
|
247
|
+
ctx.effect(() => () => {
|
|
248
|
+
window.clearInterval(timer)
|
|
249
|
+
document.documentElement.removeAttribute('data-gufa-booting')
|
|
250
|
+
}, 'gufa-code: float boot')
|
|
251
|
+
}
|
|
252
|
+
} catch (error) {
|
|
253
|
+
console.error('[gufa-code] 悬浮模式初始化失败', error)
|
|
254
|
+
}
|
|
255
|
+
|
|
221
256
|
/* HMR 重载时如果仍处于 IDE 模式,重新打上标记(卸载清理会移除它) */
|
|
222
257
|
try {
|
|
223
258
|
if (typeof G.mode?.get === 'function' && G.mode.get() === true) document.documentElement.setAttribute('data-gufa-ide', '')
|