504 lines
13 KiB
MoonBit
504 lines
13 KiB
MoonBit
///|
|
|
using @vdom {type Node, type Handlers, el, text, cls_if}
|
|
|
|
///|
|
|
fn tap(msg : Msg) -> Handlers[Msg] {
|
|
{ ..Handlers::none(), tap: Some(msg), }
|
|
}
|
|
|
|
///|
|
|
fn icon(key : String) -> Node[Msg] {
|
|
el("img", classes=["icon"], attrs=[
|
|
("alt", ""),
|
|
("draggable", "false"),
|
|
("data-icon", key),
|
|
])
|
|
}
|
|
|
|
///|
|
|
/// The `grid-column` / `grid-row` of a cell: explicit, so the packing is the model's.
|
|
fn cell_style(c : @grid.Cell) -> String {
|
|
"grid-column:\{c.col + 1} / span \{c.w};grid-row:\{c.row + 1} / span \{c.h}"
|
|
}
|
|
|
|
///|
|
|
fn handle(edge : String, e : Edge) -> Node[Msg] {
|
|
el(
|
|
"span",
|
|
classes=["resize-handle"],
|
|
attrs=[("data-edge", edge), ("aria-hidden", "true")],
|
|
handlers={ ..Handlers::none(), down: Some(ResizeBegin(e)), tap: Some(Nop), },
|
|
)
|
|
}
|
|
|
|
///|
|
|
fn Model::view_slot(
|
|
self : Model,
|
|
i : Int32,
|
|
packed : @grid.Packed,
|
|
) -> Node[Msg]? {
|
|
let s = self.home[i]
|
|
guard packed.cells[i] is Some(cell) else { return None }
|
|
let style = cell_style(cell)
|
|
match s.kind {
|
|
Widget => {
|
|
let live = !self.drawer &&
|
|
self.sheet == NoSheet &&
|
|
(self.resizing < 0 || i == self.resizing)
|
|
let attrs = [("data-slot", i.to_string())]
|
|
if live {
|
|
attrs.push(("data-wid", s.wid.to_string()))
|
|
}
|
|
let children = [
|
|
el("span", classes=["widget-label"], children=[text(s.str)]),
|
|
]
|
|
if i == self.resizing {
|
|
children.push(handle("r", Right))
|
|
children.push(handle("b", Bottom))
|
|
children.push(handle("rb", Corner))
|
|
}
|
|
Some(
|
|
el(
|
|
"li",
|
|
key=s.key,
|
|
classes=[
|
|
"widget-slot",
|
|
cls_if("lifted", i == self.lifted),
|
|
cls_if("resizing", i == self.resizing),
|
|
],
|
|
attrs~,
|
|
style~,
|
|
handlers={
|
|
..Handlers::none(),
|
|
press: Some(Lift(i)),
|
|
down: if i == self.resizing {
|
|
Some(Grab(i))
|
|
} else {
|
|
None
|
|
},
|
|
tap: if i == self.resizing {
|
|
Some(Nop)
|
|
} else {
|
|
None
|
|
},
|
|
},
|
|
children~,
|
|
),
|
|
)
|
|
}
|
|
App => {
|
|
let row = self.app_row(s.str)
|
|
let a = self.apps[row]
|
|
Some(
|
|
el(
|
|
"li",
|
|
key=s.key,
|
|
classes=["tile", cls_if("lifted", i == self.lifted)],
|
|
attrs=[("data-slot", i.to_string())],
|
|
style~,
|
|
handlers={
|
|
..Handlers::none(),
|
|
tap: Some(OpenHome(i)),
|
|
press: Some(Lift(i)),
|
|
},
|
|
children=[
|
|
icon(a.icon),
|
|
el("span", classes=["name"], children=[text(a.label)]),
|
|
],
|
|
),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn view_panel() -> Node[Msg] {
|
|
el("section", classes=["panel"], attrs=[("aria-label", "Shortcuts")], children=[
|
|
el("button", classes=["panel-btn"], handlers=tap(Recents), children=[
|
|
el("span", classes=["windows-glyph"], attrs=[("aria-hidden", "true")]),
|
|
el("span", children=[text("Windows")]),
|
|
]),
|
|
])
|
|
}
|
|
|
|
///|
|
|
/// The pill under the panel: a tap opens the drawer, where the search takes the keyboard.
|
|
fn view_search_pill() -> Node[Msg] {
|
|
el(
|
|
"button",
|
|
classes=["search-pill"],
|
|
attrs=[("aria-label", "Search apps")],
|
|
handlers=tap(OpenDrawer),
|
|
children=[
|
|
el("span", classes=["magnifier"], attrs=[("aria-hidden", "true")]),
|
|
el("span", children=[text("Search apps")]),
|
|
],
|
|
)
|
|
}
|
|
|
|
///|
|
|
fn Model::view_home(self : Model) -> Node[Msg] {
|
|
let packed = self.packed()
|
|
let slots : Array[Node[Msg]] = []
|
|
for i in 0..<self.home.length() {
|
|
if self.view_slot(i, packed) is Some(n) {
|
|
slots.push(n)
|
|
}
|
|
}
|
|
let rows = if self.home.length() > 0 { packed.rows } else { 0 }
|
|
let children = [
|
|
view_panel(),
|
|
view_search_pill(),
|
|
el(
|
|
"ul",
|
|
classes=["slots", cls_if("dragging", self.lifted >= 0)],
|
|
style="grid-template-columns:repeat(\{self.cols},minmax(0,1fr));grid-template-rows:repeat(\{rows},\{@grid.cell_px}px)",
|
|
children=slots,
|
|
),
|
|
]
|
|
if self.home.length() == 0 {
|
|
children.push(
|
|
el("p", classes=["hint"], children=[
|
|
text("Swipe down for all apps. Long-press to add apps and widgets."),
|
|
]),
|
|
)
|
|
}
|
|
el(
|
|
"div",
|
|
classes=["home"],
|
|
handlers={
|
|
..Handlers::none(),
|
|
press: Some(Menu),
|
|
tap: if self.resizing >= 0 {
|
|
Some(ResizeDone)
|
|
} else {
|
|
None
|
|
},
|
|
},
|
|
children~,
|
|
)
|
|
}
|
|
|
|
///|
|
|
fn Model::view_drawer(self : Model) -> Node[Msg] {
|
|
let tiles : Array[Node[Msg]] = []
|
|
for i in 0..<self.apps.length() {
|
|
let a = self.apps[i]
|
|
if !matches_query(a.label, self.query) {
|
|
continue
|
|
}
|
|
tiles.push(
|
|
el(
|
|
"li",
|
|
key=i + 1,
|
|
classes=["tile", cls_if("on", self.picking && self.home_of(i) >= 0)],
|
|
handlers=if self.picking {
|
|
tap(ToggleHome(i))
|
|
} else {
|
|
{
|
|
..Handlers::none(),
|
|
tap: Some(OpenApp(i)),
|
|
press: Some(AppMenu(i)),
|
|
}
|
|
},
|
|
children=[
|
|
icon(a.icon),
|
|
el("span", classes=["name"], children=[text(a.label)]),
|
|
],
|
|
),
|
|
)
|
|
}
|
|
let children : Array[Node[Msg]] = [
|
|
el("div", classes=["drawer-head"], children=[
|
|
el(
|
|
"input",
|
|
classes=["input"],
|
|
attrs=[
|
|
("type", "search"),
|
|
("placeholder", "Search apps"),
|
|
("enterkeyhint", "go"),
|
|
("aria-label", "Search apps"),
|
|
("autocomplete", "off"),
|
|
],
|
|
props=[("value", self.query)],
|
|
handlers={
|
|
..Handlers::none(),
|
|
input: Some(fn(v) { Query(v) }),
|
|
enter: Some(OpenFirst),
|
|
escape: Some(Clear),
|
|
},
|
|
),
|
|
el("button", classes=["btn", "ghost", "sm"], handlers=tap(CloseDrawer), children=[
|
|
text(if self.picking { "Done" } else { "Close" }),
|
|
]),
|
|
]),
|
|
]
|
|
if self.picking {
|
|
children.push(
|
|
el("p", classes=["drawer-note"], children=[
|
|
text("Tap apps to add them to the home screen or take them off."),
|
|
]),
|
|
)
|
|
}
|
|
if self.load_error != "" {
|
|
children.push(
|
|
el("div", classes=["alert", "destructive"], attrs=[("role", "alert")], children=[
|
|
text("Could not list apps"),
|
|
]),
|
|
)
|
|
}
|
|
children.push(el("ul", classes=["apps"], children=tiles))
|
|
if self.loading {
|
|
children.push(
|
|
el("p", classes=["empty"], children=[text("Loading apps…")]),
|
|
)
|
|
} else if tiles.length() == 0 {
|
|
children.push(el("p", classes=["empty"], children=[text("No apps match.")]))
|
|
}
|
|
el("div", classes=["drawer", cls_if("picking", self.picking)], children~)
|
|
}
|
|
|
|
///|
|
|
fn sheet_btn(label : String, msg : Msg) -> Node[Msg] {
|
|
el("button", classes=["sheet-item"], handlers=tap(msg), children=[text(label)])
|
|
}
|
|
|
|
///|
|
|
fn sheet_title(label : String) -> Node[Msg] {
|
|
el("h2", classes=["sheet-title"], children=[text(label)])
|
|
}
|
|
|
|
///|
|
|
fn Model::view_sheet(self : Model) -> Array[Node[Msg]] {
|
|
let items : Array[Node[Msg]] = []
|
|
match self.sheet {
|
|
NoSheet => ()
|
|
HomeMenu => {
|
|
items.push(sheet_title("Home screen"))
|
|
items.push(sheet_btn("Add apps", PickApps))
|
|
items.push(sheet_btn("Add widget", PickWidget))
|
|
items.push(sheet_btn("Wallpaper", Wallpaper))
|
|
items.push(sheet_btn("Settings", Settings))
|
|
items.push(sheet_btn("Check for updates", CheckUpdates))
|
|
}
|
|
Item(i) =>
|
|
if i >= 0 && i < self.home.length() {
|
|
let s = self.home[i]
|
|
let row = self.app_row(s.str)
|
|
items.push(
|
|
sheet_title(
|
|
if s.kind == Widget {
|
|
s.str
|
|
} else if row >= 0 {
|
|
self.apps[row].label
|
|
} else {
|
|
s.str
|
|
},
|
|
),
|
|
)
|
|
if i > 0 {
|
|
items.push(sheet_btn("Move earlier", MoveEarlier(i)))
|
|
}
|
|
if i < self.home.length() - 1 {
|
|
items.push(sheet_btn("Move later", MoveEarlier(i + 1)))
|
|
}
|
|
if s.kind == App {
|
|
items.push(sheet_btn("App info", InfoOf(i)))
|
|
items.push(sheet_btn("Remove from home", Remove(i)))
|
|
} else {
|
|
items.push(sheet_btn("Remove widget", Remove(i)))
|
|
}
|
|
}
|
|
DrawerApp(row) =>
|
|
if row >= 0 && row < self.apps.length() {
|
|
items.push(sheet_title(self.apps[row].label))
|
|
let h = self.home_of(row)
|
|
if h >= 0 {
|
|
items.push(sheet_btn("Remove from home", Remove(h)))
|
|
} else {
|
|
items.push(sheet_btn("Add to home", AddHome(row)))
|
|
}
|
|
items.push(sheet_btn("App info", AppInfoOf(row)))
|
|
if !self.apps[row].system {
|
|
items.push(sheet_btn("Uninstall", UninstallOf(row)))
|
|
}
|
|
}
|
|
WidgetPicker => {
|
|
items.push(sheet_title("Add a widget"))
|
|
if self.wloading {
|
|
items.push(
|
|
el("p", classes=["empty"], children=[text("Loading widgets…")]),
|
|
)
|
|
} else if self.widgets.length() == 0 {
|
|
items.push(
|
|
el("p", classes=["empty"], children=[text("No widgets installed.")]),
|
|
)
|
|
} else {
|
|
items.push(
|
|
el(
|
|
"input",
|
|
classes=["input", "widget-search"],
|
|
attrs=[
|
|
("type", "search"),
|
|
("placeholder", "Search widgets"),
|
|
("enterkeyhint", "go"),
|
|
("aria-label", "Search widgets"),
|
|
("autocomplete", "off"),
|
|
],
|
|
props=[("value", self.wquery)],
|
|
handlers={
|
|
..Handlers::none(),
|
|
input: Some(fn(v) { WQuery(v) }),
|
|
enter: Some(ChooseFirstWidget),
|
|
escape: Some(WClear),
|
|
},
|
|
),
|
|
)
|
|
}
|
|
let rows : Array[Node[Msg]] = []
|
|
for i in 0..<self.widgets.length() {
|
|
if !self.widget_matches(i) {
|
|
continue
|
|
}
|
|
let w = self.widgets[i]
|
|
let name : Array[Node[Msg]] = [text(w.label)]
|
|
if w.app != "" && w.app != w.label {
|
|
name.push(el("small", children=[text(w.app)]))
|
|
}
|
|
rows.push(
|
|
el("li", key=i + 1, handlers=tap(ChooseWidget(i)), children=[
|
|
icon(w.icon),
|
|
el("span", classes=["widget-name"], children=name),
|
|
]),
|
|
)
|
|
}
|
|
items.push(el("ul", classes=["widget-list"], children=rows))
|
|
if !self.wloading && self.widgets.length() > 0 && rows.length() == 0 {
|
|
items.push(
|
|
el("p", classes=["empty"], children=[text("No widgets match.")]),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
[
|
|
el("div", classes=["sheet-overlay"], handlers=tap(CloseSheet)),
|
|
el(
|
|
"div",
|
|
classes=["sheet"],
|
|
attrs=[("role", "dialog"), ("aria-modal", "true")],
|
|
handlers={ ..Handlers::none(), escape: Some(CloseSheet), },
|
|
children=items,
|
|
),
|
|
]
|
|
}
|
|
|
|
///|
|
|
fn Model::view_resize_bar(self : Model) -> Node[Msg] {
|
|
el("div", classes=["resize-bar"], attrs=[("role", "toolbar")], children=[
|
|
el("span", classes=["resize-hint"], children=[
|
|
text("Drag to move, or drag the handles to resize"),
|
|
]),
|
|
el(
|
|
"button",
|
|
classes=["btn", "ghost", "sm"],
|
|
handlers=tap(Remove(self.resizing)),
|
|
children=[text("Remove")],
|
|
),
|
|
el("button", classes=["btn", "primary", "sm"], handlers=tap(ResizeDone), children=[
|
|
text("Done"),
|
|
]),
|
|
])
|
|
}
|
|
|
|
///|
|
|
/// The whole screen as a tree.
|
|
pub fn view(m : Model) -> Array[Node[Msg]] {
|
|
let out : Array[Node[Msg]] = [m.view_home()]
|
|
if m.drawer {
|
|
out.push(m.view_drawer())
|
|
}
|
|
if m.sheet != NoSheet {
|
|
out.append(m.view_sheet())
|
|
} else if m.resizing >= 0 && !m.drawer {
|
|
out.push(m.view_resize_bar())
|
|
}
|
|
if m.notice != "" {
|
|
out.push(
|
|
el("div", classes=["notice"], attrs=[("role", "status")], children=[
|
|
text(m.notice),
|
|
]),
|
|
)
|
|
}
|
|
out
|
|
}
|
|
|
|
///|
|
|
/// The handlers of a tree's elements, numbered in document order (pre-order): the
|
|
/// platform layer marks each such element with its number and reports pointer input as
|
|
/// chains of them (see `input`).
|
|
pub fn handler_table(nodes : Array[Node[Msg]]) -> Array[Handlers[Msg]] {
|
|
let out : Array[Handlers[Msg]] = []
|
|
fn walk(n : Node[Msg]) -> Unit {
|
|
if n is El(e) {
|
|
if !e.handlers.is_empty() {
|
|
out.push(e.handlers)
|
|
}
|
|
for c in e.children {
|
|
walk(c)
|
|
}
|
|
}
|
|
}
|
|
|
|
for n in nodes {
|
|
walk(n)
|
|
}
|
|
out
|
|
}
|
|
|
|
///|
|
|
pub(all) enum Key {
|
|
Enter
|
|
Escape
|
|
}
|
|
|
|
///|
|
|
/// A key pressed in an input; `chain` as for `input`.
|
|
pub fn key(
|
|
m : Model,
|
|
table : Array[Handlers[Msg]],
|
|
chain : Array[Int32],
|
|
k : Key,
|
|
) -> Array[Effect] {
|
|
for id in chain {
|
|
if id >= 0 && id < table.length() {
|
|
let h = table[id]
|
|
let msg = match k {
|
|
Enter => h.enter
|
|
Escape => h.escape
|
|
}
|
|
if msg is Some(msg) {
|
|
return update(m, msg)
|
|
}
|
|
}
|
|
}
|
|
[]
|
|
}
|
|
|
|
///|
|
|
/// An input's text changed.
|
|
pub fn typed(
|
|
m : Model,
|
|
table : Array[Handlers[Msg]],
|
|
chain : Array[Int32],
|
|
value : String,
|
|
) -> Array[Effect] {
|
|
for id in chain {
|
|
if id >= 0 && id < table.length() {
|
|
if table[id].input is Some(f) {
|
|
return update(m, f(value))
|
|
}
|
|
}
|
|
}
|
|
[]
|
|
}
|