1 /*
<lambda>null2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.ui.platform
18 
19 import android.annotation.SuppressLint
20 import android.view.View
21 import androidx.collection.IntObjectMap
22 import androidx.collection.MutableIntSet
23 import androidx.compose.ui.node.OwnerScope
24 import androidx.compose.ui.semantics.Role
25 import androidx.compose.ui.semantics.ScrollAxisRange
26 import androidx.compose.ui.semantics.SemanticsActions
27 import androidx.compose.ui.semantics.SemanticsConfiguration
28 import androidx.compose.ui.semantics.SemanticsNode
29 import androidx.compose.ui.semantics.SemanticsNodeWithAdjustedBounds
30 import androidx.compose.ui.semantics.getOrNull
31 import androidx.compose.ui.text.TextLayoutResult
32 import androidx.compose.ui.util.fastForEach
33 
34 /**
35  * A snapshot of the semantics node. The children here is fixed and are taken from the time this
36  * node is constructed. While a SemanticsNode always contains the up-to-date children.
37  */
38 internal class SemanticsNodeCopy(
39     semanticsNode: SemanticsNode,
40     currentSemanticsNodes: IntObjectMap<SemanticsNodeWithAdjustedBounds>
41 ) {
42     val unmergedConfig = semanticsNode.unmergedConfig
43     val children: MutableIntSet = MutableIntSet(semanticsNode.replacedChildren.size)
44 
45     init {
46         semanticsNode.replacedChildren.fastForEach { child ->
47             if (currentSemanticsNodes.contains(child.id)) {
48                 children.add(child.id)
49             }
50         }
51     }
52 }
53 
getTextLayoutResultnull54 internal fun getTextLayoutResult(configuration: SemanticsConfiguration): TextLayoutResult? {
55     val textLayoutResults = mutableListOf<TextLayoutResult>()
56     val getLayoutResult =
57         configuration
58             .getOrNull(SemanticsActions.GetTextLayoutResult)
59             ?.action
60             ?.invoke(textLayoutResults) ?: return null
61     return if (getLayoutResult) {
62         textLayoutResults[0]
63     } else {
64         null
65     }
66 }
67 
68 @SuppressLint("PrimitiveInCollection")
getScrollViewportLengthnull69 internal fun getScrollViewportLength(configuration: SemanticsConfiguration): Float? {
70     val viewPortCalculationsResult = mutableListOf<Float>()
71     val actionResult =
72         configuration
73             .getOrNull(SemanticsActions.GetScrollViewportLength)
74             ?.action
75             ?.invoke(viewPortCalculationsResult) ?: return null
76     return if (actionResult) {
77         viewPortCalculationsResult[0]
78     } else {
79         null
80     }
81 }
82 
83 /**
84  * These objects are used as snapshot observation scopes for the purpose of sending accessibility
85  * scroll events whenever the scroll offset changes. There is one per scroller and their lifecycle
86  * is the same as the scroller's lifecycle in the semantics tree.
87  */
88 internal class ScrollObservationScope(
89     val semanticsNodeId: Int,
90     val allScopes: List<ScrollObservationScope>,
91     var oldXValue: Float?,
92     var oldYValue: Float?,
93     var horizontalScrollAxisRange: ScrollAxisRange?,
94     var verticalScrollAxisRange: ScrollAxisRange?
95 ) : OwnerScope {
96     override val isValidOwnerScope
97         get() = allScopes.contains(this)
98 }
99 
findByIdnull100 internal fun List<ScrollObservationScope>.findById(id: Int): ScrollObservationScope? {
101     for (index in indices) {
102         if (this[index].semanticsNodeId == id) {
103             return this[index]
104         }
105     }
106     return null
107 }
108 
toLegacyClassNamenull109 internal fun Role.toLegacyClassName(): String? =
110     when (this) {
111         Role.Button -> "android.widget.Button"
112         Role.Checkbox -> "android.widget.CheckBox"
113         Role.RadioButton -> "android.widget.RadioButton"
114         Role.Image -> "android.widget.ImageView"
115         Role.DropdownList -> "android.widget.Spinner"
116         Role.ValuePicker -> "android.widget.NumberPicker"
117         else -> null
118     }
119 
120 /** This function retrieves the View corresponding to a semanticsId, if it exists. */
semanticsIdToViewnull121 internal fun AndroidViewsHandler.semanticsIdToView(id: Int): View? =
122     layoutNodeToHolder.entries.firstOrNull { it.key.semanticsId == id }?.value
123