Skip to content

Add swipeLogarithmicEaseStart parameter to limit swipe distance #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import kotlin.math.log
import kotlin.math.roundToInt

/**
Expand All @@ -46,6 +47,7 @@ fun SwipeableActionsBox(
startActions: List<SwipeAction> = emptyList(),
endActions: List<SwipeAction> = emptyList(),
swipeThreshold: Dp = 40.dp,
swipeLogarithmicEaseStart: Dp = 20.dp,
backgroundUntilSwipeThreshold: Color = Color.DarkGray,
content: @Composable BoxScope.() -> Unit
) = Box(modifier) {
Expand Down Expand Up @@ -75,10 +77,16 @@ fun SwipeableActionsBox(
}

val scope = rememberCoroutineScope()
val swipeLogarithmicEaseStartPx = LocalDensity.current.run { swipeThreshold.toPx() }
Box(
modifier = Modifier
.onSizeChanged { state.layoutWidth = it.width }
.absoluteOffset { IntOffset(x = state.offset.value.roundToInt(), y = 0) }
.absoluteOffset {
IntOffset(
x = state.offset.value.logarithmicEase(swipeLogarithmicEaseStartPx).roundToInt(),
y = 0
)
}
.drawOverContent { state.ripple.draw(scope = this) }
.horizontalDraggable(
enabled = !state.isResettingOnRelease,
Expand All @@ -96,7 +104,7 @@ fun SwipeableActionsBox(
ActionIconBox(
modifier = Modifier.matchParentSize(),
action = action,
offset = state.offset.value,
offset = state.offset.value.logarithmicEase(swipeLogarithmicEaseStartPx),
backgroundColor = animatedBackgroundColor,
content = { action.value.icon() }
)
Expand Down Expand Up @@ -142,3 +150,11 @@ private fun Modifier.drawOverContent(onDraw: DrawScope.() -> Unit): Modifier {
onDraw(this)
}
}

fun Float.logarithmicEase(start: Float): Float {
return when {
this in -start..start -> this
this > start -> log((this - start) / 10 + 1, 3f) * 10 + start
else -> -((-this).logarithmicEase(start))
}
}