///| /// The few places MoonBit touches the browser: one-line JavaScript bodies for DOM /// calls and the host bridge. Everything that decides anything is in the pure packages. ///| #external type Dom ///| #external type Ev // ---- DOM ---------------------------------------------------------------------- ///| extern "js" fn dom_create(tag : String) -> Dom = #|(t) => document.createElement(t) ///| extern "js" fn dom_text(s : String) -> Dom = #|(s) => document.createTextNode(s) ///| extern "js" fn dom_set_text(d : Dom, s : String) -> Unit = #|(d, s) => { d.data = s } ///| extern "js" fn dom_attr(d : Dom, k : String, v : String) -> Unit = #|(d, k, v) => d.setAttribute(k, v) ///| extern "js" fn dom_rm_attr(d : Dom, k : String) -> Unit = #|(d, k) => d.removeAttribute(k) ///| extern "js" fn dom_prop(d : Dom, k : String, v : String) -> Unit = #|(d, k, v) => { if (d[k] !== v) d[k] = v } ///| extern "js" fn dom_class(d : Dom, v : String) -> Unit = #|(d, v) => { d.className = v } ///| /// Put `d` into `parent` right before `before` (or last when `before` is not a node), /// unless it is already there. extern "js" fn dom_place( parent : Dom, d : Dom, before : Dom, has_before : Bool, ) -> Unit = #|(p, d, b, has) => { const next = has ? b : null; if (d.parentNode !== p || d.nextSibling !== next) p.insertBefore(d, next); } ///| extern "js" fn dom_remove(d : Dom) -> Unit = #|(d) => d.remove() ///| extern "js" fn dom_root() -> Dom = #|() => document.getElementById("app") // ---- events --------------------------------------------------------------------- ///| extern "js" fn listen( target : String, ty : String, passive : Bool, f : (Ev) -> Unit, ) -> Unit = #|(target, ty, passive, f) => (target === "window" ? window : document).addEventListener(ty, f, { passive, capture: true }) ///| extern "js" fn ev_x(e : Ev) -> Double = #|(e) => e.clientX ?? 0 ///| extern "js" fn ev_y(e : Ev) -> Double = #|(e) => e.clientY ?? 0 ///| extern "js" fn ev_key(e : Ev) -> String = #|(e) => e.key ?? "" ///| extern "js" fn ev_composing(e : Ev) -> Bool = #|(e) => !!e.isComposing ///| extern "js" fn ev_primary(e : Ev) -> Bool = #|(e) => e.isPrimary !== false && (e.button ?? 0) === 0 ///| extern "js" fn ev_value(e : Ev) -> String = #|(e) => e.target && e.target.value != null ? String(e.target.value) : "" ///| extern "js" fn ev_prevent(e : Ev) -> Unit = #|(e) => { if (e.cancelable) e.preventDefault() } ///| extern "js" fn ev_touch_x(e : Ev) -> Double = #|(e) => (e.changedTouches && e.changedTouches[0] ? e.changedTouches[0].clientX : 0) ///| extern "js" fn ev_touch_y(e : Ev) -> Double = #|(e) => (e.changedTouches && e.changedTouches[0] ? e.changedTouches[0].clientY : 0) ///| extern "js" fn ev_touches(e : Ev) -> Int32 = #|(e) => (e.touches ? e.touches.length : 0) ///| /// The handler numbers (`data-n`) from the event's target up, as "3,1,0". extern "js" fn ev_chain(e : Ev) -> String = #|(e) => { const out = []; for (let n = e.target; n && n.getAttribute; n = n.parentNode) { const v = n.getAttribute("data-n"); if (v !== null) out.push(v); } return out.join(","); } ///| extern "js" fn set_timeout(ms : Int32, f : () -> Unit) -> Unit = #|(ms, f) => { setTimeout(f, ms) } ///| extern "js" fn request_frame(f : () -> Unit) -> Unit = #|(f) => { requestAnimationFrame(() => f()) } ///| extern "js" fn now_ms() -> Double = #|() => Date.now() // ---- storage and network ---------------------------------------------------------- ///| extern "js" fn storage_get(k : String) -> String = #|(k) => { try { return localStorage.getItem(k) ?? "" } catch { return "" } } ///| extern "js" fn storage_has(k : String) -> Bool = #|(k) => { try { return localStorage.getItem(k) !== null } catch { return false } } ///| extern "js" fn storage_set(k : String, v : String) -> Unit = #|(k, v) => { try { localStorage.setItem(k, v) } catch {} } ///| /// The old WebView build's `ultra.home.*` keys as a JSON object. extern "js" fn storage_legacy() -> String = #|() => { const o = {}; try { for (let i = 0; i < localStorage.length; i++) { const k = localStorage.key(i); if (k.startsWith("ultra.home.")) o[k] = localStorage.getItem(k); } } catch {} return JSON.stringify(o); } ///| extern "js" fn fetch_text(url : String, f : (Bool, String) -> Unit) -> Unit = #|(url, f) => { fetch(url, { cache: "no-store" }).then((r) => r.ok ? r.text().then((t) => f(true, t)) : f(false, "HTTP " + r.status)).catch((e) => f(false, String(e))) } // ---- page measurements (after each render) ------------------------------------------ ///| /// Measure the viewport and the home grid (into `window.__m`); false without a grid. extern "js" fn measure() -> Bool = #|() => { const s = document.querySelector(".slots"), h = document.querySelector(".home"); if (!s || !h) return false; const r = s.getBoundingClientRect(); window.__m = [innerWidth, innerHeight, r.left, r.top, r.width, parseFloat(getComputedStyle(h).paddingBottom) || 0, window.scrollY || 0]; return true; } ///| /// Measurement i: viewport width, height, grid left, top, width, the home's bottom padding. extern "js" fn measured(i : Int32) -> Double = #|(i) => window.__m[i] ///| /// The live widget slots, as the shell wants them: [[id, x, y, w, h, editing]...]. extern "js" fn widget_slots() -> String = #|() => JSON.stringify([...document.querySelectorAll(".widget-slot[data-wid]")].map((s) => { const r = s.getBoundingClientRect(); return [+s.dataset.wid, Math.round(r.left), Math.round(r.top), Math.round(r.width), Math.round(r.height), s.classList.contains("resizing") ? 1 : 0]; })) ///| extern "js" fn resize_handles() -> String = #|() => JSON.stringify([...document.querySelectorAll(".resize-handle")].map((h) => { const r = h.getBoundingClientRect(); return [Math.round(r.left), Math.round(r.top), Math.round(r.width), Math.round(r.height)]; })) ///| /// Whether the drawer is open and not animating out. extern "js" fn drawer_covering() -> Bool = #|() => !!document.querySelector(".drawer:not(.closing)") ///| /// A drawer over the home screen's widgets without a sheet on top (the shell keeps widget views placed while `cover` hides them). extern "js" fn has_drawer() -> Bool = #|() => !!document.querySelector(".drawer:not(.closing)") && !document.querySelector(".sheet-overlay") ///| extern "js" fn focus_search() -> Unit = #|() => { const i = document.querySelector(".drawer:not(.picking):not(.closing) .drawer-head input"); if (i) i.focus({ preventScroll: true }) } ///| extern "js" fn blur_active() -> Unit = #|() => { document.activeElement && document.activeElement.blur && document.activeElement.blur() } ///| extern "js" fn scroll_top() -> Unit = #|() => window.scrollTo({ top: 0, behavior: "smooth" }) ///| extern "js" fn home_at_top() -> Bool = #|() => window.scrollY <= 0 ///| /// The drawer's list is at its end (or does not scroll); false without a drawer. extern "js" fn list_at_end() -> Bool = #|() => { const l = document.querySelector(".drawer .apps"); return !!l && l.scrollTop + l.clientHeight >= l.scrollHeight - 2 } ///| /// The sheet is covering the page (a swipe must not act under it). extern "js" fn toast(s : String) -> Unit = #|(s) => { const t = document.getElementById("toast"); if (!t) return; t.textContent = s; t.classList.add("show"); clearTimeout(toast.t); toast.t = setTimeout(() => t.classList.remove("show"), 1600); } // ---- icons --------------------------------------------------------------------------- ///| /// Snapshot the page's icon images; returns how many. extern "js" fn icons_scan() -> Int32 = #|() => { window.__imgs = [...document.querySelectorAll("img.icon[data-icon]")]; return window.__imgs.length; } ///| extern "js" fn icon_key(i : Int32) -> String = #|(i) => window.__imgs[i].dataset.icon ///| /// Whether image i already shows the icon for its key. extern "js" fn icon_done(i : Int32) -> Bool = #|(i) => window.__imgs[i].__icon === window.__imgs[i].dataset.icon ///| extern "js" fn icon_set(i : Int32, src : String) -> Unit = #|(i, src) => { const img = window.__imgs[i]; img.__icon = img.dataset.icon; if (src) { img.src = src; img.classList.remove("missing"); } else img.classList.add("missing"); } ///| /// Forget what images show, so they are filled again. extern "js" fn icons_reset() -> Unit = #|() => { for (const img of document.querySelectorAll("img.icon")) img.__icon = undefined; } // ---- the host (Android's window.Android, or stand-ins in a desktop browser) ------------- ///| extern "js" fn is_android() -> Bool = #|() => !!window.Android ///| extern "js" fn h_launch(c : String) -> Unit = #|(c) => window.Android.launch(c) ///| extern "js" fn h_app_info(c : String) -> Unit = #|(c) => window.Android.appInfo(c) ///| extern "js" fn h_uninstall(c : String) -> Unit = #|(c) => window.Android.uninstall(c) ///| extern "js" fn h_add_widget(p : String) -> Unit = #|(p) => window.Android.addWidget(p) ///| extern "js" fn h_remove_widget(id : Int32) -> Unit = #|(id) => window.Android.removeWidget(id) ///| extern "js" fn h_place_widgets(json : String) -> Unit = #|(j) => window.Android.placeWidgets(j) ///| extern "js" fn h_cover_widgets(y : Int32) -> Unit = #|(y) => window.Android.coverWidgets(y) ///| extern "js" fn h_exclude_gestures(json : String) -> Unit = #|(j) => window.Android.excludeGestures && window.Android.excludeGestures(j) ///| extern "js" fn h_widget_limits(id : Int32) -> String = #|(id) => window.Android.widgetLimits(id) ///| extern "js" fn h_load_icons(json : String) -> Unit = #|(j) => window.Android.loadIcons(j) ///| extern "js" fn h_has_load_icons() -> Bool = #|() => !!(window.Android && window.Android.loadIcons) ///| extern "js" fn h_haptic() -> Unit = #|() => { if (window.Android) window.Android.haptic(); else navigator.vibrate && navigator.vibrate(10) } ///| extern "js" fn h_show_keyboard() -> Unit = #|() => window.Android && window.Android.showKeyboard() ///| extern "js" fn h_hide_keyboard() -> Unit = #|() => window.Android && window.Android.hideKeyboard() ///| extern "js" fn h_open_settings() -> Unit = #|() => window.Android.openSettings() ///| extern "js" fn h_pick_wallpaper() -> Unit = #|() => window.Android.pickWallpaper() ///| extern "js" fn h_open_recents() -> Bool = #|() => window.Android.openRecents() !== false ///| extern "js" fn h_open_accessibility_settings() -> Unit = #|() => window.Android.openAccessibilitySettings && window.Android.openAccessibilitySettings() ///| extern "js" fn h_open_self_app_info() -> Unit = #|() => window.Android.openSelfAppInfo && window.Android.openSelfAppInfo() ///| extern "js" fn h_http_get(url : String, tag : Int32) -> Unit = #|(url, tag) => window.Android.httpGet(url, tag) ///| extern "js" fn h_install_apk(url : String, version : Int32) -> Unit = #|(url, v) => window.Android.installApk(url, v) ///| extern "js" fn h_declined_version() -> Int32 = #|() => window.Android.declinedVersion() ///| extern "js" fn h_installed_version() -> Int32 = #|() => window.Android.installedVersion() // ---- what the shell calls on the page ----------------------------------------------- ///| extern "js" fn expose_init() -> Unit = #|() => { window.launcher = window.launcher || {}; } ///| extern "js" fn expose0(name : String, f : () -> Unit) -> Unit = #|(n, f) => { window.launcher[n] = f } ///| extern "js" fn expose1(name : String, f : (Int32) -> Unit) -> Unit = #|(n, f) => { window.launcher[n] = f } ///| extern "js" fn expose_b(name : String, f : (Bool) -> Unit) -> Unit = #|(n, f) => { window.launcher[n] = f } ///| extern "js" fn expose_dd(name : String, f : (Double, Double) -> Unit) -> Unit = #|(n, f) => { window.launcher[n] = f } ///| /// `f(tag, ok, text)`: a finished `httpGet`. extern "js" fn expose_result( name : String, f : (Int32, Bool, String) -> Unit, ) -> Unit = #|(n, f) => { window.launcher[n] = f } ///| /// `f(json)`: a batch of icons `{key: dataUri}`. extern "js" fn expose_json(name : String, f : (String) -> Unit) -> Unit = #|(n, f) => { window.launcher[n] = (batch) => f(JSON.stringify(batch)) } ///| /// Run `f` for each `key\nsrc` pair of a JSON object (flattened, so no arrays cross). extern "js" fn json_entries( json : String, f : (String, String) -> Unit, ) -> Unit = #|(j, f) => { const o = JSON.parse(j); for (const k in o) f(k, o[k] ?? "") } ///| /// Mark the page as running outside the shell (a desktop browser): the stylesheet gives it a backdrop. extern "js" fn mark_demo() -> Unit = #|() => document.documentElement.classList.add("demo")