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
| CancelledRecognizer progress.
Axis
type Axis
= Horizontal
| VerticalAxis lock for onPan.
Direction
type Direction
= Up
| Down
| Left
| RightSwipe 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 msgRequest whether touch input is available right now.
Returns False when the hardware has no touchscreen or the user disabled Touch in Settings.
disableBridge
disableBridge : Cmd msgStop 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 msgReceive recognized taps with the tap point.
onPan
onPan : Axis -> (PanEvent -> msg) -> Sub msgReceive pan updates locked to axis.
onSwipe
onSwipe : List Direction -> (SwipeEvent -> msg) -> Sub msgReceive completed swipes.
The direction list is a filter hint for the host. The event still reports the recognized Direction.