Skip to content
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

Increase repeat rate based on Hold duration #1912

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -16,10 +16,12 @@ import androidx.compose.ui.input.pointer.pointerInput
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.math.pow
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

fun Modifier.holdRepeatClickable(
onRepeat: () -> Unit,
onRepeat: (Int) -> Unit,
onEnd: () -> Unit,
enabled: Boolean = true
) = composed {
Expand All @@ -45,22 +47,39 @@ fun Modifier.holdRepeatClickable(
val currentJob = scope.launch {
var first = false

var increment = 0
var totalRepeatInterval = 0.milliseconds
var lastIncrementTime = 0.milliseconds
val adjustmentFactor = 1.5

try {
delay(longPressTimeout)
var repeatInterval = 100.milliseconds

while (rememberedIsEnabled) {
rememberedOnRepeat()
rememberedOnRepeat(increment)
first = true

delay(repeatInterval)

totalRepeatInterval += repeatInterval

if (totalRepeatInterval >= 1.seconds && totalRepeatInterval - lastIncrementTime >= 1.seconds) {
val factor = if (totalRepeatInterval > 3.seconds) 3 else 1
increment = (totalRepeatInterval.inWholeSeconds
.toFloat()
.pow(2) * factor * adjustmentFactor).toInt()
lastIncrementTime = totalRepeatInterval
} else {
increment = 0
}

repeatInterval = (repeatInterval - repeatIntervalDelta)
.coerceAtLeast(minRepeatInterval)
}
} finally {
if (!first) {
rememberedOnRepeat()
rememberedOnRepeat(0)
}
}
}
Expand Down Expand Up @@ -90,4 +109,4 @@ fun Modifier.holdRepeatClickable(
interactionSource = interactionSource,
indication = ripple()
)
}
}
5 changes: 3 additions & 2 deletions app/src/main/java/io/github/fate_grand_automata/ui/Stepper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import io.github.fate_grand_automata.ui.prefs.StatusWrapper
import kotlin.math.sign

@Composable
private fun DeltaButton(
Expand All @@ -41,8 +42,8 @@ private fun DeltaButton(
text,
modifier = Modifier
.holdRepeatClickable(
onRepeat = {
onCurrentValueChange((currentValue + delta).coerceIn(valueRange))
onRepeat = { increment ->
onCurrentValueChange((currentValue + delta + delta.sign * increment).coerceIn(valueRange))
},
onEnd = onCommit,
enabled = isEnabled
Expand Down