# Nacho Live UI & Theming Guide

Nacho's GPUI renderer (`nacho-gpui`) is a pure, domain-agnostic primitive renderer. Every window frame, sidebar, header, chat stream, settings panel, theme, and widget is produced dynamically by plugins calling `service.ui` (`nacho.ui-registry`).

You can edit Nacho's UI **live** in two ways:
1. **Instant (zero compile):** Call `ui_set_theme`, `ui_set_layout`, or `ui_register_surface` to tweak colors, layout, or inject declarative UI panels immediately.
2. **Full Stateful WASM Plugin (~1–2s rebuilds):** Scaffold a `wasm32-wasip2` plugin that registers a dynamic `UiProvider` (`render_ui`), handles button/toggle clicks (`ui_action`), persists state in `service.storage`, and hot-swaps live via `plugin_load_file` / `plugin_load_wasm`.

---

## 1. Slots & Priority Composition

`nacho.ui-registry` renders `UiSlot::WindowRoot` first, then recursively resolves every `UiNode::SlotRef { slot }` to all active surfaces/providers targeting `UiSlot::Named { name }`:

| Slot Name | Where it renders | Behavior |
|---|---|---|
| `WindowRoot` (`{"kind":"window_root"}`) | Entire window frame (`ui-shell`, priority `0`) | Highest priority wins (replaces whole shell). |
| `left_rail` | Left sidebar (`ui-sidebar`, priority `10`) | Stacked vertically if multiple surfaces target it. |
| `center_stage` | Main workspace (`ui-conversation`, priority `10`) | Stacked vertically if multiple surfaces target it. |
| `right_drawer` | Slide-out workbench drawer (`ui-workbench`, priority `10`) | Shown when drawer is open. |
| `sidebar_top` | Inside expanded sidebar, right below top bar | Additive extension slot (zero-height when empty). |
| `sidebar_footer` | Inside expanded sidebar, above profile dock | Additive extension slot (zero-height when empty). |
| `header_extra` | Inside chat top header bar, before right controls | Additive extension slot (zero-height when empty). |
| `composer_top` | Directly above the chat composer card | Additive extension slot (zero-height when empty). |
| `status_bar` | Thin bottom bar below main workspace | Additive extension slot (zero-height when empty). |

**Tip:** To **add** a widget without touching existing panels, target one of the additive slots (`header_extra`, `sidebar_top`, `sidebar_footer`, `composer_top`, `status_bar`). To **replace** an existing UI plugin completely, either hot-swap its plugin ID (`nacho.ui.sidebar`, `nacho.ui.conversation`, `nacho.ui.shell`) or disable it and mount your own provider for `left_rail` / `center_stage` / `WindowRoot`.

---

## 2. ThemePatch & LayoutPatch

### `ThemePatch` (via `ui_set_theme` or `service.ui` → `"set_theme_patch"`)
Higher `priority` patches overlay lower-priority ones (`ui-theme-obsidian` uses priority `0`; use priority `50+` for overrides). Colors use `#RRGGBB` or `#RRGGBBAA` hex strings:

| Field | Example | Meaning |
|---|---|---|
| `mode` | `"dark"` / `"light"` | Base palette mode |
| `canvas` | `"#0E0F13"` | Main window background |
| `surface` | `"#14161D"` | Elevated surface / cards |
| `panel` | `"#12141A"` | Sidebar / header panel fill |
| `hover` | `"#1E222D"` | Interactive hover fill |
| `code_bg` | `"#0B0D11"` | Code & diff block fill |
| `user_bubble` | `"#1D2230"` | User message bubble fill |
| `border` | `"#262A36"` | Subtle divider / border |
| `text` | `"#F2F4F8"` | Primary foreground text |
| `muted` | `"#8E95A5"` | Secondary / muted text |
| `accent` | `"#FF6B2C"` | Primary accent color |
| `success` | `"#22C55E"` | Success tone |
| `warning` | `"#F59E0B"` | Warning tone |
| `danger` | `"#EF4444"` | Danger / error tone |
| `radius` | `10` | Base corner radius in px |

### `LayoutPatch` (via `ui_set_layout` or `service.ui` → `"set_layout_patch"`)
Fields (all optional): `show_sidebar` (bool), `show_inspector` (bool), `show_status_bar` (bool), `sidebar_width` (u16 px), `inspector_width` (u16 px), `use_system_titlebar` (bool).

---

## 3. `UiNode` Primitive Reference

Every `UiNode` uses `#[serde(tag = "type", rename_all = "snake_case")]`:

### Layout & Containers
- `box`: `{ "type": "box", "style": NodeStyle, "children": [UiNode] }`
- `row` / `column`: `{ "type": "row", "gap": 8, "children": [UiNode] }`
- `grid`: `{ "type": "grid", "columns": 2, "gap": 8, "children": [UiNode] }`
- `card`: `{ "type": "card", "title": "Title", "tone": "default", "children": [UiNode] }`
- `scroll`: `{ "type": "scroll", "id": "my-scroll", "style": NodeStyle, "children": [UiNode] }`
- `clickable`: `{ "type": "clickable", "id": "...", "style": NodeStyle, "active": false, "service": "...", "plugin_id": "...", "action_id": "...", "payload": {}, "children": [UiNode] }`
- `slot_ref`: `{ "type": "slot_ref", "slot": "header_extra" }`
- `spacer`: `{ "type": "spacer" }`
- `divider`: `{ "type": "divider" }`
- `window_controls`: `{ "type": "window_controls" }`

### Content
- `text`: `{ "type": "text", "value": "Hello", "tone": "default", "bold": false }`
- `rich_text`: `{ "type": "rich_text", "value": "Title", "size": "sm", "tone": "default", "bold": true, "mono": false }` (`size`: `xs`, `sm`, `base`, `lg`, `xl`)
- `selectable_text`: `{ "type": "selectable_text", "id": "...", "value": "...", "size": "sm", "tone": "default" }`
- `markdown`: `{ "type": "markdown", "id": "...", "content": "**Bold** and `code`" }`
- `icon`: `{ "type": "icon", "name": "sparkles", "size": 14, "tone": "accent" }` — supports any Lucide icon name (`sparkles`, `cpu`, `terminal`, `zap`, `flame`, `heart`, `clock`, `sliders-horizontal`, `palette`, `wrench`, `layers`, `check`, `x`, `plus`, `trash-2`, `refresh-cw`, `panel-left`, `folder`, `git-branch`, `bot`, `users`, `eye`, `search`, `globe`, `copy`, `link`, `settings`, `plug`, etc.) **or inline SVG** (`"svg:<svg ...>...</svg>"`).
- `badge`: `{ "type": "badge", "label": "LIVE", "tone": "accent" }`
- `status_dot`: `{ "type": "status_dot", "tone": "success", "size": 8 }`
- `key_value`: `{ "type": "key_value", "key": "Tokens", "value": "12.4k" }`
- `progress`: `{ "type": "progress", "value": 42, "max": 100, "label": "Build" }`
- `code`: `{ "type": "code", "value": "fn main() {}", "language": "rust" }`
- `diff_view`: `{ "type": "diff_view", "path": "src/lib.rs", "additions": 3, "deletions": 1, "lines": [...] }`
- `spinner`: `{ "type": "spinner", "label": "Working...", "size": 18 }`
- `avatar`: `{ "type": "avatar", "asset": "nacho-logo", "size": 20, "rounded": 6 }`
- `image_gallery`: `{ "type": "image_gallery", "id": "...", "images": [...], "thumb_size": 72 }`

### Controls & Inputs
- `action_button`: `{ "type": "action_button", "id": "my-btn", "label": "Run", "icon": "zap", "tooltip": "Click me", "variant": "secondary", "service": "service.my_plugin", "plugin_id": "community.my-plugin", "action_id": "click", "payload": {}, "field_ids": [] }`
  - `variant`: `primary`, `secondary`, `ghost`, `icon`, `compact_icon`, `circle_send`, `circle_stop`, `danger`
- `toggle_switch`: `{ "type": "toggle_switch", "id": "...", "label": "Enabled", "checked": true, "service": "...", "plugin_id": "...", "action_id": "...", "payload": {} }`
- `list_item`: `{ "type": "list_item", "id": "...", "icon": "sparkles", "title": "Row", "subtitle": "Sub", "badge": "New", "active": false, "service": "...", "plugin_id": "...", "action_id": "...", "payload": {} }`
- `collapsible`: `{ "type": "collapsible", "id": "...", "title": "Details", "subtitle": null, "trailing": "ok", "icon": "terminal", "tone": "default", "default_open": false, "children": [] }`
- `text_input`: `{ "type": "text_input", "field_id": "my_field", "placeholder": "Type...", "value": "", "masked": false, "service": "...", "plugin_id": "...", "submit_action": "submit" }`
- `dropdown`: `{ "type": "dropdown", "id": "...", "label": "Pick", "variant": "secondary", "placement": "bottom", "items": [...] }`

### `NodeStyle` & Enum Values
- `axis`: `"vertical"` (default) | `"horizontal"`
- `bg`: `"transparent"`, `"canvas"`, `"panel"`, `"card"`, `"hover"`, `"code"`, `"accent"`, `"accent_soft"`, `"success_soft"`, `"warning_soft"`, `"danger_soft"`, `"user_bubble"`
- `border`: `"all"`, `"top"`, `"bottom"`, `"left"`, `"right"`
- `align`: `"start"`, `"center"`, `"end"`, `"stretch"`
- `justify`: `"start"`, `"center"`, `"end"`, `"between"`
- `tone`: `"default"`, `"accent"`, `"muted"`, `"success"`, `"warning"`, `"danger"`
- `animation`: `{ "kind": "fade_in"|"slide_up"|"slide_down"|"slide_left"|"slide_right"|"scale_in"|"spin"|"pulse", "duration_ms": 180, "easing": "ease_out"|"ease_in_out"|"linear", "repeat": false }`
- Dimensions: `gap`, `width`, `max_width`, `height`, `padding_x`, `padding_y`, `radius` (px u16), `flex_grow` (u8), `center_x` (bool), `full_height` (bool).

---

## 4. Recipes

### Recipe A: Instant Live Theme Change (Zero Compile)
Call `ui_set_theme`:
```json
{
  "mode": "dark",
  "canvas": "#0B0E14",
  "panel": "#10141D",
  "surface": "#151B26",
  "accent": "#FF7A18",
  "radius": 10,
  "priority": 60
}
```

### Recipe B: Instant Declarative Widget in Header (Zero Compile)
Call `ui_register_surface`:
```json
{
  "surface_id": "live.header-pill",
  "slot": "header_extra",
  "priority": 10,
  "root": {
    "type": "box",
    "style": {
      "axis": "horizontal",
      "align": "center",
      "gap": 6,
      "padding_x": 10,
      "padding_y": 4,
      "radius": 99,
      "bg": "accent_soft"
    },
    "children": [
      { "type": "icon", "name": "flame", "size": 12, "tone": "accent" },
      { "type": "text", "value": "Focus Mode", "tone": "accent", "bold": true }
    ]
  }
}
```

### Recipe C: Interactive Stateful WASM UI Widget Plugin
1. Call `plugin_scaffold` with `template: "ui_widget"`, `name: "focus-counter"`, `slot: "sidebar_footer"`.
2. Edit the generated `src/lib.rs` (implements `render_ui` returning `UiSurface` and `ui_action` updating `service.storage` and emitting `ui.updated`).
3. Compile with `CARGO_TARGET_DIR="$HOME/.local/share/nacho/plugin-target" cargo build --manifest-path ... --target wasm32-wasip2 --release`.
4. Call `plugin_load_file` with the compiled `.wasm` path — the live widget appears immediately in Nacho's sidebar and survives restarts!

### Recipe D: Live-Editing a Built-In UI Panel
1. Edit e.g. `plugins/ui-sidebar` or `plugins/ui-conversation` in the Nacho repo.
2. Build its WASM component: `cargo build --manifest-path plugins/ui-sidebar/Cargo.toml --target wasm32-wasip2 --release`.
3. Call `plugin_load_file` with `plugins/ui-sidebar/target/wasm32-wasip2/release/nacho_ui_sidebar.wasm`.
4. The kernel hot-swaps `nacho.ui.sidebar` live without restarting Nacho! Call `plugin_revert` with `plugin_id: "nacho.ui.sidebar"` anytime to revert to the bundled build.
