diff --git a/internal/atspi/adapter_foreground_native.go b/internal/atspi/adapter_foreground_native.go index e458671..357ed38 100644 --- a/internal/atspi/adapter_foreground_native.go +++ b/internal/atspi/adapter_foreground_native.go @@ -144,23 +144,20 @@ func (adapter *Adapter) matchForeground(source, window *C.AtspiAccessible, id Ob adapter.foregroundRecovery = 0 return true } - component := C.atspi_accessible_get_component_iface(window) - if component == nil { - if check != nil { - check.Reason = "accessible window component unavailable" - } - return false - } - bounds := C.atspi_component_get_extents(component, C.ATSPI_COORD_TYPE_SCREEN, nil) - C.g_object_unref(C.gpointer(component)) - if bounds == nil { + rectangle, ok := accessibleScreenExtents(window) + if !ok { if check != nil { check.Reason = "accessible window bounds unavailable" } return false } - rectangle := Rectangle{X: int32(bounds.x), Y: int32(bounds.y), Width: int32(bounds.width), Height: int32(bounds.height)} - C.g_free(C.gpointer(bounds)) + if !rectangle.Valid() { + // The frame itself is unbounded; its root child may carry the + // window-local bounds foregroundBounds can correlate instead. + if child, childOK := rootChildScreenExtents(window); childOK { + rectangle = foregroundBounds(rectangle, child) + } + } if check != nil { check.Bounds = rectangle } @@ -192,6 +189,36 @@ func (adapter *Adapter) matchForeground(source, window *C.AtspiAccessible, id Ob return true } +// accessibleScreenExtents reports the accessible's Component extents in +// screen coordinates. The returned rectangle may still be invalid (for +// example all -1) when the toolkit publishes no real bounds; ok only +// reports whether the query itself succeeded. +func accessibleScreenExtents(accessible *C.AtspiAccessible) (Rectangle, bool) { + component := C.atspi_accessible_get_component_iface(accessible) + if component == nil { + return Rectangle{}, false + } + bounds := C.atspi_component_get_extents(component, C.ATSPI_COORD_TYPE_SCREEN, nil) + C.g_object_unref(C.gpointer(component)) + if bounds == nil { + return Rectangle{}, false + } + rectangle := Rectangle{X: int32(bounds.x), Y: int32(bounds.y), Width: int32(bounds.width), Height: int32(bounds.height)} + C.g_free(C.gpointer(bounds)) + return rectangle, true +} + +// rootChildScreenExtents reports the screen extents of the accessible's +// first child, for frames whose own bounds are unavailable. +func rootChildScreenExtents(window *C.AtspiAccessible) (Rectangle, bool) { + child := C.atspi_accessible_get_child_at_index(window, 0, nil) + if child == nil { + return Rectangle{}, false + } + defer C.g_object_unref(C.gpointer(child)) + return accessibleScreenExtents(child) +} + // The exception is for a native menu from the panel process, never an // arbitrary panel control or a provider claiming the panel's accessible name. // Query fresh window state on the GLib owner; no popup state is retained. diff --git a/internal/atspi/foreground.go b/internal/atspi/foreground.go index e20e3ef..3e4fe27 100644 --- a/internal/atspi/foreground.go +++ b/internal/atspi/foreground.go @@ -53,6 +53,19 @@ func foregroundWindowMatches(client xorg.Foreground, pid int, bounds Rectangle) unpositionedWindowMatches(client, pid, bounds, inner) } +// Some toolkits publish no bounds on the accessible frame itself but give +// the frame's root child window-local bounds (GPUI via AccessKit, whose +// frame extents are all -1). A valid frame rectangle always wins; the +// child rectangle is only a candidate and must still pass +// foregroundWindowMatches, where its 0,0 origin routes to +// unpositionedWindowMatches (same process, exact client size). +func foregroundBounds(frame, rootChild Rectangle) Rectangle { + if frame.Valid() { + return frame + } + return rootChild +} + // Some GTK 4 X11 frames report screen extents with a 0,0 origin wherever // the window really is. Accept such a frame only when both sides name the // same process and the size equals the X11 client diff --git a/internal/atspi/foreground_test.go b/internal/atspi/foreground_test.go index 70517bc..6572e2d 100644 --- a/internal/atspi/foreground_test.go +++ b/internal/atspi/foreground_test.go @@ -66,6 +66,36 @@ func TestForegroundUnpositionedGTK4Correlation(t *testing.T) { } } +func TestForegroundRootChildBoundsCorrelation(t *testing.T) { + // GPUI via AccessKit reports frame extents of -1 and gives the frame's + // root child window-local bounds at 0,0 with the client's size. + client := xorg.Foreground{Known: true, Window: 18874369, PID: 777, + X: 262, Y: 184, Width: 500, Height: 400, OuterX: 262, OuterY: 184, OuterWidth: 500, OuterHeight: 400} + unbounded := Rectangle{-1, -1, -1, -1} + for _, test := range []struct { + name string + pid int + frame Rectangle + child Rectangle + want bool + }{ + {"unbounded frame, root child at origin", 777, unbounded, Rectangle{0, 0, 500, 400}, true}, + {"unbounded frame, root child other size", 777, unbounded, Rectangle{0, 0, 468, 368}, false}, + {"unbounded frame, root child offset origin", 777, unbounded, Rectangle{16, 16, 500, 400}, false}, + {"unbounded frame, other process", 778, unbounded, Rectangle{0, 0, 500, 400}, false}, + {"unbounded frame, unbounded child", 777, unbounded, unbounded, false}, + {"valid matching frame wins over child", 777, Rectangle{262, 184, 500, 400}, Rectangle{0, 0, 10, 10}, true}, + {"valid nonmatching frame blocks child fallback", 777, Rectangle{0, 0, 300, 200}, Rectangle{0, 0, 500, 400}, false}, + } { + t.Run(test.name, func(t *testing.T) { + bounds := foregroundBounds(test.frame, test.child) + if got := foregroundWindowMatches(client, test.pid, bounds); got != test.want { + t.Fatalf("match = %v, want %v (bounds %+v)", got, test.want, bounds) + } + }) + } +} + func TestForegroundWindowCorrelationSteamBigPictureScale(t *testing.T) { client := xorg.Foreground{Known: true, Window: 83886130, PID: 111, X: 0, Y: 0, Width: 1920, Height: 1080, OuterX: 0, OuterY: 0, OuterWidth: 1920, OuterHeight: 1080}