WIT package, MoonBit components, Node host and tests (WIP)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Sonnet 5.5 committed 2026-11-24 23:36:35 +00:00
1 parent e5548fc067
commit e0bde1d150
125 files changed
+8681

No files matched your search

+140
View File
@@ -0,0 +1,140 @@
///| Render logic for the clock widgets (pure: tokens + time in, ops out).
///|
pub fn digital_info() -> (String, String, Int, Int, Int) {
// id, name, preferred w, preferred h, min w
("org.moonplasma.clock.digital", "Digital Clock", 120, 36, 80)
}
///|
pub fn render_digital(
size : @types.Size,
t : @theme.Tokens,
now : @types.Datetime,
) -> Array[@types.DrawOp] {
let w = size.w.reinterpret_as_int()
let h = size.h.reinterpret_as_int()
let fs = t.font_size.reinterpret_as_int()
let time = @timefmt.hhmm(now.hour.to_int(), now.minute.to_int())
let date = @timefmt.date_line(
now.weekday.to_int(),
now.day.to_int(),
now.month.to_int(),
)
[
@ui.text(w / 2, h / 2 - fs / 2 + 1, time, fs + 4, t.palette.foreground, align=CENTER, bold=true),
@ui.text(w / 2, h / 2 + fs / 2 + 4, date, fs - 3, t.palette.foreground_muted, align=CENTER),
]
}
///|
pub fn binary_info() -> (String, String, Int, Int, Int) {
("org.moonplasma.clock.binary", "Binary Clock", 96, 36, 96)
}
///| BCD binary clock: six columns (H H M M S S), four rows; lit bits use the
/// accent colour, unused high bits of the tens digits are not drawn.
pub fn render_binary(
size : @types.Size,
t : @theme.Tokens,
now : @types.Datetime,
) -> Array[@types.DrawOp] {
let w = size.w.reinterpret_as_int()
let h = size.h.reinterpret_as_int()
let digits = [
(now.hour.to_int() / 10, 2),
(now.hour.to_int() % 10, 4),
(now.minute.to_int() / 10, 3),
(now.minute.to_int() % 10, 4),
(now.second.to_int() / 10, 3),
(now.second.to_int() % 10, 4),
]
let ops = []
let cols = digits.length()
let step_x = w / cols
let step_y = h / 4
let r = (if step_x < step_y { step_x } else { step_y }) / 2 - 1
for c = 0; c < cols; c = c + 1 {
let (d, nbits) = digits[c]
let bits = @timefmt.bcd_bits(d, nbits)
for i = 0; i < nbits; i = i + 1 {
// Row 3 is the least significant bit.
let row = 4 - nbits + i
let cx = c * step_x + step_x / 2
let cy = row * step_y + step_y / 2
if bits[i] {
ops.push(@ui.circle(cx, cy, r, t.palette.accent))
} else {
ops.push(@ui.circle(cx, cy, r, t.palette.border, filled=false))
}
}
}
ops
}
///|
fn test_tokens() -> @theme.Tokens {
let c = @ui.color(10, 20, 30, 255)
{
name: "t",
palette: {
desktop_top: c,
desktop_bottom: c,
surface: c,
surface_hover: c,
surface_popup: c,
foreground: c,
foreground_muted: c,
accent: c,
accent_foreground: c,
border: c,
},
font_size: 14,
radius: 6,
spacing: 6,
panel_height: 44,
}
}
///|
fn test_time() -> @types.Datetime {
{
year: 2026,
month: b'\x0B',
day: b'\x18',
hour: b'\x0E',
minute: b'\x07',
second: b'\x25',
weekday: b'\x02',
}
}
///|
test "digital clock shows HH:MM and the date" {
let ops = render_digital(@ui.size(120, 36), test_tokens(), test_time())
assert_eq(ops.length(), 2)
match ops[0] {
Text(t) => assert_eq(t.text, "14:07")
_ => fail("expected text")
}
match ops[1] {
Text(t) => assert_eq(t.text, "Tue 24 Nov")
_ => fail("expected text")
}
}
///|
test "binary clock draws one dot per displayed bit" {
let ops = render_binary(@ui.size(96, 36), test_tokens(), test_time())
// 2+4+3+4+3+4 bits
assert_eq(ops.length(), 20)
let mut lit = 0
for op in ops {
match op {
Circle(c) => if c.filled { lit += 1 }
_ => ()
}
}
// 14:07:37 -> 1,4,0,7,3,7 -> popcount 1+1+0+3+2+3
assert_eq(lit, 10)
}
+6
View File
@@ -0,0 +1,6 @@
import {
"moonplasma/desktop/interface/moonplasma/desktop/types" @types,
"moonplasma/desktop/interface/moonplasma/desktop/theme" @theme,
"moonplasma/desktop/lib/ui" @ui,
"moonplasma/desktop/lib/timefmt" @timefmt,
}
+24
View File
@@ -0,0 +1,24 @@
// Generated using `moon info`, DON'T EDIT IT
package "moonplasma/desktop/lib/clock"
import {
"moonplasma/desktop/interface/moonplasma/desktop/theme",
"moonplasma/desktop/interface/moonplasma/desktop/types",
}
// Values
pub fn binary_info() -> (String, String, Int32, Int32, Int32)
pub fn digital_info() -> (String, String, Int32, Int32, Int32)
pub fn render_binary(@types.Size, @theme.Tokens, @types.Datetime) -> Array[@types.DrawOp]
pub fn render_digital(@types.Size, @theme.Tokens, @types.Datetime) -> Array[@types.DrawOp]
// Errors
// Types and methods
// Type aliases
// Traits
+320
View File
@@ -0,0 +1,320 @@
///| 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)
}
+5
View File
@@ -0,0 +1,5 @@
import {
"moonplasma/desktop/interface/moonplasma/desktop/types" @types,
"moonplasma/desktop/interface/moonplasma/desktop/theme" @theme,
"moonplasma/desktop/lib/ui" @ui,
}
+44
View File
@@ -0,0 +1,44 @@
// Generated using `moon info`, DON'T EDIT IT
package "moonplasma/desktop/lib/launcher"
import {
"moonplasma/desktop/interface/moonplasma/desktop/theme",
"moonplasma/desktop/interface/moonplasma/desktop/types",
}
// Values
pub fn close(State) -> Unit
pub fn filter(Array[@types.AppEntry], String) -> Array[@types.AppEntry]
pub fn handle_event(Array[@types.AppEntry], State, Target, @types.InputEvent, (String) -> Result[Unit, String]) -> Bool
pub fn popup_size(Array[@types.AppEntry], State) -> @types.Size
pub let popup_w : Int32
pub fn render_button(@types.Size, @theme.Tokens, State) -> Array[@types.DrawOp]
pub fn render_popup(Array[@types.AppEntry], @theme.Tokens, State) -> Array[@types.DrawOp]
pub fn row_at(Array[@types.AppEntry], State, @types.Point) -> Int32
// Errors
// Types and methods
pub struct State {
mut open : Bool
mut query : String
mut hover : Int32
mut error : String
}
pub fn State::new() -> Self
pub(all) enum Target {
Widget
Popup
}
// Type aliases
// Traits
View File
Whitespace-only changes.
+218
View File
@@ -0,0 +1,218 @@
///| Panel layout algorithm (pure integer geometry, no WIT types).
///|
pub(all) struct SlotIn {
id : String
pref_w : Int
pref_h : Int
min_w : Int
stretch : Bool
end_align : Bool
}
///|
pub(all) struct Placed {
id : String
x : Int
y : Int
w : Int
h : Int
}
///|
pub(all) struct PanelLayout {
x : Int
y : Int
w : Int
h : Int
placements : Array[Placed]
}
///| Bottom, full-width panel. Widgets pack towards their edge in slot order.
/// Free space goes to `stretch` slots; if even the minimum widths do not fit
/// the call fails (Err) rather than silently dropping widgets.
pub fn layout(
screen_w : Int,
screen_h : Int,
panel_h : Int,
margin : Int,
spacing : Int,
slots : Array[SlotIn],
) -> Result[PanelLayout, String] {
if panel_h <= 0 || panel_h > screen_h {
return Err("panel height \{panel_h} does not fit screen height \{screen_h}")
}
let n = slots.length()
let avail = screen_w - 2 * margin - (if n > 1 { spacing * (n - 1) } else { 0 })
let widths : Array[Int] = slots.map(s => s.pref_w)
let mut total = 0
for w in widths {
total += w
}
if total > avail {
// Shrink towards min_w, taking the deficit from the widest slack first.
let mut deficit = total - avail
let mut guard_iter = 0
while deficit > 0 && guard_iter < 100000 {
guard_iter += 1
let mut best = -1
let mut best_slack = 0
for i = 0; i < n; i = i + 1 {
let slack = widths[i] - slots[i].min_w
if slack > best_slack {
best_slack = slack
best = i
}
}
if best < 0 {
return Err(
"panel overflow: slots need at least \{avail + deficit}px but only \{avail}px available",
)
}
widths[best] = widths[best] - 1
deficit -= 1
}
} else if total < avail {
let mut free = avail - total
let mut stretchers = 0
for s in slots {
if s.stretch {
stretchers += 1
}
}
if stretchers > 0 {
let share = free / stretchers
let mut rem = free - share * stretchers
for i = 0; i < n; i = i + 1 {
if slots[i].stretch {
widths[i] = widths[i] + share + (if rem > 0 { 1 } else { 0 })
if rem > 0 {
rem -= 1
}
}
}
free = 0
}
}
let inner_h = panel_h - 2 * margin
let py = screen_h - panel_h
let placed : Array[Placed?] = Array::make(n, None)
let mut x = margin
for i = 0; i < n; i = i + 1 {
if !slots[i].end_align {
placed[i] = Some(place(slots[i], x, py, margin, inner_h, widths[i]))
x += widths[i] + spacing
}
}
let mut rx = screen_w - margin
for i = n - 1; i >= 0; i = i - 1 {
if slots[i].end_align {
rx -= widths[i]
placed[i] = Some(place(slots[i], rx, py, margin, inner_h, widths[i]))
rx -= spacing
}
}
let out : Array[Placed] = []
for p in placed {
match p {
Some(v) => out.push(v)
None => abort("unreachable: unplaced slot")
}
}
Ok({ x: 0, y: py, w: screen_w, h: panel_h, placements: out })
}
///|
fn place(
s : SlotIn,
x : Int,
py : Int,
margin : Int,
inner_h : Int,
w : Int,
) -> Placed {
let h = if s.pref_h < inner_h { s.pref_h } else { inner_h }
{ id: s.id, x, y: py + margin + (inner_h - h) / 2, w, h }
}
///| Popup rectangle: above the panel, left-aligned with its anchor widget and
/// clamped inside the screen.
pub fn popup_position(
screen_w : Int,
panel_y : Int,
anchor_x : Int,
popup_w : Int,
popup_h : Int,
gap : Int,
) -> (Int, Int) {
let max_x = screen_w - popup_w
let x = if anchor_x > max_x { max_x } else { anchor_x }
let x = if x < 0 { 0 } else { x }
let y = panel_y - popup_h - gap
let y = if y < 0 { 0 } else { y }
(x, y)
}
///|
fn slot(id : String, pref : Int, min : Int, stretch : Bool, end_ : Bool) -> SlotIn {
{ id, pref_w: pref, pref_h: 30, min_w: min, stretch, end_align: end_ }
}
///|
test "start and end packing" {
let r = layout(
1000,
600,
40,
4,
6,
[slot("a", 100, 50, false, false), slot("b", 80, 40, false, true)],
).unwrap()
assert_eq((r.x, r.y, r.w, r.h), (0, 560, 1000, 40))
assert_eq((r.placements[0].x, r.placements[0].w), (4, 100))
assert_eq((r.placements[1].x, r.placements[1].w), (916, 80))
// 30px tall widget centred in 32px inner area.
assert_eq(r.placements[0].y, 565)
}
///|
test "stretch takes the free space" {
let r = layout(
500,
300,
40,
0,
0,
[slot("a", 100, 50, false, false), slot("s", 10, 10, true, false)],
).unwrap()
assert_eq(r.placements[1].w, 400)
}
///|
test "shrinks to minimum then fails loudly" {
let ok = layout(
200,
300,
40,
0,
0,
[slot("a", 150, 80, false, false), slot("b", 150, 80, false, false)],
).unwrap()
assert_eq((ok.placements[0].w, ok.placements[1].w), (100, 100))
let bad = layout(
100,
300,
40,
0,
0,
[slot("a", 150, 80, false, false), slot("b", 150, 80, false, false)],
)
assert_true(bad is Err(_))
}
///|
test "popup clamps to screen" {
assert_eq(popup_position(1000, 560, 4, 300, 200, 6), (4, 354))
assert_eq(popup_position(1000, 560, 900, 300, 200, 6), (700, 354))
assert_eq(popup_position(1000, 100, 4, 300, 200, 6), (4, 0))
}
+39
View File
@@ -0,0 +1,39 @@
// Generated using `moon info`, DON'T EDIT IT
package "moonplasma/desktop/lib/panel"
// Values
pub fn layout(Int32, Int32, Int32, Int32, Int32, Array[SlotIn]) -> Result[PanelLayout, String]
pub fn popup_position(Int32, Int32, Int32, Int32, Int32, Int32) -> (Int32, Int32)
// Errors
// Types and methods
pub(all) struct PanelLayout {
x : Int32
y : Int32
w : Int32
h : Int32
placements : Array[Placed]
}
pub(all) struct Placed {
id : String
x : Int32
y : Int32
w : Int32
h : Int32
}
pub(all) struct SlotIn {
id : String
pref_w : Int32
pref_h : Int32
min_w : Int32
stretch : Bool
end_align : Bool
}
// Type aliases
// Traits
+5
View File
@@ -0,0 +1,5 @@
import {
"moonplasma/desktop/interface/moonplasma/desktop/types" @types,
"moonplasma/desktop/interface/moonplasma/desktop/theme" @theme,
"moonplasma/desktop/lib/ui" @ui,
}
+20
View File
@@ -0,0 +1,20 @@
// Generated using `moon info`, DON'T EDIT IT
package "moonplasma/desktop/lib/shell"
import {
"moonplasma/desktop/interface/moonplasma/desktop/theme",
"moonplasma/desktop/interface/moonplasma/desktop/types",
}
// Values
pub fn render_background(@types.Size, @theme.Tokens) -> Array[@types.DrawOp]
pub fn render_panel(@types.Rect, @theme.Tokens) -> Array[@types.DrawOp]
// Errors
// Types and methods
// Type aliases
// Traits
+104
View File
@@ -0,0 +1,104 @@
///| Desktop background and panel chrome (pure render logic).
///| Vertical gradient built from horizontal bands, a soft "plasma" glow and a
/// wordmark. Everything derives from theme tokens.
pub fn render_background(
screen : @types.Size,
t : @theme.Tokens,
) -> Array[@types.DrawOp] {
let w = screen.w.reinterpret_as_int()
let h = screen.h.reinterpret_as_int()
let p = t.palette
let bands = 48
let ops = []
for i = 0; i < bands; i = i + 1 {
let y0 = h * i / bands
let y1 = h * (i + 1) / bands
ops.push(
@ui.fill_rect(
@ui.rect(0, y0, w, y1 - y0 + 1),
@ui.lerp_color(p.desktop_top, p.desktop_bottom, i, bands - 1),
),
)
}
// Plasma glow: concentric translucent discs.
let gx = w * 3 / 4
let gy = h * 3 / 10
let rings = 8
for i = 0; i < rings; i = i + 1 {
let r = 40 + (rings - i) * 22
ops.push(@ui.circle(gx, gy, r, @ui.with_alpha(p.accent, 14 + i * 4)))
}
ops.push(@ui.circle(gx, gy, 38, @ui.with_alpha(p.accent, 200)))
ops.push(@ui.circle(gx + 14, gy - 10, 34, p.desktop_top))
ops.push(
@ui.text(w / 2, h / 2 - 20, "moon-plasma", 54, @ui.with_alpha(p.foreground, 235), align=CENTER, bold=true),
)
ops.push(
@ui.text(
w / 2,
h / 2 + 24,
"every part is a WebAssembly component",
t.font_size.reinterpret_as_int() + 2,
p.foreground_muted,
align=CENTER,
),
)
ops
}
///|
pub fn render_panel(
panel : @types.Rect,
t : @theme.Tokens,
) -> Array[@types.DrawOp] {
let p = t.palette
[
@ui.fill_rect(panel, p.surface),
@ui.line(panel.x, panel.y, panel.x + @ui.rect_w(panel), panel.y, p.border),
]
}
///|
fn test_tokens() -> @theme.Tokens {
let c = @ui.color(10, 20, 30, 255)
let d = @ui.color(110, 120, 130, 255)
{
name: "t",
palette: {
desktop_top: c,
desktop_bottom: d,
surface: c,
surface_hover: c,
surface_popup: c,
foreground: c,
foreground_muted: c,
accent: c,
accent_foreground: c,
border: c,
},
font_size: 14,
radius: 6,
spacing: 6,
panel_height: 44,
}
}
///|
test "background gradient covers the screen top to bottom" {
let ops = render_background(@ui.size(800, 600), test_tokens())
match ops[0] {
FillRect(f) => {
assert_eq(f.rect.y, 0)
assert_eq(f.color.r.to_int(), 10)
}
_ => fail("expected fill")
}
match ops[47] {
FillRect(f) => {
assert_eq(f.color.r.to_int(), 110)
assert_true(f.rect.y + f.rect.h.reinterpret_as_int() >= 600)
}
_ => fail("expected fill")
}
}
View File
Whitespace-only changes.
+25
View File
@@ -0,0 +1,25 @@
// Generated using `moon info`, DON'T EDIT IT
package "moonplasma/desktop/lib/timefmt"
// Values
pub fn bcd_bits(Int32, Int32) -> Array[Bool]
pub fn date_line(Int32, Int32, Int32) -> String
pub fn hhmm(Int32, Int32) -> String
pub fn hhmmss(Int32, Int32, Int32) -> String
pub fn month_short(Int32) -> String
pub fn pad2(Int32) -> String
pub fn weekday_short(Int32) -> String
// Errors
// Types and methods
// Type aliases
// Traits
+65
View File
@@ -0,0 +1,65 @@
///| Pure time formatting helpers shared by the clock widgets.
///|
pub fn pad2(n : Int) -> String {
if n < 10 && n >= 0 { "0" + n.to_string() } else { n.to_string() }
}
///|
let weekdays : Array[String] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
///|
let months : Array[String] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
]
///| Weekday 0 = Sunday. Aborts on out-of-range input instead of guessing.
pub fn weekday_short(i : Int) -> String {
guard i >= 0 && i < 7 else { abort("weekday out of range: \{i}") }
weekdays[i]
}
///| Month 1..12.
pub fn month_short(m : Int) -> String {
guard m >= 1 && m <= 12 else { abort("month out of range: \{m}") }
months[m - 1]
}
///|
pub fn hhmm(hour : Int, minute : Int) -> String {
pad2(hour) + ":" + pad2(minute)
}
///|
pub fn hhmmss(hour : Int, minute : Int, second : Int) -> String {
hhmm(hour, minute) + ":" + pad2(second)
}
///| e.g. "Tue 24 Nov"
pub fn date_line(weekday : Int, day : Int, month : Int) -> String {
weekday_short(weekday) + " " + day.to_string() + " " + month_short(month)
}
///| The `bits` low bits of `digit`, most significant first.
pub fn bcd_bits(digit : Int, bits : Int) -> Array[Bool] {
let out = []
let mut i = bits - 1
while i >= 0 {
out.push(((digit >> i) & 1) == 1)
i = i - 1
}
out
}
///|
test "formatting" {
assert_eq(hhmm(9, 5), "09:05")
assert_eq(hhmmss(23, 59, 7), "23:59:07")
assert_eq(date_line(2, 24, 11), "Tue 24 Nov")
}
///|
test "bcd bits" {
assert_eq(bcd_bits(5, 4), [false, true, false, true])
assert_eq(bcd_bits(2, 2), [true, false])
}
+3
View File
@@ -0,0 +1,3 @@
import {
"moonplasma/desktop/interface/moonplasma/desktop/types" @types,
}
+43
View File
@@ -0,0 +1,43 @@
// Generated using `moon info`, DON'T EDIT IT
package "moonplasma/desktop/lib/ui"
import {
"moonplasma/desktop/interface/moonplasma/desktop/types",
}
// Values
pub fn circle(Int32, Int32, Int32, @types.Color, filled? : Bool) -> @types.DrawOp
pub fn color(Int32, Int32, Int32, Int32) -> @types.Color
pub fn contains(@types.Rect, @types.Point) -> Bool
pub fn fill_rect(@types.Rect, @types.Color, radius? : Int32) -> @types.DrawOp
pub fn lerp_color(@types.Color, @types.Color, Int32, Int32) -> @types.Color
pub fn line(Int32, Int32, Int32, Int32, @types.Color, width? : Int32) -> @types.DrawOp
pub fn rect(Int32, Int32, Int32, Int32) -> @types.Rect
pub fn rect_h(@types.Rect) -> Int32
pub fn rect_w(@types.Rect) -> Int32
pub fn size(Int32, Int32) -> @types.Size
pub fn stroke_rect(@types.Rect, @types.Color, width? : Int32, radius? : Int32) -> @types.DrawOp
pub fn text(Int32, Int32, String, Int32, @types.Color, align? : @types.TextAlign, bold? : Bool) -> @types.DrawOp
pub fn text_width(String, Int32) -> Int32
pub fn with_alpha(@types.Color, Int32) -> @types.Color
// Errors
// Types and methods
// Type aliases
// Traits
+171
View File
@@ -0,0 +1,171 @@
///| Shared drawing helpers for components. Pure functions over the WIT types.
///|
pub fn color(r : Int, g : Int, b : Int, a : Int) -> @types.Color {
{ r: r.to_byte(), g: g.to_byte(), b: b.to_byte(), a: a.to_byte() }
}
///|
fn chan(b : Byte) -> Int {
b.to_int()
}
///| Linear interpolation of two colours; `num/den` is the weight of `b`.
pub fn lerp_color(
a : @types.Color,
b : @types.Color,
num : Int,
den : Int,
) -> @types.Color {
guard den > 0 else { abort("lerp_color: den must be positive") }
let n = if num < 0 { 0 } else if num > den { den } else { num }
fn mix(x : Byte, y : Byte, n : Int, den : Int) -> Int {
(chan(x) * (den - n) + chan(y) * n) / den
}
color(
mix(a.r, b.r, n, den),
mix(a.g, b.g, n, den),
mix(a.b, b.b, n, den),
mix(a.a, b.a, n, den),
)
}
///|
pub fn with_alpha(c : @types.Color, a : Int) -> @types.Color {
color(chan(c.r), chan(c.g), chan(c.b), a)
}
///|
pub fn rect(x : Int, y : Int, w : Int, h : Int) -> @types.Rect {
{ x, y, w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() }
}
///|
pub fn size(w : Int, h : Int) -> @types.Size {
{ w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() }
}
///|
pub fn rect_w(r : @types.Rect) -> Int {
r.w.reinterpret_as_int()
}
///|
pub fn rect_h(r : @types.Rect) -> Int {
r.h.reinterpret_as_int()
}
///|
pub fn contains(r : @types.Rect, p : @types.Point) -> Bool {
p.x >= r.x &&
p.y >= r.y &&
p.x < r.x + rect_w(r) &&
p.y < r.y + rect_h(r)
}
///|
pub fn fill_rect(
r : @types.Rect,
c : @types.Color,
radius~ : Int = 0,
) -> @types.DrawOp {
@types.DrawOp::FillRect({ rect: r, color: c, radius: radius.reinterpret_as_uint() })
}
///|
pub fn stroke_rect(
r : @types.Rect,
c : @types.Color,
width~ : Int = 1,
radius~ : Int = 0,
) -> @types.DrawOp {
@types.DrawOp::StrokeRect({
rect: r,
color: c,
width: width.reinterpret_as_uint(),
radius: radius.reinterpret_as_uint(),
})
}
///|
pub fn line(
x0 : Int,
y0 : Int,
x1 : Int,
y1 : Int,
c : @types.Color,
width~ : Int = 1,
) -> @types.DrawOp {
@types.DrawOp::Line({
start: { x: x0, y: y0 },
end: { x: x1, y: y1 },
color: c,
width: width.reinterpret_as_uint(),
})
}
///|
pub fn circle(
cx : Int,
cy : Int,
radius : Int,
c : @types.Color,
filled~ : Bool = true,
) -> @types.DrawOp {
@types.DrawOp::Circle({
center: { x: cx, y: cy },
radius: radius.reinterpret_as_uint(),
color: c,
filled,
})
}
///| Draw `s` with `origin` at the vertical middle of the line box.
pub fn text(
x : Int,
y : Int,
s : String,
size : Int,
c : @types.Color,
align~ : @types.TextAlign = LEFT,
bold~ : Bool = false,
) -> @types.DrawOp {
@types.DrawOp::Text({
origin: { x, y },
text: s,
style: { size: size.reinterpret_as_uint(), color: c, align, bold },
})
}
///| The text-metrics contract of the PoC host: every glyph advances by
/// `ceil(0.6 * size)` pixels (monospace). Replaced by a real shaping
/// interface in a later milestone.
pub fn text_width(s : String, size : Int) -> Int {
s.length() * ((size * 6 + 9) / 10)
}
///|
test "lerp_color endpoints and midpoint" {
let a = color(0, 0, 0, 255)
let b = color(200, 100, 50, 255)
assert_eq(lerp_color(a, b, 0, 10).r.to_int(), 0)
assert_eq(lerp_color(a, b, 10, 10).r.to_int(), 200)
assert_eq(lerp_color(a, b, 5, 10).g.to_int(), 50)
assert_eq(lerp_color(a, b, 99, 10).b.to_int(), 50)
}
///|
test "contains is half-open" {
let r = rect(10, 10, 5, 5)
assert_true(contains(r, { x: 10, y: 10 }))
assert_true(contains(r, { x: 14, y: 14 }))
assert_false(contains(r, { x: 15, y: 10 }))
assert_false(contains(r, { x: 9, y: 10 }))
}
///|
test "text_width follows the 0.6em contract" {
assert_eq(text_width("12:30", 20), 60)
assert_eq(text_width("", 20), 0)
}