///| Interface Explorer: a shell tool. Toggle it from the panel's "Interface Explorer" button, then /// hover any system component (launcher, clock, panel, window title bar, ...) /// to see the WIT interface that component implements in a dialog. /// WIT text is read from the repo's `wit/` directory at runtime, so the dialog /// always shows what is on disk. ///| A system component the inspector knows about. priv struct Component { id : String title : String note : String /// (file, world name) world : (String, String)? /// (file, interface name) pairs, in display order. ifaces : Array[(String, String)] } ///| let components : Array[Component] = [ { id: "panel", title: "Panel", note: "containment: places widgets and draws the panel frame", world: Some(("wit/worlds.wit", "panel")), ifaces: [], }, { id: "desktop", title: "Desktop surface", note: "wallpaper / desktop containment", world: Some(("wit/worlds.wit", "shell")), ifaces: [], }, { id: "window-decoration", title: "Window decoration", note: "server-side title bar and buttons", world: Some(("wit/wm/worlds.wit", "decoration-provider")), ifaces: [], }, { id: "window-surface", title: "Client surface", note: "a client's committed buffer, composited by the host", world: None, ifaces: [("wit/compositor/surface.wit", "surface")], }, ] ///| Last path component of `path` (a module id may be a full file path). fn base_name(path : String) -> String { let mut start = 0 for i = 0; i < path.length(); i = i + 1 { if path[i] == '/' { start = i + 1 } } path[start:].to_string() } ///| The component behind `id`: a loaded panel module (described from its real binary: /// the plasmoid contract it exports and the capability interfaces it imports) or one /// of the fixed shell parts (panel, desktop, window decoration, client surface). fn DesktopShell::find_component(self : DesktopShell, id : String) -> Component? { match self.widget_by_slot(id) { Some(w) => { let ifaces : Array[(String, String)] = [("", "plasmoid")] for c in w.caps { ifaces.push(("", c)) } Some({ id, title: w.pl.name, note: "wasm module " + base_name(w.module_id) + ", imports: " + (if w.caps.length() == 0 { "nothing" } else { w.caps.join(", ") }), world: None, ifaces, }) } None => { for c in components { if c.id == id { return Some(c) } } None } } } ///| Read a file under the repo's wit/ directory (cached); "" when missing. fn DesktopShell::wit_file(self : DesktopShell, path : String) -> String { match self.wit_cache.get(path) { Some(t) => t None => { let bytes = @sys.read_file_bytes(path) let text = if bytes.length() == 0 { "" } else { @utf8.decode_lossy(bytes) } self.wit_cache.set(path, text) text } } } ///| The `kind name { ... }` block (with its leading /// docs), one line per entry. fn extract_block(text : String, kind : String, name : String) -> Array[String] { let lines : Array[String] = [] for l in text.split("\n") { lines.push(l.to_string()) } let head = kind + " " + name + " {" let out : Array[String] = [] let mut start = -1 for i = 0; i < lines.length(); i = i + 1 { if lines[i].has_prefix(head) { start = i break } } if start < 0 { return out } // leading doc comments let mut s = start while s > 0 && lines[s - 1].has_prefix("///") { s = s - 1 } for i = s; i < lines.length(); i = i + 1 { out.push(lines[i]) if i >= start && (lines[i] == "}" || (i == start && lines[i].has_suffix("}"))) { break } } out } ///| WIT files searched for interface/world definitions. let wit_sources : Array[String] = [ "wit/worlds.wit", "wit/plasmoid.wit", "wit/capabilities.wit", "wit/shell.wit", "wit/theme.wit", "wit/types.wit", "wit/wm/worlds.wit", "wit/wm/policy.wit", "wit/wm/decoration.wit", "wit/compositor/surface.wit", ] ///| Interface block `name` from whichever WIT file defines it. fn DesktopShell::find_interface(self : DesktopShell, name : String) -> Array[String] { for f in wit_sources { let b = extract_block(self.wit_file(f), "interface", name) if b.length() > 0 { return b } } [] } ///| All dialog lines for a component (blank line between blocks). fn DesktopShell::inspector_lines(self : DesktopShell, c : Component) -> Array[String] { let out : Array[String] = [] match c.world { Some((file, name)) => { for l in humanise_world(self.wit_file(file), name) { out.push(l) } out.push("") } None => () } // Interfaces come from the world (exports + imports) unless listed explicitly. let names : Array[String] = [] match c.world { Some((file, wname)) if c.ifaces.length() == 0 => for n in world_interfaces(self.wit_file(file), wname) { names.push(n) } _ => for fi in c.ifaces { names.push(fi.1) } } for name in names { let b = self.find_interface(name) if b.length() == 0 { out.push("(interface " + name + " not found - run from the repo root)") } else { for l in humanise_interface(name, b) { out.push(l) } } out.push("") } if names.length() == 0 && c.world is None { out.push("(no WIT: not a component)") } // drop trailing blank while out.length() > 0 && out[out.length() - 1] == "" { ignore(out.pop()) } out } ///| Word length at the start of `s` (letters, digits, '-'). fn first_word(s : String) -> String { let mut n = 0 while n < s.length() { let ch = s[n] let ok = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '-' if !ok { break } n = n + 1 } s[:n].to_string() } ///| Draw the dialog for the hovered component into `ops`. fn DesktopShell::render_inspector( self : DesktopShell, w : Int, h : Int, tokens : @theme.Tokens, ops : Array[@scene.DrawOp], ) -> Unit { match self.find_component(self.hover_id) { None => () Some(c) => { let p = tokens.palette let line_h = 18 let body_sz = 12 let header_h = 22 + 16 + 10 let text_w = 400 let dw = text_w + 28 // Word-wrap each paragraph to `text_w` using real glyph widths. A row is // a list of (word, bold) runs; `**term**` in the prose marks bold terms. let rows_all : Array[(Int, Array[(String, Bool, Bool)])] = [] // (kind: 0 body, 1 heading, 2 blank) for para in self.inspector_lines(c) { if para == "" { rows_all.push((2, [])) } else if para.has_prefix("# ") { rows_all.push((1, [(para[2:].to_string(), true, false)])) } else { // (word, bold, glued to the previous word: no space between) let words : Array[(String, Bool, Bool)] = [] let mut is_bold = false let mut prev_space = true for seg in para.split("**") { let sg = seg.to_string() let mut first_word = true for wd in sg.split(" ") { let wd = wd.to_string() if wd.length() > 0 { words.push((wd, is_bold, first_word && !sg.has_prefix(" ") && !prev_space)) } first_word = false } if sg.length() > 0 { prev_space = sg.has_suffix(" ") } is_bold = !is_bold } let space = @renderer.text_width(" ", body_sz, false, false) let mut cur : Array[(String, Bool, Bool)] = [] let mut cw = 0 for wv in words { let ww = @renderer.text_width(wv.0, body_sz, wv.1, false) if cur.length() > 0 && !wv.2 && cw + space + ww > text_w { rows_all.push((0, cur)) cur = [] cw = 0 } cw = cw + (if cur.length() > 0 && !wv.2 { space } else { 0 }) + ww cur.push(wv) } if cur.length() > 0 { rows_all.push((0, cur)) } } } // Compact dialog: at most ~14 visible rows (or what fits the screen), // the rest scrolls with the mouse wheel. let fit = (h - 16 - header_h - 12) / line_h let max_rows = if fit < 14 { fit } else { 14 } let max_rows = if max_rows < 1 { 1 } else { max_rows } let rows = if rows_all.length() < max_rows { rows_all.length() } else { max_rows } let max_scroll = rows_all.length() - rows if self.inspect_scroll > max_scroll { self.inspect_scroll = max_scroll } if self.inspect_scroll < 0 { self.inspect_scroll = 0 } let first = self.inspect_scroll let dh = header_h + rows * line_h + 12 let mut dx = self.hover_x + 18 let mut dy = self.hover_y + 18 if dx + dw > w { dx = w - dw - 4 } if dx < 4 { dx = 4 } if dy + dh > h { dy = self.hover_y - dh - 12 } if dy < 4 { dy = 4 } let local : Array[@types.DrawOp] = [] let r = tokens.radius.reinterpret_as_int() local.push(@ui.fill_rect(@ui.rect(0, 0, dw, dh), p.surface_popup, radius=r + 4)) local.push(@ui.stroke_rect(@ui.rect(0, 0, dw, dh), p.accent, radius=r + 4)) local.push(@ui.text(14, 16, c.title, 14, p.foreground, bold=true)) // Clip the note to the popup so a long path or import list can't run past its edge. let mut note = c.note while note.length() > 3 && @renderer.text_width(note, 11, false, false) > dw - 28 { note = note[:note.length() - 3].to_string() if @renderer.text_width(note + "..", 11, false, false) <= dw - 28 { note = note + ".." break } } local.push(@ui.text(14, 34, note, 11, p.foreground_muted)) local.push(@ui.fill_rect(@ui.rect(10, 46, dw - 20, 1), p.border)) let mut y = 58 + line_h / 2 let space = @renderer.text_width(" ", body_sz, false, false) for i = first; i < rows_all.length() && i < first + rows; i = i + 1 { let (kind, runs) = rows_all[i] if kind == 1 { local.push(@ui.text(14, y, runs[0].0, 13, p.accent, bold=true)) } else if kind == 0 { let mut x = 14 for run in runs { let (wd, b, glued) = run if glued { x = x - space } local.push(@ui.text(x, y, wd, body_sz, if b { p.accent } else { p.foreground }, bold=b)) x = x + @renderer.text_width(wd, body_sz, b, false) + space } } y = y + line_h } if max_scroll > 0 { // Scrollbar track + thumb on the right edge, plus a hint. let th = rows * line_h let thumb = if th * rows / rows_all.length() < 12 { 12 } else { th * rows / rows_all.length() } let ty = 52 + (th - thumb) * first / max_scroll local.push(@ui.fill_rect(@ui.rect(dw - 8, 52, 3, th), p.border)) local.push(@ui.fill_rect(@ui.rect(dw - 8, ty, 3, thumb), p.accent)) local.push(@ui.text(dw - 24 - 90, 34, "scroll for more", 11, p.foreground_muted)) } let scene_ops : Array[@scene.DrawOp] = [] translate(local, dx, dy, scene_ops) for op in scene_ops { ops.push(op) } } } } ///| True while inspect mode is on. pub fn DesktopShell::inspecting(self : DesktopShell) -> Bool { self.inspect_on } ///| Turn inspect mode on/off. The launcher popup is left as it is. pub fn DesktopShell::set_inspecting(self : DesktopShell, on : Bool) -> Unit { self.inspect_on = on self.hover_id = "" } ///| Shell component under (x, y): a panel widget id, "panel" for bare panel, /// "" for anything else (windows or the desktop - the compositor resolves those). pub fn DesktopShell::component_at(self : DesktopShell, x : Int, y : Int) -> String { for entry in self.comps { let (id, r) = entry if inside(r, x, y) { return id } } if inside(self.panel_rect, x, y) { "panel" } else { "" } } ///| Record what the pointer is over while inspecting. pub fn DesktopShell::set_hover(self : DesktopShell, id : String, x : Int, y : Int) -> Unit { if id != self.hover_id { self.inspect_scroll = 0 } self.hover_id = id self.hover_x = x self.hover_y = y } ///| Dialog lines for component `id` (for tests and tooling). pub fn DesktopShell::inspector_lines_for(self : DesktopShell, id : String) -> Array[String] { match self.find_component(id) { Some(c) => self.inspector_lines(c) None => [] } } ///| Mouse-wheel scroll of the explorer dialog (lines, + = down). True if consumed. pub fn DesktopShell::scroll_inspector(self : DesktopShell, lines : Int) -> Bool { if !self.inspect_on || self.hover_id == "" { return false } self.inspect_scroll = self.inspect_scroll + lines if self.inspect_scroll < 0 { self.inspect_scroll = 0 } true }