Files
launcher/app/model.mbt
T
nandi 472a48f4bd
Launcher / test (push) Successful in 1m59s
Launcher / apk (push) Successful in 38s
Add an icon size setting (#3)
2026-11-24 22:43:04 +00:00

378 lines
9.2 KiB
MoonBit

///|
/// The launcher as an Elm-style program, pure: `Model`, `Msg`, `update` (returns the
/// `Effect`s for the platform to run) and `view` (returns a `vdom` tree). It ports
/// legacy/web/app.py, the LPython app, and the gesture handling that lived in its
/// index.html. Nothing here touches a platform: the web/ package runs the effects and
/// renders the tree, and the tests below drive the whole thing headlessly.
///|
pub(all) struct AppInfo {
label : String
component : String
icon : String
/// A system app: it has no Uninstall.
system : Bool
}
///|
pub(all) struct WidgetInfo {
label : String
provider : String
icon : String
/// The label of the app the widget comes from (for the picker's search).
app : String
/// Natural height in CSS px.
h : Int32
}
///|
/// How a widget may be resized, in CSS px (the shell's `widgetLimits`): its natural
/// width, min/max size (0: no max), and whether it resizes horizontally / vertically.
pub(all) struct Limits {
w : Int32
min_w : Int32
min_h : Int32
max_w : Int32
max_h : Int32
h : Bool
v : Bool
}
///|
pub let default_limits : Limits = {
w: 0,
min_w: 120,
min_h: 80,
max_w: 0,
max_h: 0,
h: true,
v: true,
}
///|
#derive(Eq, Debug)
pub(all) enum Sheet {
NoSheet
/// The menu of the empty home.
HomeMenu
/// The menu of a home slot.
Item(Int32)
/// The menu of a drawer app (row in the app list).
DrawerApp(Int32)
WidgetPicker
RecentsSetup
/// The icon size slider and presets.
IconSize
}
///|
#derive(Eq, Debug)
pub(all) enum Edge {
Left
Right
Top
Bottom
Corner
}
///|
/// A resize in progress: a handle was pressed on the widget whose viewport box and
/// grid edges were captured on down.
pub(all) struct ResizeDrag {
edge : Edge
slot : Int32
left : Double
top : Double
right : Double
bottom : Double
anchor_col : Int32
anchor_row : Int32
right_col : Int32
bottom_row : Int32
mut col : Int32
mut row : Int32
mut cols : Int32
mut rows : Int32
}
///|
/// Where the lifted slot was held, and the cell it was last sent to.
pub(all) struct Carry {
dx : Double
dy : Double
mut cell : Int32
}
///|
pub(all) struct Model {
mut apps : Array[AppInfo]
mut loading : Bool
mut load_error : String
mut widgets : Array[WidgetInfo]
mut wloading : Bool
/// The home screen, in order.
mut home : Array[@grid.Slot]
mut next_key : Int32
/// Grid columns, and rows that fill the screen (0 until the page has measured).
mut cols : Int32
mut nrows : Int32
/// Where the grid is on the screen (px, viewport) and how wide a column is.
mut grid_left : Double
mut grid_top : Double
mut col_w : Double
/// Icon size in percent of the original (see grid/scale.mbt), applied to the home
/// grid and the drawer.
mut icon_pct : Int32
/// Why the saved icon size could not be used (empty: it could); shown in the icon
/// size sheet. The bad stored value is left alone until a size is chosen.
mut icon_error : String
/// The last measurement, kept so a size change can lay out again without one.
mut vw : Int32
mut grid_w : Double
/// Px of screen below the top of the grid that rows can fill.
mut avail_h : Double
/// Columns of the drawer's app list.
mut drawer_cols : Int32
mut query : String
mut wquery : String
mut drawer : Bool
/// The drawer is animating out before detaching.
mut drawer_closing : Bool
mut drawer_gen : Int32
/// The drawer is in "add apps" mode: taps toggle home slots.
mut picking : Bool
mut sheet : Sheet
/// The package being uninstalled: its home slot goes when it leaves the app list.
mut gone : String
/// Home slot picked up by a long press, being dragged.
mut lifted : Int32
/// Widget slot showing resize handles.
mut resizing : Int32
/// The lifted slot was grabbed while showing handles: drop it back into them.
mut regrab : Bool
mut carry : Carry?
mut resize : ResizeDrag?
/// Native widget views report their drags through `NativeDrag`; where it began.
mut native_from : (Double, Double)?
/// Widget row being added.
mut chosen : Int32
mut limits : Map[Int32, Limits]
mut notice : String
mut notice_gen : Int32
// ---- self-update
mut installed : Int32
mut declined : Int32
mut checking : Bool
mut manual_check : Bool
mut apk_url : String?
mut release_tag : String
/// The layout JSON last handed to `SaveHome`.
mut saved : String
/// A pointer drag (lift or resize) happened since the pointer went down: the swipe
/// check that follows the touch ignores it.
mut dragged : Bool
/// A widget just added whose limits will set its starting width.
mut fresh : Int32
mut gesture : @gesture.State
}
///|
pub fn Model::new() -> Model {
{
apps: [],
loading: true,
load_error: "",
widgets: [],
wloading: false,
home: [],
next_key: 1,
cols: 5,
nrows: 0,
grid_left: 0.0,
grid_top: 0.0,
col_w: 0.0,
icon_pct: @grid.scale_default,
icon_error: "",
vw: 0,
grid_w: 0.0,
avail_h: 0.0,
drawer_cols: 4,
query: "",
wquery: "",
drawer: false,
drawer_closing: false,
drawer_gen: 0,
picking: false,
sheet: NoSheet,
gone: "",
lifted: -1,
resizing: -1,
regrab: false,
carry: None,
resize: None,
native_from: None,
chosen: -1,
limits: {},
notice: "",
notice_gen: 0,
installed: 0,
declined: -1,
checking: false,
manual_check: false,
apk_url: None,
release_tag: "",
saved: "",
dragged: false,
fresh: -1,
gesture: @gesture.State::new(),
}
}
///|
/// The most slots the home screen holds.
pub let home_cap : Int32 = 200
///|
pub let app_cap : Int32 = 512
///|
#derive(Eq, Debug)
/// What a view node can send. Raw pointer input goes through `Input` and the gesture
/// recogniser first; everything else is a Msg a handler or the shell sends directly.
pub(all) enum Msg {
// ---- from the platform
/// Boot: the saved layout (this app's JSON, and the old WebView build's
/// `ultra.home.*` localStorage as a JSON object), the clock, and the self-update state.
Restore(saved~ : String?, legacy~ : String?, icon_size~ : String?)
Started(
now~ : Int64,
last_check~ : Int64,
declined~ : Int32,
installed~ : Int32,
)
/// The page measured itself: viewport size, and where the home grid is.
Measured(
vw~ : Int32,
vh~ : Int32,
grid_left~ : Double,
grid_top~ : Double,
grid_w~ : Double,
bottom_pad~ : Double,
scroll_y~ : Double,
)
AppsLoaded(String?)
WidgetsLoaded(String?)
/// The shell's answer to `QueryLimits`.
LimitsKnown(Int32, String?)
/// A package changed: look at the app list again.
Reload
/// The Home button or intent.
GoHome
Back
WidgetAdded(Int32)
/// A long press on a native widget view lifts its slot.
WidgetMenu(Int32)
/// A press on the native view of the widget showing handles picks it up.
WidgetGrab(Int32)
NativeDrag(Double, Double)
NativeDrop(Bool)
/// The swipe the page saw: dx, dy, ms, whether the home is scrolled to the top, and
/// whether the drawer's list is at its end.
Touch(
dx~ : Double,
dy~ : Double,
ms~ : Int32,
home_top~ : Bool,
list_end~ : Bool,
)
ReleaseFetched(String?)
VersionFetched(String?)
NoticeDone(Int32)
// ---- from handlers
Query(String)
Clear
WQuery(String)
WClear
OpenDrawer
CloseDrawer
DrawerClosed(Int32)
OpenApp(Int32)
OpenFirst
OpenHome(Int32)
AddHome(Int32)
ToggleHome(Int32)
AppMenu(Int32)
AppInfoOf(Int32)
UninstallOf(Int32)
Menu
CloseSheet
PickApps
PickWidget
ChooseWidget(Int32)
ChooseFirstWidget
Remove(Int32)
/// Swap the slot with the one before it.
MoveEarlier(Int32)
InfoOf(Int32)
Settings
/// Open the icon size sheet.
IconSizeMenu
/// The slider's value (text, as the input reports it).
SetIconSize(String)
/// A preset button, in percent.
IconPreset(Int32)
Wallpaper
Recents
RecentsUnavailable
OpenAccessibilitySettings
OpenSelfAppInfo
CheckUpdates
Lift(Int32)
Grab(Int32)
ResizeBegin(Edge)
ResizeDone
/// Does nothing; stands in a chain to stop a tap reaching an element below.
Nop
/// The recogniser saw a drag / the pointer lift (see `input`).
Dragged(x~ : Double, y~ : Double, x0~ : Double, y0~ : Double)
Released(moved~ : Bool, held~ : Bool)
}
///|
#derive(Eq, Debug)
pub(all) enum Effect {
Launch(String)
AppInfo(String)
Uninstall(String)
AddWidget(String)
RemoveWidget(Int32)
OpenSettings
PickWallpaper
OpenRecents
OpenAccessibilitySettings
OpenSelfAppInfo
Haptic
/// Fetch apps.json / widgets.json and answer `AppsLoaded` / `WidgetsLoaded`.
LoadApps
LoadWidgets
/// Ask the shell for a widget's limits and answer `LimitsKnown`.
QueryLimits(Int32)
SaveHome(String)
/// Store the icon size (percent).
SaveIconSize(Int32)
/// Write a line to the platform's error log.
LogError(String)
SaveLastCheck(Int64)
/// Send the message after this many ms.
Delay(Int32, Msg)
/// After this many ms, feed `Hold(token)` to `input` (the long-press timer).
HoldTimer(Int32, Int32)
FetchRelease(String)
FetchVersion(String)
InstallApk(String, Int32)
/// The drawer opened with its search in front: focus it and raise the keyboard.
FocusSearch
HideKeyboard
ScrollTop
}