Pebble.Touch

Touch gestures for Pebble watch apps (not watchfaces).

The Pebble SDK currently restricts touch to watchapps. Subscribe from Platform.application, not Platform.watchface. On watches without a touchscreen, or when the user disables Touch in Settings, supported returns False and gesture subscriptions do not fire.

import Pebble.Touch as Touch

type Msg
    = TouchSupported Bool
    | Tapped Touch.Point
    | Panned Touch.PanEvent
    | Swiped Touch.SwipeEvent

init _ =
    ( model
    , Cmd.batch
        [ Touch.supported TouchSupported
        , Touch.enableNavigation
        ]
    )

subscriptions _ =
    Sub.batch
        [ Touch.onTap Tapped
        , Touch.onPan Touch.Vertical Panned
        , Touch.onSwipe [ Touch.Left, Touch.Right ] Swiped
        ]

enableNavigation lets the system map taps and swipes to button events so existing Pebble.Button subscriptions keep working. Do not enable navigation on a window that also uses onTap / onPan / onSwipe — the system would consume those touches. Call disableBridge on a later screen when those gesture subscriptions should receive the touches again.

For a runnable example, use the watch-demo-touch project template in the IDE.

Gesture APIs require SDK/firmware 4.33+. On older firmware, supported is False and recognizers are not attached.

Types

Point

type alias Point =
    { x : Int
    , y : Int
    }

Screen-relative pixel coordinate.

Phase

type Phase
    = Started
    | Updated
    | Completed
    | Cancelled

Recognizer progress.

Axis

type Axis
    = Horizontal
    | Vertical

Axis lock for onPan.

Direction

type Direction
    = Up
    | Down
    | Left
    | Right

Swipe direction reported by onSwipe.

PanEvent

type alias PanEvent =
    { phase : Phase
    , totalX : Int
    , totalY : Int
    , sinceStartX : Int
    , sinceStartY : Int
    , sincePrevX : Int
    , sincePrevY : Int
    , velocityX : Int
    , velocityY : Int
    }

A pan (single-axis drag) update.

SwipeEvent

type alias SwipeEvent =
    { direction : Direction
    , velocityX : Int
    , velocityY : Int
    }

A completed swipe.

Commands

supported

supported : (Bool -> msg) -> Cmd msg

Request whether touch input is available right now.

Returns False when the hardware has no touchscreen or the user disabled Touch in Settings.

enableNavigation

enableNavigation : Cmd msg

Opt in to system touch navigation (taps/swipes become button events).

disableBridge

disableBridge : Cmd msg

Stop the system from turning touches into button events.

Gesture subscriptions can then receive taps, pans, and swipes on the same window that previously used enableNavigation.

Subscriptions

onTap

onTap : (Point -> msg) -> Sub msg

Receive recognized taps with the tap point.

onPan

onPan : Axis -> (PanEvent -> msg) -> Sub msg

Receive pan updates locked to axis.

onSwipe

onSwipe : List Direction -> (SwipeEvent -> msg) -> Sub msg

Receive completed swipes.

The direction list is a filter hint for the host. The event still reports the recognized Direction.