Files
launcher/app/update.mbt
T
SpriteandClaude Sonnet 5.5 5258ac22ad Add the README, screenshots, legacy CI moved to manual, cleanups
Root README for the new layout; legacy/README.md points back. Legacy Gitea and
GitHub workflows run only by hand from legacy/ and the Gitea one does not
publish unless asked. Fix compiler warnings.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 01:12:48 +00:00

816 lines
20 KiB
MoonBit

///|
fn trimmed(s : String) -> String {
s.trim().to_owned()
}
///|
/// The lower-cased, trimmed search text matches the label.
pub fn matches_query(label : String, query : String) -> Bool {
let q = trimmed(query).to_lower()
q == "" || label.to_lower().contains(q)
}
///|
pub fn Model::app_row(self : Model, component : String) -> Int32 {
for i in 0..<self.apps.length() {
if self.apps[i].component == component {
return i
}
}
-1
}
///|
/// The home slot holding the app at `row` in the app list, or -1.
pub fn Model::home_of(self : Model, row : Int32) -> Int32 {
let comp = self.apps[row].component
for i in 0..<self.home.length() {
if self.home[i].kind == App && self.home[i].str == comp {
return i
}
}
-1
}
///|
pub fn Model::slot_of_widget(self : Model, wid : Int32) -> Int32 {
for i in 0..<self.home.length() {
if self.home[i].kind == Widget && self.home[i].wid == wid {
return i
}
}
-1
}
///|
/// The first app (row in the list) whose label matches the search.
pub fn Model::first_match(self : Model) -> Int32 {
for i in 0..<self.apps.length() {
if matches_query(self.apps[i].label, self.query) {
return i
}
}
-1
}
///|
pub fn Model::widget_matches(self : Model, i : Int32) -> Bool {
matches_query(self.widgets[i].label, self.wquery) ||
(
matches_query(self.widgets[i].app, self.wquery) &&
trimmed(self.wquery) != ""
)
}
///|
pub fn Model::first_widget_match(self : Model) -> Int32 {
for i in 0..<self.widgets.length() {
if self.widget_matches(i) {
return i
}
}
-1
}
///|
/// Home slots that are not drawn: apps that are not installed.
pub fn Model::hidden(self : Model, i : Int32) -> Bool {
self.home[i].kind == App && self.app_row(self.home[i].str) < 0
}
///|
pub fn Model::packed(self : Model) -> @grid.Packed {
@grid.pack(self.home, self.cols, self.nrows, hidden=fn(i) { self.hidden(i) })
}
///|
fn Model::say(self : Model, text : String, fx : Array[Effect]) -> Unit {
self.notice = text
self.notice_gen += 1
fx.push(Delay(1800, NoticeDone(self.notice_gen)))
}
///|
fn Model::push_slot(self : Model, s : @grid.Slot) -> Unit {
self.home.push(s)
}
///|
fn Model::new_key(self : Model) -> Int32 {
let k = self.next_key
self.next_key += 1
k
}
///|
fn Model::remove_slot(self : Model, at : Int32, fx : Array[Effect]) -> Unit {
if at < 0 || at >= self.home.length() {
return
}
if self.home[at].kind == Widget {
fx.push(RemoveWidget(self.home[at].wid))
}
ignore(self.home.remove(at))
if self.lifted == at {
self.lifted = -1
} else if self.lifted > at {
self.lifted -= 1
}
if self.resizing == at {
self.resizing = -1
} else if self.resizing > at {
self.resizing -= 1
}
}
///|
fn Model::add_app(self : Model, row : Int32, fx : Array[Effect]) -> Unit {
if self.home_of(row) >= 0 {
self.say("Already on the home screen", fx)
} else if self.home.length() >= home_cap {
self.say("The home screen is full", fx)
} else {
self.push_slot(
@grid.Slot::app(self.apps[row].component, key=self.new_key()),
)
self.say("Added to home", fx)
}
}
///|
/// Once an app uninstalled from the drawer is out of the list, take it off home.
fn Model::drop_gone(self : Model, fx : Array[Effect]) -> Unit {
if self.gone == "" {
return
}
if self.app_row(self.gone) >= 0 {
return
}
for i in 0..<self.home.length() {
if self.home[i].kind == App && self.home[i].str == self.gone {
self.remove_slot(i, fx)
break
}
}
self.gone = ""
}
///|
fn Model::close_all(self : Model, fx : Array[Effect]) -> Unit {
if self.drawer {
fx.push(HideKeyboard)
}
self.query = ""
self.drawer = false
self.picking = false
self.sheet = NoSheet
self.lifted = -1
self.resizing = -1
self.regrab = false
self.carry = None
self.resize = None
}
///|
fn Model::open_drawer(self : Model, fx : Array[Effect]) -> Unit {
if self.sheet == NoSheet && !self.drawer {
self.drawer = true
fx.push(FocusSearch)
}
}
///|
fn Model::close_drawer(self : Model, fx : Array[Effect]) -> Unit {
if self.drawer && self.sheet == NoSheet {
self.drawer = false
self.picking = false
self.query = ""
fx.push(HideKeyboard)
}
}
// ---- dragging -----------------------------------------------------------------
///|
/// The box (px, viewport) of slot `i`, from the packing.
fn Model::slot_box(
self : Model,
packed : @grid.Packed,
i : Int32,
) -> (Double, Double, Double, Double)? {
match packed.cells[i] {
Some(c) => {
let (x, y, w, h) = @grid.box(c, self.col_w)
Some((x + self.grid_left, y + self.grid_top, w, h))
}
None => None
}
}
///|
fn inside(b : (Double, Double, Double, Double), x : Double, y : Double) -> Bool {
x >= b.0 && x <= b.0 + b.2 && y >= b.1 && y <= b.1 + b.3
}
///|
/// The slot (not the lifted one) under a point, or -1.
fn Model::slot_at(
self : Model,
packed : @grid.Packed,
x : Double,
y : Double,
) -> Int32 {
for i in 0..<self.home.length() {
if i == self.lifted {
continue
}
if self.slot_box(packed, i) is Some(b) && inside(b, x, y) {
return i
}
}
-1
}
///|
/// The pointer is at x, y with a slot lifted (it went down at x0, y0). An app over
/// another slot takes its place in the order; over an empty cell it is left in that cell.
/// A widget goes to the cell under it wherever that is (apps flow around it).
fn Model::drag_lifted(
self : Model,
x : Double,
y : Double,
x0 : Double,
y0 : Double,
) -> Unit {
if self.lifted < 0 || self.lifted >= self.home.length() || self.col_w <= 0.0 {
return
}
let packed = self.packed()
let lifted = self.home[self.lifted]
let carry = match self.carry {
Some(c) => c
None => {
let (bx, by) = match self.slot_box(packed, self.lifted) {
Some(b) => (b.0, b.1)
None => (x0, y0)
}
let c = { dx: x0 - bx, dy: y0 - by, cell: -1, }
self.carry = Some(c)
c
}
}
let widget = lifted.kind == Widget
if !widget && self.slot_box(packed, self.lifted) is Some(b) && inside(b, x, y) {
return
}
let half_w = self.col_w / 2.0
let half_h = @grid.cell_px.to_double() / 2.0
let (fc, fr) = @grid.cell_at(
x - self.grid_left - half_w,
y - self.grid_top - half_h,
self.col_w,
)
let mut to = self.slot_at(packed, x, y)
if to < 0 {
let cx = self.grid_left +
fc.to_double() * (self.col_w + @grid.col_gap.to_double()) +
half_w
let cy = self.grid_top +
fr.to_double() * (@grid.cell_px + @grid.row_gap).to_double() +
half_h
to = self.slot_at(packed, cx, cy)
}
if to >= 0 && !widget {
carry.cell = -1
lifted.x = -1 // back into the flow, at that place in the order
lifted.y = -1
@grid.move_slot(self.home, self.lifted, to)
self.lifted = to
return
}
let grid_bottom = self.grid_top +
(packed.rows * (@grid.cell_px + @grid.row_gap)).to_double()
if y > grid_bottom {
return // below the grid
}
let (col, row) = @grid.cell_at(
x - self.grid_left - carry.dx,
y - self.grid_top - carry.dy,
self.col_w,
)
let cell = row * 64 + @cmp.minimum(col, 63)
if cell != carry.cell {
carry.cell = cell
@grid.pin(self.home, self.lifted, col, row, self.cols, self.nrows)
}
}
///|
/// The pointer is up. Letting go without moving opens the item's menu (a widget gets
/// resize handles instead); letting go of a dragged one just drops it, and whatever it
/// then covers moves on to free cells.
fn Model::drop_lifted(self : Model, moved : Bool) -> Unit {
self.carry = None
if self.lifted < 0 || self.lifted >= self.home.length() {
self.lifted = -1
self.regrab = false
return
}
let s = self.home[self.lifted]
if s.x >= 0 {
@grid.clear_cells(self.home, self.lifted, self.cols)
}
if !moved || self.regrab {
match s.kind {
Widget => self.resizing = self.lifted
App => self.sheet = Item(self.lifted)
}
}
self.lifted = -1
self.regrab = false
}
///|
fn Model::begin_resize(self : Model, edge : Edge, fx : Array[Effect]) -> Unit {
let i = self.resizing
if i < 0 || i >= self.home.length() {
return
}
let s = self.home[i]
let (left, top) = match self.slot_box(self.packed(), i) {
Some(b) => (b.0, b.1)
None => (self.grid_left, self.grid_top)
}
self.resize = Some({
edge,
slot: i,
left,
top,
cols: s.cols(self.cols),
rows: s.height(),
})
fx.push(QueryLimits(s.wid))
}
///|
/// Dragging a handle snaps the widget to whole columns and rows, within its limits.
fn Model::drag_resize(
self : Model,
r : ResizeDrag,
x : Double,
y : Double,
fx : Array[Effect],
) -> Unit {
if r.slot >= self.home.length() || self.col_w <= 0.0 {
return
}
let s = self.home[r.slot]
let lim = self.limits.get(s.wid).unwrap_or(default_limits)
let n = self.cols
let mut cols = r.cols
let mut rows = r.rows
if r.edge != Bottom && lim.h {
cols = @grid.fit(
x - r.left,
1,
n,
self.col_w,
@grid.col_gap.to_double(),
lim.min_w,
lim.max_w,
)
}
if r.edge != Right && lim.v {
rows = @grid.fit(
y - r.top,
1,
20,
@grid.cell_px.to_double(),
@grid.row_gap.to_double(),
lim.min_h,
lim.max_h,
)
}
if cols == r.cols && rows == r.rows {
return
}
r.cols = cols
r.rows = rows
fx.push(Haptic)
s.w = if cols >= n { 0 } else { cols }
s.rows = rows
if s.x >= 0 {
@grid.pin(self.home, r.slot, s.x, s.y, self.cols, self.nrows)
@grid.clear_cells(self.home, r.slot, self.cols)
}
}
// ---- update -------------------------------------------------------------------
///|
/// Apply a message to the model; returns the effects for the platform to run. The
/// saved layout is kept in step automatically (a `SaveHome` whenever it changed).
pub fn update(m : Model, msg : Msg) -> Array[Effect] {
let fx : Array[Effect] = []
step(m, msg, fx)
let json = save_json(m.home)
if json != m.saved {
m.saved = json
fx.push(SaveHome(json))
}
fx
}
///|
fn Model::choose_widget(self : Model, i : Int32, fx : Array[Effect]) -> Unit {
if i >= 0 && i < self.widgets.length() {
if self.home.length() < home_cap {
self.chosen = i
fx.push(AddWidget(self.widgets[i].provider))
} else {
self.say("The home screen is full", fx)
}
self.sheet = NoSheet
}
}
///|
fn step(m : Model, msg : Msg, fx : Array[Effect]) -> Unit {
match msg {
Restore(saved~, legacy~) => {
let next = Ref(m.next_key)
let mut slots = None
if saved is Some(text) {
slots = load_json(text, next)
if slots is Some(_) {
m.saved = text
}
}
if slots is None && legacy is Some(text) {
slots = Some(legacy_import(text, next))
}
if slots is Some(s) {
m.home = s
}
m.next_key = next.val
fx.push(LoadApps)
}
Started(now~, last_check~, declined~, installed~) => {
m.installed = installed
m.declined = declined
if @updater.should_check(now, last_check, manual=false) && !m.checking {
m.checking = true
m.manual_check = false
fx.push(SaveLastCheck(now))
fx.push(FetchRelease(@updater.release_api))
}
}
Measured(vw~, vh~, grid_left~, grid_top~, grid_w~, bottom_pad~) => {
m.cols = @grid.columns_for(vw)
m.grid_left = grid_left
m.grid_top = grid_top
m.col_w = @grid.column_width(grid_w, m.cols)
m.nrows = @grid.rows_fit(vh.to_double() - bottom_pad - grid_top)
}
AppsLoaded(text) => {
m.loading = false
match text.bind(parse_apps) {
Some(apps) => {
m.apps = apps
m.load_error = ""
m.drop_gone(fx)
}
None => m.load_error = "no response"
}
if m.sheet is DrawerApp(_) {
m.sheet = NoSheet // rows may have moved
}
}
WidgetsLoaded(text) => {
m.wloading = false
m.widgets = text.bind(parse_widgets).unwrap_or([])
}
LimitsKnown(wid, text) => {
let lim = match text {
Some(t) => parse_limits(t)
None => default_limits
}
m.limits[wid] = lim
if m.fresh == wid {
m.fresh = -1
let i = m.slot_of_widget(wid)
if i >= 0 && m.col_w > 0.0 {
m.home[i].w = @grid.widget_cols(lim.w, m.col_w, m.cols)
}
}
}
Reload => fx.push(LoadApps)
GoHome => {
m.close_all(fx)
fx.push(ScrollTop)
}
Back =>
if m.lifted >= 0 {
m.lifted = -1
m.regrab = false
m.carry = None
} else if m.resizing >= 0 {
m.resizing = -1
} else if m.sheet != NoSheet {
m.sheet = NoSheet
} else if m.drawer {
m.drawer = false
m.picking = false
m.query = ""
fx.push(HideKeyboard)
}
WidgetAdded(id) =>
if m.chosen >= 0 &&
m.chosen < m.widgets.length() &&
m.home.length() < home_cap {
let w = m.widgets[m.chosen]
m.push_slot(
@grid.Slot::widget(
w.label,
id,
rows=@grid.widget_rows(w.h),
key=m.new_key(),
),
)
m.chosen = -1
m.fresh = id
fx.push(QueryLimits(id))
}
WidgetMenu(wid) => {
let i = m.slot_of_widget(wid)
if i >= 0 && !m.drawer && m.sheet == NoSheet {
m.resizing = -1
m.regrab = false
m.lifted = i
m.native_from = None
}
}
WidgetGrab(wid) => {
let i = m.slot_of_widget(wid)
if i >= 0 && i == m.resizing && !m.drawer && m.sheet == NoSheet {
m.resizing = -1
m.regrab = true
m.lifted = i
m.native_from = None
}
}
NativeDrag(x, y) => {
m.dragged = true
let (x0, y0) = match m.native_from {
Some(p) => p
None => {
m.native_from = Some((x, y))
(x, y)
}
}
m.drag_lifted(x, y, x0, y0)
}
NativeDrop(moved) => {
m.native_from = None
m.drop_lifted(moved)
}
Touch(dx~, dy~, ms~, home_top~, list_end~) =>
if !m.dragged && m.lifted < 0 && m.sheet == NoSheet {
match @gesture.classify_swipe(dx, dy, ms) {
Some(Down) => if !m.drawer && home_top { m.open_drawer(fx) }
Some(Up) => if m.drawer && list_end { m.close_drawer(fx) }
None => ()
}
}
ReleaseFetched(text) => {
let release = text.bind(@updater.parse_release)
match release {
Some({ version_url: Some(v), apk_url: Some(a), tag, }) => {
m.apk_url = Some(a)
m.release_tag = tag
fx.push(FetchVersion(v))
}
_ => {
m.checking = false
if m.manual_check {
m.say("Couldn't check for updates", fx)
}
}
}
}
VersionFetched(text) => {
m.checking = false
let latest = match text.bind(@updater.parse_version) {
Some(v) => Some(v)
None => @updater.tag_version(m.release_tag)
}
match (latest, m.apk_url) {
(Some(latest), Some(url)) =>
match
@updater.decide(
latest,
m.installed,
m.declined,
manual=m.manual_check,
) {
UpToDate(n) =>
if m.manual_check {
m.say("ultra Home is up to date (build \{n})", fx)
}
Declined(_) => ()
Install(n) => {
m.say("Downloading launcher update (build \{n})…", fx)
fx.push(InstallApk(url, n))
}
}
_ => if m.manual_check { m.say("Couldn't check for updates", fx) }
}
}
NoticeDone(gen) => if gen == m.notice_gen { m.notice = "" }
Query(q) => m.query = q
Clear => m.query = ""
WQuery(q) => m.wquery = q
WClear => m.wquery = ""
OpenDrawer => m.open_drawer(fx)
CloseDrawer => m.close_drawer(fx)
OpenApp(row) =>
if row >= 0 && row < m.apps.length() {
fx.push(Launch(m.apps[row].component))
m.close_all(fx)
}
OpenFirst => {
let i = m.first_match()
if i >= 0 && trimmed(m.query) != "" {
fx.push(Launch(m.apps[i].component))
m.close_all(fx)
}
}
OpenHome(i) =>
if i >= 0 && i < m.home.length() {
m.resizing = -1
fx.push(Launch(m.home[i].str))
}
AddHome(row) => {
m.add_app(row, fx)
m.sheet = NoSheet
}
ToggleHome(row) => {
let i = m.home_of(row)
if i >= 0 {
m.remove_slot(i, fx)
} else if m.home.length() < home_cap {
m.push_slot(@grid.Slot::app(m.apps[row].component, key=m.new_key()))
}
}
AppMenu(row) =>
if m.drawer && m.sheet == NoSheet {
fx.push(Haptic)
m.sheet = DrawerApp(row)
}
AppInfoOf(row) => {
fx.push(AppInfo(m.apps[row].component))
m.sheet = NoSheet
}
UninstallOf(row) => {
m.gone = m.apps[row].component
fx.push(Uninstall(m.gone))
m.sheet = NoSheet
}
Menu => {
m.resizing = -1
fx.push(Haptic)
m.sheet = HomeMenu
}
CloseSheet => m.sheet = NoSheet
PickApps => {
m.sheet = NoSheet
m.drawer = true
m.picking = true
}
PickWidget => {
m.sheet = WidgetPicker
m.wquery = ""
m.wloading = true
fx.push(LoadWidgets)
}
ChooseWidget(i) => m.choose_widget(i, fx)
ChooseFirstWidget => m.choose_widget(m.first_widget_match(), fx)
Remove(i) => {
m.resizing = -1
m.remove_slot(i, fx)
m.sheet = NoSheet
}
MoveEarlier(k) => {
if k > 0 && k < m.home.length() {
let a = m.home[k]
m.home[k] = m.home[k - 1]
m.home[k - 1] = a
}
m.sheet = NoSheet
}
InfoOf(i) => {
if i >= 0 && i < m.home.length() {
fx.push(AppInfo(m.home[i].str))
}
m.sheet = NoSheet
}
Settings => {
fx.push(OpenSettings)
m.sheet = NoSheet
}
Wallpaper => {
fx.push(PickWallpaper)
m.sheet = NoSheet
}
Recents => fx.push(OpenRecents)
CheckUpdates => {
m.sheet = NoSheet
if m.checking {
m.say("Already checking for updates", fx)
} else {
m.checking = true
m.manual_check = true
m.say("Checking for updates…", fx)
fx.push(FetchRelease(@updater.release_api))
}
}
Lift(i) =>
if !m.drawer && m.sheet == NoSheet && i != m.lifted && i < m.home.length() {
fx.push(Haptic)
m.resizing = -1
m.regrab = false
m.lifted = i
}
Grab(i) =>
if i >= 0 && i == m.resizing && !m.drawer && m.sheet == NoSheet {
m.resizing = -1
m.regrab = true
m.lifted = i
}
ResizeBegin(edge) => m.begin_resize(edge, fx)
ResizeDone => m.resizing = -1
Nop => ()
Dragged(x~, y~, x0~, y0~) => {
m.dragged = true
match m.resize {
Some(r) => m.drag_resize(r, x, y, fx)
None => m.drag_lifted(x, y, x0, y0)
}
}
Released(moved~, held~) => {
ignore(held)
m.resize = None
m.drop_lifted(moved)
}
}
}
// ---- pointer input --------------------------------------------------------------
///|
/// Feed raw pointer input through the gesture recogniser and on to the handlers.
/// `table` holds the handlers of the nodes of the tree last rendered (`vdom` numbers the
/// elements that have any, in document order); an input's `chain` lists those numbers
/// from the innermost element up. Returns the effects to run.
pub fn input(
m : Model,
table : Array[@vdom.Handlers[Msg]],
inp : @gesture.Input,
) -> Array[Effect] {
let fx : Array[Effect] = []
fn nearest(
chain : Array[Int32],
pick : (@vdom.Handlers[Msg]) -> Msg?,
) -> Msg? {
for id in chain {
if id >= 0 && id < table.length() {
if pick(table[id]) is Some(msg) {
return Some(msg)
}
}
}
None
}
if inp is Down(..) {
m.dragged = false
}
for out in m.gesture.step(inp) {
let msg : Msg? = match out {
Schedule(ms, token) => {
fx.push(HoldTimer(ms, token))
None
}
Began(chain~, ..) => nearest(chain, fn(h) { h.down })
Tap(chain~) => nearest(chain, fn(h) { h.tap })
Press(chain~) => nearest(chain, fn(h) { h.press })
Drag(x~, y~, x0~, y0~) => Some(Dragged(x~, y~, x0~, y0~))
Release(moved~, held~) => Some(Released(moved~, held~))
}
if msg is Some(msg) {
fx.append(update(m, msg))
}
}
fx
}