Fix panel drag release flicker
This commit is contained in:
1 parent
0a2f0eed5f
commit
03376af446
1 file changed
+20
-10
@@ -1,7 +1,7 @@
|
|||||||
package com.example.aiapp
|
package com.example.aiapp
|
||||||
|
|
||||||
import androidx.activity.compose.BackHandler
|
import androidx.activity.compose.BackHandler
|
||||||
import androidx.compose.animation.core.animateFloatAsState
|
import androidx.compose.animation.core.Animatable
|
||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
@@ -73,29 +73,39 @@ fun SubagentPanel(
|
|||||||
onOpenSubagent: (SubagentSummary) -> Unit,
|
onOpenSubagent: (SubagentSummary) -> Unit,
|
||||||
content: @Composable () -> Unit,
|
content: @Composable () -> Unit,
|
||||||
) {
|
) {
|
||||||
|
val scope = rememberCoroutineScope()
|
||||||
var open by remember(summary.id) { mutableStateOf(false) }
|
var open by remember(summary.id) { mutableStateOf(false) }
|
||||||
var dragging by remember(summary.id) { mutableStateOf(false) }
|
var dragging by remember(summary.id) { mutableStateOf(false) }
|
||||||
var draggedReveal by remember(summary.id) { mutableFloatStateOf(0f) }
|
var draggedReveal by remember(summary.id) { mutableFloatStateOf(0f) }
|
||||||
val animatedReveal by animateFloatAsState(if (open) 1f else 0f, label = "subagent panel")
|
val animatedReveal = remember(summary.id) { Animatable(0f) }
|
||||||
val reveal = if (dragging) draggedReveal else animatedReveal
|
val reveal = if (dragging) draggedReveal else animatedReveal.value
|
||||||
val flingThreshold = with(LocalDensity.current) { FLING_THRESHOLD.toPx() }
|
val flingThreshold = with(LocalDensity.current) { FLING_THRESHOLD.toPx() }
|
||||||
|
|
||||||
fun startDrag() {
|
suspend fun startDrag() {
|
||||||
draggedReveal = reveal
|
animatedReveal.stop()
|
||||||
|
draggedReveal = animatedReveal.value
|
||||||
dragging = true
|
dragging = true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun finishDrag(velocity: Float) {
|
suspend fun finishDrag(velocity: Float) {
|
||||||
open =
|
val targetOpen =
|
||||||
when {
|
when {
|
||||||
velocity < -flingThreshold -> true
|
velocity < -flingThreshold -> true
|
||||||
velocity > flingThreshold -> false
|
velocity > flingThreshold -> false
|
||||||
else -> draggedReveal >= OPEN_THRESHOLD
|
else -> draggedReveal >= OPEN_THRESHOLD
|
||||||
}
|
}
|
||||||
|
open = targetOpen
|
||||||
|
animatedReveal.snapTo(draggedReveal)
|
||||||
dragging = false
|
dragging = false
|
||||||
|
animatedReveal.animateTo(if (targetOpen) 1f else 0f)
|
||||||
}
|
}
|
||||||
|
|
||||||
BackHandler(enabled = open) { open = false }
|
fun setOpen(targetOpen: Boolean) {
|
||||||
|
open = targetOpen
|
||||||
|
scope.launch { animatedReveal.animateTo(if (targetOpen) 1f else 0f) }
|
||||||
|
}
|
||||||
|
|
||||||
|
BackHandler(enabled = open) { setOpen(false) }
|
||||||
|
|
||||||
BoxWithConstraints(Modifier.fillMaxSize()) {
|
BoxWithConstraints(Modifier.fillMaxSize()) {
|
||||||
val panelWidth = maxWidth * 0.88f
|
val panelWidth = maxWidth * 0.88f
|
||||||
@@ -126,7 +136,7 @@ fun SubagentPanel(
|
|||||||
.alpha(reveal * 0.32f)
|
.alpha(reveal * 0.32f)
|
||||||
.background(MaterialTheme.colorScheme.scrim)
|
.background(MaterialTheme.colorScheme.scrim)
|
||||||
.semantics { contentDescription = "Dismiss subagent panel" }
|
.semantics { contentDescription = "Dismiss subagent panel" }
|
||||||
.clickable { open = false }
|
.clickable { setOpen(false) }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,7 +154,7 @@ fun SubagentPanel(
|
|||||||
settings = settings,
|
settings = settings,
|
||||||
summary = summary,
|
summary = summary,
|
||||||
active = open,
|
active = open,
|
||||||
onClose = { open = false },
|
onClose = { setOpen(false) },
|
||||||
onOpenSubagent = onOpenSubagent,
|
onOpenSubagent = onOpenSubagent,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user