1+ import androidx.compose.foundation.IndicationNodeFactory
2+ import androidx.compose.foundation.interaction.FocusInteraction
3+ import androidx.compose.foundation.interaction.HoverInteraction
4+ import androidx.compose.foundation.interaction.InteractionSource
5+ import androidx.compose.foundation.interaction.PressInteraction
6+ import androidx.compose.ui.Modifier
7+ import androidx.compose.ui.graphics.drawscope.ContentDrawScope
8+ import androidx.compose.ui.node.DelegatableNode
9+ import androidx.compose.ui.node.DrawModifierNode
10+ import androidx.compose.ui.node.invalidateDraw
11+ import kotlinx.coroutines.launch
12+
13+
14+ object NoIndication : IndicationNodeFactory {
15+
16+ override fun create (interactionSource : InteractionSource ): DelegatableNode =
17+ DefaultDebugIndicationInstance (interactionSource)
18+
19+ override fun hashCode (): Int = - 1
20+
21+ override fun equals (other : Any? ) = other == = this
22+
23+ private class DefaultDebugIndicationInstance (private val interactionSource : InteractionSource ) :
24+ Modifier .Node (), DrawModifierNode {
25+ private var isPressed = false
26+ private var isHovered = false
27+ private var isFocused = false
28+ override fun onAttach () {
29+ coroutineScope.launch {
30+ var pressCount = 0
31+ var hoverCount = 0
32+ var focusCount = 0
33+ interactionSource.interactions.collect { interaction ->
34+ when (interaction) {
35+ is PressInteraction .Press -> pressCount++
36+ is PressInteraction .Release -> pressCount--
37+ is PressInteraction .Cancel -> pressCount--
38+ is HoverInteraction .Enter -> hoverCount++
39+ is HoverInteraction .Exit -> hoverCount--
40+ is FocusInteraction .Focus -> focusCount++
41+ is FocusInteraction .Unfocus -> focusCount--
42+ }
43+ val pressed = pressCount > 0
44+ val hovered = hoverCount > 0
45+ val focused = focusCount > 0
46+ var invalidateNeeded = false
47+ if (isPressed != pressed) {
48+ isPressed = pressed
49+ invalidateNeeded = true
50+ }
51+ if (isHovered != hovered) {
52+ isHovered = hovered
53+ invalidateNeeded = true
54+ }
55+ if (isFocused != focused) {
56+ isFocused = focused
57+ invalidateNeeded = true
58+ }
59+ if (invalidateNeeded) invalidateDraw()
60+ }
61+ }
62+ }
63+
64+ override fun ContentDrawScope.draw () {
65+ drawContent()
66+ }
67+ }
68+ }
0 commit comments