Marcin Wisniowski
12/15/2023, 7:05 PMModifier.onClick
triggers not only for left clicks, but also for right clicks, even though PointerMatcher.Primary
is used. Am I missing something about how the PointerMatcher works or is this a bug?Alexander Maryanovsky
12/16/2023, 10:06 AM@OptIn(ExperimentalFoundationApi::class)
fun main() = singleWindowApplication {
var clickCount by remember { mutableStateOf(0) }
Box(
Modifier
.fillMaxSize()
.background(if (clickCount.mod(2) == 1) Color.Red else Color.Green)
.onClick {
clickCount += 1
}
)
}
Marcin Wisniowski
12/16/2023, 1:04 PMMarcin Wisniowski
01/03/2024, 10:06 AMMarcin Wisniowski
01/03/2024, 10:06 AMAlexander Maryanovsky
01/03/2024, 10:08 AMAlexander Maryanovsky
01/03/2024, 10:09 AMAlexander Maryanovsky
01/03/2024, 10:09 AMMarcin Wisniowski
01/03/2024, 10:09 AMMarcin Wisniowski
01/03/2024, 10:31 AMonDrag
Modifier that matches both mouse buttons. Here is your example with the bug:
@OptIn(ExperimentalFoundationApi::class)
fun main() = singleWindowApplication {
var clickCount by remember { mutableStateOf(0) }
Column(
modifier = Modifier.fillMaxSize()
) {
Box(
Modifier
.fillMaxWidth()
.weight(1f)
.background(if (clickCount.mod(2) == 1) Color.Red else Color.Green)
.onClick {
clickCount += 1
}
)
Spacer(Modifier.height(100.dp))
Box(
modifier = Modifier.onDrag(matcher = PointerMatcher.Primary + PointerMatcher.mouse(PointerButton.Secondary)) {}
) {
Text("Drag")
}
}
}
The onDrag
is on an unrelated element. In my actual application it was in a different Window
which is why it felt so random: it was only happening when the other, unrelated application window was open at the same time. It also seems to happen only if the onDrag
matches both mouse buttons. If I change the matcher to only Primary or only Secondary, it's fine.Alexander Maryanovsky
01/03/2024, 10:45 AMAlexander Maryanovsky
01/03/2024, 11:02 AMAlexander Maryanovsky
01/03/2024, 11:08 AMAlexander Maryanovsky
01/03/2024, 11:22 AMPointerMatcher.Primary + …
Alexander Maryanovsky
01/03/2024, 11:24 AMfun PointerMatcher.Companion.Primary() = mouse(PointerButton.Primary) + touch + stylus + eraser
and then
PointerMatcher.Primary() + ...
Marcin Wisniowski
01/03/2024, 2:06 PMMarcin Wisniowski
01/03/2024, 2:26 PMonDrag(matcher = PointerMatcher.Primary)
Doesn't work:
onDrag(matcher = PointerMatcher.mouse(PointerButton.Primary) + PointerMatcher.touch + PointerMatcher.stylus + PointerMatcher.eraser)
Even though the second version is exactly what PointerMatcher.Primary
is.Alexander Maryanovsky
01/03/2024, 2:27 PMMarcin Wisniowski
01/03/2024, 2:28 PMAlexander Maryanovsky
01/03/2024, 3:11 PM@OptIn(ExperimentalFoundationApi::class)
fun main() = singleWindowApplication {
Box(
modifier = Modifier
.fillMaxSize()
.onDrag(
matcher = PointerMatcher.mouse(PointerButton.Primary) +
PointerMatcher.touch +
PointerMatcher.stylus +
PointerMatcher.eraser +
PointerMatcher.mouse(PointerButton.Secondary),
onDrag = {
println("Dragged $it")
}
)
) {
Text("Drag")
}
}
Alexander Maryanovsky
01/03/2024, 3:11 PMMarcin Wisniowski
01/03/2024, 4:17 PM@OptIn(ExperimentalFoundationApi::class)
fun main() = singleWindowApplication {
var center by remember { mutableStateOf(Offset.Zero) }
val springSpec = remember { spring<Offset>() }
val snapSpec = remember { snap<Offset>() }
var spec by remember { mutableStateOf<AnimationSpec<Offset>>(springSpec) }
val animatedCenter by animateOffsetAsState(Offset(center.x, center.y), spec)
Box(
modifier = Modifier
.fillMaxSize()
.onDrag(matcher = PointerMatcher.mouse(PointerButton.Primary)) {
println("Dragged: $it")
spec = snapSpec
center = center.copy()
}
) {}
}
Only prints "Dragged" once or a few times before stopping. If you use matcher = PointerMatcher.Primary
, or remember
the PointerMatcher.mouse(PointerButton.Primary)
, it works fine. Removing any of the two setters in the onDrag
makes it work again as well. Removing the unused animatedCenter
makes it work again too, but with all of it together it has the issue.Alexander Maryanovsky
01/03/2024, 4:28 PMdetectDragGestures
whenever the matcher changes.Marcin Wisniowski
01/03/2024, 9:17 PMAlexander Maryanovsky
01/03/2024, 9:39 PM