# Trackpad gestures

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

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](/assets/docs/input/trackpad-gestures-macos.gif)

## Events per gesture

| Gesture | macOS | Windows | Linux |
| --- | --- | --- | --- |
| Two-finger scroll | `PanStart` / `PanMove` / `PanEnd` | `PanStart` / `PanMove` / `PanEnd` (precision touchpad) | `Scroll` |
| Mouse wheel | `Scroll` | `Scroll` | `Scroll` |
| Pinch, Ctrl + wheel | `ScaleStart` / `ScaleChange` / `ScaleEnd` | `Scale*` | `Scale*` |
| Rotation | two `Touch` pointers | none | two `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.

```kotlin
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`.

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

`detectTransformGestures` no longer sees a pinch. Use `Modifier.transformable` or listen for the
`Scale*` events. See [Migrate from 2.x](/docs/migrate/from-2.x#handle-pinch-and-pan-events).

```kotlin
@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.

- `panOffset` uses the same 10 dp per unit that Compose Desktop applies to a wheel notch, so a pan
  and a notch scroll the same distance.
- `PanEnd` arrives after macOS's momentum tail, so Compose does not start a second fling.
- Ctrl + wheel has no end phase: `ScaleEnd` follows 120 ms after the last tick. During a touchpad
  pinch on Windows, Ctrl + wheel folds into the open pinch.
- On Windows, every window binds a DirectManipulation viewport, configured like Chromium's. It
  claims precision-touchpad contacts only; touchscreens keep `WM_POINTER`. A sequence is a pan
  until its scale moves, then a pinch until it ends. `-Dnucleus.tao.directManipulation=false`
  restores the legacy wheel emulation.
- On Linux, GDK reports scale and rotation as one gesture. It starts as a pinch, and rotation takes
  over once it has turned 10° with the zoom within ±10%.
- `TaoWindow.onTrackpadGesture` exposes the raw stream (macOS and Windows). Registering a listener
  replaces the one `DecoratedWindow` uses, which stops pinch and rotation events for that window.

## API reference

| Symbol | Notes |
| --- | --- |
| `PointerEventType.PanStart` / `PanMove` / `PanEnd` | macOS trackpad and Windows precision-touchpad scroll |
| `PointerInputChange.panOffset` | Pan delta in pixels |
| `PointerEventType.ScaleStart` / `ScaleChange` / `ScaleEnd` | Pinch and Ctrl + wheel |
| `PointerInputChange.scaleFactor` | Zoom ratio per event |
| `-Dnucleus.tao.trackpadPanEvents=false` | Send trackpad pans as `Scroll` |
| `-Dnucleus.tao.directManipulation=false` | Windows: drop DirectManipulation, back to the legacy wheel emulation |

## What's next

- [Pen and touch input](/docs/input/pen-stylus): touchscreen contacts and pressure.
- [Pointer icons](/docs/input/pointer-icons): cursors beyond the four Compose defines.
- [Migrate from 2.x](/docs/migrate/from-2.x): every behavior change in 3.0.
