@sonicloud/dsh-sonicloud-device 0.1.3 → 0.1.5

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.
Files changed (74) hide show
  1. package/CHANGELOG.md +37 -1
  2. package/CHANGELOG.zh.md +37 -1
  3. package/README.md +18 -14
  4. package/README.zh.md +9 -5
  5. package/dist/build.json +1 -1
  6. package/dist/client.js +2112 -120
  7. package/docs/contributing/development.md +7 -5
  8. package/docs/specs/ci-cd.md +5 -4
  9. package/docs/specs/mouse-settings.md +1 -1
  10. package/docs/specs/multilingual.md +98 -0
  11. package/docs/specs/smart-mouse-plugin.md +6 -3
  12. package/locale/de.json +7 -0
  13. package/locale/en.json +7 -0
  14. package/locale/es.json +7 -0
  15. package/locale/fr.json +7 -0
  16. package/locale/ja.json +7 -0
  17. package/locale/pt.json +7 -0
  18. package/locale/ru.json +7 -0
  19. package/locale/zh.json +7 -0
  20. package/native/desktop/VOICE_SOURCE.json +2 -2
  21. package/native/desktop/VoicePanel.cs +70 -33
  22. package/native/desktop/VoicePanel.swift +58 -21
  23. package/native/desktop/darwin-arm64/Sonicloud Voice.app/Contents/MacOS/sonicloud-voice +0 -0
  24. package/native/screenshot/README.md +9 -4
  25. package/native/screenshot/SOURCE.json +48 -17
  26. package/native/screenshot/darwin-arm64/screentool-macos-1.2.2.zip +0 -0
  27. package/native/screenshot/helper-i18n/de.json +85 -0
  28. package/native/screenshot/helper-i18n/es.json +85 -0
  29. package/native/screenshot/helper-i18n/fr.json +85 -0
  30. package/native/screenshot/patches/{hide-cloud-actions.patch → macos/hide-cloud-actions.patch} +84 -76
  31. package/native/screenshot/patches/macos/mac-clang-compat.patch +14 -0
  32. package/native/screenshot/patches/macos/mac-color-panel.patch +40 -0
  33. package/native/screenshot/patches/macos/mac-permissions.patch +549 -0
  34. package/native/screenshot/patches/macos/mac-ui-focus.patch +658 -0
  35. package/native/screenshot/patches/macos/upstream-files.json +77 -0
  36. package/native/screenshot/patches/upstream-files.json +12 -8
  37. package/native/screenshot/win32-x64/i18n/de.json +85 -0
  38. package/native/screenshot/win32-x64/i18n/es.json +85 -0
  39. package/native/screenshot/win32-x64/i18n/fr.json +85 -0
  40. package/native/screenshot/win32-x64/screenhelper.exe +0 -0
  41. package/package.json +5 -3
  42. package/src/asr/auth.mjs +9 -8
  43. package/src/asr/config.mjs +1 -1
  44. package/src/asr/stream.mjs +25 -24
  45. package/src/desktop/presentation.mjs +18 -12
  46. package/src/desktop/screenshot-shortcut-worker.mjs +188 -0
  47. package/src/desktop/screenshot-shortcut.mjs +266 -0
  48. package/src/desktop/screenshot-worker.mjs +49 -32
  49. package/src/desktop/screenshot.mjs +46 -26
  50. package/src/devices/actions.mjs +7 -6
  51. package/src/devices/config.mjs +2 -1
  52. package/src/devices/diagnostics.mjs +4 -3
  53. package/src/devices/manager.mjs +77 -43
  54. package/src/devices/native-worker.mjs +4 -3
  55. package/src/devices/native.mjs +9 -8
  56. package/src/devices/queue.mjs +4 -3
  57. package/src/devices/recognition.mjs +5 -4
  58. package/src/devices/store.mjs +3 -2
  59. package/src/devices/translate.mjs +11 -10
  60. package/src/host/ai-conversation.mjs +8 -7
  61. package/src/host/index.mjs +13 -3
  62. package/src/host/locale.mjs +44 -0
  63. package/src/shared/devices.mjs +13 -12
  64. package/src/shared/i18n.mjs +47 -0
  65. package/src/shared/locales/de.json +181 -0
  66. package/src/shared/locales/en.json +181 -0
  67. package/src/shared/locales/es.json +181 -0
  68. package/src/shared/locales/fr.json +181 -0
  69. package/src/shared/locales/ja.json +181 -0
  70. package/src/shared/locales/pt.json +181 -0
  71. package/src/shared/locales/ru.json +181 -0
  72. package/src/shared/locales/zh.json +181 -0
  73. package/src/shared/remote.mjs +7 -1
  74. package/src/shared/screenshot-shortcut.mjs +50 -0
@@ -0,0 +1,549 @@
1
+ diff --git a/CMakeLists.txt b/CMakeLists.txt
2
+ --- a/CMakeLists.txt
3
+ +++ b/CMakeLists.txt
4
+ @@ -259,6 +259,11 @@ if(SCREENTOOL_BUILD_TESTS)
5
+ add_executable(test_helper_config tests/test_helper_config.cpp)
6
+ target_link_libraries(test_helper_config PRIVATE ${QT_LINK}::Test screentool_core)
7
+ add_test(NAME helper_config COMMAND test_helper_config)
8
+ + if(APPLE)
9
+ + add_executable(test_mac_permissions tests/test_mac_permissions.cpp)
10
+ + target_link_libraries(test_mac_permissions PRIVATE ${QT_LINK}::Test ${QT_LINK}::Widgets screentool_ipc)
11
+ + add_test(NAME mac_permissions COMMAND test_mac_permissions)
12
+ + endif()
13
+ if(TARGET PixPinScrollCapture)
14
+ add_executable(test_cloud_actions tests/test_cloud_actions.cpp)
15
+ target_link_libraries(test_cloud_actions PRIVATE ${QT_LINK}::Test ${QT_LINK}::Widgets screentool_core)
16
+ diff --git a/core/screen_label.cpp b/core/screen_label.cpp
17
+ --- a/core/screen_label.cpp
18
+ +++ b/core/screen_label.cpp
19
+ @@ -333,7 +333,7 @@ void ScreenLabel::changeEvent(QEvent *event)
20
+ QLabel::changeEvent(event);
21
+ }
22
+
23
+ -void ScreenLabel::stopScreenshot()
24
+ +void ScreenLabel::stopScreenshot(const QString& reason)
25
+ {
26
+ if (capturing_)
27
+ {
28
+ @@ -354,7 +354,7 @@ void ScreenLabel::stopScreenshot()
29
+ #endif
30
+ close();
31
+ // [HELPER-IPC] 宿主 cancel_capture 路径
32
+ - emit sessionFinished(QStringLiteral("canceled"));
33
+ + emit sessionFinished(reason);
34
+ }
35
+ // [FOLLOW-ANCHOR] 会话结束停跟屏(tick 亦有自守卫,此处显式停更直观)
36
+ m_followTimer->stop();
37
+ @@ -1580,61 +1580,14 @@ void ScreenLabel::onScrollCaptureTriggered()
38
+ return;
39
+ }
40
+ #ifdef Q_OS_MAC
41
+ - // [MAC-A11Y] 双权限自检(每进程各提醒一次,授权后不再出现;弹窗在
42
+ - // hide()/PixPin UI 之前完成,不进长截图画面):
43
+ - // 1) 辅助功能——PixPin 工具条"自动滚动"注入的合成滚轮事件会被未授权
44
+ - // 进程静默丢弃(2026-09-17 实证:未授权连发滚轮无效;真实鼠标手动
45
+ - // 滚动不受影响);
46
+ - // 2) 屏幕录制——PixPin 拼接帧走 CGDisplay 旧采集 API,无此权限只回
47
+ - // 壁纸:成图黑底(2026-09-20 实证亮度 37-48 vs 186 双态)。
48
+ - qInfo() << "[MAC-A11Y] AXIsProcessTrusted =" << AXIsProcessTrusted()
49
+ - << " ScreenCapturePreflight =" << CGPreflightScreenCaptureAccess();
50
+ - // [MAC-A11Y] 引导文案中的 app 路径必须给真实路径:helper 的部署形态
51
+ - // 有两种(独立 deps/screenTool/ 与 electron 包内 Resources/deps/
52
+ - // screenTool/),写死任一都会误导另一种形态的用户(2026-09-20 实证:
53
+ - // electron 用户照 deps 路径找不到 app)。从自身可执行文件位置回推
54
+ - // bundle 根展示。
55
+ - const QString helperAppPath = [] {
56
+ - QDir dir(QCoreApplication::applicationDirPath());
57
+ - dir.cdUp(); dir.cdUp(); // MacOS/ → Contents/ → screenhelper.app/
58
+ - return dir.absolutePath();
59
+ - }();
60
+ - static bool s_a11yPrompted = false;
61
+ - if (!s_a11yPrompted && !AXIsProcessTrusted())
62
+ + qInfo() << "[MAC-PERMISSIONS] a11y =" << AXIsProcessTrusted()
63
+ + << " screen =" << CGPreflightScreenCaptureAccess();
64
+ + // Optional Accessibility must not interrupt manual scrolling. No dialogs here.
65
+ + if (!CGPreflightScreenCaptureAccess())
66
+ {
67
+ - s_a11yPrompted = true;
68
+ - if (QMessageBox::question(nullptr, tr("Accessibility permission"),
69
+ - // [I18N-2] 源串改英文(中文源串会让其余语言显示中文;zh 目录
70
+ - // 译文即原中文文案,见 res/i18n/ts/screentool_zh.ts)
71
+ - tr("\"Auto scroll\" in scroll screenshots requires the Accessibility "
72
+ - "permission (manual scrolling is not affected).\n\n"
73
+ - "Open System Settings → Privacy & Security → Accessibility, "
74
+ - "click \"+\" to add and check screenhelper.app (path: %1; press "
75
+ - "Cmd+Shift+G in the dialog and paste the path above to locate "
76
+ - "it). It takes effect immediately after adding.\n\n"
77
+ - "Open System Settings now?").arg(helperAppPath)) == QMessageBox::Yes)
78
+ - {
79
+ - QDesktopServices::openUrl(QUrl(QStringLiteral(
80
+ - "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")));
81
+ - }
82
+ - }
83
+ - static bool s_srPrompted = false;
84
+ - if (!s_srPrompted && !CGPreflightScreenCaptureAccess())
85
+ - {
86
+ - s_srPrompted = true;
87
+ - if (QMessageBox::question(nullptr, tr("Screen recording permission"),
88
+ - // [I18N-2] 同上:源串英文化,zh 目录携带原中文文案
89
+ - tr("Scroll screenshots require the Screen Recording permission, "
90
+ - "otherwise the captured image will have a black background.\n\n"
91
+ - "Open System Settings → Privacy & Security → Screen Recording, "
92
+ - "click \"+\" to add and check screenhelper.app (path: %1; press "
93
+ - "Cmd+Shift+G in the dialog and paste the path above to locate "
94
+ - "it). Restart screenhelper after adding for it to take effect.\n\n"
95
+ - "Open System Settings now?").arg(helperAppPath)) == QMessageBox::Yes)
96
+ - {
97
+ - QDesktopServices::openUrl(QUrl(QStringLiteral(
98
+ - "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture")));
99
+ - }
100
+ + stopScreenshot(QStringLiteral("permission_required"));
101
+ + emit screenPermissionRequired();
102
+ + return;
103
+ }
104
+ #endif
105
+ m_scrollCaptureParams.captureRect = cutRect.toRect();
106
+ diff --git a/core/screen_label.h b/core/screen_label.h
107
+ --- a/core/screen_label.h
108
+ +++ b/core/screen_label.h
109
+ @@ -109,7 +109,7 @@ public:
110
+
111
+ void showScreen(); // interface
112
+ void updateUiLanguage();
113
+ - void stopScreenshot();
114
+ + void stopScreenshot(const QString& reason = QStringLiteral("canceled"));
115
+ // [PIN-EDIT M2] 贴图编辑模式:入口/查询/取消(网关冲突规则调用 cancel)
116
+ // [R4-1] 原窗口编辑:编辑器作为子部件嵌入宿主贴图窗口(对齐参考 EnterEdit
117
+ // 的"贴图窗口自身变画布"),覆盖其内容区,全程只有一个窗口
118
+ @@ -266,6 +266,7 @@ private slots:
119
+
120
+
121
+ signals:
122
+ + void screenPermissionRequired();
123
+ void signalCutMap(const QPixmap); //获取截图
124
+ void signalOCRMap(const QPixmap, const QString& source); // [PIN-EDIT M2] 增 source(region/scroll/pin)
125
+ void captureFinished();
126
+ diff --git a/ipc/helper_gateway.cpp b/ipc/helper_gateway.cpp
127
+ --- a/ipc/helper_gateway.cpp
128
+ +++ b/ipc/helper_gateway.cpp
129
+ @@ -28,11 +28,14 @@
130
+ #include <QPointer>
131
+ #include <QScreen>
132
+ #include <QTimer>
133
+ +#include <QSignalBlocker>
134
+ #ifdef Q_OS_MAC
135
+ // [MAC-PERM-GUIDE] state_report 权限字段:AXIsProcessTrusted/
136
+ // CGPreflightScreenCaptureAccess(ApplicationServices)
137
+ #include <ApplicationServices/ApplicationServices.h>
138
+ #include <QDir>
139
+ +#include <QDesktopServices>
140
+ +#include <QUrl>
141
+ #endif
142
+
143
+ #ifndef SCREENHELPER_VERSION
144
+ @@ -209,6 +212,9 @@ void HelperGateway::onFrame(const QJsonObject& header, const QByteArray& payload
145
+ "pin_edit", "i18n"};
146
+ #ifdef ENABLE_PIXPIN_SCROLL_CAPTURE
147
+ caps.append(QStringLiteral("scroll_capture"));
148
+ +#endif
149
+ +#ifdef Q_OS_MAC
150
+ + caps.append(QStringLiteral("permission_preflight"));
151
+ #endif
152
+ // [I18N-1] lang=当前生效目录码(宿主展示/对账用;语言清单走 list_languages)
153
+ sendEvent(QJsonObject{{"type", "hello_ack"},
154
+ @@ -222,6 +228,11 @@ void HelperGateway::onFrame(const QJsonObject& header, const QByteArray& payload
155
+ }
156
+ else if (type == QStringLiteral("set_config"))
157
+ {
158
+ + if (header.contains(QStringLiteral("permission_request")))
159
+ + {
160
+ + requestPermission(header.value(QStringLiteral("permission_request")).toString());
161
+ + return;
162
+ + }
163
+ const QString oldLang = HelperConfig::instance().lang();
164
+ const QString oldTheme = HelperConfig::instance().uiTheme();
165
+ const QString oldIcon = HelperConfig::instance().uiIcon();
166
+ @@ -294,17 +305,25 @@ void HelperGateway::onFrame(const QJsonObject& header, const QByteArray& payload
167
+ }
168
+ else if (type == QStringLiteral("start_capture"))
169
+ {
170
+ - // [PIN-EDIT M2] 冲突规则:编辑会话被截图抢占 → 先取消(贴图恢复原样)
171
+ - if (m_pinEditing)
172
+ - cancelPinEdit();
173
+ - ensureLabel();
174
+ - if (m_capturing)
175
+ - m_label->stopScreenshot(); // 重复下发先复位会话
176
+ - m_label->showScreen();
177
+ - m_capturing = true;
178
+ - // [FOLLOW-ANCHOR] 会话启动即报锚定屏;此后蒙层跟屏重锚(光标换屏、
179
+ - // 首次按下前)由 screenReanchored → sendSessionStarted 补发刷新参数
180
+ - sendSessionStarted();
181
+ + // Check before creating a capture window or reading pixels.
182
+ + if (!screenPermissionGranted())
183
+ + {
184
+ + if (m_label && m_capturing)
185
+ + {
186
+ + const QSignalBlocker blocker(m_label);
187
+ + m_label->stopScreenshot(QStringLiteral("permission_required"));
188
+ + }
189
+ + m_capturing = false;
190
+ + sendStateReport();
191
+ + // End the pending request so the client watchdog does not relaunch us.
192
+ + sendEvent(QJsonObject{{"type", "session_ended"},
193
+ + {"reason", QStringLiteral("permission_required")}});
194
+ + sendEvent(QJsonObject{{"type", "error"},
195
+ + {"code", QStringLiteral("permission_screen_required")},
196
+ + {"message", QStringLiteral("Screen Recording permission is required before capture")}});
197
+ + return;
198
+ + }
199
+ + beginCapture();
200
+ }
201
+ else if (type == QStringLiteral("cancel_capture"))
202
+ {
203
+ @@ -313,28 +332,7 @@ void HelperGateway::onFrame(const QJsonObject& header, const QByteArray& payload
204
+ }
205
+ else if (type == QStringLiteral("query_state"))
206
+ {
207
+ - // [MAC-PERM-GUIDE] mac 下附加权限实测字段:宿主启动引导用(helper 是
208
+ - // 屏幕录制/辅助功能的权限主体,宿主自身无法代查)。旧宿主忽略未知
209
+ - // 字段;新宿主按 helper_ver>=1.2.2 判定字段可用。win 不附加(宿主
210
+ - // 侧无引导场景)。
211
+ - QJsonObject rep{{"type", "state_report"},
212
+ - {"pins_alive", m_pinMap.size()},
213
+ - {"capturing", m_capturing},
214
+ - {"helper_ver", QLatin1String(SCREENHELPER_VERSION)},
215
+ - {"ui_theme", ThemeManager::instance().currentName()}};
216
+ -#ifdef Q_OS_MAC
217
+ - {
218
+ - // 仅 bundle 形态可推出有效路径;裸二进制/异常部署(如直接跑
219
+ - // build 产物)上跳两级会得到误导性目录——校验以 .app 结尾,
220
+ - // 不匹配则不报(宿主文案回退为"见截图弹窗提示")
221
+ - QDir d(QCoreApplication::applicationDirPath());
222
+ - if (d.cdUp() && d.cdUp() && d.dirName().endsWith(QLatin1String(".app")))
223
+ - rep.insert(QStringLiteral("helper_app"), d.absolutePath());
224
+ - }
225
+ - rep.insert(QStringLiteral("perm_a11y"), AXIsProcessTrusted());
226
+ - rep.insert(QStringLiteral("perm_screen"), CGPreflightScreenCaptureAccess());
227
+ -#endif
228
+ - sendEvent(rep);
229
+ + sendStateReport();
230
+ }
231
+ else if (type == QStringLiteral("shutdown"))
232
+ {
233
+ @@ -357,6 +355,128 @@ void HelperGateway::onFrame(const QJsonObject& header, const QByteArray& payload
234
+ }
235
+ }
236
+
237
+ +bool HelperGateway::screenPermissionGranted() const
238
+ +{
239
+ +#ifdef Q_OS_MAC
240
+ + return CGPreflightScreenCaptureAccess();
241
+ +#else
242
+ + return true;
243
+ +#endif
244
+ +}
245
+ +
246
+ +bool HelperGateway::accessibilityPermissionGranted() const
247
+ +{
248
+ +#ifdef Q_OS_MAC
249
+ + return AXIsProcessTrusted();
250
+ +#else
251
+ + return true;
252
+ +#endif
253
+ +}
254
+ +
255
+ +void HelperGateway::promptScreenPermission()
256
+ +{
257
+ +#ifdef Q_OS_MAC
258
+ + CGRequestScreenCaptureAccess();
259
+ + QDesktopServices::openUrl(QUrl(QStringLiteral(
260
+ + "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture")));
261
+ +#endif
262
+ +}
263
+ +
264
+ +void HelperGateway::promptAccessibilityPermission()
265
+ +{
266
+ +#ifdef Q_OS_MAC
267
+ + const void* keys[] = { kAXTrustedCheckOptionPrompt };
268
+ + const void* values[] = { kCFBooleanTrue };
269
+ + CFDictionaryRef options = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 1,
270
+ + &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
271
+ + AXIsProcessTrustedWithOptions(options);
272
+ + CFRelease(options);
273
+ + QDesktopServices::openUrl(QUrl(QStringLiteral(
274
+ + "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")));
275
+ +#endif
276
+ +}
277
+ +
278
+ +void HelperGateway::requestPermission(const QString& permission)
279
+ +{
280
+ + if (permission != QLatin1String("screen") && permission != QLatin1String("accessibility"))
281
+ + {
282
+ + sendEvent(QJsonObject{{"type", "error"},
283
+ + {"code", QStringLiteral("permission_request_invalid")},
284
+ + {"message", QStringLiteral("Unknown permission request")}});
285
+ + return;
286
+ + }
287
+ + if (m_capturing || m_pinEditing)
288
+ + {
289
+ + sendStateReport();
290
+ + sendEvent(QJsonObject{{"type", "error"},
291
+ + {"code", QStringLiteral("permission_request_busy")},
292
+ + {"message", QStringLiteral("Finish capture or pin editing before permission setup")}});
293
+ + return;
294
+ + }
295
+ +#ifdef Q_OS_MAC
296
+ + QTimer::singleShot(0, this, [this, permission] {
297
+ + // A capture can start between receiving the command and this callback.
298
+ + if (m_capturing || m_pinEditing)
299
+ + {
300
+ + sendStateReport();
301
+ + sendEvent(QJsonObject{{"type", "error"},
302
+ + {"code", QStringLiteral("permission_request_busy")},
303
+ + {"message", QStringLiteral("Finish capture or pin editing before permission setup")}});
304
+ + return;
305
+ + }
306
+ + if (permission == QLatin1String("screen") && !screenPermissionGranted())
307
+ + promptScreenPermission();
308
+ + else if (permission == QLatin1String("accessibility") && !accessibilityPermissionGranted())
309
+ + promptAccessibilityPermission();
310
+ + // Requesting is asynchronous; this reports status, never a grant promise.
311
+ + sendStateReport();
312
+ + });
313
+ +#else
314
+ + sendEvent(QJsonObject{{"type", "error"},
315
+ + {"code", QStringLiteral("permission_request_unsupported")},
316
+ + {"message", QStringLiteral("Permission setup is only available on macOS")}});
317
+ +#endif
318
+ +}
319
+ +
320
+ +void HelperGateway::sendStateReport()
321
+ +{
322
+ + // [MAC-PERM-GUIDE] mac 下附加权限实测字段:宿主启动引导用(helper 是
323
+ + // 屏幕录制/辅助功能的权限主体,宿主自身无法代查)。旧宿主忽略未知
324
+ + // 字段;新宿主按 helper_ver>=1.2.2 判定字段可用。win 不附加(宿主
325
+ + // 侧无引导场景)。
326
+ + QJsonObject rep{{"type", "state_report"},
327
+ + {"pins_alive", m_pinMap.size()},
328
+ + {"capturing", m_capturing},
329
+ + {"pin_editing", m_pinEditing},
330
+ + {"helper_ver", QLatin1String(SCREENHELPER_VERSION)},
331
+ + {"ui_theme", ThemeManager::instance().currentName()}};
332
+ +#ifdef Q_OS_MAC
333
+ + {
334
+ + // 仅 bundle 形态可推出有效路径;裸二进制/异常部署(如直接跑
335
+ + // build 产物)上跳两级会得到误导性目录——校验以 .app 结尾,
336
+ + // 不匹配则不报(宿主文案回退为"见截图弹窗提示")
337
+ + QDir d(QCoreApplication::applicationDirPath());
338
+ + if (d.cdUp() && d.cdUp() && d.dirName().endsWith(QLatin1String(".app")))
339
+ + rep.insert(QStringLiteral("helper_app"), d.absolutePath());
340
+ + }
341
+ + rep.insert(QStringLiteral("perm_a11y"), accessibilityPermissionGranted());
342
+ + rep.insert(QStringLiteral("perm_screen"), screenPermissionGranted());
343
+ +#endif
344
+ + sendEvent(rep);
345
+ +}
346
+ +
347
+ +void HelperGateway::beginCapture()
348
+ +{
349
+ + if (m_pinEditing)
350
+ + cancelPinEdit();
351
+ + ensureLabel();
352
+ + if (m_capturing)
353
+ + m_label->stopScreenshot();
354
+ + m_label->showScreen();
355
+ + m_capturing = true;
356
+ + sendSessionStarted();
357
+ +}
358
+ +
359
+ void HelperGateway::ensureLabel()
360
+ {
361
+ if (m_label)
362
+ @@ -366,6 +486,12 @@ void HelperGateway::ensureLabel()
363
+ connect(m_label, &ScreenLabel::signalOCRMap, this, &HelperGateway::onOcrMap);
364
+ connect(m_label, &ScreenLabel::convertImage, this, &HelperGateway::onConvertImage);
365
+ connect(m_label, &ScreenLabel::sessionFinished, this, &HelperGateway::onSessionFinished);
366
+ + connect(m_label, &ScreenLabel::screenPermissionRequired, this, [this] {
367
+ + sendStateReport();
368
+ + sendEvent(QJsonObject{{"type", "error"},
369
+ + {"code", QStringLiteral("permission_screen_required")},
370
+ + {"message", QStringLiteral("Screen Recording permission is required before capture")}});
371
+ + });
372
+ connect(m_label, &ScreenLabel::pinCreated, this, &HelperGateway::onPinCreated);
373
+ // [FOLLOW-ANCHOR] 跟屏重锚 → 补发 session_started 刷新锚定屏参数
374
+ connect(m_label, &ScreenLabel::screenReanchored, this, [this]() {
375
+ diff --git a/ipc/helper_gateway.h b/ipc/helper_gateway.h
376
+ --- a/ipc/helper_gateway.h
377
+ +++ b/ipc/helper_gateway.h
378
+ @@ -28,6 +28,7 @@ class QImage;
379
+ // 中继宿主),relay 应答经 ocr_result 命令回流 deliverResult
380
+ class HelperGateway : public QObject
381
+ {
382
+ + friend class TestMacPermissions;
383
+ Q_OBJECT
384
+ public:
385
+ explicit HelperGateway(QObject* parent = nullptr);
386
+ @@ -60,9 +61,18 @@ private slots:
387
+ void onPinEdited(quintptr id, const PinDocument& doc);
388
+ void onPinEditCancelled(quintptr id);
389
+
390
+ +protected:
391
+ + virtual bool screenPermissionGranted() const;
392
+ + virtual bool accessibilityPermissionGranted() const;
393
+ + virtual void promptScreenPermission();
394
+ + virtual void promptAccessibilityPermission();
395
+ + virtual void beginCapture();
396
+ + virtual void sendEvent(const QJsonObject& header, const QByteArray& payload = QByteArray());
397
+ +
398
+ private:
399
+ void ensureLabel();
400
+ - void sendEvent(const QJsonObject& header, const QByteArray& payload = QByteArray());
401
+ + void sendStateReport();
402
+ + void requestPermission(const QString& permission);
403
+ // [FOLLOW-ANCHOR] 上报截图会话锚定屏参数(start_capture 与跟屏重锚共用)
404
+ void sendSessionStarted();
405
+ // [I18N] 语言目录清单上报(list_languages 应答 / i18n_dir 重载刷新共用)
406
+ diff --git a/tests/test_mac_permissions.cpp b/tests/test_mac_permissions.cpp
407
+ new file mode 100644
408
+ --- /dev/null
409
+ +++ b/tests/test_mac_permissions.cpp
410
+ @@ -0,0 +1,139 @@
411
+ +#include <QtTest/QtTest>
412
+ +#include <QJsonArray>
413
+ +#include <QJsonObject>
414
+ +#include "../ipc/helper_gateway.h"
415
+ +
416
+ +class PermissionGateway : public HelperGateway
417
+ +{
418
+ +public:
419
+ + bool screen = false;
420
+ + bool accessibility = false;
421
+ + int screenRequests = 0;
422
+ + int accessibilityRequests = 0;
423
+ + int captureStarts = 0;
424
+ + QList<QJsonObject> events;
425
+ + void dispatch(const QJsonObject& value)
426
+ + {
427
+ + QVERIFY(QMetaObject::invokeMethod(this, "onFrame", Qt::DirectConnection,
428
+ + Q_ARG(QJsonObject, value), Q_ARG(QByteArray, QByteArray())));
429
+ + }
430
+ + QJsonObject last(const QString& type) const
431
+ + {
432
+ + for (auto i = events.crbegin(); i != events.crend(); ++i)
433
+ + if (i->value("type").toString() == type) return *i;
434
+ + return {};
435
+ + }
436
+ +protected:
437
+ + bool screenPermissionGranted() const override { return screen; }
438
+ + bool accessibilityPermissionGranted() const override { return accessibility; }
439
+ + void promptScreenPermission() override { ++screenRequests; }
440
+ + void promptAccessibilityPermission() override { ++accessibilityRequests; }
441
+ + void beginCapture() override { ++captureStarts; sendEvent(QJsonObject{{"type", "session_started"}}); }
442
+ + void sendEvent(const QJsonObject& header, const QByteArray& = QByteArray()) override { events.append(header); }
443
+ +};
444
+ +
445
+ +class TestMacPermissions : public QObject
446
+ +{
447
+ + Q_OBJECT
448
+ +private slots:
449
+ + void missingScreenNeverCreatesOverlay();
450
+ + void optionalAccessibilityDoesNotBlockCapture();
451
+ + void stateAndCapabilityReport();
452
+ + void requestValidationAndBusyGuard();
453
+ + void explicitRequestsNeverPretendGranted();
454
+ + void busyRaceDoesNotPrompt();
455
+ +};
456
+ +
457
+ +void TestMacPermissions::missingScreenNeverCreatesOverlay()
458
+ +{
459
+ + PermissionGateway gateway;
460
+ + gateway.dispatch(QJsonObject{{"type", "start_capture"}});
461
+ + QCOMPARE(gateway.captureStarts, 0);
462
+ + QVERIFY(gateway.m_label == nullptr);
463
+ + QVERIFY(!gateway.m_capturing);
464
+ + QVERIFY(gateway.last("session_started").isEmpty());
465
+ + QCOMPARE(gateway.last("session_ended").value("reason").toString(), QStringLiteral("permission_required"));
466
+ + QCOMPARE(gateway.last("error").value("code").toString(), QStringLiteral("permission_screen_required"));
467
+ + QVERIFY(!gateway.last("state_report").value("perm_screen").toBool());
468
+ +}
469
+ +
470
+ +void TestMacPermissions::optionalAccessibilityDoesNotBlockCapture()
471
+ +{
472
+ + PermissionGateway gateway;
473
+ + gateway.screen = true;
474
+ + gateway.accessibility = false;
475
+ + gateway.dispatch(QJsonObject{{"type", "start_capture"}});
476
+ + QCOMPARE(gateway.captureStarts, 1);
477
+ + QVERIFY(gateway.last("error").isEmpty());
478
+ + QCOMPARE(gateway.accessibilityRequests, 0);
479
+ +}
480
+ +
481
+ +void TestMacPermissions::stateAndCapabilityReport()
482
+ +{
483
+ + PermissionGateway gateway;
484
+ + gateway.dispatch(QJsonObject{{"type", "hello"}});
485
+ + QVERIFY(gateway.last("hello_ack").value("capabilities").toArray().contains(QStringLiteral("permission_preflight")));
486
+ + gateway.m_pinEditing = true;
487
+ + gateway.dispatch(QJsonObject{{"type", "query_state"}});
488
+ + const auto report = gateway.last("state_report");
489
+ + QVERIFY(report.contains("perm_screen"));
490
+ + QVERIFY(report.contains("perm_a11y"));
491
+ + QVERIFY(report.value("pin_editing").toBool());
492
+ + QVERIFY(gateway.m_label == nullptr);
493
+ + gateway.m_pinEditing = false;
494
+ +}
495
+ +
496
+ +void TestMacPermissions::requestValidationAndBusyGuard()
497
+ +{
498
+ + PermissionGateway gateway;
499
+ + for (const auto& value : QList<QJsonValue>{QJsonValue(""), QJsonValue("both"), QJsonValue(true), QJsonValue(1), QJsonValue(QJsonObject{})})
500
+ + {
501
+ + gateway.dispatch(QJsonObject{{"type", "set_config"}, {"permission_request", value}});
502
+ + QCOMPARE(gateway.last("error").value("code").toString(), QStringLiteral("permission_request_invalid"));
503
+ + }
504
+ + for (const bool editing : {false, true})
505
+ + {
506
+ + gateway.m_capturing = !editing;
507
+ + gateway.m_pinEditing = editing;
508
+ + gateway.dispatch(QJsonObject{{"type", "set_config"}, {"permission_request", "screen"}});
509
+ + QCOMPARE(gateway.last("error").value("code").toString(), QStringLiteral("permission_request_busy"));
510
+ + }
511
+ + gateway.m_capturing = false;
512
+ + gateway.m_pinEditing = false;
513
+ + QCoreApplication::processEvents();
514
+ + QCOMPARE(gateway.screenRequests, 0);
515
+ + QCOMPARE(gateway.accessibilityRequests, 0);
516
+ + QVERIFY(gateway.m_label == nullptr);
517
+ +}
518
+ +
519
+ +void TestMacPermissions::explicitRequestsNeverPretendGranted()
520
+ +{
521
+ + PermissionGateway gateway;
522
+ + gateway.dispatch(QJsonObject{{"type", "set_config"}, {"permission_request", "screen"}});
523
+ + QTRY_COMPARE(gateway.screenRequests, 1);
524
+ + QVERIFY(!gateway.last("state_report").value("perm_screen").toBool());
525
+ + gateway.dispatch(QJsonObject{{"type", "set_config"}, {"permission_request", "accessibility"}});
526
+ + QTRY_COMPARE(gateway.accessibilityRequests, 1);
527
+ + QVERIFY(!gateway.last("state_report").value("perm_a11y").toBool());
528
+ + QCOMPARE(gateway.captureStarts, 0);
529
+ + QVERIFY(gateway.m_label == nullptr);
530
+ + gateway.screen = true;
531
+ + gateway.dispatch(QJsonObject{{"type", "set_config"}, {"permission_request", "screen"}});
532
+ + QCoreApplication::processEvents();
533
+ + QCOMPARE(gateway.screenRequests, 1);
534
+ + QVERIFY(gateway.last("state_report").value("perm_screen").toBool());
535
+ +}
536
+ +
537
+ +void TestMacPermissions::busyRaceDoesNotPrompt()
538
+ +{
539
+ + PermissionGateway gateway;
540
+ + gateway.dispatch(QJsonObject{{"type", "set_config"}, {"permission_request", "screen"}});
541
+ + gateway.m_capturing = true;
542
+ + QCoreApplication::processEvents();
543
+ + QCOMPARE(gateway.screenRequests, 0);
544
+ + QCOMPARE(gateway.last("error").value("code").toString(), QStringLiteral("permission_request_busy"));
545
+ + gateway.m_capturing = false;
546
+ +}
547
+ +
548
+ +QTEST_MAIN(TestMacPermissions)
549
+ +#include "test_mac_permissions.moc"