@hfyjq/crannia 1.1.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/NOTICE +48 -45
- package/README.md +1 -1
- package/THIRD-PARTY-FRONTEND.md +5266 -5266
- package/THIRD-PARTY-PYTHON-linux.md +174 -0
- package/THIRD-PARTY-PYTHON.md +176 -162
- package/THIRD-PARTY.md +323 -316
- package/apps/desktop/quotes.txt +56 -56
- package/bus/core.py +677 -671
- package/bus/loaders/channel_loader.py +64 -64
- package/bus/loaders/provider_loader.py +4 -7
- package/bus/loaders/skill_loader.py +46 -27
- package/bus/loaders/skill_md.py +9 -9
- package/bus/mcp_disabled.py +4 -6
- package/bus/quarantine.py +10 -13
- package/bus/registry.py +2315 -2284
- package/bus/skill_state.py +124 -124
- package/channels/cli_channel/handler.py +3 -3
- package/channels/cli_channel/manifest.yaml +5 -5
- package/channels/dingtalk_channel/handler.py +32 -32
- package/channels/dingtalk_channel/manifest.yaml +12 -12
- package/channels/feishu_channel/handler.py +33 -33
- package/channels/feishu_channel/manifest.yaml +12 -12
- package/channels/qq_bot_channel/__init__.py +1 -1
- package/channels/qq_bot_channel/handler.py +24 -24
- package/channels/qq_bot_channel/manifest.yaml +4 -4
- package/channels/telegram_channel/__init__.py +1 -1
- package/channels/telegram_channel/handler.py +21 -21
- package/channels/telegram_channel/manifest.yaml +4 -4
- package/channels/telegram_channel/media.py +231 -231
- package/channels/textfmt.py +58 -58
- package/channels/wechat_channel/__init__.py +1 -1
- package/channels/wechat_channel/converter.py +1 -1
- package/channels/wechat_channel/handler.py +8 -8
- package/channels/wechat_channel/manifest.yaml +5 -5
- package/channels/wechat_channel/media.py +178 -178
- package/core/activity.py +753 -709
- package/core/agent_loop.py +7673 -7522
- package/core/allowlist.py +2 -3
- package/core/approval.py +34 -24
- package/core/approval_timeout.py +256 -228
- package/core/artifacts.py +3 -3
- package/core/attachment_gc.py +21 -0
- package/core/auto_review.py +2 -2
- package/core/browser/cdp.py +5 -2
- package/core/browser/discover.py +55 -19
- package/core/browser/session.py +4 -4
- package/core/cache_break_detector.py +97 -0
- package/core/cache_breakpoints.py +103 -0
- package/core/cache_policies.py +255 -0
- package/core/cancellation.py +3 -3
- package/core/channel_configs.py +3 -3
- package/core/channel_turns.py +13 -13
- package/core/child_approval.py +2 -3
- package/core/code_intel.py +11 -11
- package/core/command_policy.py +13 -7
- package/core/computer/annotate.py +231 -11
- package/core/computer/capture.py +140 -1
- package/core/computer/guard.py +7 -4
- package/core/computer/input.py +452 -26
- package/core/computer/session.py +563 -381
- package/core/computer/tools.py +2170 -928
- package/core/computer/uia.py +100 -1
- package/core/computer/watch.py +1 -1
- package/core/computer/win32.py +302 -7
- package/core/config_requirements.py +205 -205
- package/core/cron/runner.py +4 -4
- package/core/daily_log.py +2 -2
- package/core/dedup.py +2 -2
- package/core/egress_channels.py +326 -0
- package/core/egress_gate.py +96 -8
- package/core/egress_preapproved.py +59 -3
- package/core/endpoint.py +141 -141
- package/core/events.py +17 -8
- package/core/extension.py +2 -2
- package/core/file_history.py +10 -10
- package/core/file_tools.py +3 -3
- package/core/github_config.py +1 -1
- package/core/group_digest.py +3 -4
- package/core/group_minutes.py +5 -5
- package/core/group_reply.py +24 -0
- package/core/groups.py +5 -9
- package/core/hooks.py +15 -15
- package/core/i18n.py +2 -2
- package/core/ice/canary.py +3 -3
- package/core/ice/net_heuristics.py +2 -2
- package/core/im_routing.py +2 -2
- package/core/image_input.py +183 -43
- package/core/inbound.py +3 -3
- package/core/jobs.py +6 -6
- package/core/last_good.py +150 -150
- package/core/leaf_md.py +3 -3
- package/core/llm_errors.py +36 -27
- package/core/mail_config.py +2 -2
- package/core/memory/compressor.py +8 -8
- package/core/memory/manager.py +30 -45
- package/core/memory/session_memory.py +1 -2
- package/core/memory/short_term.py +4 -4
- package/core/memory_extract.py +1 -1
- package/core/memory_organize.py +8 -8
- package/core/memory_store.py +7 -9
- package/core/memory_transfer.py +2 -2
- package/core/model_capabilities.py +2 -2
- package/core/model_configs.py +219 -68
- package/core/model_profiles.py +333 -0
- package/core/modes.py +12 -11
- package/core/net_proxy.py +3 -3
- package/core/pipeline/scheduler.py +48 -43
- package/core/pipeline/stage.py +3 -0
- package/core/pipeline/stages/agent_loop.py +36 -15
- package/core/pipeline/stages/group_reply.py +22 -20
- package/core/pipeline/stages/post_process.py +7 -7
- package/core/pipeline/stages/pre_process.py +360 -68
- package/core/prompt_assembler.py +13 -4
- package/core/recycle.py +227 -16
- package/core/response_language.py +1 -1
- package/core/retention.py +2 -2
- package/core/rg.py +4 -3
- package/core/security.py +26 -8
- package/core/services.py +8 -8
- package/core/session_model.py +3 -5
- package/core/session_pool.py +35 -5
- package/core/session_recap.py +40 -3
- package/core/session_workspace.py +2 -3
- package/core/settings_baseline.py +2 -2
- package/core/settings_transfer.py +25 -1
- package/core/shell.py +52 -20
- package/core/side_call.py +3 -3
- package/core/skill_config.py +164 -0
- package/core/skill_factory.py +2 -2
- package/core/skill_guidance.py +2 -4
- package/core/skill_index.py +3 -4
- package/core/skill_settings.py +1 -1
- package/core/skill_sources.py +7 -7
- package/core/ssh_config.py +129 -0
- package/core/state_registry.py +67 -5
- package/core/subagent.py +28 -5
- package/core/subagent_defs.py +3 -3
- package/core/summarizer.py +4 -5
- package/core/tasks_notify.py +6 -6
- package/core/tiers.py +2 -2
- package/core/todos.py +1 -1
- package/core/token_calibrator.py +1 -4
- package/core/tool_outcome.py +108 -108
- package/core/touched_files.py +1 -1
- package/core/trace.py +12 -3
- package/core/unlock.py +4 -4
- package/core/update_tui.py +184 -158
- package/core/updater.py +5 -3
- package/core/version.py +14 -14
- package/core/vision_budget.py +235 -0
- package/core/web_cache.py +152 -152
- package/core/web_request.py +474 -0
- package/core/window_estimate.py +1 -1
- package/core/workspace.py +56 -26
- package/crannia_tray.py +278 -278
- package/licenses/GPL-3.0.txt +619 -619
- package/licenses/LGPL-2.1.txt +502 -502
- package/licenses/LGPL-3.0.txt +165 -165
- package/licenses/MIT-chat-on-wechat.txt +18 -18
- package/licenses/MIT-deepseek-harness.txt +21 -21
- package/licenses/Molio-LICENSE.txt +44 -44
- package/locales/en.json +1876 -1755
- package/main.py +154 -154
- package/mcpx/client.py +12 -1
- package/mcpx/manager.py +530 -522
- package/mcpx/servers/web_search.py +5 -13
- package/package.json +15 -23
- package/providers/anthropic/client.py +35 -33
- package/providers/base.py +13 -13
- package/providers/cache_policies.json +294 -0
- package/providers/cost_table.json +3779 -3779
- package/providers/lmstudio/client.py +2 -2
- package/providers/model_capabilities.json +6665 -6665
- package/providers/model_profiles.json +629 -0
- package/providers/ollama/client.py +2 -2
- package/providers/openai_compat.py +37 -16
- package/providers/openrouter/client.py +2 -2
- package/providers/qwen/__init__.py +1 -1
- package/providers/qwen/client.py +2 -2
- package/providers/qwen/manifest.yaml +16 -16
- package/providers/rate_limiter.py +2 -2
- package/providers/siliconflow/client.py +2 -2
- package/providers/validation.py +171 -171
- package/providers/vision_profiles.json +89 -0
- package/providers/window_table.json +8909 -8909
- package/requirements.txt +59 -51
- package/sbom-linux-x64.json +4580 -0
- package/sbom.json +4600 -4413
- package/skills/diagram/SKILL.md +47 -0
- package/skills/diagram/__init__.py +1 -0
- package/skills/diagram/handler.py +294 -0
- package/skills/diagram/layout.py +174 -0
- package/skills/diagram/manifest.yaml +106 -0
- package/skills/diagram/mermaid.py +170 -0
- package/skills/diagram/svg.py +223 -0
- package/skills/entertainment/SKILL.md +35 -35
- package/skills/entertainment/__init__.py +1 -1
- package/skills/entertainment/handler.py +91 -91
- package/skills/entertainment/manifest.yaml +26 -26
- package/skills/file_parser/GUIDANCE.md +10 -10
- package/skills/file_parser/handler.py +33 -11
- package/skills/github/SKILL.md +32 -32
- package/skills/github/__init__.py +1 -1
- package/skills/mail_parser/SKILL.md +68 -68
- package/skills/mail_parser/__init__.py +1 -1
- package/skills/mail_parser/handler.py +1 -1
- package/skills/media_gen/GUIDANCE.md +19 -19
- package/skills/media_gen/SKILL.md +43 -43
- package/skills/media_gen/__init__.py +1 -1
- package/skills/media_gen/handler.py +1 -1
- package/skills/media_gen/manifest.yaml +93 -93
- package/skills/netops/SKILL.md +45 -0
- package/skills/netops/__init__.py +1 -0
- package/skills/netops/handler.py +418 -0
- package/skills/netops/manifest.yaml +118 -0
- package/skills/office_parser/_excel_io.py +3 -3
- package/skills/office_parser/handler.py +2 -2
- package/skills/office_parser/ppt_tools.py +1 -1
- package/skills/office_parser/pptx_export/console_encoding.py +27 -27
- package/skills/office_parser/pptx_export/resource_paths.py +88 -88
- package/skills/office_parser/pptx_export/svg_finalize/__init__.py +12 -12
- package/skills/office_parser/pptx_export/svg_finalize/crop_images.py +366 -366
- package/skills/office_parser/pptx_export/svg_finalize/embed_images.py +288 -288
- package/skills/office_parser/pptx_export/svg_finalize/fix_image_aspect.py +388 -388
- package/skills/office_parser/pptx_export/svg_finalize/flatten_tspan.py +818 -818
- package/skills/office_parser/pptx_export/svg_finalize/svg_rect_to_path.py +345 -345
- package/skills/office_parser/pptx_export/svg_to_pptx/__init__.py +14 -14
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/__init__.py +1 -1
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/context.py +186 -186
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/converter.py +781 -781
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/paths.py +429 -429
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/utils.py +40 -21
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/__init__.py +1 -1
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/builder.py +1267 -1267
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/dimensions.py +155 -155
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/discovery.py +101 -101
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/narration.py +236 -236
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/notes.py +257 -257
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/slide_xml.py +136 -136
- package/skills/office_parser/pptx_export/svg_to_pptx/tspan_flattener.py +44 -44
- package/skills/office_parser/pptx_export/svg_to_pptx/use_expander.py +129 -129
- package/skills/office_parser/references/LICENSE-Office-Word-MCP-Server.txt +20 -20
- package/skills/office_parser/references/LICENSE-excel-mcp-server.txt +21 -21
- package/skills/office_parser/references/layout_library.md +816 -816
- package/skills/office_parser/references/ppt-master/LICENSE +21 -21
- package/skills/office_parser/references/ppt-master/references/executor-base.md +474 -474
- package/skills/office_parser/references/ppt-master/references/shared-standards.md +1002 -1002
- package/skills/office_parser/references/ppt-master/references/strategist.md +880 -880
- package/skills/office_parser/references/ppt-master/references/template-designer.md +402 -402
- package/skills/office_parser/references/ppt-master/references/visual-review.md +240 -240
- package/skills/office_parser/references/ppt-master/references/visual-styles/_index.md +106 -106
- package/skills/office_parser/references/ppt-master/references/visual-styles/blueprint.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/brutalist.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/chalkboard.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/dark-tech.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/data-journalism.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/editorial.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/glassmorphism.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/ink-notes.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/ink-wash.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/memphis.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/paper-cut.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/photo-editorial.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/pixel-art.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/sketch-notes.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/soft-rounded.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/swiss-minimal.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/vintage-poster.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/zine.md +37 -37
- package/skills/office_parser/references/spec_lock.md +235 -235
- package/skills/office_parser/soffice.py +68 -68
- package/skills/office_parser/word_tools.py +3 -3
- package/skills/ssh/SKILL.md +81 -0
- package/skills/ssh/__init__.py +1 -0
- package/skills/ssh/handler.py +609 -0
- package/skills/ssh/manifest.yaml +182 -0
- package/tui/crannia.cmd +134 -134
- package/tui/crannia.sh +82 -0
- package/tui/crannia_tui/__init__.py +1 -1
- package/tui/crannia_tui/app.py +240 -17
- package/tui/crannia_tui/app.tcss +21 -0
- package/tui/crannia_tui/clipboard.py +78 -10
- package/tui/crannia_tui/commands.py +37 -0
- package/tui/crannia_tui/events.py +6 -0
- package/tui/crannia_tui/i18n.py +164 -164
- package/tui/crannia_tui/locales/en.json +31 -6
- package/tui/crannia_tui/paths.py +4 -1
- package/tui/crannia_tui/profile.py +178 -169
- package/tui/crannia_tui/settings.py +134 -134
- package/tui/crannia_tui/setup.py +44 -5
- package/tui/crannia_tui/ui.py +156 -16
- package/tui/crannia_tui/update.py +393 -389
- package/vendor/ripgrep/COPYING.txt +3 -3
- package/vendor/ripgrep/LICENSE-MIT.txt +21 -21
- package/vendor/ripgrep/NOTICE-vscode-ripgrep.txt +13 -13
- package/vendor/ripgrep/README.md +30 -14
- package/vendor/ripgrep/UNLICENSE.txt +24 -24
- package/vendor/ripgrep/linux-x64/rg +0 -0
- package/vendor/ripgrep/package-source.json +15 -16
- package/web/__main__.py +12 -15
- package/web/server.py +8481 -8594
- package/web/static/img/channel-telegram.svg +1 -1
- package/web/static/img/channel-wechat.svg +1 -1
- package/web/templates/logs.html +49 -49
- package/web/templates/models.html +46 -46
package/core/computer/uia.py
CHANGED
|
@@ -245,6 +245,10 @@ class UIElement:
|
|
|
245
245
|
offscreen: bool
|
|
246
246
|
focusable: bool
|
|
247
247
|
value: str = ""
|
|
248
|
+
#: 这个元素所属的顶层窗口句柄(UIA `CurrentNativeWindowHandle`)。
|
|
249
|
+
#: 0 = 读不到。弹窗的元素会挂在主窗的树下,但这个值指向真正的那个窗口 ——
|
|
250
|
+
#: 元素编号只在它自己窗口的观察里有效,跨窗口点必须重新观察那个窗口。
|
|
251
|
+
native_hwnd: int = 0
|
|
248
252
|
|
|
249
253
|
@property
|
|
250
254
|
def depth(self) -> int:
|
|
@@ -355,6 +359,7 @@ def snapshot(hwnd: int, *, max_elements: int = 250, max_depth: int = 12,
|
|
|
355
359
|
a11y_id = _bstr(elem, 29) # get_CurrentAutomationId
|
|
356
360
|
cls = _bstr(elem, 30) # get_CurrentClassName
|
|
357
361
|
focusable = bool(_int_prop(elem, 27)) # get_CurrentIsKeyboardFocusable
|
|
362
|
+
native = _ptr_prop(elem, 36) # get_CurrentNativeWindowHandle
|
|
358
363
|
value = ""
|
|
359
364
|
if ctype in ("Edit", "Document"):
|
|
360
365
|
pat = _pattern(elem, VALUE_PATTERN)
|
|
@@ -367,7 +372,7 @@ def snapshot(hwnd: int, *, max_elements: int = 250, max_depth: int = 12,
|
|
|
367
372
|
index=len(out.elements), path=path, name=name,
|
|
368
373
|
control_type=ctype, automation_id=a11y_id, class_name=cls,
|
|
369
374
|
rect=rect, enabled=enabled, offscreen=offscreen,
|
|
370
|
-
focusable=focusable, value=value))
|
|
375
|
+
focusable=focusable, value=value, native_hwnd=native))
|
|
371
376
|
if depth >= max_depth:
|
|
372
377
|
out.truncated = True
|
|
373
378
|
return
|
|
@@ -419,6 +424,65 @@ def resolve(hwnd: int, path: tuple[int, ...]):
|
|
|
419
424
|
_release(walker)
|
|
420
425
|
|
|
421
426
|
|
|
427
|
+
def align_with_menu(elements: list[UIElement], items: list[tuple]) -> tuple[int, dict[int, int]]:
|
|
428
|
+
"""把 Win32 菜单表的真实矩形写回元素;返回 `(改了几个, {元素下标: 命令 id})`。
|
|
429
|
+
|
|
430
|
+
## 为什么需要它
|
|
431
|
+
|
|
432
|
+
`#32768` 是自绘菜单,UIA 的 `BoundingRectangle` 是估算值(项高按窗口高度
|
|
433
|
+
均分,与真实行高不一致),按它算出来的中心会落到相邻项上。
|
|
434
|
+
`win32.menu_items()` 读的是菜单自己的表 —— 那才是真相。
|
|
435
|
+
|
|
436
|
+
## 配对顺序
|
|
437
|
+
|
|
438
|
+
1. 按命令 id 配:元素的 `automation_id` 通常是菜单命令 id 的十进制
|
|
439
|
+
—— 这条对索引/顺序怎么变都成立;
|
|
440
|
+
2. 余下的只在两边条数相同时按序号配;条数不同就不猜。
|
|
441
|
+
|
|
442
|
+
配不到一半就整体放弃(一个都不改):半对半错比全错更难发现,
|
|
443
|
+
而模型会把错的那一半当成"UIA 说这里就是这一项"。
|
|
444
|
+
"""
|
|
445
|
+
if not elements or not items:
|
|
446
|
+
return 0, {}
|
|
447
|
+
by_id: dict[int, tuple[int, tuple]] = {}
|
|
448
|
+
for mid, _text, rect in items:
|
|
449
|
+
if mid and mid > 0:
|
|
450
|
+
by_id.setdefault(int(mid), (int(mid), rect))
|
|
451
|
+
picked: dict[int, tuple[int, tuple]] = {} # 元素下标 → (命令 id, 矩形)
|
|
452
|
+
used: set[int] = set()
|
|
453
|
+
for el in elements:
|
|
454
|
+
try:
|
|
455
|
+
aid = int((el.automation_id or "").strip() or 0)
|
|
456
|
+
except (TypeError, ValueError):
|
|
457
|
+
aid = 0
|
|
458
|
+
hit = by_id.get(aid) if aid else None
|
|
459
|
+
if hit is not None and hit[0] not in used:
|
|
460
|
+
picked[el.index] = hit
|
|
461
|
+
used.add(hit[0])
|
|
462
|
+
if len(picked) < max(1, len(elements) // 2):
|
|
463
|
+
picked = {}
|
|
464
|
+
#: 序号配对只在两边都是菜单项时成立:`elements` 里还有 Menu 容器与
|
|
465
|
+
#: 根节点,而菜单表里还有分隔符(`id=0`)。各按各的口径筛干净,
|
|
466
|
+
#: 条数才对得上。
|
|
467
|
+
menu_els = [el for el in elements if el.control_type == "MenuItem"]
|
|
468
|
+
rows = [it for it in items if int(it[0]) != 0]
|
|
469
|
+
if menu_els and len(menu_els) == len(rows):
|
|
470
|
+
for el, (mid, _text, rect) in zip(menu_els, rows):
|
|
471
|
+
picked[el.index] = (int(mid), rect)
|
|
472
|
+
elif len(items) == len(elements):
|
|
473
|
+
for el, (mid, _text, rect) in zip(elements, items):
|
|
474
|
+
picked[el.index] = (int(mid), rect)
|
|
475
|
+
if not picked:
|
|
476
|
+
return 0, {}
|
|
477
|
+
ids: dict[int, int] = {}
|
|
478
|
+
for el in elements:
|
|
479
|
+
hit = picked.get(el.index)
|
|
480
|
+
if hit is not None:
|
|
481
|
+
el.rect = hit[1]
|
|
482
|
+
ids[el.index] = hit[0]
|
|
483
|
+
return len(picked), ids
|
|
484
|
+
|
|
485
|
+
|
|
422
486
|
@win32.dpi_wrapped
|
|
423
487
|
def click_point(hwnd: int, path: tuple[int, ...]) -> tuple[int, int]:
|
|
424
488
|
"""元素的屏幕物理坐标中心点。`input.click` 用它。"""
|
|
@@ -472,6 +536,41 @@ def invoke(hwnd: int, path: tuple[int, ...]) -> None:
|
|
|
472
536
|
_release(el)
|
|
473
537
|
|
|
474
538
|
|
|
539
|
+
@win32.dpi_wrapped
|
|
540
|
+
def activate(hwnd: int, path: tuple[int, ...]) -> str | None:
|
|
541
|
+
"""按控件自己声明的模式触发它,不注入坐标。返回用上的模式名,都不支持返回 `None`。
|
|
542
|
+
|
|
543
|
+
顺序 `Invoke` → `Select` → `Toggle`,对应「按一下 / 选中 / 切换」三种语义,
|
|
544
|
+
从最接近一次点击的那个开始。
|
|
545
|
+
|
|
546
|
+
判据是「控件能做什么」,不是「它画在哪」:矩形是估算的、可能被遮挡、
|
|
547
|
+
随 DPI 换算,而模式调用只走 UIA 的语义通道,这些都不影响它。
|
|
548
|
+
|
|
549
|
+
`None` 不是错误 —— 调用方退回坐标点击即可。支持但拒绝执行(控件禁用、
|
|
550
|
+
状态不允许)才是错误:那时抛出去,不假装成功。
|
|
551
|
+
"""
|
|
552
|
+
el = resolve(hwnd, path)
|
|
553
|
+
try:
|
|
554
|
+
for pattern_id, name in ((INVOKE_PATTERN, "Invoke"),
|
|
555
|
+
(SELECTION_ITEM_PATTERN, "Select"),
|
|
556
|
+
(TOGGLE_PATTERN, "Toggle")):
|
|
557
|
+
pat = _pattern(el, pattern_id)
|
|
558
|
+
if not pat:
|
|
559
|
+
continue
|
|
560
|
+
try:
|
|
561
|
+
# 三个模式的执行方法都在各自 vtable[3],都无参
|
|
562
|
+
hr = _fn(pat, 3, [ctypes.c_void_p])(pat)
|
|
563
|
+
finally:
|
|
564
|
+
_release(pat)
|
|
565
|
+
if hr < 0:
|
|
566
|
+
raise UIAError("这个控件支持 %s,但此刻拒绝执行(可能已禁用)—— "
|
|
567
|
+
"重新观察,或改用坐标点击" % name)
|
|
568
|
+
return name
|
|
569
|
+
return None
|
|
570
|
+
finally:
|
|
571
|
+
_release(el)
|
|
572
|
+
|
|
573
|
+
|
|
475
574
|
@win32.dpi_wrapped
|
|
476
575
|
def focus(hwnd: int, path: tuple[int, ...]) -> None:
|
|
477
576
|
el = resolve(hwnd, path)
|
package/core/computer/watch.py
CHANGED
|
@@ -254,7 +254,7 @@ def _do_disable(reason: str) -> None:
|
|
|
254
254
|
try:
|
|
255
255
|
session.disable(reason)
|
|
256
256
|
except Exception as exc: # noqa: BLE001
|
|
257
|
-
print("[computer]
|
|
257
|
+
print("[computer] stop takeover failed (%s): %s: %s"
|
|
258
258
|
% (reason, type(exc).__name__, exc))
|
|
259
259
|
|
|
260
260
|
|
package/core/computer/win32.py
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
1. 坐标一律物理像素 —— 进这个模块的每个跨进程调用都套 `dpi_aware()`。
|
|
9
9
|
不声明 DPI 感知时 Windows 返回虚拟化坐标,125% 缩放下点击系统性偏移,
|
|
10
10
|
而且不报错。
|
|
11
|
-
2.
|
|
11
|
+
2. 句柄一律 `wintypes.HWND` / `c_void_p` 作 `restype` —— ctypes 默认返回
|
|
12
12
|
`c_int`,在 x64 上会把 64 位 HWND 截断,症状是"窗口时有时无"。
|
|
13
13
|
3. 只读枚举不抛异常 —— 单个窗口取不到标题/进程名时给空串并留着它,
|
|
14
14
|
坏一个窗口不该让整张列表消失。
|
|
@@ -58,6 +58,11 @@ RDW_ALLCHILDREN = 0x0080
|
|
|
58
58
|
RDW_UPDATENOW = 0x0100
|
|
59
59
|
GA_ROOT = 2
|
|
60
60
|
|
|
61
|
+
#: `#32768` 弹出菜单用这条消息交出它自己的 `HMENU`(WinUser.h:`WM_USER + 0x1E1`)
|
|
62
|
+
MN_GETHMENU = 0x0400 + 0x1E1
|
|
63
|
+
SMTO_ABORTIFHUNG = 0x0002
|
|
64
|
+
MF_BYPOSITION = 0x00000400
|
|
65
|
+
|
|
61
66
|
|
|
62
67
|
class GUITHREADINFO(ctypes.Structure):
|
|
63
68
|
_fields_ = [
|
|
@@ -201,6 +206,20 @@ if IS_WINDOWS: # pragma: no cover - 平台
|
|
|
201
206
|
user32.GetWindowDpiAwarenessContext.restype = ctypes.c_void_p
|
|
202
207
|
user32.GetAwarenessFromDpiAwarenessContext.argtypes = [ctypes.c_void_p]
|
|
203
208
|
user32.GetAwarenessFromDpiAwarenessContext.restype = ctypes.c_int
|
|
209
|
+
# 键盘布局 / 物理键状态(读数与旁路都靠它)
|
|
210
|
+
# `GetKeyboardLayout(0)` 问的是调用线程的布局 —— 后台工作线程
|
|
211
|
+
# 可能一直停在进程启动时。要问"目标窗口会不会吃键",必须按目标
|
|
212
|
+
# 窗口的线程取(见 `input.keyboard_layout`)。
|
|
213
|
+
user32.GetKeyboardLayout.argtypes = [wintypes.DWORD]
|
|
214
|
+
user32.GetKeyboardLayout.restype = wintypes.HANDLE
|
|
215
|
+
user32.GetKeyboardLayoutList.argtypes = [ctypes.c_int,
|
|
216
|
+
ctypes.POINTER(wintypes.HANDLE)]
|
|
217
|
+
user32.GetKeyboardLayoutList.restype = ctypes.c_int
|
|
218
|
+
user32.PostMessageW.argtypes = [wintypes.HWND, wintypes.UINT,
|
|
219
|
+
wintypes.WPARAM, wintypes.LPARAM]
|
|
220
|
+
user32.PostMessageW.restype = wintypes.BOOL
|
|
221
|
+
user32.GetAsyncKeyState.argtypes = [ctypes.c_int]
|
|
222
|
+
user32.GetAsyncKeyState.restype = ctypes.c_short
|
|
204
223
|
|
|
205
224
|
kernel32.GetCurrentProcessId.argtypes = []
|
|
206
225
|
kernel32.GetCurrentProcessId.restype = wintypes.DWORD
|
|
@@ -214,6 +233,11 @@ if IS_WINDOWS: # pragma: no cover - 平台
|
|
|
214
233
|
wintypes.HANDLE, wintypes.DWORD, wintypes.LPWSTR,
|
|
215
234
|
ctypes.POINTER(wintypes.DWORD)]
|
|
216
235
|
kernel32.QueryFullProcessImageNameW.restype = wintypes.BOOL
|
|
236
|
+
kernel32.GetProcessTimes.argtypes = [
|
|
237
|
+
wintypes.HANDLE, ctypes.POINTER(wintypes.FILETIME),
|
|
238
|
+
ctypes.POINTER(wintypes.FILETIME), ctypes.POINTER(wintypes.FILETIME),
|
|
239
|
+
ctypes.POINTER(wintypes.FILETIME)]
|
|
240
|
+
kernel32.GetProcessTimes.restype = wintypes.BOOL
|
|
217
241
|
|
|
218
242
|
advapi32.OpenProcessToken.argtypes = [
|
|
219
243
|
wintypes.HANDLE, wintypes.DWORD, ctypes.POINTER(wintypes.HANDLE)]
|
|
@@ -233,6 +257,23 @@ if IS_WINDOWS: # pragma: no cover - 平台
|
|
|
233
257
|
wintypes.LPCWSTR, wintypes.LPCWSTR, ctypes.c_int]
|
|
234
258
|
shell32.ShellExecuteW.restype = ctypes.c_void_p
|
|
235
259
|
|
|
260
|
+
# 菜单:MN_GETHMENU 交出 HMENU,之后一切按菜单自己的表读(见 menu_items)
|
|
261
|
+
user32.SendMessageTimeoutW.argtypes = [
|
|
262
|
+
wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM,
|
|
263
|
+
wintypes.UINT, wintypes.UINT, ctypes.POINTER(ctypes.c_size_t)]
|
|
264
|
+
user32.SendMessageTimeoutW.restype = wintypes.LPARAM
|
|
265
|
+
user32.GetMenuItemCount.argtypes = [ctypes.c_void_p]
|
|
266
|
+
user32.GetMenuItemCount.restype = ctypes.c_int
|
|
267
|
+
user32.GetMenuItemRect.argtypes = [
|
|
268
|
+
wintypes.HWND, ctypes.c_void_p, wintypes.UINT, ctypes.POINTER(RECT)]
|
|
269
|
+
user32.GetMenuItemRect.restype = wintypes.BOOL
|
|
270
|
+
user32.GetMenuItemID.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
271
|
+
user32.GetMenuItemID.restype = wintypes.UINT
|
|
272
|
+
user32.GetMenuStringW.argtypes = [
|
|
273
|
+
ctypes.c_void_p, wintypes.UINT, wintypes.LPWSTR, ctypes.c_int,
|
|
274
|
+
wintypes.UINT]
|
|
275
|
+
user32.GetMenuStringW.restype = ctypes.c_int
|
|
276
|
+
|
|
236
277
|
|
|
237
278
|
# DPI
|
|
238
279
|
class dpi_aware:
|
|
@@ -387,6 +428,30 @@ def logical_window_rect(hwnd) -> tuple[int, int, int, int]:
|
|
|
387
428
|
return (0, 0, 0, 0)
|
|
388
429
|
|
|
389
430
|
|
|
431
|
+
def cursor_pos() -> tuple[int, int] | None:
|
|
432
|
+
"""光标此刻的物理坐标;取不到就 `None`(不是 `(0, 0)`)。
|
|
433
|
+
|
|
434
|
+
## 为什么它是"事实"而不是"意图"
|
|
435
|
+
|
|
436
|
+
注入点是我们要它去哪,回读值是它实际去了哪。两者在有 DPI 缩放、
|
|
437
|
+
多屏、或者注入被系统改写的场合会不一样,而模型看到的红十字如果画的是
|
|
438
|
+
意图点,就等于拿一个假设当反馈 —— 点偏了还会以为点对了。
|
|
439
|
+
|
|
440
|
+
`None` 与 `(0, 0)` 必须分开:左上角是一个合法坐标,用它表示"不知道"
|
|
441
|
+
会让"光标在 0,0"和"读不到"变成同一件事。
|
|
442
|
+
"""
|
|
443
|
+
if not IS_WINDOWS:
|
|
444
|
+
return None
|
|
445
|
+
try:
|
|
446
|
+
with dpi_aware():
|
|
447
|
+
pt = wintypes.POINT()
|
|
448
|
+
if user32.GetCursorPos(ctypes.byref(pt)):
|
|
449
|
+
return (int(pt.x), int(pt.y))
|
|
450
|
+
except Exception: # noqa: BLE001 - 回读失败不该反噬点击
|
|
451
|
+
return None
|
|
452
|
+
return None
|
|
453
|
+
|
|
454
|
+
|
|
390
455
|
def virtual_screen() -> tuple[int, int, int, int]:
|
|
391
456
|
"""返回 (x, y, 宽, 高) —— 覆盖所有显示器的虚拟屏幕(物理像素)。"""
|
|
392
457
|
_require_windows()
|
|
@@ -410,6 +475,9 @@ class WindowInfo:
|
|
|
410
475
|
visible: bool
|
|
411
476
|
minimized: bool
|
|
412
477
|
cloaked: bool
|
|
478
|
+
#: 进程启动时间(FILETIME,0 = 读不到)。窗口句柄与 pid 都会被系统
|
|
479
|
+
#: 回收给新进程,只比 pid 会认错对象;这个值跟着进程走。
|
|
480
|
+
started: int = 0
|
|
413
481
|
|
|
414
482
|
@property
|
|
415
483
|
def width(self) -> int:
|
|
@@ -445,16 +513,32 @@ def _class_name(hwnd) -> str:
|
|
|
445
513
|
return ""
|
|
446
514
|
|
|
447
515
|
|
|
448
|
-
def
|
|
516
|
+
def _process_meta(pid: int) -> tuple[str, int]:
|
|
517
|
+
"""进程的可执行路径与启动时间(FILETIME,0 = 读不到)。
|
|
518
|
+
|
|
519
|
+
启动时间用来分辨"同一个 pid 换了进程":进程退出后系统会把 pid 发给
|
|
520
|
+
新进程,而窗口句柄也会被回收给新窗口 —— 只认 pid 会认错对象。
|
|
521
|
+
一次 `OpenProcess` 同时取两样,枚举时不多花系统调用。
|
|
522
|
+
"""
|
|
449
523
|
h = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
|
|
450
524
|
if not h:
|
|
451
|
-
return ""
|
|
525
|
+
return "", 0
|
|
452
526
|
try:
|
|
527
|
+
path = ""
|
|
453
528
|
size = wintypes.DWORD(32768)
|
|
454
529
|
buf = ctypes.create_unicode_buffer(size.value)
|
|
455
530
|
if kernel32.QueryFullProcessImageNameW(h, 0, buf, ctypes.byref(size)):
|
|
456
|
-
|
|
457
|
-
|
|
531
|
+
path = buf.value or ""
|
|
532
|
+
started = 0
|
|
533
|
+
created = wintypes.FILETIME()
|
|
534
|
+
exited = wintypes.FILETIME()
|
|
535
|
+
kt = wintypes.FILETIME()
|
|
536
|
+
ut = wintypes.FILETIME()
|
|
537
|
+
if kernel32.GetProcessTimes(h, ctypes.byref(created), ctypes.byref(exited),
|
|
538
|
+
ctypes.byref(kt), ctypes.byref(ut)):
|
|
539
|
+
started = ((int(created.dwHighDateTime) & 0xFFFFFFFF) << 32) | \
|
|
540
|
+
(int(created.dwLowDateTime) & 0xFFFFFFFF)
|
|
541
|
+
return path, started
|
|
458
542
|
finally:
|
|
459
543
|
kernel32.CloseHandle(h)
|
|
460
544
|
|
|
@@ -497,7 +581,7 @@ def window_info(hwnd) -> WindowInfo | None:
|
|
|
497
581
|
with dpi_aware():
|
|
498
582
|
pid = wintypes.DWORD(0)
|
|
499
583
|
user32.GetWindowThreadProcessId(h, ctypes.byref(pid))
|
|
500
|
-
path =
|
|
584
|
+
path, started = _process_meta(pid.value) if pid.value else ("", 0)
|
|
501
585
|
return WindowInfo(
|
|
502
586
|
hwnd=h,
|
|
503
587
|
pid=int(pid.value),
|
|
@@ -509,6 +593,7 @@ def window_info(hwnd) -> WindowInfo | None:
|
|
|
509
593
|
visible=bool(user32.IsWindowVisible(h)),
|
|
510
594
|
minimized=bool(user32.IsIconic(h)),
|
|
511
595
|
cloaked=_is_cloaked(h),
|
|
596
|
+
started=started,
|
|
512
597
|
)
|
|
513
598
|
|
|
514
599
|
|
|
@@ -555,7 +640,7 @@ def _window_info_no_dpi(hwnd) -> WindowInfo | None:
|
|
|
555
640
|
return None
|
|
556
641
|
pid = wintypes.DWORD(0)
|
|
557
642
|
user32.GetWindowThreadProcessId(h, ctypes.byref(pid))
|
|
558
|
-
path =
|
|
643
|
+
path, started = _process_meta(pid.value) if pid.value else ("", 0)
|
|
559
644
|
return WindowInfo(
|
|
560
645
|
hwnd=h,
|
|
561
646
|
pid=int(pid.value),
|
|
@@ -567,6 +652,7 @@ def _window_info_no_dpi(hwnd) -> WindowInfo | None:
|
|
|
567
652
|
visible=bool(user32.IsWindowVisible(h)),
|
|
568
653
|
minimized=bool(user32.IsIconic(h)),
|
|
569
654
|
cloaked=_is_cloaked(h),
|
|
655
|
+
started=started,
|
|
570
656
|
)
|
|
571
657
|
|
|
572
658
|
|
|
@@ -580,6 +666,133 @@ def is_foreground(hwnd) -> bool:
|
|
|
580
666
|
return foreground_hwnd() == int(hwnd or 0)
|
|
581
667
|
|
|
582
668
|
|
|
669
|
+
# 键盘布局 / 物理键状态
|
|
670
|
+
def thread_id_of(hwnd) -> int:
|
|
671
|
+
"""目标窗口的线程 id;读不到返回 0。"""
|
|
672
|
+
if not IS_WINDOWS:
|
|
673
|
+
return 0
|
|
674
|
+
try:
|
|
675
|
+
return int(user32.GetWindowThreadProcessId(int(hwnd), None) or 0)
|
|
676
|
+
except Exception: # noqa: BLE001
|
|
677
|
+
return 0
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
def installed_layouts() -> list[tuple[int, int]]:
|
|
681
|
+
"""已安装的键盘布局 `[(HKL, langid)]`;读不到返回空列表。"""
|
|
682
|
+
if not IS_WINDOWS:
|
|
683
|
+
return []
|
|
684
|
+
try:
|
|
685
|
+
n = int(user32.GetKeyboardLayoutList(0, None))
|
|
686
|
+
if n <= 0:
|
|
687
|
+
return []
|
|
688
|
+
arr = (wintypes.HANDLE * n)()
|
|
689
|
+
got = int(user32.GetKeyboardLayoutList(n, arr))
|
|
690
|
+
out = []
|
|
691
|
+
for h in arr[:max(0, got)]:
|
|
692
|
+
h = int(h or 0)
|
|
693
|
+
if h:
|
|
694
|
+
out.append((h, h & 0xFFFF))
|
|
695
|
+
return out
|
|
696
|
+
except Exception: # noqa: BLE001
|
|
697
|
+
return []
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
def post_input_language_change(hwnd, hkl) -> bool:
|
|
701
|
+
"""请求目标窗口切换输入语言(`WM_INPUTLANGCHANGEREQUEST`)。
|
|
702
|
+
|
|
703
|
+
这是系统给"窗口级输入语言切换"的官方消息:作用在目标线程上,
|
|
704
|
+
不用注入 Win+Space(Win 键被护栏禁止),也不会打开系统界面。
|
|
705
|
+
真机实测:中文 0x0804 <-> 英语 0x0409 都能切、都能读回。
|
|
706
|
+
"""
|
|
707
|
+
if not IS_WINDOWS:
|
|
708
|
+
return False
|
|
709
|
+
try:
|
|
710
|
+
return bool(user32.PostMessageW(int(hwnd), 0x0050, 0,
|
|
711
|
+
int(hkl)))
|
|
712
|
+
except Exception: # noqa: BLE001
|
|
713
|
+
return False
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
def keyboard_layout_of(hwnd) -> int | None:
|
|
717
|
+
"""目标窗口线程的 HKL;读不到返回 None(不猜)。
|
|
718
|
+
|
|
719
|
+
为什么不是 `GetKeyboardLayout(0)`:那个问的是调用线程。框架跑在
|
|
720
|
+
后台工作线程里,它的布局可能一直停在进程启动时,与用户此刻在目标
|
|
721
|
+
应用里用的输入法无关。判"目标会不会吃键"只能问目标线程。
|
|
722
|
+
"""
|
|
723
|
+
if not IS_WINDOWS:
|
|
724
|
+
return None
|
|
725
|
+
try:
|
|
726
|
+
tid = thread_id_of(hwnd)
|
|
727
|
+
hkl = int(user32.GetKeyboardLayout(tid) or 0)
|
|
728
|
+
return hkl or None
|
|
729
|
+
except Exception: # noqa: BLE001
|
|
730
|
+
return None
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
def key_is_down(vk: int) -> bool | None:
|
|
734
|
+
"""这个虚拟键此刻是不是按着的(全局状态);读不到返回 None。"""
|
|
735
|
+
if not IS_WINDOWS:
|
|
736
|
+
return None
|
|
737
|
+
try:
|
|
738
|
+
return bool(int(user32.GetAsyncKeyState(int(vk))) & 0x8000)
|
|
739
|
+
except Exception: # noqa: BLE001
|
|
740
|
+
return None
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
def thread_focus(tid: int) -> tuple[int, int]:
|
|
744
|
+
"""目标线程的 `(hwndActive, hwndFocus)`;读不到返回 `(0, 0)`。
|
|
745
|
+
|
|
746
|
+
与"前台窗口"是两件事:前台仍是主窗时,线程的活动窗口完全可能是
|
|
747
|
+
另一个顶层窗口(Photoshop 的浮动操作栏 `OS_ViewContainer`)。
|
|
748
|
+
键盘事件跟着焦点走,不跟着前台走。
|
|
749
|
+
"""
|
|
750
|
+
if not IS_WINDOWS or not tid:
|
|
751
|
+
return 0, 0
|
|
752
|
+
try:
|
|
753
|
+
gi = GUITHREADINFO()
|
|
754
|
+
gi.cbSize = ctypes.sizeof(GUITHREADINFO)
|
|
755
|
+
if not user32.GetGUIThreadInfo(int(tid), ctypes.byref(gi)):
|
|
756
|
+
return 0, 0
|
|
757
|
+
return int(gi.hwndActive or 0), int(gi.hwndFocus or 0)
|
|
758
|
+
except Exception: # noqa: BLE001
|
|
759
|
+
return 0, 0
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
def root_of(hwnd) -> int:
|
|
763
|
+
"""窗口的根窗口(GA_ROOT);读不到返回 0。"""
|
|
764
|
+
if not IS_WINDOWS or not hwnd:
|
|
765
|
+
return 0
|
|
766
|
+
try:
|
|
767
|
+
return int(user32.GetAncestor(int(hwnd), 2) or 0) # GA_ROOT = 2
|
|
768
|
+
except Exception: # noqa: BLE001
|
|
769
|
+
return 0
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
def window_brief(hwnd) -> str:
|
|
773
|
+
"""给读数用的一行:`类名「标题」`(没有标题就只给类名)。"""
|
|
774
|
+
if not IS_WINDOWS or not hwnd:
|
|
775
|
+
return ""
|
|
776
|
+
try:
|
|
777
|
+
cls = _class_name(int(hwnd)) or "?"
|
|
778
|
+
buf = ctypes.create_unicode_buffer(256)
|
|
779
|
+
user32.GetWindowTextW(int(hwnd), buf, 256)
|
|
780
|
+
t = (buf.value or "").strip()
|
|
781
|
+
return "%s「%s」" % (cls, t[:40]) if t else cls
|
|
782
|
+
except Exception: # noqa: BLE001
|
|
783
|
+
return ""
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
def class_name_of(hwnd) -> str:
|
|
787
|
+
"""窗口类名;读不到返回空串。"""
|
|
788
|
+
if not IS_WINDOWS or not hwnd:
|
|
789
|
+
return ""
|
|
790
|
+
try:
|
|
791
|
+
return _class_name(int(hwnd)) or ""
|
|
792
|
+
except Exception: # noqa: BLE001
|
|
793
|
+
return ""
|
|
794
|
+
|
|
795
|
+
|
|
583
796
|
def pick_main_window(windows, pids):
|
|
584
797
|
"""从枚举结果里挑出 Crannia 主窗。
|
|
585
798
|
|
|
@@ -605,6 +818,88 @@ def pick_main_window(windows, pids):
|
|
|
605
818
|
return max(mine, key=area) if mine else None
|
|
606
819
|
|
|
607
820
|
|
|
821
|
+
def menu_items(hwnd: int) -> list[tuple[int, str, tuple[int, int, int, int]]]:
|
|
822
|
+
"""`#32768` 弹出菜单的真实项表:`[(命令 id, 文字, 屏幕矩形), …]`。
|
|
823
|
+
|
|
824
|
+
## 为什么不用 UIA 的 `BoundingRectangle`
|
|
825
|
+
|
|
826
|
+
`#32768` 是应用自绘的菜单。UIA 给它的 `BoundingRectangle` 是估算值:
|
|
827
|
+
项高按窗口高度均分,与真实行高不一致,越往下偏得越多 —— 按它算出来的
|
|
828
|
+
中心会落到相邻项上。菜单自己的表才是准的。
|
|
829
|
+
|
|
830
|
+
## 数据源
|
|
831
|
+
|
|
832
|
+
标准 Win32 弹出菜单响应 `MN_GETHMENU`,交出它自己的 `HMENU`;之后
|
|
833
|
+
`GetMenuItemCount` / `GetMenuItemID` / `GetMenuItemRect` 读的全是菜单元数据。
|
|
834
|
+
`GetMenuItemRect` 的 `hwnd` 传 `NULL` → 返回屏幕坐标(文档明说),与 UIA
|
|
835
|
+
在同一个坐标系里(两者都在 `dpi_aware()` 下取)。
|
|
836
|
+
|
|
837
|
+
## 读不到就返回空表
|
|
838
|
+
|
|
839
|
+
不是 Win32 菜单(Electron / 自绘浮层)· 菜单已经收起 · 跨权限读不到 ——
|
|
840
|
+
一律 `[]`。调用方拿空表就退回原路,不编坐标:
|
|
841
|
+
「点错了却以为点对了」比「明说这个菜单没有表」贵得多。
|
|
842
|
+
"""
|
|
843
|
+
if not IS_WINDOWS:
|
|
844
|
+
return []
|
|
845
|
+
h = int(hwnd or 0)
|
|
846
|
+
if not h:
|
|
847
|
+
return []
|
|
848
|
+
try:
|
|
849
|
+
with dpi_aware():
|
|
850
|
+
if _class_name(h) not in ("#32768", "#32769"):
|
|
851
|
+
return []
|
|
852
|
+
out = ctypes.c_size_t(0)
|
|
853
|
+
user32.SendMessageTimeoutW(wintypes.HWND(h), MN_GETHMENU, 0, 0,
|
|
854
|
+
SMTO_ABORTIFHUNG, 1000,
|
|
855
|
+
ctypes.byref(out))
|
|
856
|
+
hmenu = int(out.value or 0)
|
|
857
|
+
if not hmenu:
|
|
858
|
+
return []
|
|
859
|
+
count = int(user32.GetMenuItemCount(ctypes.c_void_p(hmenu)))
|
|
860
|
+
if count <= 0 or count > 2000:
|
|
861
|
+
return []
|
|
862
|
+
items: list[tuple[int, str, tuple[int, int, int, int]]] = []
|
|
863
|
+
buf = ctypes.create_unicode_buffer(256)
|
|
864
|
+
for pos in range(count):
|
|
865
|
+
r = RECT()
|
|
866
|
+
if not user32.GetMenuItemRect(None, ctypes.c_void_p(hmenu), pos,
|
|
867
|
+
ctypes.byref(r)):
|
|
868
|
+
continue
|
|
869
|
+
if r.right <= r.left or r.bottom <= r.top:
|
|
870
|
+
continue
|
|
871
|
+
mid = int(user32.GetMenuItemID(ctypes.c_void_p(hmenu), pos))
|
|
872
|
+
text = ""
|
|
873
|
+
try:
|
|
874
|
+
if user32.GetMenuStringW(ctypes.c_void_p(hmenu), pos, buf,
|
|
875
|
+
256, MF_BYPOSITION) > 0:
|
|
876
|
+
text = buf.value.replace("&", "")
|
|
877
|
+
except Exception: # noqa: BLE001 - 文字读不到不要紧
|
|
878
|
+
text = ""
|
|
879
|
+
items.append((mid, text, (r.left, r.top, r.right, r.bottom)))
|
|
880
|
+
return items
|
|
881
|
+
except Exception: # noqa: BLE001 - 读不到就是没有
|
|
882
|
+
return []
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
def point_hit(hwnd: int, x: int, y: int) -> bool | None:
|
|
886
|
+
"""点 `(x,y)` 命中的根窗口是不是 `hwnd`;读不到返回 None(不猜)。
|
|
887
|
+
|
|
888
|
+
与 `window_health` 里的 `point_hit` 同一判据,但那个只看窗口中心 ——
|
|
889
|
+
模型真正要点的可能是别处。`None` 的调用方不该拒绝(读不到 ≠ 被挡)。
|
|
890
|
+
"""
|
|
891
|
+
if not IS_WINDOWS:
|
|
892
|
+
return None
|
|
893
|
+
try:
|
|
894
|
+
with dpi_aware():
|
|
895
|
+
hp = user32.WindowFromPoint(wintypes.POINT(int(x), int(y)))
|
|
896
|
+
if not hp:
|
|
897
|
+
return None
|
|
898
|
+
return bool(user32.GetAncestor(hp, GA_ROOT) == int(hwnd))
|
|
899
|
+
except Exception: # noqa: BLE001
|
|
900
|
+
return None
|
|
901
|
+
|
|
902
|
+
|
|
608
903
|
def window_health(hwnd) -> dict:
|
|
609
904
|
"""窗口现在能不能收输入。
|
|
610
905
|
|