diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index f19992e..32f2dba 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -467,6 +467,20 @@ pub fn MoonPlasmaCompositor::handle_input( self.ptr_y = y let xi = x.to_int() let yi = y.to_int() + if self.shell.inspecting() { + self.leave_pointer() + let id = match self.shell.component_at(xi, yi) { + "" => + match self.window_at(xi, yi) { + Some((_, true)) => "window-surface" + Some((_, false)) => "window-decoration" + None => "desktop" + } + sid => sid + } + self.shell.set_hover(id, xi, yi) + return + } if self.shell.pointer_move(xi, yi) || self.shell.hit(xi, yi) { self.leave_pointer() return @@ -500,6 +514,11 @@ pub fn MoonPlasmaCompositor::handle_input( _ => self.leave_pointer() } } + Scroll(v) => { + // ~10 units per wheel notch -> 3 lines. + let lines = if v > 0.0 { (v / 10.0).to_int() + 1 } else { (v / 10.0).to_int() - 1 } + ignore(self.shell.scroll_inspector(lines)) + } PointerButton(button, pressed) => { let xi = self.ptr_x.to_int() let yi = self.ptr_y.to_int() @@ -542,7 +561,12 @@ pub fn MoonPlasmaCompositor::handle_input( } } Key(code, pressed) => - if self.shell.popup_open() { + if self.shell.inspecting() { + // Escape leaves inspect mode; other keys are not forwarded while inspecting. + if code == 1U && pressed { + self.shell.set_inspecting(false) + } + } else if self.shell.popup_open() { if pressed { let name = key_name(code, (self.mods & 1U) != 0U) if name.length() > 0 { diff --git a/compositor/nested/nested.mbt b/compositor/nested/nested.mbt index 655c098..ec0640f 100644 --- a/compositor/nested/nested.mbt +++ b/compositor/nested/nested.mbt @@ -7,6 +7,8 @@ pub(all) enum InputEvent { PointerMove(Double, Double) PointerButton(UInt, Bool) + /// Vertical wheel scroll in wl_fixed units / 256 (positive = down). + Scroll(Double) Key(UInt, Bool) Modifiers(UInt, UInt, UInt, UInt) } @@ -241,6 +243,14 @@ fn NestedWindow::dispatch( let state = r.u32() self.input.push(PointerButton(button, state == 1U)) } + 4U => { + let _time = r.u32() + let axis = r.u32() + let value = r.i32().to_double() / 256.0 + if axis == 0U { + self.input.push(Scroll(value)) + } + } _ => () } } else if self.live_buffers.contains(obj) { diff --git a/compositor/shell/inspector.mbt b/compositor/shell/inspector.mbt new file mode 100644 index 0000000..f2174e7 --- /dev/null +++ b/compositor/shell/inspector.mbt @@ -0,0 +1,364 @@ +///| 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 +} diff --git a/compositor/shell/shell.mbt b/compositor/shell/shell.mbt index a6cd8ab..274714b 100644 --- a/compositor/shell/shell.mbt +++ b/compositor/shell/shell.mbt @@ -135,6 +135,15 @@ pub(all) struct DesktopShell { 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. @@ -167,6 +176,14 @@ pub fn DesktopShell::new(display : String) -> DesktopShell { 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: {}, } } @@ -212,6 +229,10 @@ fn DesktopShell::launch(self : DesktopShell, id : String) -> Result[Unit, String ///| 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) { @@ -230,7 +251,12 @@ pub fn DesktopShell::cursor_at(self : DesktopShell, x : Int, y : Int) -> UInt { ///| 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 inside(self.launcher_rect, x, y) { + 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), ) @@ -289,15 +315,19 @@ pub fn DesktopShell::render( 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) @@ -308,9 +338,17 @@ pub fn DesktopShell::render( @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 } } @@ -345,5 +383,8 @@ pub fn DesktopShell::render( } 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) }