Fixed context menu showing old state when changing the list of items

This commit is contained in:
Abdelilah El Aissaoui 2024-07-13 19:10:17 +02:00
parent 24e35d01dd
commit bdb4bcb702
No known key found for this signature in database
GPG Key ID: 7587FC860F594869

View File

@ -52,7 +52,7 @@ private var lastCheck: Long = 0
private const val MIN_TIME_BETWEEN_POPUPS_IN_MS = 20
@Composable
fun ContextMenu(enabled: Boolean = true,items: () -> List<ContextMenuElement>, function: @Composable () -> Unit) {
fun ContextMenu(enabled: Boolean = true, items: () -> List<ContextMenuElement>, function: @Composable () -> Unit) {
Box(modifier = Modifier.contextMenu(enabled, items), propagateMinConstraints = true) {
function()
}
@ -69,17 +69,17 @@ fun DropdownMenu(items: () -> List<ContextMenuElement>, function: @Composable ()
@OptIn(ExperimentalComposeUiApi::class)
@Composable
private fun Modifier.contextMenu(enabled: Boolean, items: () -> List<ContextMenuElement>): Modifier {
val (contentMenuData, setContentMenuData) = remember { mutableStateOf<ContextMenuData?>(null) }
val (mouseEvent, setMouseEvent) = remember { mutableStateOf<MouseEvent?>(null) }
val modifier = this.pointerInput(enabled) {
awaitPointerEventScope {
while (true) {
val lastMouseEvent = awaitFirstDownEvent()
val mouseEvent = lastMouseEvent.awtEventOrNull
val lastPointerEvent = awaitFirstDownEvent()
val lastMouseEvent = lastPointerEvent.awtEventOrNull
if (mouseEvent != null && enabled) {
if (lastMouseEvent.button.isSecondary) {
lastMouseEvent.changes.forEach {
if (lastMouseEvent != null && enabled) {
if (lastPointerEvent.button.isSecondary) {
lastPointerEvent.changes.forEach {
it.consume()
}
@ -89,7 +89,7 @@ private fun Modifier.contextMenu(enabled: Boolean, items: () -> List<ContextMenu
} else {
lastCheck = currentCheck
setContentMenuData(ContextMenuData(items(), mouseEvent))
setMouseEvent(lastMouseEvent)
}
}
}
@ -97,25 +97,23 @@ private fun Modifier.contextMenu(enabled: Boolean, items: () -> List<ContextMenu
}
}
if (contentMenuData != null && contentMenuData.items.isNotEmpty()) {
DisableSelection {
showPopup(
contentMenuData.mouseEvent.x,
contentMenuData.mouseEvent.y,
contentMenuData.items,
onDismissRequest = { setContentMenuData(null) }
)
if (mouseEvent != null) {
val contextMenuElements = items()
if (contextMenuElements.isNotEmpty()) {
DisableSelection {
showPopup(
mouseEvent.x,
mouseEvent.y,
contextMenuElements,
onDismissRequest = { setMouseEvent(null) }
)
}
}
}
return modifier
}
class ContextMenuData(
val items: List<ContextMenuElement>,
val mouseEvent: MouseEvent,
)
@Composable
private fun Modifier.dropdownMenu(items: () -> List<ContextMenuElement>): Modifier {
val (isClicked, setIsClicked) = remember { mutableStateOf(false) }