Files
launcher/gesture/gesture.mbt
SpriteandClaude Sonnet 5.5 17f81c60c8 moon fmt
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 01:08:53 +00:00

150 lines
3.9 KiB
MoonBit

///|
/// Pointer gestures as a pure state machine. The platform layer reports raw pointer
/// input (down, move, up, cancel, and the long-press timer firing); this decides what
/// it was and answers with `Out`s. It knows nothing about what was touched: a target is
/// a `chain` of node ids from the innermost element up, and the app resolves them.
///
/// - a stationary hold for `hold_ms` is a *press* (long press); moving more than
/// `slop` px first cancels it
/// - a press that ends without moving is a *release* with `moved = false`
/// (the home screen opens the item's menu)
/// - letting go without a hold or a move is a *tap*
/// - once the pointer has moved past the slop, every move is a *drag*
/// (whether it means anything depends on what the app has lifted)
///|
/// How long a stationary hold takes to become a long press.
pub let hold_ms : Int32 = 500
///|
/// How far (px) the pointer may travel before it stops being a tap or a hold.
pub let slop : Double = 10.0
///|
#derive(Eq, Debug)
pub(all) enum Input {
Down(x~ : Double, y~ : Double, chain~ : Array[Int32])
Move(x~ : Double, y~ : Double)
Up(x~ : Double, y~ : Double)
Cancel
/// The timer started by `Schedule(_, token)` fired.
Hold(Int32)
}
///|
#derive(Eq, Debug)
pub(all) enum Out {
/// Start a timer of this many ms, then send `Hold(token)`.
Schedule(Int32, Int32)
/// The pointer went down on this chain (the app may act at once, e.g. grab a widget).
Began(chain~ : Array[Int32], x~ : Double, y~ : Double)
Tap(chain~ : Array[Int32])
Press(chain~ : Array[Int32])
/// The pointer is at x, y; it went down at x0, y0.
Drag(x~ : Double, y~ : Double, x0~ : Double, y0~ : Double)
/// The pointer is up. `moved`: it travelled past the slop; `held`: a press fired.
Release(moved~ : Bool, held~ : Bool)
}
///|
pub(all) struct State {
mut down : Bool
mut x0 : Double
mut y0 : Double
mut moved : Bool
mut held : Bool
mut chain : Array[Int32]
mut token : Int32
}
///|
pub fn State::new() -> State {
{
down: false,
x0: 0.0,
y0: 0.0,
moved: false,
held: false,
chain: [],
token: 0,
}
}
///|
/// Feed one input; returns what happened.
pub fn State::step(self : State, input : Input) -> Array[Out] {
match input {
Down(x~, y~, chain~) => {
self.down = true
self.x0 = x
self.y0 = y
self.moved = false
self.held = false
self.chain = chain
self.token += 1
[Began(chain~, x~, y~), Schedule(hold_ms, self.token)]
}
Move(x~, y~) =>
if !self.down {
[]
} else {
if !self.moved {
let dx = x - self.x0
let dy = y - self.y0
if (dx * dx + dy * dy).sqrt() >= slop {
self.moved = true
self.token += 1 // a hold timer still running is stale now
}
}
if self.moved {
[Drag(x~, y~, x0=self.x0, y0=self.y0)]
} else {
[]
}
}
Up(..) | Cancel =>
if !self.down {
[]
} else {
let chain = self.chain
let moved = self.moved
let held = self.held
self.down = false
self.token += 1
let tap : Array[Out] = if !moved && !held && input is Up(..) {
[Tap(chain~)]
} else {
[]
}
[..tap, Release(moved~, held~)]
}
Hold(token) =>
if self.down && token == self.token && !self.moved && !self.held {
self.held = true
[Press(chain=self.chain)]
} else {
[]
}
}
}
///|
#derive(Eq, Debug)
pub(all) enum Swipe {
Down
Up
}
///|
/// A touch that travelled dx, dy px in dt ms is a swipe when it was quick, long enough
/// and more vertical than horizontal.
pub fn classify_swipe(dx : Double, dy : Double, dt : Int32) -> Swipe? {
if dt >= 700 || dy.abs() < 60.0 || dx.abs() > dy.abs() {
None
} else if dy > 0.0 {
Some(Down)
} else {
Some(Up)
}
}