///| 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: "launcher", title: "Applications launcher", note: "plasmoid widget + popup, host-brokered catalog/launch", world: Some(("wit/worlds.wit", "launcher-widget")), ifaces: [ ("wit/plasmoid.wit", "plasmoid"), ("wit/capabilities.wit", "app-catalog"), ("wit/capabilities.wit", "app-launch"), ], }, { id: "clock", title: "Digital clock", note: "plasmoid widget with the host clock capability", world: Some(("wit/worlds.wit", "clock-widget")), ifaces: [("wit/plasmoid.wit", "plasmoid"), ("wit/capabilities.wit", "clock")], }, { id: "layout", title: "Layout indicator", note: "shows the window manager's layout mode", world: Some(("wit/wm/worlds.wit", "window-manager")), ifaces: [("wit/wm/policy.wit", "policy")], }, { id: "tasks", title: "Task list", note: "one pill per window, from the window manager's state", world: Some(("wit/wm/worlds.wit", "window-manager")), ifaces: [("wit/wm/policy.wit", "policy")], }, { id: "panel", title: "Panel", note: "containment: places widgets and draws the panel frame", world: Some(("wit/worlds.wit", "panel")), ifaces: [("wit/shell.wit", "panel-layout")], }, { id: "desktop", title: "Desktop surface", note: "wallpaper / desktop containment", world: Some(("wit/worlds.wit", "shell")), ifaces: [("wit/shell.wit", "desktop-surface")], }, { id: "window-decoration", title: "Window decoration", note: "server-side title bar and buttons", world: Some(("wit/wm/worlds.wit", "decoration-provider")), ifaces: [("wit/wm/decoration.wit", "decoration")], }, { id: "window-surface", title: "Client surface", note: "a client's committed buffer, composited by the host", world: None, ifaces: [("wit/compositor/surface.wit", "surface")], }, { id: "inspect", title: "Interface Explorer", note: "this tool: a native shell feature, it has no WIT component", world: None, ifaces: [], }, ] ///| let wit_keywords : Array[String] = [ "package", "interface", "world", "use", "type", "record", "enum", "variant", "flags", "resource", "import", "export", "include", "func", "constructor", "static", ] ///| fn find_component(id : String) -> Component? { 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 } ///| 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)) => { let w = extract_block(self.wit_file(file), "world", name) if w.length() > 0 { out.push("// " + file) for l in w { out.push(l) } out.push("") } } None => () } for fi in c.ifaces { let (file, name) = fi let text = self.wit_file(file) let b = extract_block(text, "interface", name) out.push("// " + file) if b.length() == 0 { out.push("(interface " + name + " not found - run from the repo root)") } else { for l in b { out.push(l) } } out.push("") } if c.ifaces.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 find_component(self.hover_id) { None => () Some(c) => { let p = tokens.palette let char_w = 6 let line_h = 14 let header_h = 22 + 16 + 10 let all = self.inspector_lines(c) // 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 all.length() < max_rows { all.length() } else { max_rows } let max_scroll = 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 mut m = 0 for l in all { if l.length() > m { m = l.length() } } let body_w = m * char_w + 20 let head_w = (if c.title.length() + 4 > c.note.length() { c.title.length() + 4 } else { c.note.length() }) * char_w let dw = (if body_w > head_w { body_w } else { head_w }) + 28 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, 12, p.foreground, bold=true)) local.push(@ui.text(14, 32, c.note, 10, p.foreground_muted)) local.push(@ui.fill_rect(@ui.rect(10, 42, dw - 20, 1), p.border)) let mut y = 52 + line_h / 2 for i = first; i < all.length() && i < first + rows; i = i + 1 { let l = all[i] let trimmed = l.trim_start(chars=" ").to_string() let indent = l.length() - trimmed.length() let x = 14 + indent * char_w if trimmed.has_prefix("//") { local.push(@ui.text(x, y, trimmed, 10, p.foreground_muted)) } else { let kw = first_word(trimmed) if wit_keywords.contains(kw) { local.push(@ui.text(x, y, kw, 10, p.accent)) let rest = trimmed[kw.length():].to_string() if rest.length() > 0 { local.push(@ui.text(x + kw.length() * char_w, y, rest, 10, p.foreground)) } } else { local.push(@ui.text(x, y, trimmed, 10, p.foreground)) } } 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 / all.length() < 12 { 12 } else { th * rows / all.length() } let ty = 48 + (th - thumb) * first / max_scroll local.push(@ui.fill_rect(@ui.rect(dw - 8, 48, 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, 32, "scroll for more", 10, p.foreground_muted)) } translate(local, dx, dy, ops) } } } ///| 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 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 }