separate safe mode button into component

This commit is contained in:
qimiko committed 2024-06-16 02:45:41 -07:00
1 parent b1daf9c6b1
commit 03bb6cd8b8
1 file changed
+50 -33
@@ -6,6 +6,7 @@ import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
@@ -152,6 +153,49 @@ fun onSettings(context: Context) {
context.startActivity(launchIntent)
}
@Composable
fun LongPressButton(onClick: () -> Unit, onLongPress: () -> Unit, enabled: Boolean = true, modifier: Modifier = Modifier, content: @Composable RowScope.() -> Unit) {
// compose apis don't provide a good way of adding long press to a button
var isLongPress by remember { mutableStateOf(false) }
val interactionSource = remember { MutableInteractionSource() }
val viewConfiguration = LocalViewConfiguration.current
val haptics = LocalHapticFeedback.current
LaunchedEffect(interactionSource) {
interactionSource.interactions.collectLatest { interaction ->
when (interaction) {
is PressInteraction.Press -> {
delay(viewConfiguration.longPressTimeoutMillis)
// perform a second delay to make the action more obvious
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
delay(viewConfiguration.longPressTimeoutMillis)
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
isLongPress = true
onLongPress()
}
is PressInteraction.Release -> {
if (!isLongPress) {
onClick()
}
isLongPress = false
}
}
}
}
Button(
onClick = {},
enabled = enabled,
modifier = modifier,
interactionSource = interactionSource,
content = content
)
}
@Composable
fun PlayButton(
stopAutomaticLaunch: Boolean,
@@ -187,42 +231,15 @@ fun PlayButton(
}
}
// compose apis don't provide a good way of adding long press to a button
val interactionSource = remember { MutableInteractionSource() }
val viewConfiguration = LocalViewConfiguration.current
val haptics = LocalHapticFeedback.current
LaunchedEffect(interactionSource) {
interactionSource.interactions.collectLatest { interaction ->
when (interaction) {
is PressInteraction.Press -> {
launchInSafeMode = false
delay(viewConfiguration.longPressTimeoutMillis)
// perform a second delay to make the action more obvious
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
delay(viewConfiguration.longPressTimeoutMillis)
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
Row {
LongPressButton(
enabled = !blockLaunch,
onLongPress = {
showSafeModeDialog = true
}
is PressInteraction.Release -> {
if (!showSafeModeDialog) {
},
onClick = {
onPlayGame(launchInSafeMode)
}
}
}
}
}
Row {
Button(
onClick = { },
enabled = !blockLaunch,
interactionSource = interactionSource
) {
Icon(
Icons.Filled.PlayArrow,