https://kotlinlang.org logo
#compose
Title
# compose
f

Francisco

09/22/2023, 10:49 PM
Hey everyone! I’m trying to track taps on Compose elements. Parting from a certain decorView and some x and y positions, I’m trying to gather information on the tapped element. I’ll open a thread so I don’t bloat too much 😅
I’ve found two different approaches so far: reflectively accessing
SemanticsOwner
and accessing
Owner
. SemanticsOwner I’m traversing the decorView and its children until I find an AndroidComposeView, and then I access the semanticsOwner field. Then I use
semanticsOwner.getAllSemanticsNodes()
, so I’m left with a list of
SemanticNodes
. From here, I access the
SemanticsConfiguration
on each node in the tapped position, and check if I find an
"OnClick"
key to know if the element is clickable. If it’s clickable, I search for the AccessibilityAction label of the
"OnClick"
key, or the value of the
"ContentDescription"
key. This is working correctly. I can find the
"OnClick"
key, but calling
getAllSemanticsNodes()
on a background thread is causing some exceptions, and calling it on the main thread takes around 5ms, which is way more than what I’d like. Owner Once I find the AndroidComposeView, I cast it as
Owner
, and then access
owner.root
. This leaves me with a
LayoutNode
, from which I can traverse the children until I find one with a
SemanticsModifier
, and with
"OnClick"
on its
SemanticsConfiguration
. Then, I look for the same keys as above. This is not working. I’m not sure why, but while I can access
SemanticsConfiguration
, I’m never reaching the node that actually has an
"OnClick"
modifier. For each
LayoutNode
, I tried accessing its children with
node.children
and
node.zSortedChildren
, but I never reach the clickable node. Does anyone know if there’s another alternative, or if there’s something I should change on these approaches to make them work as intended?
2 Views