Add ax_events: reactive UIA focus-change wait#424
Merged
Conversation
The accessibility recorder polls focus every ~250ms, missing fast transitions and reacting late. wait_for_focus_change blocks on the native AddFocusChangedEventHandler and returns the moment focus moves - zero-latency, miss-free, the accessibility-tree analogue of wait_for_window. The real event handler is registered/unregistered under a lock on the calling thread (COMObject + queue). Extends the backend ABC + Windows UIA backend via the fake-backend seam.
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 23 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Why
The accessibility recorder polls the focused element every ~250 ms, so it can miss a fast focus transition and reacts a quarter-second late. UIA exposes real events:
wait_for_focus_changeblocks on the nativeAddFocusChangedEventHandlerand returns the moment focus moves — the zero-latency, miss-free "wait until focus lands on the dialog" primitive, the accessibility-tree analogue ofwait_for_window/wait_for_image.Eighth and final feature of the ROUND-15 native-UIA depth lane (which is now complete: virtualized, ax_props, table_pattern, transform_window, legacy_accessible, advanced TextPattern, selection_view, ax_events).
Design
base.py_unsupporteddefault) + a real subscription inwindows_backend.py: acomtypes.COMObjectfocus handler pushes events onto aqueue.Queue,wait_for_focus_changeblocks onevents.get(timeout=...)and registers / unregisters the handler under athreading.Lock(per the concurrency rules), returning the newly-focused element orNoneon timeout. Falls back toNoneif the handler interface is unavailable.__all__→AC_wait_for_focus_change→ read-onlyac_wait_for_focus_changeMCP tool → Script Builder (Native UI). Qt-free verified.Tests
test/unit_test/headless/test_ax_events_batch.py— fake backend covers element-returned, timeout → None, the unsupported-backend error, the executor{changed, element}wrapper, and 5-layer wiring. 13 passed with the selection_view sibling. The real COM event path is not run in CI (no Windows backend), matching the rest of the accessibility lane.