1 /*
2  * Copyright 2020 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.focus
18 
19 import androidx.compose.ui.focus.FocusStateImpl.Active
20 import androidx.compose.ui.focus.FocusStateImpl.ActiveParent
21 import androidx.compose.ui.focus.FocusStateImpl.Captured
22 import androidx.compose.ui.focus.FocusStateImpl.Inactive
23 import androidx.compose.ui.node.DelegatableNode
24 import androidx.compose.ui.node.Nodes
25 import androidx.compose.ui.node.requireOwner
26 import androidx.compose.ui.node.visitSelfAndChildren
27 
28 /**
29  * Implement this interface create a modifier node that can be used to observe focus state changes
30  * to a [FocusTargetNode] down the hierarchy.
31  */
32 interface FocusEventModifierNode : DelegatableNode {
33 
34     /**
35      * A parent FocusEventNode is notified of [FocusState] changes to the [FocusTargetNode]
36      * associated with this [FocusEventModifierNode].
37      */
onFocusEventnull38     fun onFocusEvent(focusState: FocusState)
39 }
40 
41 internal fun FocusEventModifierNode.invalidateFocusEvent() {
42     requireOwner().focusOwner.scheduleInvalidation(this)
43 }
44 
getFocusStatenull45 internal fun FocusEventModifierNode.getFocusState(): FocusState {
46     visitSelfAndChildren(Nodes.FocusTarget) {
47         when (val focusState = it.focusState) {
48             // If we find a focused child, we use that child's state as the aggregated state.
49             Active,
50             ActiveParent,
51             Captured -> return focusState
52             // We use the Inactive state only if we don't have a focused child.
53             // ie. we ignore this child if another child provides aggregated state.
54             Inactive -> return@visitSelfAndChildren
55         }
56     }
57     return Inactive
58 }
59