@@ -15,7 +15,7 @@ import type {
1515 SnapshotOptions ,
1616 SnapshotQualityNote ,
1717} from '@agent-device/kernel/snapshot' ;
18- import { isPositiveFiniteRect , pickLargestRect } from '@agent-device/kernel/rect' ;
18+ import { isPositiveFiniteRect , pickLargestRect , rectContains } from '@agent-device/kernel/rect' ;
1919import {
2020 isAgentTarget ,
2121 readAndroidSiblingOrder ,
@@ -25,7 +25,6 @@ import {
2525import { collectAndroidHiddenNodes } from './ui-hierarchy-visibility.ts' ;
2626import { scopePresentedAndroidSnapshot } from './ui-hierarchy-scope.ts' ;
2727import { isCollectionContainerType , shouldIncludeAndroidNode } from './ui-hierarchy-inclusion.ts' ;
28- import { collectComposeRoleFolds } from './ui-hierarchy-compose-role.ts' ;
2928import {
3029 createAndroidSnapshotPresentationNode ,
3130 isAndroidSnapshotPresentationFailure ,
@@ -553,3 +552,55 @@ function analyzeAndroidTree(root: AndroidNode): AndroidSnapshotAnalysis {
553552 }
554553 return { rawNodeCount, maxDepth } ;
555554}
555+
556+ // Compose's `SemanticsNode.emitFakeNodes` gives a merging node that has a role and children an
557+ // extra childless child, which alone carries the role's legacy class name. That child takes no
558+ // touch and lies inside its parent, which stays a generic `android.view.View`. A native
559+ // `android.view.View` is not a `ViewGroup`, so only a virtual hierarchy gives one children.
560+ const COMPOSE_ROLE_CLASS_NAMES = new Set ( [
561+ 'android.widget.button' ,
562+ 'android.widget.checkbox' ,
563+ 'android.widget.radiobutton' ,
564+ 'android.widget.spinner' ,
565+ 'android.widget.numberpicker' ,
566+ ] ) ;
567+ const COMPOSE_CONTAINER_CLASS_NAME = 'android.view.view' ;
568+
569+ /** Each Compose container the regular projection publishes with its role node's class. */
570+ function collectComposeRoleFolds (
571+ root : AndroidUiHierarchy ,
572+ presentationBudget ?: AndroidSnapshotPresentationBudget ,
573+ ) : ReadonlyMap < AndroidNode , AndroidNode > {
574+ const folds = new Map < AndroidNode , AndroidNode > ( ) ;
575+ const visit = ( node : AndroidNode ) : void => {
576+ presentationBudget ?. check ( 'work' ) ;
577+ const roleNode = composeRoleNodeOf ( node ) ;
578+ if ( roleNode ) folds . set ( node , roleNode ) ;
579+ for ( const child of node . children ) visit ( child ) ;
580+ } ;
581+ visit ( root ) ;
582+ return folds ;
583+ }
584+
585+ function composeRoleNodeOf ( node : AndroidNode ) : AndroidNode | undefined {
586+ if ( ! isAgentTarget ( node ) || node . type ?. toLowerCase ( ) !== COMPOSE_CONTAINER_CLASS_NAME ) {
587+ return undefined ;
588+ }
589+ return node . children . find ( ( child ) => isComposeRoleNode ( child , node ) ) ;
590+ }
591+
592+ function isComposeRoleNode ( child : AndroidNode , parent : AndroidNode ) : boolean {
593+ return (
594+ COMPOSE_ROLE_CLASS_NAMES . has ( child . type ?. toLowerCase ( ) ?? '' ) &&
595+ child . children . length === 0 &&
596+ child . visibleToUser !== false &&
597+ ! isAgentTarget ( child ) &&
598+ ! child . label &&
599+ ! child . identifier &&
600+ liesInside ( child . rect , parent . rect )
601+ ) ;
602+ }
603+
604+ function liesInside ( inner : Rect | undefined , outer : Rect | undefined ) : boolean {
605+ return isPositiveFiniteRect ( inner ) && outer !== undefined && rectContains ( outer , inner ) ;
606+ }
0 commit comments