Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(appshot): offer only the big parts of a window when hovering in the area picker (#794)

Ruben Lütke696e30904e56
diff --git a/docs/appshots.md b/docs/appshots.md
index 1517b6857a..e3d6cfc208 100644
--- a/docs/appshots.md
+++ b/docs/appshots.md
@@ -62,9 +62,9 @@ blur previews, markings and windows while keeping Qt loaded. The picker provides
pixels. No lens, no boxed labels, no banner;
- hovering lifts the window under the pointer out of the dim, and a click
without a drag takes exactly that window. On Windows it offers the parts
- inside the window too (a sidebar, a list, a field, a panel, a button):
- the smallest part under the pointer lights up as soon as the window has
- been read (typically 10-200 ms), the mouse wheel steps out to the larger
+ inside the window too (a sidebar, a file list, the page, a panel, a
+ picture): the smallest part under the pointer lights up as soon as the
+ window has been read (typically 10-200 ms), the mouse wheel steps out to the larger
parts around it and the whole window (and back in), and a click takes
what is lit;
- a drag clears the dim and tints the selected area a translucent grey
@@ -161,8 +161,11 @@ skips minimized and (on Windows) cloaked windows; where no list is available
(Wayland) only dragging works. The parts of a window come from UI Automation
(`dodaios/appshot/picker/elements.py`): one cached query per window on a
worker thread, the window under the pointer first, each window read once per
-pick; text runs, links, scroll bars and parts under 16 px are left out, and
-nested parts that are nearly the same box count as one wheel step. Off
+pick. Only containers and pictures count (panes, groups, documents, lists,
+trees, tables, toolbars, tabs, text areas, images); single buttons, rows,
+items, links and web components are left out, and so is any part under
+40 px a side, under 1.5 % of the window, or mostly hidden under another
+window. Nested parts that are nearly the same box count as one wheel step. Off
Windows, and for windows that expose no parts, hovering snaps to whole
windows as before. The picker gives up after fifteen minutes
without a result.
diff --git a/dodaios/appshot/picker/elements.py b/dodaios/appshot/picker/elements.py
index 99318d1497..757f4ba491 100644
--- a/dodaios/appshot/picker/elements.py
+++ b/dodaios/appshot/picker/elements.py
@@ -29,6 +29,13 @@ from PySide6.QtCore import QObject, Signal
MAX_PARTS = 3000
#: How long one window may keep UI Automation busy (ms) before it gives up.
UIA_TIMEOUT_MS = 1000
+#: The kinds of element that make a part: containers and pictures. Single
+#: buttons, rows, items, tabs, text runs, links and web components are left
+#: out; they made hovering light up pieces nobody meant to take.
+PART_KINDS = (
+ "DataGrid", "Document", "Edit", "Group", "Image", "List", "Menu", "MenuBar",
+ "Pane", "StatusBar", "Tab", "Table", "ToolBar", "Tree", "Window",
+)
Scan = Callable[[int], list[list[int]]]
@@ -142,17 +149,7 @@ def uia_scan() -> Scan | None:
)
uia.TransactionTimeout = UIA_TIMEOUT_MS
uia.ConnectionTimeout = UIA_TIMEOUT_MS
- kinds = [
- getattr(uia_api, f"UIA_{name}ControlTypeId")
- for name in (
- "Button", "ComboBox", "Custom", "DataGrid", "DataItem", "Document",
- "Edit", "Group", "Header", "Image", "List", "ListItem", "Menu",
- "MenuBar", "MenuItem", "Pane", "SplitButton", "StatusBar", "Tab",
- "TabItem", "Table", "ToolBar", "Tree", "TreeItem", "Window",
- )
- ]
- # Text runs, links, scroll bars and separators are left out: too
- # small or too many to be a part anyone means to take.
+ kinds = [getattr(uia_api, f"UIA_{name}ControlTypeId") for name in PART_KINDS]
wanted = uia.CreateAndCondition(
uia.CreateOrConditionFromArray(
[uia.CreatePropertyCondition(uia_api.UIA_ControlTypePropertyId, k) for k in kinds]
@@ -181,4 +178,4 @@ def uia_scan() -> Scan | None:
return scan
-__all__ = ["MAX_PARTS", "ElementScanner", "uia_scan"]
+__all__ = ["MAX_PARTS", "PART_KINDS", "ElementScanner", "uia_scan"]
diff --git a/dodaios/appshot/picker/markup_model.py b/dodaios/appshot/picker/markup_model.py
index 2962550bd0..64ff7dfc91 100644
--- a/dodaios/appshot/picker/markup_model.py
+++ b/dodaios/appshot/picker/markup_model.py
@@ -584,47 +584,76 @@ def clip_to(box: Box, sel: Box) -> Box | None:
# Snapping to the parts of a window
# --------------------------------------------------------------------------
-#: Parts of a window smaller than this (logical px, either side) are no
-#: snap targets: icons and single words would make the pointer jumpy.
-MIN_PART_PX = 16.0
+#: A part must be at least this wide and tall (logical px) ...
+MIN_PART_PX = 40.0
+#: ... and cover at least this share of its window. Hovering then offers the
+#: big pieces (a sidebar, a file list, the page, a picture), not every
+#: button, row and text field.
+MIN_PART_SHARE = 0.015
+#: A part more than this share hidden under windows above it is left out:
+#: lighting it up would light the window on top of it too.
+MAX_PART_COVERED = 0.3
-def window_parts(window: Box, boxes: list[Box], min_px: float = MIN_PART_PX) -> list[Box]:
- """A window's parts (panels, lists, fields, buttons), cut to the window.
+def _overlap(a: Box, b: Box) -> float:
+ shared = clip_to(a, b)
+ return shared[2] * shared[3] if shared is not None else 0.0
- Slivers, copies and parts that are the window itself are dropped; what is
- left is what hovering may lift out of the dim instead of the whole window.
+
+def window_parts(
+ window: Box,
+ boxes: list[Box],
+ *,
+ min_px: float = MIN_PART_PX,
+ min_share: float = MIN_PART_SHARE,
+ covers: list[Box] | None = None,
+) -> list[Box]:
+ """A window's parts worth snapping to, cut to the window.
+
+ Slivers, small pieces, copies, parts that are the window itself and parts
+ mostly hidden under the windows in ``covers`` (those above this one) are
+ dropped; what is left is what hovering may lift out of the dim instead of
+ the whole window.
"""
out: list[Box] = []
seen: set[tuple[int, int, int, int]] = set()
whole = tuple(round(v) for v in window)
+ least = window[2] * window[3] * min_share
for box in boxes:
clipped = clip_to(box, window)
if clipped is None or clipped[2] < min_px or clipped[3] < min_px:
continue
+ area = clipped[2] * clipped[3]
+ if area < least:
+ continue
key = (round(clipped[0]), round(clipped[1]), round(clipped[2]), round(clipped[3]))
if key in seen or key == whole:
continue
+ if covers and sum(_overlap(clipped, top) for top in covers) > area * MAX_PART_COVERED:
+ continue
seen.add(key)
out.append(clipped)
return out
#: Two nested parts this close on every edge (logical px, or this share of
-#: the larger side) count as one: a wheel step must visibly change the area.
-NEAR_PX = 12.0
-NEAR_SHARE = 0.05
+#: the larger box along that axis) count as one: a wheel step must visibly
+#: change the area, and the light must not flicker between near-identical
+#: wrappers. Per axis, so a web page under a browser's 120 px of bars stays
+#: its own step in a tall window.
+NEAR_PX = 16.0
+NEAR_SHARE = 0.06
def _nearly_same(a: Box, b: Box) -> bool:
- reach = max(NEAR_PX, NEAR_SHARE * max(a[2], a[3], b[2], b[3]))
- edges = (
- (a[0], b[0]),
- (a[1], b[1]),
- (a[0] + a[2], b[0] + b[2]),
- (a[1] + a[3], b[1] + b[3]),
+ across = max(NEAR_PX, NEAR_SHARE * max(a[2], b[2]))
+ down = max(NEAR_PX, NEAR_SHARE * max(a[3], b[3]))
+ return (
+ abs(a[0] - b[0]) <= across
+ and abs((a[0] + a[2]) - (b[0] + b[2])) <= across
+ and abs(a[1] - b[1]) <= down
+ and abs((a[1] + a[3]) - (b[1] + b[3])) <= down
)
- return all(abs(x - y) <= reach for x, y in edges)
def snap_candidates(point: Point, window: Box, parts: list[Box]) -> list[Box]:
@@ -725,7 +754,9 @@ __all__ = [
"HANDLES",
"HIGHLIGHT",
"LINE",
+ "MAX_PART_COVERED",
"MIN_PART_PX",
+ "MIN_PART_SHARE",
"MOVE",
"PALETTE",
"PEN",
diff --git a/dodaios/appshot/picker/markup_window.py b/dodaios/appshot/picker/markup_window.py
index 76b1f6aa8f..5f23eca557 100644
--- a/dodaios/appshot/picker/markup_window.py
+++ b/dodaios/appshot/picker/markup_window.py
@@ -222,7 +222,8 @@ class MarkupWindow(_SelectWindow):
)
]
window = self._snaps[indices[0]]
- parts = mm.window_parts(_box(window), boxes)
+ above = [_box(rect) for rect in self._snaps[: indices[0]]]
+ parts = mm.window_parts(_box(window), boxes, covers=above)
self._parts[handle] = [QRectF(*box) for box in parts]
if self._pointer is not None and self._choosing():
self._follow_parts(self._pointer)
diff --git a/tests/unit/appshot/test_picker_window_parts.py b/tests/unit/appshot/test_picker_window_parts.py
index a629b7d331..ffd4c712da 100644
--- a/tests/unit/appshot/test_picker_window_parts.py
+++ b/tests/unit/appshot/test_picker_window_parts.py
@@ -23,13 +23,15 @@ needs_qt = pytest.mark.skipif(
W, H = 1600, 900
WINDOW = [100, 100, 1000, 700]
-#: A sidebar, a list item in it, a content pane and a pane's inner area that
-#: is nearly the pane itself.
+#: A sidebar, a list in it, a content pane, the pane's inner area that is
+#: nearly the pane itself, a single button row (too small to offer) and a
+#: speck.
PARTS = [
[100, 100, 250, 700],
- [110, 200, 230, 40],
+ [110, 200, 230, 120],
[350, 100, 750, 700],
[356, 104, 740, 690],
+ [110, 400, 230, 30],
[0, 0, 5, 5],
]
MONITORS = [
@@ -49,18 +51,28 @@ def test_window_parts_are_cut_to_the_window_without_copies_or_slivers() -> None:
(50.0, 150.0, 200.0, 100.0), # sticks out on the left
(50.0, 150.0, 200.0, 100.0), # the same again
(100.0, 100.0, 1000.0, 700.0), # the window itself
- (300.0, 300.0, 10.0, 10.0), # too small
+ (300.0, 300.0, 10.0, 10.0), # a speck
+ (300.0, 300.0, 400.0, 30.0), # a button row: thinner than 40 px
+ (300.0, 400.0, 90.0, 90.0), # a tile: under 1.5 % of the window
(2000.0, 0.0, 100.0, 100.0), # outside
],
)
assert parts == [(100.0, 150.0, 150.0, 100.0)]
+def test_a_part_mostly_under_another_window_is_not_offered() -> None:
+ window = (0.0, 0.0, 1000.0, 1000.0)
+ sidebar = (0.0, 0.0, 300.0, 1000.0)
+ page = (300.0, 0.0, 700.0, 1000.0)
+ on_top = [(250.0, 0.0, 750.0, 1000.0)]
+ assert mm.window_parts(window, [sidebar, page], covers=on_top) == [sidebar]
+
+
def test_candidates_run_from_the_smallest_part_out_to_the_window() -> None:
window = (100.0, 100.0, 1000.0, 700.0)
parts = [tuple(float(v) for v in p) for p in PARTS[:4]]
assert mm.snap_candidates((120, 210), window, parts) == [
- (110.0, 200.0, 230.0, 40.0),
+ (110.0, 200.0, 230.0, 120.0),
(100.0, 100.0, 250.0, 700.0),
window,
]
@@ -69,6 +81,12 @@ def test_candidates_run_from_the_smallest_part_out_to_the_window() -> None:
assert mm.snap_candidates((5, 5), window, parts) == [window]
+def test_a_page_under_browser_bars_stays_its_own_step() -> None:
+ window = (0.0, 0.0, 2560.0, 1392.0)
+ page = (0.0, 120.0, 2560.0, 1272.0)
+ assert mm.snap_candidates((900, 700), window, [page]) == [page, window]
+
+
def test_the_layout_carries_a_handle_beside_each_window(monkeypatch) -> None:
from dodaios.platform import window_state as ws
from dodaios.screen_context import ports
@@ -246,7 +264,7 @@ def test_hovering_asks_for_the_parts_and_lights_the_smallest_one(picker) -> None
assert win._hole() == win._snaps[0], "until its parts arrive, the whole window lights up"
win.add_parts(4242, PARTS)
assert (win._hole().x(), win._hole().y(), win._hole().width(), win._hole().height()) == (
- 110, 200, 230, 40,
+ 110, 200, 230, 120,
)
@@ -276,6 +294,14 @@ def test_a_new_part_under_the_pointer_starts_at_the_smallest_again(picker) -> No
assert win._hole().width() == 750
+@needs_qt
+def test_small_pieces_are_never_lit(picker) -> None:
+ owner, win, events, asked = picker
+ win.add_parts(4242, PARTS)
+ _move(win, 120, 410)
+ assert win._hole().width() == 250, "the button row is skipped: the sidebar lights up"
+
+
@needs_qt
def test_parts_of_another_window_are_ignored(picker) -> None:
owner, win, events, asked = picker