Skip to content

Commit fffa218

Browse files
committed
library: Move HoldDownInteraction to interfaces pkg
1 parent 7bfc5e5 commit fffa218

File tree

2 files changed

+59
-53
lines changed

2 files changed

+59
-53
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package top.yukonga.miuix.kmp.interfaces
2+
3+
import androidx.compose.foundation.interaction.Interaction
4+
import androidx.compose.foundation.interaction.InteractionSource
5+
import androidx.compose.foundation.interaction.MutableInteractionSource
6+
import androidx.compose.runtime.Composable
7+
import androidx.compose.runtime.LaunchedEffect
8+
import androidx.compose.runtime.State
9+
import androidx.compose.runtime.mutableStateOf
10+
import androidx.compose.runtime.remember
11+
import top.yukonga.miuix.kmp.interfaces.HoldDownInteraction.Hold
12+
import top.yukonga.miuix.kmp.interfaces.HoldDownInteraction.Release
13+
14+
/**
15+
* An interaction related to hold down events.
16+
*
17+
* @see Hold
18+
* @see Release
19+
*/
20+
interface HoldDownInteraction : Interaction {
21+
/**
22+
* An interaction representing a hold down event on a component.
23+
*
24+
* @see Release
25+
*/
26+
class Hold : HoldDownInteraction
27+
28+
/**
29+
* An interaction representing a [Hold] event being released on a component.
30+
*
31+
* @property hold the source [Hold] interaction that is being released
32+
*
33+
* @see Hold
34+
*/
35+
class Release(val hold: Hold) : HoldDownInteraction
36+
}
37+
38+
/**
39+
* Subscribes to this [MutableInteractionSource] and returns a [State] representing whether this
40+
* component is selected or not.
41+
*
42+
* @return [State] representing whether this component is being focused or not
43+
*/
44+
@Composable
45+
fun InteractionSource.collectIsHeldDownAsState(): State<Boolean> {
46+
val isHeldDown = remember { mutableStateOf(false) }
47+
LaunchedEffect(this) {
48+
val holdInteraction = mutableListOf<HoldDownInteraction.Hold>()
49+
interactions.collect { interaction ->
50+
when (interaction) {
51+
is HoldDownInteraction.Hold -> holdInteraction.add(interaction)
52+
is HoldDownInteraction.Release -> holdInteraction.remove(interaction.hold)
53+
}
54+
isHeldDown.value = holdInteraction.isNotEmpty()
55+
}
56+
}
57+
return isHeldDown
58+
}

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/utils/MiuixIndication.kt

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,16 @@ import androidx.compose.foundation.Indication
66
import androidx.compose.foundation.IndicationNodeFactory
77
import androidx.compose.foundation.interaction.FocusInteraction
88
import androidx.compose.foundation.interaction.HoverInteraction
9-
import androidx.compose.foundation.interaction.Interaction
109
import androidx.compose.foundation.interaction.InteractionSource
11-
import androidx.compose.foundation.interaction.MutableInteractionSource
1210
import androidx.compose.foundation.interaction.PressInteraction
13-
import androidx.compose.runtime.Composable
14-
import androidx.compose.runtime.LaunchedEffect
15-
import androidx.compose.runtime.State
16-
import androidx.compose.runtime.mutableStateOf
17-
import androidx.compose.runtime.remember
1811
import androidx.compose.ui.Modifier
1912
import androidx.compose.ui.graphics.Color
2013
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
2114
import androidx.compose.ui.node.DelegatableNode
2215
import androidx.compose.ui.node.DrawModifierNode
2316
import kotlinx.coroutines.Job
2417
import kotlinx.coroutines.launch
18+
import top.yukonga.miuix.kmp.interfaces.HoldDownInteraction
2519

2620
/**
2721
* Miuix default [Indication] that draws a rectangular overlay when pressed.
@@ -112,49 +106,3 @@ class MiuixIndication(
112106
}
113107
}
114108
}
115-
116-
/**
117-
* An interaction related to hold down events.
118-
*
119-
* @see Hold
120-
* @see Release
121-
*/
122-
interface HoldDownInteraction : Interaction {
123-
/**
124-
* An interaction representing a hold down event on a component.
125-
*
126-
* @see Release
127-
*/
128-
class Hold : HoldDownInteraction
129-
130-
/**
131-
* An interaction representing a [Hold] event being released on a component.
132-
*
133-
* @property hold the source [Hold] interaction that is being released
134-
*
135-
* @see Hold
136-
*/
137-
class Release(val hold: Hold) : HoldDownInteraction
138-
}
139-
140-
/**
141-
* Subscribes to this [MutableInteractionSource] and returns a [State] representing whether this
142-
* component is selected or not.
143-
*
144-
* @return [State] representing whether this component is being focused or not
145-
*/
146-
@Composable
147-
fun InteractionSource.collectIsHeldDownAsState(): State<Boolean> {
148-
val isHeldDown = remember { mutableStateOf(false) }
149-
LaunchedEffect(this) {
150-
val holdInteraction = mutableListOf<HoldDownInteraction.Hold>()
151-
interactions.collect { interaction ->
152-
when (interaction) {
153-
is HoldDownInteraction.Hold -> holdInteraction.add(interaction)
154-
is HoldDownInteraction.Release -> holdInteraction.remove(interaction.hold)
155-
}
156-
isHeldDown.value = holdInteraction.isNotEmpty()
157-
}
158-
}
159-
return isHeldDown
160-
}

0 commit comments

Comments
 (0)