Skip to content

Commit

Permalink
library: Cancel transition on quick click
Browse files Browse the repository at this point in the history
  • Loading branch information
YuKongA committed Oct 20, 2024
1 parent 9a0b5e9 commit a85b179
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ androidx-window = "1.3.0"
compose-plugin = "1.7.0"
dokka = "1.9.20"
kotlin = "2.0.21"
kotlinx-datetime = "0.6.0"

[libraries]
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity-compose" }
androidx-window = { group = "androidx.window", name = "window", version.ref = "androidx-window" }
jetbrains-compose-window-size = { module = "org.jetbrains.compose.material3:material3-window-size-class", version.ref = "compose-plugin" }
jetbrains-kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" }

[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
Expand Down
1 change: 1 addition & 0 deletions miuix/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ kotlin {
implementation(compose.ui)
implementation(compose.components.resources)
implementation(libs.jetbrains.compose.window.size)
implementation(libs.jetbrains.kotlinx.datetime)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.ui.node.DelegatableNode
import androidx.compose.ui.node.DrawModifierNode
import androidx.compose.ui.node.invalidateDraw
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock

/**
* Miuix default [Indication] that draws a rectangular overlay when pressed.
Expand All @@ -37,6 +38,8 @@ class MiuixIndication(
private var isHovered = false
private var isFocused = false
private var isPopupExpanded = false
private var lastClickTime = 0L
private val clickThreshold = 300L
private val animatedAlpha = Animatable(0f)

override fun onAttach() {
Expand All @@ -62,6 +65,11 @@ class MiuixIndication(

is PressInteraction.Cancel, is PressInteraction.Release -> {
isPressed = false
val currentTime = Clock.System.now().toEpochMilliseconds()
if (currentTime - lastClickTime < clickThreshold) {
animatedAlpha.snapTo(0f)
}
lastClickTime = currentTime
if (!isHovered && !isFocused && !isPopupExpanded) {
animatedAlpha.animateTo(0f)
} else if (isPopupExpanded) {
Expand Down

0 comments on commit a85b179

Please sign in to comment.