Skip to content

Commit 4566ce9

Browse files
fix snapping behavior
1 parent 2690649 commit 4566ce9

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

Library/lib/src/commonMain/kotlin/ui/Sniffer.kt

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ fun SnifferIndicator() {
3838
val rootSize = remember { mutableStateOf(IntSize.Zero) }
3939
val buttonSize = remember { mutableStateOf(IntSize.Zero) }
4040
val widgetPosition = remember { mutableStateOf(IntOffset((-8 * density.density).toInt(), 0)) }
41+
val isDragging = remember { mutableStateOf(false) }
4142

4243
Box(
4344
modifier = Modifier
@@ -53,7 +54,14 @@ fun SnifferIndicator() {
5354
.offset { widgetPosition.value }
5455
.pointerInput(Unit) {
5556
detectDragGestures(
57+
onDrag = { change, dragAmount ->
58+
isDragging.value = true
59+
val finalPos =
60+
widgetPosition.value + IntOffset(x = dragAmount.x.toInt(), y = dragAmount.y.toInt())
61+
widgetPosition.value = finalPos
62+
},
5663
onDragEnd = {
64+
isDragging.value = false
5765
val rootRect = rootSize.value.toIntRect()
5866

5967
if (widgetPosition.value.x.absoluteValue < rootRect.center.x - buttonSize.value.width / 2) {
@@ -66,16 +74,23 @@ fun SnifferIndicator() {
6674
.copy(x = -1 * rootSize.value.width + buttonSize.value.width + (8 * this.density).toInt())
6775
}
6876
},
69-
onDrag = { change, dragAmount ->
70-
val finalPos =
71-
widgetPosition.value + IntOffset(x = dragAmount.x.toInt(), y = dragAmount.y.toInt())
72-
widgetPosition.value = finalPos
73-
}
7477
)
7578
}
7679
.onSizeChanged {
7780
buttonSize.value = it
81+
82+
val rootRect = rootSize.value.toIntRect()
83+
if (widgetPosition.value.x.absoluteValue < rootRect.center.x - buttonSize.value.width / 2) {
84+
// snap right
85+
widgetPosition.value = widgetPosition.value
86+
.copy(x = (-8 * density.density).toInt())
87+
} else {
88+
// snap left
89+
widgetPosition.value = widgetPosition.value
90+
.copy(x = -1 * rootSize.value.width + buttonSize.value.width + (8 * density.density).toInt())
91+
}
7892
},
93+
isDragging = isDragging
7994
)
8095
}
8196
}
@@ -84,6 +99,7 @@ fun SnifferIndicator() {
8499
@Composable
85100
fun SnifferIndicator(
86101
modifier: Modifier = Modifier,
102+
isDragging: State<Boolean> = mutableStateOf(false),
87103
onClick: () -> Unit = {}
88104
) {
89105
val scope = rememberCoroutineScope()
@@ -100,6 +116,9 @@ fun SnifferIndicator(
100116
maximizeJob.value = scope.launch {
101117
isMinimized = false
102118
delay(3000)
119+
while (isDragging.value) {
120+
delay(2000)
121+
}
103122
ensureActive()
104123
isMinimized = true
105124
}
@@ -115,14 +134,14 @@ fun SnifferIndicator(
115134

116135
Column(
117136
modifier = modifier
118-
.clip(RoundedCornerShape(24.dp))
137+
.clip(RoundedCornerShape(16.dp))
119138
.clickable {
120139
if (isMinimized)
121140
temporaryMaximize.invoke()
122141
else
123142
onClick.invoke()
124143
}
125-
.border(BorderStroke(1f.dp, Color(0xffffffff)), RoundedCornerShape(24.dp))
144+
.border(BorderStroke(1f.dp, Color(0xffffffff)), RoundedCornerShape(16.dp))
126145
.background(
127146
Color(0xff2e2e2e).copy(
128147
alpha = if (isMinimized) .3f else .9f

0 commit comments

Comments
 (0)