diff --git a/internal/atspi/mouse_review_native.go b/internal/atspi/mouse_review_native.go index 0a22ad6..4188671 100644 --- a/internal/atspi/mouse_review_native.go +++ b/internal/atspi/mouse_review_native.go @@ -99,6 +99,86 @@ func nativeAccessibleAtPoint(source *C.AtspiAccessible, x, y int) *C.AtspiAccess return child } +// maxPointChildren bounds the fallback scan of one object's children. +const maxPointChildren = 512 + +// nativeChildContainingPoint finds the smallest child containing the point by +// asking each child. Some providers, Chromium tables among them, stop their +// own hit test at a container, so the child under the point is never reached. +func nativeChildContainingPoint(ctx context.Context, source *C.AtspiAccessible, x, y int) *C.AtspiAccessible { + count := min(int(C.atspi_accessible_get_child_count(source, nil)), maxPointChildren) + sourceID := nativeObjectID(source) + var best *C.AtspiAccessible + bestArea := int64(1 << 62) + for i := 0; i < count && ctx.Err() == nil; i++ { + child := C.atspi_accessible_get_child_at_index(source, C.gint(i), nil) + if child == nil { + continue + } + if nativeObjectID(child) != sourceID && nativeContainsPoint(child, x, y) { + if area := nativePointArea(child); best == nil || area < bestArea { + if best != nil { + C.g_object_unref(C.gpointer(best)) + } + best, bestArea = child, area + continue + } + } + C.g_object_unref(C.gpointer(child)) + } + return best +} + +// maxPointSearchDepth and maxPointSearchNodes bound the descendant search. +const ( + maxPointSearchDepth = 3 + maxPointSearchNodes = 1024 +) + +// nativeDescendantContainingPoint searches a few levels below source for the +// smallest object containing the point. GTK notebooks report a page tab's +// extents as only its label, so the page under the finger is a grandchild of +// the tab list that no direct child contains. +func nativeDescendantContainingPoint(ctx context.Context, source *C.AtspiAccessible, x, y int) *C.AtspiAccessible { + type entry struct { + node *C.AtspiAccessible + depth int + } + sourceID := nativeObjectID(source) + queue := []entry{{(*C.AtspiAccessible)(C.g_object_ref(C.gpointer(source))), 0}} + visited := 0 + var best *C.AtspiAccessible + bestArea := int64(1 << 62) + for len(queue) > 0 && visited < maxPointSearchNodes && ctx.Err() == nil { + current := queue[0] + queue = queue[1:] + if current.depth < maxPointSearchDepth { + count := min(int(C.atspi_accessible_get_child_count(current.node, nil)), maxPointChildren) + for i := 0; i < count && visited < maxPointSearchNodes && ctx.Err() == nil; i++ { + child := C.atspi_accessible_get_child_at_index(current.node, C.gint(i), nil) + if child == nil { + continue + } + visited++ + if nativeObjectID(child) != sourceID && nativeContainsPoint(child, x, y) { + if area := nativePointArea(child); best == nil || area < bestArea { + if best != nil { + C.g_object_unref(C.gpointer(best)) + } + best, bestArea = (*C.AtspiAccessible)(C.g_object_ref(C.gpointer(child))), area + } + } + queue = append(queue, entry{child, current.depth + 1}) + } + } + C.g_object_unref(C.gpointer(current.node)) + } + for _, rest := range queue { + C.g_object_unref(C.gpointer(rest.node)) + } + return best +} + func nativePointText(ctx context.Context, source, window *C.AtspiAccessible, x, y int) (word, line string, lineStart, nativeOffset int) { if C.atspi_accessible_get_role(source, nil) == C.ATSPI_ROLE_PASSWORD_TEXT { return "", "", -1, -1 @@ -216,11 +296,19 @@ func (adapter *Adapter) nativeReviewAtPoint(ctx context.Context, x, y, pid int) defer func() { C.g_object_unref(C.gpointer(current)) }() for depth := 0; depth < 24 && ctx.Err() == nil; depth++ { child := nativeAccessibleAtPoint(current, x, y) - if child == nil { - break - } - if nativeObjectID(child) == nativeObjectID(current) || !nativeContainsPoint(child, x, y) { + if child != nil && (nativeObjectID(child) == nativeObjectID(current) || !nativeContainsPoint(child, x, y)) { C.g_object_unref(C.gpointer(child)) + child = nil + } + if child == nil { + // The provider's hit test ended here. Ask the children directly + // before settling on a container such as a table or list. + child = nativeChildContainingPoint(ctx, current, x, y) + } + if child == nil { + child = nativeDescendantContainingPoint(ctx, current, x, y) + } + if child == nil { break } C.g_object_unref(C.gpointer(current))