"""Check the packaged floating UI on an already-connected DxDesigner run.""" from __future__ import annotations import argparse import ctypes import json import os from pathlib import Path import shlex import time import win32gui import win32api import win32process from pywinauto import Application from pywinauto import mouse from packaged_menu import tools_running, wait_for def main(): parser = argparse.ArgumentParser() parser.add_argument("executable", type=Path) parser.add_argument("--leave-collapsed", action="store_true") options = parser.parse_args() executable = options.executable.resolve() tool = wait_for(lambda: next((p for p in tools_running() if Path(p.executable).resolve() == executable and "--request" in p.command_line), None)) args = shlex.split(tool.command_line, posix=False) request = Path(args[args.index("--request") + 1].strip('"')) snapshot = json.loads(request.read_text(encoding="utf-8-sig")) app = Application(backend="uia").connect(process=tool.pid, timeout=15) def floating(): handles = [] def inspect(handle, _): if (win32process.GetWindowThreadProcessId(handle)[1] == tool.pid and win32gui.GetWindowText(handle) == "一叶知秋悬浮入口" and win32gui.IsWindowVisible(handle)): handles.append(handle) win32gui.EnumWindows(inspect, None) return handles[0] if handles else None collapsed_handle = floating() if collapsed_handle: app.window(handle=collapsed_handle).wrapper_object().invoke() window = app.window(title="一叶知秋_重构版") window.wait("visible", timeout=15) main_handle = window.handle nav = window.descendants(control_type="ListItem") assert [item.window_text() for item in nav][-2:] == ["PDF 导出", "设置"] next(item for item in nav if item.window_text() == "PDF 导出").select() assert [item.window_text() for item in nav if item.is_selected()] == ["PDF 导出"] assert "桥接已连接" in [item.window_text() for item in window.descendants(control_type="Text")] bounds = win32gui.GetClientRect(main_handle) dpi = ctypes.windll.user32.GetDpiForWindow(main_handle) assert abs(bounds[2] - 960 * dpi / 96) <= 2, (bounds, dpi) assert abs(bounds[3] - 600 * dpi / 96) <= 2, (bounds, dpi) progress = request.parent / "apply-progress.json" before = progress.stat().st_mtime_ns window.child_window(title="收起为悬浮窗", control_type="Button").invoke() handle = wait_for(floating) assert not win32gui.IsWindowVisible(main_handle) assert win32gui.GetWindowLong(handle, -20) & 0x8 # WS_EX_TOPMOST wait_for(lambda: progress.stat().st_mtime_ns > before, 15) first = win32gui.GetWindowRect(handle) # Qt derives global mouse coordinates from the actual Windows pointer. # Exercise a real drag on the verified, topmost floating button, then # restore the pointer. SendMessage alone cannot verify this gesture. width, height = win32gui.GetClientRect(handle)[2:] x, y = width // 2, height // 2 pointer = win32gui.GetCursorPos() start = (first[0] + x, first[1] + y) finish = (start[0] - 30, start[1] - 20) try: mouse.press(coords=start) mouse.move(coords=finish) mouse.release(coords=finish) finally: mouse.release(coords=win32gui.GetCursorPos()) win32api.SetCursorPos(pointer) moved = wait_for(lambda: win32gui.GetWindowRect(handle) if win32gui.GetWindowRect(handle) != first else None) assert not win32gui.IsWindowVisible(main_handle) app.window(handle=handle).wrapper_object().invoke() window.wait("visible", timeout=15) assert not floating() assert [item.window_text() for item in window.descendants(control_type="ListItem") if item.is_selected()] == ["PDF 导出"] old_snapshot = snapshot["snapshot_id"] window.child_window(title="刷新设计快照", control_type="Button").invoke() def refreshed(): try: data = json.loads(request.read_text(encoding="utf-8-sig")) return data if data["snapshot_id"] != old_snapshot else None except (OSError, ValueError): return None fresh = wait_for(refreshed, 15) assert fresh["design_path"] == snapshot["design_path"] window.minimize() handle = wait_for(floating) assert not win32gui.IsWindowVisible(main_handle) app.window(handle=handle).wrapper_object().invoke() window.wait("visible", timeout=15) if options.leave_collapsed: window.child_window(title="收起为悬浮窗", control_type="Button").invoke() wait_for(floating) result = {"executable": str(executable), "pid": tool.pid, "request": str(request), "project": snapshot["design_path"], "dpi": dpi, "client_rect": bounds, "pdf_above_settings": True, "floating_heartbeat_updated": True, "drag_before": first, "drag_after": moved, "restore_kept_pdf_page": True, "fresh_snapshot": fresh["snapshot_id"], "native_minimize_restored": True, "left_collapsed": options.leave_collapsed} evidence = Path(os.environ["LOCALAPPDATA"]) / "DxProductivity/acceptance/compact-floating-20261011" evidence.mkdir(parents=True, exist_ok=True) (evidence / "native-ui.json").write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8") print("PASS COMPACT NATIVE FLOATING UI", json.dumps(result, ensure_ascii=False), flush=True) if __name__ == "__main__": main()