Hello! I can't seem to find a way to detect the mo...
# compose-desktop
k
Hello! I can't seem to find a way to detect the mouse wheel rolling, to create a zoom function. Thanks!
i
You can use mouseScrollFilter. P.S. There still no docs for it, and this function probably will change in the future (in 1.1)
k
OK! Thanks very much for the reply Igor! πŸ˜ƒ
So when breaking changes are inevitably made with the cutting edge software, how do we as developers find out about them?
i
do we as developers find out about them?
The application won't compile, if API is used directly (to fix that, the developer of the application would just change the code) Or crashes in runtime with
MethodNotFoundException
, if API is used in some library that depends on the older Compose (the developer of the library should release a new version of it). P.S. Between Compose 1.0 and 2.0 there will be no breaking changes for non-experimental API. All we can do - is to mark some old API as Deprecated (if we introduce a better API)
k
OK. Thanks! And I guess, keep an eye on the release notes!
m
Yeah, as far as it's nicely documented I'm totally OK with any breaking change
πŸ‘πŸ» 1
k
Especially in bleeding edge stages of stuff. For me as a hobby programmer it might get frustrating, but I totally see the necessity at times. 😎
Hello again @Igor Demin. Sorry to bug you! I'm using IntelliJ IDEA Community to build a basic Compose Desktop program. Just a single file, with main() function. I find the sample code you linked, but I can't work out where in my GUI to place the
onMouseScroll
code. I've managed to get the
mouseScrollFilter
coded in. I just have an application, and Columns and a Box. Can you give me a hint? Thanks!
i
Will this help?
Copy code
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.mouse.mouseScrollFilter
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.singleWindowApplication

@OptIn(ExperimentalComposeUiApi::class)
fun main() = singleWindowApplication {
    Box(
        Modifier
            .size(200.dp)
            .background(Color.Red)
            .mouseScrollFilter { event, _ ->
                println(event.delta)
                true
            }
    )
}
πŸ‘ŒπŸ» 1
k
Thanks! I'll have a look.
Hey! Well, I got some values out, responding to the mouse scroll wheel! So I should be able to customise it from there!! Thank you so much mate! And well done with all your dev/support work. Cheers! πŸ–₯οΈπŸ€ πŸ‘πŸ»
πŸ‘ 1
Well, I got my responses to the scroll wheel! Thanks so much. It seems that
event.delta
MouseScrollUnit
is a custom type? I added
.toString() == "Line(value=-3.0"
, but is there a better way to detect forward and backward scrolling using the MouseScrollUnit directly somehow? Thanks for your help tonight! I've really made some progress. 😎
i
MouseScrollUnit can be Line or Page, depending on the system settings. better to check them both, as described here
k
OK. Thanks very very much. I'll read up on that and try to refine what I have. Cheers!! πŸ€“πŸ“–
@Igor Demin My last question will be, how do we test for positive or negative
.Line
or
.Page
values, so that we know if the wheel has been rolled forward or backward? There was listed in the sample code, lines like
MouseScrollUnit.Line/Page(1f,-1f,3f,-3f)
. Can we test that somehow? Thanks again.
i
What you mean by
test
? Just retrieve
delta
, as described here, and check it with
if
, or apply
sign
function.
k
Yes, check it with
if
was what I've done here. I'll look into the
sign
function hint. Thank you!
Well, after a gazillion attempts with the Sign function, it was an IntelliJ error tip that pointed me to Cast the event.delta value to the original MouseScrollUnit.Line value! So it's working like this:
Copy code
var zoomDir = sign((event.delta as MouseScrollUnit.Line).value)
Thank you so much @Igor Demin for your patience and knowledge! Cheers!
πŸ‘ 1