Skip to content

Instantly share code, notes, and snippets.

@maiatoday
Last active May 26, 2022 18:43
Show Gist options
  • Save maiatoday/f2cfe9ece96c481732822128209650b7 to your computer and use it in GitHub Desktop.
Save maiatoday/f2cfe9ece96c481732822128209650b7 to your computer and use it in GitHub Desktop.
CursorVisible
@Composable
fun CursorVisible(content: @Composable () -> Unit) {
val boxSize = 100.dp
val boxPx = with(LocalDensity.current) { boxSize.toPx() }
var offset by remember { mutableStateOf(Offset(0f, 0f)) }
var visible by remember { mutableStateOf(false) }
Box(modifier = Modifier
.fillMaxSize()
.pointerInput(Unit) {
detectTapGestures {
offset = it - Offset(boxPx/2, boxPx/2)
visible = !visible
}
}
) {
if (visible) {
Box(
Modifier
.offset { offset.round() }
.size(boxSize)
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
change.consumeAllChanges()
offset += dragAmount
}
}
) {
content()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment