diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index ccafd65..4c4cf4f 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -22,6 +22,7 @@ pub(all) struct MoonPlasmaCompositor { work_area : @wm.Rect windows : Map[UInt64, HostWindow] damage : @scene.DamageTracker + shell : @shell.DesktopShell mut layout_mode : @wm.LayoutMode } @@ -53,6 +54,7 @@ pub fn MoonPlasmaCompositor::new( work_area, windows: {}, damage: @scene.DamageTracker::new(), + shell: @shell.DesktopShell::new(), layout_mode: @wm.LayoutMode::Tiling, }) } @@ -260,7 +262,7 @@ pub fn MoonPlasmaCompositor::compose_frame( @wm.LayoutMode::Floating => "Floating" @wm.LayoutMode::Monocle => "Monocle" } - let (bg_ops, panel_ops) = @scene.build_shell_ops( + let (bg_ops, panel_ops) = self.shell.render( self.output_w, self.output_h, self.theme, diff --git a/compositor/host/moon.pkg b/compositor/host/moon.pkg index f7e5889..99cb7e5 100644 --- a/compositor/host/moon.pkg +++ b/compositor/host/moon.pkg @@ -3,6 +3,7 @@ import { "moonplasma/desktop/lib/wm" @wm, "moonplasma/desktop/compositor/sys" @sys, "moonplasma/desktop/compositor/scene" @scene, + "moonplasma/desktop/compositor/shell" @shell, "moonplasma/desktop/compositor/renderer" @renderer, "moonplasma/desktop/compositor/wayland" @wayland, } diff --git a/compositor/shell/moon.pkg b/compositor/shell/moon.pkg new file mode 100644 index 0000000..448d2b9 --- /dev/null +++ b/compositor/shell/moon.pkg @@ -0,0 +1,14 @@ +import { + "moonbitlang/core/builtin", + "moonplasma/desktop/interface/moonplasma/desktop/types" @types, + "moonplasma/desktop/interface/moonplasma/desktop/theme" @theme, + "moonplasma/desktop/lib/ui" @ui, + "moonplasma/desktop/lib/shell" @libshell, + "moonplasma/desktop/lib/panel" @panel, + "moonplasma/desktop/lib/clock" @clock, + "moonplasma/desktop/lib/launcher" @launcher, + "moonplasma/desktop/compositor/scene" @scene, + "moonplasma/desktop/compositor/sys" @sys, +} + +supported_targets = "-all+native" diff --git a/compositor/shell/shell.mbt b/compositor/shell/shell.mbt new file mode 100644 index 0000000..6d3b669 --- /dev/null +++ b/compositor/shell/shell.mbt @@ -0,0 +1,198 @@ +///| Loads the desktop shell (lib/shell background + panel chrome, lib/panel +/// layout, lib/launcher button, lib/clock widget) into the native compositor. +/// The shell logic is the same code the wasm components wrap; this package only +/// adapts its WIT-shaped types to the compositor's scene types. + +///| +fn conv_color(c : @types.Color) -> @scene.Rgba { + { r: c.r, g: c.g, b: c.b, a: c.a } +} + +///| +fn conv_theme(t : @scene.ThemeTokens) -> @theme.Tokens { + let p = t.palette + let col = fn(c : @scene.Rgba) -> @types.Color { { r: c.r, g: c.g, b: c.b, a: c.a } } + { + name: t.name, + palette: { + desktop_top: col(p.desktop_top), + desktop_bottom: col(p.desktop_bottom), + surface: col(p.surface), + surface_hover: col(p.surface_hover), + surface_popup: col(p.surface_popup), + foreground: col(p.foreground), + foreground_muted: col(p.foreground_muted), + accent: col(p.accent), + accent_foreground: col(p.accent_foreground), + border: col(p.border), + }, + font_size: t.font_size.reinterpret_as_uint(), + radius: t.radius.reinterpret_as_uint(), + spacing: t.spacing.reinterpret_as_uint(), + panel_height: t.panel_height.reinterpret_as_uint(), + } +} + +///| Translate a widget render-list into scene ops, offset to screen coordinates. +fn translate( + ops : Array[@types.DrawOp], + dx : Int, + dy : Int, + out : Array[@scene.DrawOp], +) -> Unit { + let rect = fn(r : @types.Rect) -> @scene.Rect { + { + x: r.x + dx, + y: r.y + dy, + w: r.w.reinterpret_as_int(), + h: r.h.reinterpret_as_int(), + } + } + for op in ops { + match op { + FillRect(f) => + out.push( + @scene.DrawOp::FillRect( + rect(f.rect), + conv_color(f.color), + f.radius.reinterpret_as_int(), + ), + ) + StrokeRect(s) => + out.push( + @scene.DrawOp::StrokeRect( + rect(s.rect), + conv_color(s.color), + s.width.reinterpret_as_int(), + s.radius.reinterpret_as_int(), + ), + ) + Line(l) => + out.push( + @scene.DrawOp::Line( + l.start.x + dx, + l.start.y + dy, + l.end.x + dx, + l.end.y + dy, + conv_color(l.color), + l.width.reinterpret_as_int(), + ), + ) + Circle(c) => + out.push( + @scene.DrawOp::Circle( + c.center.x + dx, + c.center.y + dy, + c.radius.reinterpret_as_int(), + conv_color(c.color), + c.filled, + ), + ) + Text(t) => { + let align = match t.style.align { + LEFT => @scene.TextAlign::Left + CENTER => @scene.TextAlign::Center + RIGHT => @scene.TextAlign::Right + } + out.push( + @scene.DrawOp::Text( + t.origin.x + dx, + t.origin.y + dy, + t.text, + t.style.size.reinterpret_as_int(), + conv_color(t.style.color), + align, + t.style.bold, + ), + ) + } + PushClip(r) => out.push(@scene.DrawOp::PushClip(rect(r))) + PopClip => out.push(@scene.DrawOp::PopClip) + } + } +} + +///| Current local time in the WIT `datetime` shape. +fn now() -> @types.Datetime { + { + year: @sys.wall_time_field(0).reinterpret_as_uint(), + month: @sys.wall_time_field(1).to_byte(), + day: @sys.wall_time_field(2).to_byte(), + hour: @sys.wall_time_field(3).to_byte(), + minute: @sys.wall_time_field(4).to_byte(), + second: @sys.wall_time_field(5).to_byte(), + weekday: @sys.wall_time_field(6).to_byte(), + } +} + +///| The desktop shell: wallpaper + bottom panel with launcher, task list and clock. +pub(all) struct DesktopShell { + launcher_state : @launcher.State +} + +///| +pub fn DesktopShell::new() -> DesktopShell { + { launcher_state: @launcher.State::new() } +} + +///| Build the (background, panel) render-lists for the current frame. +pub fn DesktopShell::render( + self : DesktopShell, + w : Int, + h : Int, + theme : @scene.ThemeTokens, + layout_label : String, + window_titles : Array[String], +) -> (Array[@scene.DrawOp], Array[@scene.DrawOp]) { + let tokens = conv_theme(theme) + let bg : Array[@scene.DrawOp] = [] + translate(@libshell.render_background(@ui.size(w, h), tokens), 0, 0, bg) + + let (_, _, clock_w, clock_h, clock_min) = @clock.digital_info() + let slots : Array[@panel.SlotIn] = [ + { id: "launcher", pref_w: 190, pref_h: 36, min_w: 170, stretch: false, end_align: false }, + { id: "layout", pref_w: 96, pref_h: 36, min_w: 80, stretch: false, end_align: false }, + { id: "tasks", pref_w: 100, pref_h: 36, min_w: 0, stretch: true, end_align: false }, + { id: "clock", pref_w: clock_w, pref_h: clock_h, min_w: clock_min, stretch: false, end_align: true }, + ] + let panel_ops : Array[@scene.DrawOp] = [] + match @panel.layout(w, h, theme.panel_height, 4, tokens.spacing.reinterpret_as_int(), slots) { + Err(_) => () + Ok(pl) => { + let pr = @ui.rect(pl.x, pl.y, pl.w, pl.h) + translate(@libshell.render_panel(pr, tokens), 0, 0, panel_ops) + for p in pl.placements { + let size = @ui.size(p.w, p.h) + let local : Array[@types.DrawOp] = match p.id { + "launcher" => @launcher.render_button(size, tokens, self.launcher_state) + "clock" => @clock.render_digital(size, tokens, now()) + "layout" => [ + @ui.fill_rect(@ui.rect(0, 0, p.w, p.h), tokens.palette.surface_hover, radius=tokens.radius.reinterpret_as_int()), + @ui.text(p.w / 2, p.h / 2, layout_label, tokens.font_size.reinterpret_as_int() - 2, tokens.palette.foreground, align=CENTER), + ] + _ => [] + } + translate(local, p.x, p.y, panel_ops) + if p.id == "tasks" { + let mut tx = 0 + for title in window_titles { + let pill_w = 140 + if tx + pill_w <= p.w { + translate( + [ + @ui.fill_rect(@ui.rect(tx, 0, pill_w, p.h), tokens.palette.surface_hover, radius=tokens.radius.reinterpret_as_int()), + @ui.text(tx + 10, p.h / 2, title, tokens.font_size.reinterpret_as_int() - 2, tokens.palette.foreground), + ], + p.x, + p.y, + panel_ops, + ) + tx = tx + pill_w + 6 + } + } + } + } + } + } + (bg, panel_ops) +} diff --git a/compositor/sys/sys.mbt b/compositor/sys/sys.mbt index dd016b2..5285f68 100644 --- a/compositor/sys/sys.mbt +++ b/compositor/sys/sys.mbt @@ -170,3 +170,11 @@ pub fn sleep_ms(ms : Int) -> Unit { pub fn getpid() -> Int { c_getpid() } + +///| +extern "C" fn c_wall_time_field(which : Int) -> Int = "mp_wall_time_field" + +///| Local wall clock: 0 year, 1 month (1-12), 2 day, 3 hour, 4 minute, 5 second, 6 weekday (0 = Sunday). +pub fn wall_time_field(which : Int) -> Int { + c_wall_time_field(which) +} diff --git a/compositor/sys/sys_stub.c b/compositor/sys/sys_stub.c index 9160918..de36863 100644 --- a/compositor/sys/sys_stub.c +++ b/compositor/sys/sys_stub.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "moonbit.h" @@ -291,3 +292,21 @@ MOONBIT_FFI_EXPORT int32_t mp_getpid(void) { return (int32_t)getpid(); } + +/* Local wall-clock field: 0 year, 1 month(1-12), 2 day, 3 hour, 4 minute, 5 second, 6 weekday(0=Sun). */ +MOONBIT_FFI_EXPORT +int32_t mp_wall_time_field(int32_t which) { + time_t t = time(NULL); + struct tm tmv; + localtime_r(&t, &tmv); + switch (which) { + case 0: return tmv.tm_year + 1900; + case 1: return tmv.tm_mon + 1; + case 2: return tmv.tm_mday; + case 3: return tmv.tm_hour; + case 4: return tmv.tm_min; + case 5: return tmv.tm_sec; + case 6: return tmv.tm_wday; + default: return 0; + } +} diff --git a/interface/moonplasma/desktop/theme/moon.pkg b/interface/moonplasma/desktop/theme/moon.pkg index 7825dc5..8e9871f 100644 --- a/interface/moonplasma/desktop/theme/moon.pkg +++ b/interface/moonplasma/desktop/theme/moon.pkg @@ -3,3 +3,12 @@ import { } warnings = "-44" + +// ffi.mbt is generated wasm-only; native builds (the compositor) get abort stubs +// from native_stub.mbt so the pure `Tokens` types stay usable. +options( + targets: { + "ffi.mbt": ["wasm", "wasm-gc"], + "native_stub.mbt": ["native"], + }, +) diff --git a/interface/moonplasma/desktop/theme/native_stub.mbt b/interface/moonplasma/desktop/theme/native_stub.mbt new file mode 100644 index 0000000..ef94d77 --- /dev/null +++ b/interface/moonplasma/desktop/theme/native_stub.mbt @@ -0,0 +1,33 @@ +///| Native stand-ins for the wasm component-model FFI in ffi.mbt. The native +/// compositor uses only the `Tokens`/`Palette` types; `get_tokens` is a guest +/// import that has no meaning outside a wasm component, so it aborts. + +///| +fn wasmImportGetTokens(_p0 : Int) -> Unit { + abort("theme.get-tokens is a wasm component import; unavailable on native") +} + +///| +fn mbt_ffi_free(_position : Int) -> Unit { + abort("wasm-only") +} + +///| +fn mbt_ffi_ptr2str(_ptr : Int, _len : Int) -> String { + abort("wasm-only") +} + +///| +fn mbt_ffi_load32(_offset : Int) -> Int { + abort("wasm-only") +} + +///| +fn mbt_ffi_malloc(_size : Int) -> Int { + abort("wasm-only") +} + +///| +fn mbt_ffi_load8_u(_offset : Int) -> Int { + abort("wasm-only") +}