From 14df3668ce12495e67f5a97a9e0c5308914e127b Mon Sep 17 00:00:00 2001 From: xrh_mac <527189223@qq.com> Date: Mon, 9 Mar 2026 19:41:46 +0800 Subject: [PATCH] fix: prevent inspect highlighter crash on float rects feat: add --show-parents option to inspect --- src/pyax/__main__.py | 4 ++++ src/pyax/_cli.py | 22 ++++++++++++++++++++++ src/pyax/_highlighter.py | 5 ++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/pyax/__main__.py b/src/pyax/__main__.py index 665896b..40b57cc 100644 --- a/src/pyax/__main__.py +++ b/src/pyax/__main__.py @@ -134,6 +134,9 @@ def inspect( depth: Annotated[ int, typer.Option(help="Depth of subtree to show") ] = 0, + show_parents: Annotated[ + bool, typer.Option(help="Show parent chain from selected element to app root") + ] = False, json: Annotated[bool, typer.Option(help="Output in JSON format")] = False, ): cli_inspect( @@ -144,6 +147,7 @@ def inspect( list_attributes, list_actions, -1 if show_subtree and depth == 0 else depth, + show_parents, json, ) diff --git a/src/pyax/_cli.py b/src/pyax/_cli.py index 5c621a9..ab2fb5f 100644 --- a/src/pyax/_cli.py +++ b/src/pyax/_cli.py @@ -238,6 +238,7 @@ def inspect( list_attributes, list_actions, depth, + show_parents, json, ): app = _get_target_application(app_name) @@ -266,6 +267,25 @@ def _show(element): depth=depth, ) + def _show_parent_chain(element): + _CONSOLE.print("[bold]Parent chain (selected -> root)[/bold]") + idx = 0 + current = element + while current: + obj = _element_to_dict( + current, attributes, all_attributes, list_attributes, list_actions + ) + if "AXChildren" in obj: + obj.pop("AXChildren") + _CONSOLE.print( + f"{idx:>2}: " + _obj_to_pretty_string(current, obj, role_markup="cyan") + ) + parent = current.parent + if not parent or parent == current: + break + current = parent + idx += 1 + if dom_id: _show(_get_target_uielement(app_name, None, dom_id)) return @@ -279,3 +299,5 @@ def _show(element): print() _show(element) + if show_parents: + _show_parent_chain(element) diff --git a/src/pyax/_highlighter.py b/src/pyax/_highlighter.py index 5ecceb9..6dcf79b 100644 --- a/src/pyax/_highlighter.py +++ b/src/pyax/_highlighter.py @@ -109,6 +109,9 @@ def draw_rect( if not flip_y else QtGui.QGuiApplication.primaryScreen().size().height() - rect["y"] ) - painter.drawRect(rect["x"] - self.x(), y, rect["w"], rect["h"]) + # Qt drawRect(x, y, w, h) expects ints; AX frame values can be floats. + painter.drawRect( + int(rect["x"] - self.x()), int(y), int(rect["w"]), int(rect["h"]) + ) painter.end() self.label.setPixmap(canvas)