321 lines
8.4 KiB
MoonBit
321 lines
8.4 KiB
MoonBit
///| Kickoff-style application launcher: state machine + render logic. Pure;
|
|
/// host capabilities (app list, launch) are passed in by the component stub.
|
|
|
|
///| Which surface an event is addressed to (mirrors the WIT `surface` enum).
|
|
pub(all) enum Target {
|
|
Widget
|
|
Popup
|
|
}
|
|
|
|
///|
|
|
pub struct State {
|
|
mut open : Bool
|
|
mut query : String
|
|
mut hover : Int
|
|
mut error : String
|
|
}
|
|
|
|
///|
|
|
pub fn State::new() -> State {
|
|
{ open: false, query: "", hover: -1, error: "" }
|
|
}
|
|
|
|
///|
|
|
pub let popup_w : Int = 340
|
|
|
|
///|
|
|
let header_h : Int = 52
|
|
|
|
///|
|
|
let row_h : Int = 46
|
|
|
|
///|
|
|
let max_rows : Int = 8
|
|
|
|
///|
|
|
fn ascii_lower(s : String) -> String {
|
|
let sb = StringBuilder()
|
|
for c in s {
|
|
if c >= 'A' && c <= 'Z' {
|
|
sb.write_char(Int::unsafe_to_char(c.to_int() + 32))
|
|
} else {
|
|
sb.write_char(c)
|
|
}
|
|
}
|
|
sb.to_string()
|
|
}
|
|
|
|
///|
|
|
fn contains_ci(hay : String, needle : String) -> Bool {
|
|
if needle.length() == 0 {
|
|
return true
|
|
}
|
|
ascii_lower(hay).contains(ascii_lower(needle))
|
|
}
|
|
|
|
///| KRunner-ish filter: case-insensitive substring over name, comment and category.
|
|
pub fn filter(apps : Array[@types.AppEntry], query : String) -> Array[@types.AppEntry] {
|
|
apps.filter(a => {
|
|
contains_ci(a.name, query) ||
|
|
contains_ci(a.comment, query) ||
|
|
contains_ci(a.category, query)
|
|
})
|
|
}
|
|
|
|
///|
|
|
fn visible_rows(n : Int) -> Int {
|
|
if n > max_rows { max_rows } else { n }
|
|
}
|
|
|
|
///|
|
|
pub fn popup_size(apps : Array[@types.AppEntry], st : State) -> @types.Size {
|
|
let rows = visible_rows(filter(apps, st.query).length())
|
|
let rows = if rows == 0 { 1 } else { rows }
|
|
@ui.size(popup_w, header_h + rows * row_h + 8)
|
|
}
|
|
|
|
///|
|
|
pub fn close(st : State) -> Unit {
|
|
st.open = false
|
|
st.query = ""
|
|
st.hover = -1
|
|
}
|
|
|
|
///| The panel button: a crescent moon plus a label.
|
|
pub fn render_button(
|
|
size : @types.Size,
|
|
t : @theme.Tokens,
|
|
st : State,
|
|
) -> Array[@types.DrawOp] {
|
|
let w = size.w.reinterpret_as_int()
|
|
let h = size.h.reinterpret_as_int()
|
|
let bg = if st.open { t.palette.accent } else { t.palette.surface_hover }
|
|
let fg = if st.open { t.palette.accent_foreground } else { t.palette.foreground }
|
|
let r = t.radius.reinterpret_as_int()
|
|
let cy = h / 2
|
|
[
|
|
@ui.fill_rect(@ui.rect(0, 0, w, h), bg, radius=r),
|
|
// Crescent: light disc with a button-coloured disc biting into it.
|
|
@ui.circle(18, cy, 9, fg),
|
|
@ui.circle(22, cy - 3, 8, bg),
|
|
@ui.text(36, cy, "Applications", t.font_size.reinterpret_as_int(), fg, bold=true),
|
|
]
|
|
}
|
|
|
|
///|
|
|
pub fn render_popup(
|
|
apps : Array[@types.AppEntry],
|
|
t : @theme.Tokens,
|
|
st : State,
|
|
) -> Array[@types.DrawOp] {
|
|
let size = popup_size(apps, st)
|
|
let w = size.w.reinterpret_as_int()
|
|
let h = size.h.reinterpret_as_int()
|
|
let fs = t.font_size.reinterpret_as_int()
|
|
let r = t.radius.reinterpret_as_int()
|
|
let p = t.palette
|
|
let ops = [
|
|
@ui.fill_rect(@ui.rect(0, 0, w, h), p.surface_popup, radius=r + 4),
|
|
@ui.stroke_rect(@ui.rect(0, 0, w, h), p.border, radius=r + 4),
|
|
// search field
|
|
@ui.fill_rect(@ui.rect(10, 10, w - 20, 32), p.surface, radius=r),
|
|
]
|
|
let q = if st.query.length() == 0 {
|
|
@ui.text(20, 26, "Type to search applications...", fs - 1, p.foreground_muted)
|
|
} else {
|
|
@ui.text(20, 26, st.query + "_", fs, p.foreground)
|
|
}
|
|
ops.push(q)
|
|
let shown = filter(apps, st.query)
|
|
if shown.length() == 0 {
|
|
ops.push(
|
|
@ui.text(w / 2, header_h + row_h / 2, "No matching applications", fs, p.foreground_muted, align=CENTER),
|
|
)
|
|
}
|
|
let n = visible_rows(shown.length())
|
|
for i = 0; i < n; i = i + 1 {
|
|
let a = shown[i]
|
|
let y = header_h + i * row_h
|
|
if i == st.hover {
|
|
ops.push(@ui.fill_rect(@ui.rect(6, y, w - 12, row_h - 2), p.accent, radius=r))
|
|
}
|
|
let fg = if i == st.hover { p.accent_foreground } else { p.foreground }
|
|
let sub = if i == st.hover { p.accent_foreground } else { p.foreground_muted }
|
|
ops.push(@ui.circle(30, y + row_h / 2 - 1, 15, p.surface_hover))
|
|
ops.push(@ui.text(30, y + row_h / 2 - 1, a.glyph, fs, fg, align=CENTER, bold=true))
|
|
ops.push(@ui.text(56, y + 14, a.name, fs, fg, bold=true))
|
|
ops.push(@ui.text(56, y + 31, a.comment, fs - 3, sub))
|
|
}
|
|
if st.error.length() > 0 {
|
|
ops.push(@ui.text(14, h - 12, st.error, fs - 3, @ui.color(240, 90, 90, 255)))
|
|
}
|
|
ops
|
|
}
|
|
|
|
///| Row index under a popup-local point, or -1.
|
|
pub fn row_at(apps : Array[@types.AppEntry], st : State, p : @types.Point) -> Int {
|
|
if p.x < 6 || p.x >= popup_w - 6 || p.y < header_h {
|
|
return -1
|
|
}
|
|
let i = (p.y - header_h) / row_h
|
|
if i < visible_rows(filter(apps, st.query).length()) { i } else { -1 }
|
|
}
|
|
|
|
///| Handle an event. `launch` is the host capability. Returns true on redraw.
|
|
pub fn handle_event(
|
|
apps : Array[@types.AppEntry],
|
|
st : State,
|
|
target : Target,
|
|
ev : @types.InputEvent,
|
|
launch : (String) -> Result[Unit, String],
|
|
) -> Bool {
|
|
match (target, ev) {
|
|
(Widget, PointerDown(_)) => {
|
|
if st.open {
|
|
close(st)
|
|
} else {
|
|
st.open = true
|
|
st.error = ""
|
|
}
|
|
true
|
|
}
|
|
(Popup, PointerMove(p)) => {
|
|
let i = row_at(apps, st, p)
|
|
if i != st.hover {
|
|
st.hover = i
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
(Popup, PointerLeave) =>
|
|
if st.hover != -1 {
|
|
st.hover = -1
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
(Popup, PointerDown(p)) => {
|
|
let i = row_at(apps, st, p)
|
|
if i >= 0 {
|
|
activate(apps, st, i, launch)
|
|
}
|
|
true
|
|
}
|
|
(_, FocusLost) =>
|
|
if st.open {
|
|
close(st)
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
(_, KeyDown(k)) =>
|
|
if !st.open {
|
|
false
|
|
} else if k == "Escape" {
|
|
close(st)
|
|
true
|
|
} else if k == "Backspace" {
|
|
if st.query.length() > 0 {
|
|
st.query = st.query[:st.query.length() - 1].to_owned()
|
|
}
|
|
st.hover = -1
|
|
true
|
|
} else if k == "Enter" {
|
|
if filter(apps, st.query).length() > 0 {
|
|
activate(apps, st, 0, launch)
|
|
}
|
|
true
|
|
} else if k.length() == 1 {
|
|
st.query = st.query + k
|
|
st.hover = -1
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
_ => false
|
|
}
|
|
}
|
|
|
|
///| Launch the i-th filtered app; a failure is shown in the popup (not hidden).
|
|
fn activate(
|
|
apps : Array[@types.AppEntry],
|
|
st : State,
|
|
i : Int,
|
|
launch : (String) -> Result[Unit, String],
|
|
) -> Unit {
|
|
let a = filter(apps, st.query)[i]
|
|
match launch(a.id) {
|
|
Ok(_) => close(st)
|
|
Err(e) => st.error = "Launch failed: " + e
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn sample_apps() -> Array[@types.AppEntry] {
|
|
[
|
|
{ id: "a", name: "Terminal", comment: "Command line", category: "System", glyph: ">_" },
|
|
{ id: "b", name: "Files", comment: "Browse folders", category: "Utilities", glyph: "F" },
|
|
{ id: "c", name: "Web Browser", comment: "Browse the web", category: "Internet", glyph: "W" },
|
|
]
|
|
}
|
|
|
|
///|
|
|
test "filter is case-insensitive over name/comment/category" {
|
|
let apps = sample_apps()
|
|
assert_eq(filter(apps, "").length(), 3)
|
|
assert_eq(filter(apps, "TERM").length(), 1)
|
|
assert_eq(filter(apps, "folders")[0].id, "b")
|
|
assert_eq(filter(apps, "internet")[0].id, "c")
|
|
assert_eq(filter(apps, "zzz").length(), 0)
|
|
}
|
|
|
|
///|
|
|
test "open, type, launch via keyboard" {
|
|
let apps = sample_apps()
|
|
let st = State::new()
|
|
let launched : Array[String] = []
|
|
let launch = id => {
|
|
launched.push(id)
|
|
Ok(())
|
|
}
|
|
assert_true(handle_event(apps, st, Widget, PointerDown({ x: 1, y: 1 }), launch))
|
|
assert_true(st.open)
|
|
assert_true(handle_event(apps, st, Popup, KeyDown("f"), launch))
|
|
assert_true(handle_event(apps, st, Popup, KeyDown("i"), launch))
|
|
assert_eq(st.query, "fi")
|
|
assert_true(handle_event(apps, st, Popup, KeyDown("Enter"), launch))
|
|
assert_eq(launched, ["b"])
|
|
assert_false(st.open)
|
|
}
|
|
|
|
///|
|
|
test "click on a row launches; failure is surfaced and keeps the menu open" {
|
|
let apps = sample_apps()
|
|
let st = State::new()
|
|
st.open = true
|
|
let i = row_at(apps, st, { x: 20, y: 52 + 46 + 5 })
|
|
assert_eq(i, 1)
|
|
let _ = handle_event(
|
|
apps,
|
|
st,
|
|
Popup,
|
|
PointerDown({ x: 20, y: 52 + 46 + 5 }),
|
|
_ => Err("boom"),
|
|
)
|
|
assert_true(st.open)
|
|
assert_eq(st.error, "Launch failed: boom")
|
|
}
|
|
|
|
///|
|
|
test "focus loss closes and hover tracks the pointer" {
|
|
let apps = sample_apps()
|
|
let st = State::new()
|
|
st.open = true
|
|
assert_true(handle_event(apps, st, Popup, PointerMove({ x: 20, y: 60 }), _ => Ok(())))
|
|
assert_eq(st.hover, 0)
|
|
assert_false(handle_event(apps, st, Popup, PointerMove({ x: 21, y: 61 }), _ => Ok(())))
|
|
assert_true(handle_event(apps, st, Widget, FocusLost, _ => Ok(())))
|
|
assert_false(st.open)
|
|
}
|