Vlad Ostaci
11/21/2022, 10:24 AMBox(
modifier = Modifier.onClick(
matcher = PointerMatcher.mouse(PointerButton.Secondary), // Right Mouse Button
onClick = {
println("Right Click")
}
)
) {
Text(text = "Right click on me", modifier = Modifier.align(Alignment.Center))
}
Oleksandr Karpovich [JB]
11/21/2022, 11:11 AMval point = remember { mutableStateOf<Point?>(null) }
Box(modifier = Modifier
.size(100.dp)
.background(Color.Yellow)
.onClick { println("Clicked at point = ${point.value}") }
.pointerInput(Unit) {
while (currentCoroutineContext().isActive) {
awaitPointerEventScope {
point.value = awaitPointerEvent().awtEventOrNull?.point
}
}
}
)
So the code above will track the mouse position within the Box, and then when click occurs we can use the saved position.
But it’s worth to mention that it “knows” only the last position, so if you press down the mouse button at 5,5 and then move it to 20,20 and release the button, then in onClick it will print [x=20,y=20]
if you need to know the first position (the position of the press event), then the code above needs to be adjusted, but pointerInput should still helpVlad Ostaci
11/21/2022, 12:58 PMModifier.onPointerEvent(PointerEventType.Press) { event ->
if (event.buttons.isSecondaryPressed) {
val clickPosition = event.changes.firstOrNull()?.position
println("Right click: $clickPosition")
}
event.changes.forEach { e -> e.consume() }
}
Kirill Grouchnikov
11/21/2022, 1:47 PMPointerButton.Secondary
and PointerEvent.buttons.isSecondaryPressed
properly mapped to the platform-specific popup-trigger logic?
For example, on macOS you should display popup on Ctrl+Left Mouse click. With Swing / AWT, that is handled in here that has special handling for it (no native code, at least not yet) and exposed in a platform-consistent fashion in MouseEvent.isPopupTrigger
.
Unless you’re willing to replicate that platform-specific logic, I would suggest either filing a feature request to add isPopupTrigger
to Compose’s PointerEvent
, or revisit your assumption that right click is identical to popup trigger.Oleksandr Karpovich [JB]
11/21/2022, 4:04 PMAreNo, those are not mapped to platform-specific popup-trigger logic. Those are merely representations of pointer buttons indexes.andPointerButton.Secondary
properly mapped to the platform-specific popup-trigger logic?PointerEvent.buttons.isSecondaryPressed
Kirill Grouchnikov
11/21/2022, 4:12 PMOleksandr Karpovich [JB]
11/21/2022, 4:14 PMKirill Grouchnikov
11/21/2022, 4:15 PMVlad Ostaci
11/21/2022, 4:15 PMKirill Grouchnikov
11/21/2022, 4:16 PMOleksandr Karpovich [JB]
11/21/2022, 4:18 PMawtEventOrNull
should give the access to all the necessary detailsVlad Ostaci
11/21/2022, 4:18 PMVlad Ostaci
11/21/2022, 7:37 PMisPopupTrigger
is always false.
Modifier.pointerInput(Unit) {
while (true) {
val lastMouseEvent = awaitPointerEventScope { awaitPointerEvent() }
val awtEventOrNull = lastMouseEvent.awtEventOrNull
if (awtEventOrNull?.isPopupTrigger == true) {
println("Show popup")
}
}
}
Vlad Ostaci
11/21/2022, 7:51 PM