Tower Guidev2
11/14/2022, 2:45 PMdebounce
for clickables and have this Modifier
extension...Tower Guidev2
11/14/2022, 2:48 PMinline fun Modifier.debounceClickable(
enabled: Boolean = true,
onClickLabel: String? = null,
role: Role? = null,
debounceInterval: Long = 400L,
crossinline onClick: () -> Unit,
): Modifier = composed(
inspectorInfo = debugInspectorInfo {
name = "debounceClickable"
properties["enabled"] = enabled
properties["onClickLabel"] = onClickLabel
properties["role"] = role
properties["onClick"] = onClick()
}
) {
var lastClickTime by remember { mutableStateOf(0L) }
val currentTime = Instant.now().toEpochMilli()
clickable(
enabled = enabled,
onClickLabel = onClickLabel,
onClick = {
if ((currentTime - lastClickTime) < debounceInterval) doNothing()
else {
onClick()
lastClickTime = currentTime
}
},
role = role,
indication = LocalIndication.current,
interactionSource = remember { MutableInteractionSource() }
)
}
what can i employ for IconButtons
though?
as they use their own Modifier clickable
@Composable
fun IconButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
colors: IconButtonColors = IconButtonDefaults.iconButtonColors(),
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable () -> Unit
) {
Box(
modifier =
modifier
.minimumTouchTargetSize()
.size(IconButtonTokens.StateLayerSize)
.background(color = colors.containerColor(enabled).value)
.clickable(
onClick = onClick,
enabled = enabled,
role = Role.Button,
interactionSource = interactionSource,
indication = rememberRipple(
bounded = false,
radius = IconButtonTokens.StateLayerSize / 2
)
),
contentAlignment = Alignment.Center
) {
val contentColor = colors.contentColor(enabled).value
CompositionLocalProvider(LocalContentColor provides contentColor, content = content)
}
}
Zoltan Demant
11/15/2022, 5:10 AMModifier
. That way you can specify it to any composable that accepts onClick, etc.