Skip to content

A window on a second display is captured and read on that display - #65

Open
kevin9327 wants to merge 1 commit into
awlevin:mainfrom
kevin9327:fix/second-display
Open

kevin9327 wants to merge 1 commit into
awlevin:mainfrom
kevin9327:fix/second-display

Conversation

@kevin9327

Copy link
Copy Markdown

Fixes #38.

What was wrong

The macOS adapter assumed the main display everywhere:

  • screenshot() ran screencapture -D 1.
  • display_scale() divided by the main display's width.
  • frontmost_window_bounds, focused_field and the AX frames came back in global points, and ax_walk pruned everything outside (0, 0, w, h).

A window on any other display (including one left of or above the main display, with negative coordinates) produced a capture that didn't show it, plus an empty AX walk.

What changed (macos.py only; the Desktop surface is unchanged)

  • screenshot() picks the display under the topmost on-screen app window's center. Displays come from CGGetActiveDisplayList, whose first entry is the main display, which is also screencapture's -D order. It captures that display with -D n and remembers its number and bounds for the reads that follow.
  • As the issue suggests, if the capture's aspect ratio doesn't match that display's (more than 2% off), the numbering disagreed and it falls back to -D 1, which is the current behaviour.
  • While a non-main display is the captured one:
    • display_scale uses that display's width.
    • frontmost_window_bounds, focused_field and the frames fed to walk_actionable are shifted by its origin, so they're in that display's points with its top-left corner at 0, 0, the same way a main-display capture always worked.
    • click_at adds the origin back.
    • scroll already parks the pointer at the window's global center, so it doesn't change.
  • With the window on the main display, every value is the same as before. The display choice uses CGWindowListCopyWindowInfo only, so it adds no AppleScript round trip to the capture.
  • docs/known-limits.md: "Only the main display is captured" now describes the remaining limit. One display is read per step: the one under the window's center on macOS, and the primary monitor on Windows. A window straddling two displays is read only on that one.

I kept this inside the adapter rather than swapping adapters with platform_adapter.using(), which the issue also suggested. Every read for a step already starts with desktop.screenshot(), so the adapter can choose the display at that point. That avoids a new Desktop method that windows.py and the OSWorld adapter would also need. I can restructure it as a using() wrapper if you'd prefer that shape.

Tests

I don't have a multi-display Mac, so tests/test_displays.py runs the real macos functions with Quartz, AX and screencapture replaced by mocked platform data. The setup is a 1440×900 main display at the origin and a 1512×982 built-in display at (-1512, 200) captured at 2×, with the frontmost window on the built-in display. The tests check:

  • the capture comes from -D 2, the scale is 2.0, and the window bounds are relative to the built-in display;
  • a click at a capture point lands at the matching global point on the built-in display;
  • an AX button on the built-in display is kept (not pruned as off-display) and its frame is shifted;
  • a window on the main display reads exactly as before;
  • a capture whose shape doesn't match the chosen display falls back to -D 1.

On main, four of these fail and the main-display one passes:

E   assert [1] == [2]
E   assert [(150.0, 120....150.0, 120.0)] == [(-1362.0, 32...362.0, 320.0)]
E   assert [1] == [2, 1]
FAILED tests/test_displays.py::test_a_window_on_a_second_display_is_captured_there_and_read_in_its_points
FAILED tests/test_displays.py::test_a_click_on_a_second_display_capture_lands_on_that_display
FAILED tests/test_displays.py::test_controls_on_a_second_display_are_kept_and_shifted
FAILED tests/test_displays.py::test_a_capture_that_does_not_match_the_display_falls_back_to_the_main_display
4 failed, 1 passed

With the change, all five pass. Run locally on Linux (Python 3.12, uv sync --frozen --extra rapidocr, the Linux CI job's setup):

  • uv run ruff check .: all checks passed
  • uv run ruff format --check .: 90 files already formatted
  • uv run pytest -q: 736 passed, 1 skipped, 1 xfailed

Not verified: a real run on macOS with two displays. In particular, I haven't confirmed on hardware that screencapture -D numbers displays in CGGetActiveDisplayList order. The aspect-ratio fallback is there in case it doesn't. I have no run folder to replay either, so I can't include a before/after replay note. A check on a real two-display setup like the one in the issue would be welcome.

The macOS adapter captured only the main display and read every frame
against it, so a window on another display gave an empty step (awlevin#38).
Capture the display under the frontmost window's center, and read the
window, the focused field, and the accessibility frames in that display's
points; a click adds the display's origin back. A capture whose shape is
not that display's falls back to the main display.

Fixes awlevin#38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows on a second display are invisible

1 participant