Last active
May 26, 2022 18:43
-
-
Save maiatoday/f2cfe9ece96c481732822128209650b7 to your computer and use it in GitHub Desktop.
CursorVisible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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