Add the pure MoonBit core: grid, gestures, updater, vdom, app

grid: packing, pinning, resize steps (port of Layout.js and app.py's grid logic).
gesture: pointer state machine (tap, long press, drag, release, swipe).
updater: release/version parsing and the update decision.
vdom: pure virtual DOM with an HTML-text renderer for tests.
app: Elm-style Model/Msg/update/view with effects as data, persistence (JSON
layout, import of the old WebView build's localStorage), drawer/search, menus,
drag and resize. 60 headless tests drive it through pointer input.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Sonnet 5.5 committed 2026-11-23 00:55:16 +00:00
1 parent 66e8e90da1
commit 28e0284d9b
28 files changed
+4278

No files matched your search

+141
View File
@@ -0,0 +1,141 @@
///|
/// 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 : Int = 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[Int])
Move(x~ : Double, y~ : Double)
Up(x~ : Double, y~ : Double)
Cancel
/// The timer started by `Schedule(_, token)` fired.
Hold(Int)
}
///|
#derive(Eq, Debug)
pub(all) enum Out {
/// Start a timer of this many ms, then send `Hold(token)`.
Schedule(Int, Int)
/// The pointer went down on this chain (the app may act at once, e.g. grab a widget).
Began(chain~ : Array[Int], x~ : Double, y~ : Double)
Tap(chain~ : Array[Int])
Press(chain~ : Array[Int])
/// 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[Int]
mut token : Int
}
///|
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 : Int) -> Swipe? {
if dt >= 700 || dy.abs() < 60.0 || dx.abs() > dy.abs() {
None
} else if dy > 0.0 {
Some(Down)
} else {
Some(Up)
}
}
+66
View File
@@ -0,0 +1,66 @@
///|
test "a tap: down then up without moving" {
let g = State::new()
assert_eq(
g.step(Down(x=10.0, y=10.0, chain=[3, 1])),
[Began(chain=[3, 1], x=10.0, y=10.0), Schedule(500, 1)],
)
assert_eq(g.step(Up(x=11.0, y=10.0)), [Tap(chain=[3, 1]), Release(moved=false, held=false)])
}
///|
test "a long press: the timer fires, then the release is not a tap" {
let g = State::new()
ignore(g.step(Down(x=0.0, y=0.0, chain=[7])))
assert_eq(g.step(Hold(1)), [Press(chain=[7])])
assert_eq(g.step(Up(x=0.0, y=0.0)), [Release(moved=false, held=true)])
}
///|
test "moving past the slop cancels the hold and starts a drag" {
let g = State::new()
ignore(g.step(Down(x=0.0, y=0.0, chain=[7])))
assert_eq(g.step(Move(x=5.0, y=0.0)), [])
assert_eq(g.step(Move(x=20.0, y=0.0)), [Drag(x=20.0, y=0.0, x0=0.0, y0=0.0)])
assert_eq(g.step(Hold(1)), []) // the stale timer does nothing
assert_eq(g.step(Move(x=30.0, y=2.0)), [Drag(x=30.0, y=2.0, x0=0.0, y0=0.0)])
assert_eq(g.step(Up(x=30.0, y=2.0)), [Release(moved=true, held=false)])
}
///|
test "a press then a drag, then release" {
let g = State::new()
ignore(g.step(Down(x=0.0, y=0.0, chain=[7])))
ignore(g.step(Hold(1)))
assert_eq(g.step(Move(x=40.0, y=0.0)), [Drag(x=40.0, y=0.0, x0=0.0, y0=0.0)])
assert_eq(g.step(Up(x=40.0, y=0.0)), [Release(moved=true, held=true)])
}
///|
test "cancel releases without a tap; stray input is ignored" {
let g = State::new()
assert_eq(g.step(Up(x=0.0, y=0.0)), [])
assert_eq(g.step(Move(x=0.0, y=0.0)), [])
ignore(g.step(Down(x=0.0, y=0.0, chain=[1])))
assert_eq(g.step(Cancel), [Release(moved=false, held=false)])
assert_eq(g.step(Hold(1)), [])
}
///|
test "a stale timer from an earlier press is ignored" {
let g = State::new()
ignore(g.step(Down(x=0.0, y=0.0, chain=[1])))
ignore(g.step(Up(x=0.0, y=0.0)))
ignore(g.step(Down(x=0.0, y=0.0, chain=[2])))
assert_eq(g.step(Hold(1)), [])
assert_eq(g.step(Hold(3)), [Press(chain=[2])])
}
///|
test "swipes" {
assert_eq(classify_swipe(5.0, 100.0, 300), Some(Down))
assert_eq(classify_swipe(5.0, -100.0, 300), Some(Up))
assert_eq(classify_swipe(5.0, 50.0, 300), None) // too short
assert_eq(classify_swipe(120.0, 100.0, 300), None) // more sideways than down
assert_eq(classify_swipe(5.0, 100.0, 900), None) // too slow
}
View File
Whitespace-only changes.
+57
View File
@@ -0,0 +1,57 @@
// Generated using `moon info`, DON'T EDIT IT
package "experiments/launcher/gesture"
import {
"moonbitlang/core/debug",
}
// Values
pub fn classify_swipe(Double, Double, Int32) -> Swipe?
pub let hold_ms : Int32
pub let slop : Double
// Errors
// Types and methods
#derive(Eq, @debug.Debug)
pub(all) enum Input {
Down(x~ : Double, y~ : Double, chain~ : Array[Int32])
Move(x~ : Double, y~ : Double)
Up(x~ : Double, y~ : Double)
Cancel
Hold(Int32)
}
#derive(Eq, @debug.Debug)
pub(all) enum Out {
Schedule(Int32, Int32)
Began(chain~ : Array[Int32], x~ : Double, y~ : Double)
Tap(chain~ : Array[Int32])
Press(chain~ : Array[Int32])
Drag(x~ : Double, y~ : Double, x0~ : Double, y0~ : Double)
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() -> Self
pub fn State::step(Self, Input) -> Array[Out]
#derive(Eq, @debug.Debug)
pub(all) enum Swipe {
Down
Up
}
// Type aliases
// Traits