///| 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 apps : Array[@types.AppEntry] exec : Map[String, String] /// Wayland socket handed to launched apps as WAYLAND_DISPLAY. display : String mut screen_w : Int mut launcher_rect : @scene.Rect mut popup_rect : @scene.Rect // WIT inspector state (see inspector.mbt) mut inspect_on : Bool mut hover_id : String mut hover_x : Int mut inspect_scroll : Int mut hover_y : Int mut panel_rect : @scene.Rect comps : Array[(String, @scene.Rect)] wit_cache : Map[String, String] } ///| Apps the launcher offers, kept only if their executable exists. /// (id, name, comment, category, glyph, executable) let candidates : Array[(String, String, String, String, String, String)] = [ ("org.moonplasma.terminal", "Terminal", "Command-line shell", "System", ">_", "foot"), ("org.moonplasma.konsole", "Konsole", "KDE terminal", "System", ">_", "konsole"), ("org.moonplasma.files", "Files", "Browse folders and files", "Utilities", "F", "dolphin"), ("org.moonplasma.browser", "Web Browser", "Browse the web", "Internet", "W", "firefox"), ("org.moonplasma.editor", "Text Editor", "Edit plain text files", "Utilities", "T", "kate"), ("org.moonplasma.settings", "System Settings", "Configure the desktop", "System", "S", "systemsettings"), ] ///| pub fn DesktopShell::new(display : String) -> DesktopShell { let apps : Array[@types.AppEntry] = [] let exec : Map[String, String] = {} for c in candidates { let (id, name, comment, category, glyph, bin) = c if @sys.has_exe(bin) { apps.push({ id, name, comment, category, glyph }) exec.set(id, bin) } } { launcher_state: @launcher.State::new(), apps, exec, display, screen_w: 0, launcher_rect: { x: 0, y: 0, w: 0, h: 0 }, popup_rect: { x: 0, y: 0, w: 0, h: 0 }, inspect_on: false, hover_id: "", hover_x: 0, inspect_scroll: 0, hover_y: 0, panel_rect: { x: 0, y: 0, w: 0, h: 0 }, comps: [], wit_cache: {}, } } ///| Truncate `s` with ".." so it fits `width` px (the renderer's small text is 6 px per char). fn fit_title(s : String, width : Int) -> String { let max_chars = width / 6 if s.length() <= max_chars { s } else { s[:max_chars - 2].to_string() + ".." } } ///| fn inside(r : @scene.Rect, x : Int, y : Int) -> Bool { r.w > 0 && x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h } ///| True while the launcher popup is open (it then owns keyboard input). pub fn DesktopShell::popup_open(self : DesktopShell) -> Bool { self.launcher_state.open } ///| True if (x, y) is over the launcher button or its open popup. pub fn DesktopShell::hit(self : DesktopShell, x : Int, y : Int) -> Bool { inside(self.launcher_rect, x, y) || (self.launcher_state.open && inside(self.popup_rect, x, y)) } ///| fn DesktopShell::launch(self : DesktopShell, id : String) -> Result[Unit, String] { match self.exec.get(id) { None => Err("no executable for " + id) Some(bin) => if @sys.spawn(bin, self.display) { Ok(()) } else { Err("could not start " + bin) } } } ///| wp_cursor_shape_v1 value for the shell UI under (x, y): 4 pointer (hand) over the /// launcher button and popup entries, 9 text over the search field, else 0 (not shell-specific). pub fn DesktopShell::cursor_at(self : DesktopShell, x : Int, y : Int) -> UInt { if self.inspect_on { // help cursor everywhere while inspecting; hand over the toggle itself return if self.component_at(x, y) == "inspect" { 4U } else { 3U } } if inside(self.launcher_rect, x, y) { 4U } else if self.launcher_state.open && inside(self.popup_rect, x, y) { if self.launcher_state.hover >= 0 { 4U } else if y - self.popup_rect.y < 52 { 9U } else { 1U } } else { 0U } } ///| Pointer press at (x, y). Returns true when the shell consumed it. pub fn DesktopShell::pointer_down(self : DesktopShell, x : Int, y : Int) -> Bool { let launch = fn(id : String) { self.launch(id) } if self.component_at(x, y) == "inspect" { self.set_inspecting(!self.inspect_on) true } else if self.inspect_on { true // inspecting: clicks don't reach widgets or windows } else if inside(self.launcher_rect, x, y) { ignore( @launcher.handle_event(self.apps, self.launcher_state, Widget, PointerDown({ x: x - self.launcher_rect.x, y: y - self.launcher_rect.y }), launch), ) true } else if self.launcher_state.open { if inside(self.popup_rect, x, y) { ignore( @launcher.handle_event(self.apps, self.launcher_state, Popup, PointerDown({ x: x - self.popup_rect.x, y: y - self.popup_rect.y }), launch), ) true } else { // Click outside: dismiss, and let the click through. ignore(@launcher.handle_event(self.apps, self.launcher_state, Popup, FocusLost, launch)) false } } else { false } } ///| Pointer motion; returns true when over the popup (so windows don't see it). pub fn DesktopShell::pointer_move(self : DesktopShell, x : Int, y : Int) -> Bool { if self.launcher_state.open { let launch = fn(id : String) { self.launch(id) } if inside(self.popup_rect, x, y) { ignore( @launcher.handle_event(self.apps, self.launcher_state, Popup, PointerMove({ x: x - self.popup_rect.x, y: y - self.popup_rect.y }), launch), ) return true } ignore(@launcher.handle_event(self.apps, self.launcher_state, Popup, PointerLeave, launch)) } false } ///| Key name as used by lib/launcher ("Escape", "Backspace", "Enter" or one character). pub fn DesktopShell::key_down(self : DesktopShell, name : String) -> Unit { let launch = fn(id : String) { self.launch(id) } ignore(@launcher.handle_event(self.apps, self.launcher_state, Popup, KeyDown(name), launch)) } ///| 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) self.screen_w = w 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: "inspect", pref_w: 132, pref_h: 36, min_w: 120, 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] = [] self.comps.clear() self.panel_rect = { x: 0, y: 0, w: 0, h: 0 } 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) self.panel_rect = { x: pl.x, y: pl.y, w: pl.w, h: 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(), hour12=true) "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), ] "inspect" => { let on = self.inspect_on [ @ui.fill_rect(@ui.rect(0, 0, p.w, p.h), if on { tokens.palette.accent } else { tokens.palette.surface_hover }, radius=tokens.radius.reinterpret_as_int()), @ui.text(p.w / 2, p.h / 2, "Interface Explorer", tokens.font_size.reinterpret_as_int() - 2, if on { tokens.palette.accent_foreground } else { tokens.palette.foreground }, align=CENTER, bold=true), ] } _ => [] } translate(local, p.x, p.y, panel_ops) self.comps.push((p.id, { x: p.x, y: p.y, w: p.w, h: p.h })) if p.id == "launcher" { self.launcher_rect = { x: p.x, y: p.y, w: p.w, h: p.h } } 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, fit_title(title, pill_w - 20), tokens.font_size.reinterpret_as_int() - 2, tokens.palette.foreground), ], p.x, p.y, panel_ops, ) tx = tx + pill_w + 6 } } } } } } if self.launcher_state.open { let size = @launcher.popup_size(self.apps, self.launcher_state) let pw = size.w.reinterpret_as_int() let ph = size.h.reinterpret_as_int() let (px, py) = @panel.popup_position(w, h - theme.panel_height, self.launcher_rect.x, pw, ph, 6) self.popup_rect = { x: px, y: py, w: pw, h: ph } translate(@launcher.render_popup(self.apps, tokens, self.launcher_state), px, py, panel_ops) } else { self.popup_rect = { x: 0, y: 0, w: 0, h: 0 } } if self.inspect_on { self.render_inspector(w, h, tokens, panel_ops) } (bg, panel_ops) }