<https://blog.jetbrains.com/kotlin/2022/10/compose...
# compose-desktop
v
https://blog.jetbrains.com/kotlin/2022/10/compose-multiplatform-1-2-is-out/#New_onClick_modifier How do I get the position of the Right click?
Copy code
Box(
  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))
}
o
onClick doesn’t provide the position. The mouse position can be retrieved by using Modifier.pointeInput. Here is an example:
Copy code
val 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 help
v
Thank you @Oleksandr Karpovich [JB] I also found out this other way:
Copy code
Modifier.onPointerEvent(PointerEventType.Press) { event ->
  if (event.buttons.isSecondaryPressed) {
    val clickPosition = event.changes.firstOrNull()?.position
    println("Right click: $clickPosition")
  }
  event.changes.forEach { e -> e.consume() }
}
k
Are
PointerButton.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.
o
Are
PointerButton.Secondary
and
PointerEvent.buttons.isSecondaryPressed
properly mapped to the platform-specific popup-trigger logic?
No, those are not mapped to platform-specific popup-trigger logic. Those are merely representations of pointer buttons indexes.
k
Then the handling of what is and is not a popup trigger will not be consistent with other (native, electron, web) apps across the various platforms with the proposed snippets
o
that’s true. As I see the question was rather about right click than about popup trigger.
k
Correct, but the two things are not identical. If the “right click” is the substitute for when popup menu should be shown, then it’s not the right substitute.
v
Do I have to write that logic at the moment to handle for each platform. My goal is to show a popup when the user Right clicks on Windows and whatever other variations might be for other platforms
k
For desktop-only variants where you can drop to the underlying AWT events, you can go along the lines of what I do in Aurora.
o
yes, as for now, I think
awtEventOrNull
should give the access to all the necessary details
v
Thank you Kirill, will have a look
I'm testing it on Windows but
isPopupTrigger
is always false.
Copy code
Modifier.pointerInput(Unit) {
  while (true) {
    val lastMouseEvent = awaitPointerEventScope { awaitPointerEvent() }
    val awtEventOrNull = lastMouseEvent.awtEventOrNull

    if (awtEventOrNull?.isPopupTrigger == true) {
      println("Show popup")
    }
  }
}
It actually works on Windows as well. I didn't test it properly.