Applicable XR devices
This guidance helps you build experiences for these types of XR devices.
The onIndirectPointerGesture modifier lets component receive and respond
to high-level indirect pointer events, such as those originating from a device's
touchpad. Use this modifier to capture and process gestures that come from a
touchpad or similar source.
API surface
There are four callbacks that your app can use to control handling:
onClick: Triggered on a successful tap or click without significant horizontal movement.onSwipeForward: Triggered when a horizontal swipe exceeds the distance and velocity threshold in the forward direction.onSwipeBackward: Triggered when a horizontal swipe exceeds the distance and velocity threshold in the backward direction.Enabled: When set tofalse, the modifier is ignored and no callbacks are invoked.
System behavior for swiping and scrolling
The system uses a touch slop threshold to differentiate between a click and a swipe.
- If the pointer moves significantly during a down state,
onClickis cancelled. - If the pointer backtracks significantly during a movement, the swipe gesture is invalidated.
Example: Set up handling for swipes and clicks on a component
The following code sets up handling for swipes and clicks on a focusable Box:
@Composable @Sampled fun OnIndirectPointerGestureSample() { Box( modifier = Modifier.fillMaxSize() .onIndirectPointerGesture( enabled = true, onSwipeForward = { /* onSwipeForward */ }, onSwipeBackward = { /* onSwipeBackward */ }, onClick = { /* onClick */ }, ) .focusTarget() ) { // App() } }
Key points about the code
onIndirectPointerGesturerequires focus, so thefocusTargetis also applied to make theBoxfocusable. You can usefocusTargetor another focus-enabling modifier such assurface. Without focus, the modifier can't act upon indirect pointer events.- This example implements both the
onSwipeForwardandonClickcallbacks, so swipe and click gestures that are detected are intercepted and consumed, and don't reach outwards to parent containers. However, you can also leave a specific callback null to pass through a gesture to anonIndirectPointerGesturemodifier in a parent container. -