Rewrite the launcher in MoonBit (old code moves to legacy/) (#1)
This commit was merged in pull request #1.
This commit is contained in:
1 parent
7d2bf5996f
commit
2fa7bd745b
150 files changed
+8319
-462
No files matched your search
+495
@@ -0,0 +1,495 @@
|
||||
///|
|
||||
/// The page runtime: renders `@app.view` into the DOM, turns pointer, touch and key
|
||||
/// events into `@app` input, runs the effects it returns against the host (Android's
|
||||
/// `window.Android`, or stand-ins that show toasts in a desktop browser), and exposes
|
||||
/// `window.launcher.*` for the Android shell to call.
|
||||
|
||||
///|
|
||||
let model : @app.Model = @app.Model::new()
|
||||
|
||||
///|
|
||||
let state : Ref[PageState] = Ref(PageState::new())
|
||||
|
||||
///|
|
||||
priv struct PageState {
|
||||
mut tree : Array[@vdom.Node[@app.Msg]]
|
||||
mut table : Array[@vdom.Handlers[@app.Msg]]
|
||||
mut rendered : Array[RNode]
|
||||
mut render_pending : Bool
|
||||
mut focus_pending : Bool
|
||||
mut placed : String
|
||||
mut excluded : String
|
||||
mut cover_y : Int32
|
||||
mut cover_last : Double
|
||||
mut cover_loop : Bool
|
||||
mut measured : String
|
||||
mut touch : (Double, Double, Double)?
|
||||
mut demo_widget : Int32
|
||||
mut http_tag : Int32
|
||||
http : Map[Int32, (Bool, String) -> Unit]
|
||||
icon_src : Map[String, String]
|
||||
icon_asked : Map[String, Bool]
|
||||
icon_tries : Map[String, Int32]
|
||||
}
|
||||
|
||||
///|
|
||||
fn PageState::new() -> PageState {
|
||||
{
|
||||
tree: [],
|
||||
table: [],
|
||||
rendered: [],
|
||||
render_pending: false,
|
||||
focus_pending: false,
|
||||
placed: "",
|
||||
excluded: "[]",
|
||||
cover_y: 0,
|
||||
cover_last: 0.0,
|
||||
cover_loop: false,
|
||||
measured: "",
|
||||
touch: None,
|
||||
demo_widget: 100,
|
||||
http_tag: 0,
|
||||
http: {},
|
||||
icon_src: {},
|
||||
icon_asked: {},
|
||||
icon_tries: {},
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn dispatch(msg : @app.Msg) -> Unit {
|
||||
run(@app.update(model, msg))
|
||||
}
|
||||
|
||||
///|
|
||||
fn feed(inp : @gesture.Input) -> Unit {
|
||||
run(@app.input(model, state.val.table, inp))
|
||||
}
|
||||
|
||||
///|
|
||||
fn run(fx : Array[@app.Effect]) -> Unit {
|
||||
for e in fx {
|
||||
exec(e)
|
||||
}
|
||||
schedule_render()
|
||||
}
|
||||
|
||||
///|
|
||||
fn schedule_render() -> Unit {
|
||||
if state.val.render_pending {
|
||||
return
|
||||
}
|
||||
state.val.render_pending = true
|
||||
request_frame(render)
|
||||
}
|
||||
|
||||
///|
|
||||
fn http_get(url : String, f : (Bool, String) -> Unit) -> Unit {
|
||||
if is_android() {
|
||||
let tag = state.val.http_tag + 1
|
||||
state.val.http_tag = tag
|
||||
state.val.http[tag] = f
|
||||
h_http_get(url, tag)
|
||||
} else {
|
||||
fetch_text(url, f)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn exec(e : @app.Effect) -> Unit {
|
||||
let android = is_android()
|
||||
match e {
|
||||
Launch(c) => if android { h_launch(c) } else { toast("Open " + c) }
|
||||
AppInfo(c) =>
|
||||
if android {
|
||||
h_app_info(c)
|
||||
} else {
|
||||
toast("App info for " + c)
|
||||
}
|
||||
Uninstall(c) =>
|
||||
if android {
|
||||
h_uninstall(c)
|
||||
} else {
|
||||
toast("Uninstall " + c)
|
||||
}
|
||||
AddWidget(p) =>
|
||||
if android {
|
||||
h_add_widget(p)
|
||||
} else {
|
||||
let id = state.val.demo_widget
|
||||
state.val.demo_widget = id + 1
|
||||
set_timeout(0, fn() { dispatch(WidgetAdded(id)) })
|
||||
}
|
||||
RemoveWidget(id) => if android { h_remove_widget(id) }
|
||||
OpenSettings =>
|
||||
if android {
|
||||
h_open_settings()
|
||||
} else {
|
||||
toast("Open system settings")
|
||||
}
|
||||
PickWallpaper =>
|
||||
if android {
|
||||
h_pick_wallpaper()
|
||||
} else {
|
||||
toast("Pick a wallpaper")
|
||||
}
|
||||
OpenRecents =>
|
||||
if android {
|
||||
h_open_recents()
|
||||
} else {
|
||||
toast("Show open windows")
|
||||
}
|
||||
Haptic => h_haptic()
|
||||
LoadApps =>
|
||||
fetch_text("apps.json", fn(ok, text) {
|
||||
dispatch(AppsLoaded(if ok { Some(text) } else { None }))
|
||||
})
|
||||
LoadWidgets =>
|
||||
fetch_text("widgets.json", fn(ok, text) {
|
||||
dispatch(WidgetsLoaded(if ok { Some(text) } else { None }))
|
||||
})
|
||||
QueryLimits(id) => {
|
||||
let json = if android {
|
||||
h_widget_limits(id)
|
||||
} else {
|
||||
"{\"minW\":120,\"minH\":80,\"maxW\":0,\"maxH\":0,\"h\":true,\"v\":true}"
|
||||
}
|
||||
dispatch(LimitsKnown(id, Some(json)))
|
||||
}
|
||||
SaveHome(json) => storage_set("launcher.home", json)
|
||||
SaveLastCheck(t) => storage_set("launcher.update.lastCheck", t.to_string())
|
||||
Delay(ms, msg) => set_timeout(ms, fn() { dispatch(msg) })
|
||||
HoldTimer(ms, token) => set_timeout(ms, fn() { feed(Hold(token)) })
|
||||
FetchRelease(url) =>
|
||||
if android {
|
||||
http_get(url, fn(ok, text) {
|
||||
dispatch(ReleaseFetched(if ok { Some(text) } else { None }))
|
||||
})
|
||||
} else {
|
||||
toast("Check for updates")
|
||||
set_timeout(0, fn() { dispatch(ReleaseFetched(None)) })
|
||||
}
|
||||
FetchVersion(url) =>
|
||||
http_get(url, fn(ok, text) {
|
||||
dispatch(VersionFetched(if ok { Some(text) } else { None }))
|
||||
})
|
||||
InstallApk(url, n) =>
|
||||
if android {
|
||||
h_install_apk(url, n)
|
||||
} else {
|
||||
toast("Install update \{n}")
|
||||
}
|
||||
FocusSearch => state.val.focus_pending = true
|
||||
HideKeyboard => {
|
||||
blur_active()
|
||||
h_hide_keyboard()
|
||||
}
|
||||
ScrollTop => scroll_top()
|
||||
}
|
||||
}
|
||||
|
||||
// ---- rendering ---------------------------------------------------------------------
|
||||
|
||||
///|
|
||||
fn render() -> Unit {
|
||||
let st = state.val
|
||||
st.render_pending = false
|
||||
st.tree = @app.view(model)
|
||||
st.table = @app.handler_table(st.tree)
|
||||
st.rendered = patch_root(dom_root(), st.rendered, st.tree)
|
||||
after_render()
|
||||
}
|
||||
|
||||
///|
|
||||
fn after_render() -> Unit {
|
||||
let st = state.val
|
||||
fill_icons()
|
||||
if measure() {
|
||||
let key = "\{measured(0)},\{measured(1)},\{measured(2)},\{measured(3)},\{measured(4)},\{measured(5)}"
|
||||
if key != st.measured {
|
||||
st.measured = key
|
||||
dispatch(
|
||||
Measured(
|
||||
vw=measured(0).to_int(),
|
||||
vh=measured(1).to_int(),
|
||||
grid_left=measured(2),
|
||||
grid_top=measured(3),
|
||||
grid_w=measured(4),
|
||||
bottom_pad=measured(5),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
if is_android() {
|
||||
place()
|
||||
cover()
|
||||
}
|
||||
if st.focus_pending {
|
||||
st.focus_pending = false
|
||||
focus_search()
|
||||
h_show_keyboard()
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Tell the shell where the live widget slots are, and keep its edge swipes off the
|
||||
/// resize handles.
|
||||
fn place() -> Unit {
|
||||
let st = state.val
|
||||
let json = widget_slots()
|
||||
// The drawer unmarks the slots, but the home under it doesn't move, so the widgets
|
||||
// stay where they were and `cover` hides them as the drawer slides over them.
|
||||
if json != st.placed && !has_drawer() {
|
||||
st.placed = json
|
||||
h_place_widgets(json)
|
||||
}
|
||||
let handles = resize_handles()
|
||||
if handles != st.excluded {
|
||||
st.excluded = handles
|
||||
h_exclude_gestures(handles)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Native widgets sit above the page, so the drawer can't cover them by itself. While
|
||||
/// it slides down the shell hides them above its bottom edge each frame, a little ahead
|
||||
/// of it (by its speed), since the shell trails a frame.
|
||||
fn cover() -> Unit {
|
||||
let st = state.val
|
||||
let y = drawer_cover(st.cover_last).to_int()
|
||||
st.cover_last = drawer_bottom()
|
||||
if y != st.cover_y {
|
||||
st.cover_y = y
|
||||
h_cover_widgets(y)
|
||||
}
|
||||
if y != 0 && y != -1 && !st.cover_loop {
|
||||
st.cover_loop = true
|
||||
request_frame(fn() {
|
||||
state.val.cover_loop = false
|
||||
cover()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ---- icons -------------------------------------------------------------------------
|
||||
|
||||
///|
|
||||
/// Each `<img class="icon" data-icon=KEY>` gets its src from here. On a phone the shell
|
||||
/// draws icons on its own thread and hands them back as data: URIs in batches, so the
|
||||
/// WebView never loads icons through request interception; in a desktop browser the key
|
||||
/// already is a URL.
|
||||
fn fill_icons() -> Unit {
|
||||
let st = state.val
|
||||
let n = icons_scan()
|
||||
let want : Array[Json] = []
|
||||
let loader = h_has_load_icons()
|
||||
for i in 0..<n {
|
||||
if icon_done(i) {
|
||||
continue
|
||||
}
|
||||
let k = icon_key(i)
|
||||
if !loader {
|
||||
icon_set(i, k)
|
||||
} else if st.icon_src.get(k) is Some(src) {
|
||||
icon_set(i, src)
|
||||
} else if !st.icon_asked.contains(k) {
|
||||
st.icon_asked[k] = true
|
||||
want.push(Json::string(k))
|
||||
}
|
||||
}
|
||||
if want.length() > 0 {
|
||||
h_load_icons(Json::array(want).stringify())
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// A batch from the shell: `{key: dataUri}`. A key it couldn't draw ("") is asked for
|
||||
/// again, up to 3 times, with backoff; until then, and after, it shows as a blank disc.
|
||||
fn got_icons(json : String) -> Unit {
|
||||
let st = state.val
|
||||
let mut retry = 0
|
||||
json_entries(json, fn(k, src) {
|
||||
st.icon_src[k] = src
|
||||
st.icon_asked.remove(k)
|
||||
let tries = st.icon_tries.get(k).unwrap_or(0)
|
||||
if src == "" && tries < 3 {
|
||||
st.icon_tries[k] = tries + 1
|
||||
retry = tries + 1
|
||||
}
|
||||
})
|
||||
fill_icons()
|
||||
if retry > 0 {
|
||||
set_timeout(800 * retry, fn() {
|
||||
let keys : Array[String] = []
|
||||
for kv in st.icon_src {
|
||||
if kv.1 == "" {
|
||||
keys.push(kv.0)
|
||||
}
|
||||
}
|
||||
for k in keys {
|
||||
st.icon_src.remove(k)
|
||||
}
|
||||
icons_reset()
|
||||
fill_icons()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ---- input -------------------------------------------------------------------------
|
||||
|
||||
///|
|
||||
fn parse_chain(s : String) -> Array[Int32] {
|
||||
if s == "" {
|
||||
return []
|
||||
}
|
||||
let out : Array[Int32] = []
|
||||
for part in s.split(",") {
|
||||
out.push(@string.parse_int(part) catch { _ => -1 })
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
///|
|
||||
fn is_dragging() -> Bool {
|
||||
model.lifted >= 0 || model.resize is Some(_)
|
||||
}
|
||||
|
||||
///|
|
||||
fn wire_events() -> Unit {
|
||||
listen("document", "pointerdown", true, fn(e) {
|
||||
if ev_primary(e) {
|
||||
feed(Down(x=ev_x(e), y=ev_y(e), chain=parse_chain(ev_chain(e))))
|
||||
}
|
||||
})
|
||||
listen("document", "pointermove", true, fn(e) {
|
||||
feed(Move(x=ev_x(e), y=ev_y(e)))
|
||||
})
|
||||
listen("document", "pointerup", true, fn(e) { feed(Up(x=ev_x(e), y=ev_y(e))) })
|
||||
listen("document", "pointercancel", true, fn(_) { feed(Cancel) })
|
||||
// keep the page from scrolling under a lifted slot or a handle being dragged
|
||||
listen("document", "touchmove", false, fn(e) {
|
||||
if is_dragging() {
|
||||
ev_prevent(e)
|
||||
}
|
||||
})
|
||||
listen("document", "contextmenu", false, fn(e) { ev_prevent(e) })
|
||||
listen("document", "input", true, fn(e) {
|
||||
run(
|
||||
@app.typed(model, state.val.table, parse_chain(ev_chain(e)), ev_value(e)),
|
||||
)
|
||||
})
|
||||
listen("document", "keydown", true, fn(e) {
|
||||
if ev_composing(e) {
|
||||
return
|
||||
}
|
||||
let k = ev_key(e)
|
||||
if k == "Enter" {
|
||||
run(@app.key(model, state.val.table, parse_chain(ev_chain(e)), Enter))
|
||||
} else if k == "Escape" {
|
||||
run(@app.key(model, state.val.table, parse_chain(ev_chain(e)), Escape))
|
||||
}
|
||||
})
|
||||
// Swipes: down on the home opens the drawer, up closes it once its list is at the end.
|
||||
listen("document", "touchstart", true, fn(e) {
|
||||
state.val.touch = if ev_touches(e) == 1 {
|
||||
Some((ev_touch_x(e), ev_touch_y(e), now_ms()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
listen("document", "touchend", true, fn(e) {
|
||||
match state.val.touch {
|
||||
Some((x0, y0, t0)) => {
|
||||
state.val.touch = None
|
||||
dispatch(
|
||||
Touch(
|
||||
dx=ev_touch_x(e) - x0,
|
||||
dy=ev_touch_y(e) - y0,
|
||||
ms=(now_ms() - t0).to_int(),
|
||||
home_top=home_at_top(),
|
||||
list_end=list_at_end(),
|
||||
),
|
||||
)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
})
|
||||
listen("window", "resize", true, fn(_) { schedule_render() })
|
||||
listen("window", "scroll", true, fn(_) {
|
||||
if is_android() {
|
||||
request_frame(place)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
///|
|
||||
/// What the Android shell calls: `window.launcher.*`.
|
||||
fn expose_api() -> Unit {
|
||||
expose_init()
|
||||
expose0("reload", fn() {
|
||||
state.val.icon_src.clear()
|
||||
state.val.icon_asked.clear()
|
||||
state.val.icon_tries.clear()
|
||||
icons_reset()
|
||||
dispatch(Reload)
|
||||
})
|
||||
expose0("home", fn() { dispatch(GoHome) })
|
||||
expose0("back", fn() { dispatch(Back) })
|
||||
expose1("widgetAdded", fn(id) { dispatch(WidgetAdded(id)) })
|
||||
expose1("widgetMenu", fn(id) { dispatch(WidgetMenu(id)) })
|
||||
expose1("widgetGrab", fn(id) { dispatch(WidgetGrab(id)) })
|
||||
expose_dd("widgetDrag", fn(x, y) { dispatch(NativeDrag(x, y)) })
|
||||
expose_b("widgetDrop", fn(moved) { dispatch(NativeDrop(moved)) })
|
||||
expose0("place", fn() {
|
||||
state.val.placed = ""
|
||||
place()
|
||||
})
|
||||
expose_json("icons", got_icons)
|
||||
expose_result("httpResult", fn(tag, ok, text) {
|
||||
if state.val.http.get(tag) is Some(f) {
|
||||
state.val.http.remove(tag)
|
||||
f(ok, text)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
///|
|
||||
fn num(key : String) -> Int64 {
|
||||
let s = storage_get(key)
|
||||
if s == "" {
|
||||
0L
|
||||
} else {
|
||||
@string.parse_int64(s) catch {
|
||||
_ => 0L
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn main {
|
||||
if !is_android() {
|
||||
mark_demo()
|
||||
}
|
||||
expose_api()
|
||||
wire_events()
|
||||
let saved = if storage_has("launcher.home") {
|
||||
Some(storage_get("launcher.home"))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
let legacy = if saved is None { Some(storage_legacy()) } else { None }
|
||||
dispatch(Restore(saved~, legacy~))
|
||||
if is_android() {
|
||||
// the shell keeps the build whose install the user cancelled (-1: none)
|
||||
let declined = h_declined_version()
|
||||
dispatch(
|
||||
Started(
|
||||
now=now_ms().to_int64(),
|
||||
last_check=num("launcher.update.lastCheck"),
|
||||
declined~,
|
||||
installed=h_installed_version(),
|
||||
),
|
||||
)
|
||||
}
|
||||
render()
|
||||
}
|
||||
Reference in new issue
Block a user