https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
k

Kebbin

12/05/2021, 10:10 AM
Hello, I'm just trying 1.0.0 and I can't get objects to pick up the mouse wheel (
.isTertiaryPressed
) button anymore. I was using the
Modifier.pointerInput(Unit)
in 1.0.0-beta5 with Kotlin 1.5.31, and tried just dropping the Compose
-beta5
in my Gradle file to upgrade to the release version. I also tried
.onPointerEvent(PointerEventType.Press)
in 1.0.0, but still it doesn't work. The scroll wheel rotation still works, and the Primary button presses OK. When pressing the wheel button while holding the Primary button down first, my debugging will show that the middle button is being pressed OK, but my conditional code is not running.
i

Igor Demin

12/05/2021, 10:19 AM
There were some changes in pointer events behavior. Does this snippet work on your machine?
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.*
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.singleWindowApplication

@OptIn(ExperimentalComposeUiApi::class)
fun main() = singleWindowApplication {
    var text by remember { mutableStateOf("Press me") }

    Box(
        Modifier
            .fillMaxSize()
            .onPointerEvent(PointerEventType.Press) {
                val position = it.changes.first().position
                text = when {
                    it.buttons.isPrimaryPressed && it.buttons.isTertiaryPressed -> "Left+Middle click $position"
                    it.buttons.isPrimaryPressed -> "Left click $position"
                    it.buttons.isTertiaryPressed -> "Middle click $position"
                    else -> text
                }
            },
        contentAlignment = Alignment.Center
    ) {
        Text(text, fontSize = 30.sp)
    }
}
k

Kebbin

12/05/2021, 10:21 AM
I thought there might have been, so went and looked at that page. I didn't try that code verbatim, but used that structure, and guidance from the Plug-In prompts.
i

Igor Demin

12/05/2021, 10:21 AM
but my conditional code is not running
could your show the condition code?
k

Kebbin

12/05/2021, 10:22 AM
//                .onPointerEvent(PointerEventType.Press) {
//                    val clicks = it.buttons
//                    val keys = it.keyboardModifiers
//                    val wheelBtn = clicks.isTertiaryPressed
//                    val ctrlBtn = keys.isCtrlPressed
//
//                    val pan = it.calculatePan()
//
//                    val panning = wheelBtn && !ctrlBtn
//                    val rotating = wheelBtn && ctrlBtn
//
//                    if (panning) offset += pan
//
//                    if (rotating) {
//                        rotationZ -= pan.x
//                        rotationX += pan.y
//                        if (rotationX > maxTilt) rotationX = maxTilt
//                        if (rotationX < minTilt) rotationX = minTilt
//                        if (rotationZ > 180) rotationZ -= 360
//                        if (rotationZ < -179) rotationZ += 360
//                    }
//
//                    // This resets the view in case of mad pan-zoom-rotes!
//                    if (clicks.isPrimaryPressed) {
//                        scale = 1f
//                        rotationX = 0f
//                        rotationZ = 0f
//                        offset = Offset.Zero
//                    }
//
//                }
That is under a Surface( modifier = Modifier .......
The
.onPointerEvent()
code wouldn't work when I tried going back to -beta5.
i

Igor Demin

12/05/2021, 10:30 AM
if (clicks.isPrimaryPressed) {
It should be
if (!panning && !rotating && clicks.isPrimaryPressed) {
When we click with the wheel button, and the left button is still pressed,
isPrimaryPressed
will return true We have fixed a bug, when onPointerEvent/pointerEvent wrongly reported pressed buttons, so it is probably because of that your code behaves differently in 1.0.0.
k

Kebbin

12/05/2021, 10:31 AM
I expected changes, so not an issue. I'll cogitate what you've written and try that. Thanks Igor for your help again, and congrats on the release!
i

Igor Demin

12/05/2021, 10:31 AM
if (!panning && !rotating && clicks.isPrimaryPressed)
or not. one minute 🙂
k

Kebbin

12/05/2021, 10:31 AM
😁
.pointerInput(Unit) {
                    forEachGesture {
                        awaitPointerEventScope {
                            awaitFirstDown()
                            do {
                                val event = awaitPointerEvent()
                                val clicks = event.buttons
                                val keys = event.keyboardModifiers
                                val wheelBtn = clicks.isTertiaryPressed
                                val ctrlBtn = keys.isCtrlPressed

                                val pan = event.calculatePan()
                                val panning = wheelBtn && !ctrlBtn
                                val rotating = wheelBtn && ctrlBtn

                                if (panning) offset += pan

                                if (rotating) {
                                    rotationZ -= pan.x
                                    rotationX += pan.y
                                    if (rotationX > maxTilt) rotationX = maxTilt
                                    if (rotationX < minTilt) rotationX = minTilt
                                    if (rotationZ > 180) rotationZ -= 360
                                    if (rotationZ < -179) rotationZ += 360
                                }

                                // This resets the view in case of mad pan-zoom-rotes!
                                if (clicks.isPrimaryPressed) {
                                    scale = 1f
                                    rotationX = 0f
                                    rotationZ = 0f
                                    offset = Offset.Zero
                                }
                                println("$wheelBtn $ctrlBtn")
                                //println("Pan $panning Rot $rotating rotX $rotationX rotZ $rotationZ")
                                //println("Pan $pan Pan2 $pan2 Dist $panDist Dir ${panDir * 180 / PI} Scale $scale rotX $rotationX rotZ $rotationZ")
                            } while (event.changes.any { it.pressed })
                        }
                    }
                }
This was my original code that worked perfectly in beta5.
I've gone back to beta5 so I can continue working on some other conceptual stuff.
i

Igor Demin

12/05/2021, 10:38 AM
Resetting with the left button should happen, even if the middle button is pressed, right? In that case, the condition should be:
if (clicks.isPrimaryPressed && !isPrimaryPressedPrevious) {
     println("reset")
}
isPrimaryPressedPrevious = clicks.isPrimaryPressed
Where
isPrimaryPressedPrevious
is:
var isPrimaryPressedPrevious by remember { mutableStateOf(false) }
It is somewhat inconvenient, but probably this is the only way, when we need to work with multiple pressed buttons.
k

Kebbin

12/05/2021, 10:40 AM
Hmm. OK, I'll look that up. My main issue was that even with no buttons pressed, the wheel button was causing no response. I better head to bed, so thanks for your help!
The resetting with the left button was just a tool I built in while debugging.
i

Igor Demin

12/05/2021, 10:41 AM
the wheel button was causing no response.
not sure, but that is probably because we reset after we try to rotate
k

Kebbin

12/05/2021, 10:41 AM
I could've made it anything else, but it was just convenient.
Reset the Composable after rotating, you mean?
I wonder if it's something to do with the
.forEachGesture
code that I got from a sample online? Separating out each click as a separate event?
i

Igor Demin

12/05/2021, 10:44 AM
When the left button is pressed, and we try to rotate with the middle button, we change rotationZ/rotationX in
if (rotating)
condition, but after that we execute code in
if (clicks.isPrimaryPressed)
condition, which sets rotationZ/rotationX to 0.0
k

Kebbin

12/05/2021, 10:46 AM
OK, but if I don't touch the left button, and try to rotate the view with only the middle button and Ctrl key, it still doesn't do anything? It should only reset the view if the mouse is moved with the left button down?
i

Igor Demin

12/05/2021, 10:46 AM
forEachGesture
probably, it checks the state of
it.changes.first().changedToDown()
. and we had a bug in it, when we press multiple buttons.
it still doesn't do anything
Oh, in that case, it is something else.
k

Kebbin

12/05/2021, 10:47 AM
Ah, that might be part of it then, but still it should work with the new
.onPointerEvent
code I wrote. Which should I be using?
.onPointerEvent
or
.pointerInput
?
As a side note, how do you do that Quote Reply business you've done there?
With the grey bar, quoting my text?
Oh, in that case, it is something else.
Found it! 🤓 OK, well I'll have to leave it with you mate! Goodnight!
🎉 1
i

Igor Demin

12/05/2021, 10:52 AM
onPointerEvent
is basically a simplified
pointerInput
(you can look at the code inside). I would recommend to use
onPointerEvent
(
pointerInput
can be helpful for more complex event handling, that is based on couroutines) Also,
forEachGesture
doesn't work well with multiple pressed buttons, so I suggest to avoid it.
k

Kebbin

12/05/2021, 10:53 AM
OK thanks very much. 😎👍🏻
i

Igor Demin

12/05/2021, 10:53 AM
As a side note, how do you do that Quote Reply business you've done there?
Just type ">", and Slack transforms it to quote 🙂
k

Kebbin

12/05/2021, 10:54 AM
Oh! That's easy then! Thanks!
Cool!
🤩 1
🙂 1
Bold italic strike
code.segment
Quote!
😄
💤
Ha! I got things to move again Igor!! I needed to use
.onPointerEvent(PointerEventType.Move) {
instead of
.Press)
!! I discovered all the buttons and keyModifiers were working fine, but I was getting no
.calculatePan()
, so I looked into the PointerEventTypes available. Looks like I might be able to use the
.Scroll
to detect the mouse wheel, instead of
.mouseScrollFilter
? What would you recommend? Also on that thought, is there any issue with having multiple
.onPointerEvent
modifiers, or is that just the way to do it? I guess it is the only option, as you'd need different types of
PointerEventType
parameters for different input types....
In my old code from the online sample I used, it had a do {} while () loop to keep tracking the mouse while the wheel button was down. This one is much neater!
i

Igor Demin

12/07/2021, 9:46 AM
I needed to use 
.onPointerEvent(PointerEventType.Move) {
  instead of 
.Press)
Oh, right, the small detail that didn’t catch an eye 🙂
Looks like I might be able to use the 
.Scroll
 to detect the mouse wheel, instead of 
.mouseScrollFilter
 ? What would you recommend?
mouseScrollFilter
will be deprecated in 1.1 or in 1.2, better to use
.Scroll
.
or is that just the way to do it
Yes, that is the way in which
.onPointerEvent
should be used. Each
onPointerEvent
creates a separate coroutine, so if your application is big, it can create 500 coroutines instead of 200 (if we would use
pointerInput
). And it doesn’t sound scary, because coroutines are lightweight.
k

Kebbin

12/07/2021, 9:46 AM
All good! Ah, OK, thanks for the heads up! I'm having trouble finding the delta extraction anyway!!
Awesome. A nicely scaling infrastructure then! Thanks mate! I'll try
.Scroll
now....
Actually, yes
PointerEventType.Scroll
is what I'm working on now. I thought you meant a .Scroll modifier! 🥴
I can't find how to extract the scroll delta.
Ah, I think I have it now. I found a section on Mouse scroll listeners in the GitHub 1.0.0 release docs. Thanks to whoever has been updating all those docs!!!!!!!!! That is a massive job, and one I'm supposed to be helping with on our hobby project, but I keep getting distracted lately with coding!! 🤓
🙂 1
Yep, all working. Got what I needed! Thanks!
Maaaate!! And I've got "Click & Drag to New Position" working! Sooo easy! Not like in T...oFX like I was trying to use earlier...... 🤭
If only we can sort out the touch targets at X / Y rotated angles........ Not that I would know enough to help, but where would I look to find the code that manages touch targets??
Sorry, I'll ask the others in the thread where we were talking about it!