Nucleus
Input

Trackpad gestures

Handle trackpad scrolling, pinch-to-zoom and rotation with standard Compose modifiers.

View as Markdown

Trackpad gestures reach Compose as standard pointer events. Modifier.scrollable, the lazy lists and Modifier.transformable handle them with no extra code, and you add no dependency beyond nucleus-application.

A pinch zooms the poster, a two-finger scroll moves it, a rotation turns it; the bar names the gesture the window receives

Events per gesture

GesturemacOSWindowsLinux
Two-finger scrollPanStart / PanMove / PanEndPanStart / PanMove / PanEnd (precision touchpad)Scroll
Mouse wheelScrollScrollScroll
Pinch, Ctrl + wheelScaleStart / ScaleChange / ScaleEndScale*Scale*
Rotationtwo Touch pointersnonetwo Touch pointers

Scroll

Scrolling containers work as they are. Only a custom pointerInput handler needs care: on macOS and on a Windows precision touchpad a swipe arrives as PanMove, so a handler that listens for Scroll alone misses it.

Modifier.pointerInput(Unit) {
    awaitPointerEventScope {
        while (true) {
            val event = awaitPointerEvent()
            val change = event.changes.first()
            when (event.type) {
                PointerEventType.PanMove -> move(change.panOffset)
                PointerEventType.Scroll -> move(change.scrollDelta)
                else -> Unit
            }
        }
    }
}

To receive every trackpad step as Scroll instead, start the JVM with -Dnucleus.tao.trackpadPanEvents=false.

Changed in 3.0

On Windows, a precision touchpad now scrolls with Pan* events and the OS inertia, as on macOS. Handlers that only listen for Scroll must also handle PanMove.

Zoom with a pinch

Changed in 3.0

detectTransformGestures no longer sees a pinch. Use Modifier.transformable or listen for the Scale* events. See Migrate from 2.x.

@Composable
fun ZoomableImage() {
    var scale by remember { mutableFloatStateOf(1f) }
    val state = rememberTransformableState { zoomChange, _, _ -> scale *= zoomChange }
    Box(
        Modifier
            .fillMaxSize()
            .graphicsLayer(scaleX = scale, scaleY = scale)
            .transformable(state),
    )
}

In a custom handler, read PointerInputChange.scaleFactor on ScaleChange. It is a ratio per event: above 1f zooms in, below 1f zooms out.

When the OS cancels a pinch, for example because the system takes over the gesture, the pinch ends with ScaleEnd and Nucleus then cancels the window's pointer input. Gesture handlers see a cancellation, not a release. A trackpad pan in progress ends with PanEnd first, and the next finger movement starts a new one.

Rotate

Modifier.transformable and detectTransformGestures read the rotation angle on macOS and Linux. Windows reports no trackpad rotation. A pinch and a rotation never run at the same time: the gesture that starts first keeps the trackpad until it ends.

API reference

SymbolNotes
PointerEventType.PanStart / PanMove / PanEndmacOS trackpad and Windows precision-touchpad scroll
PointerInputChange.panOffsetPan delta in pixels
PointerEventType.ScaleStart / ScaleChange / ScaleEndPinch and Ctrl + wheel
PointerInputChange.scaleFactorZoom ratio per event
-Dnucleus.tao.trackpadPanEvents=falseSend trackpad pans as Scroll
-Dnucleus.tao.directManipulation=falseWindows: drop DirectManipulation, back to the legacy wheel emulation

What's next

Was this page helpful?
Edit on GitHub

Last updated on