Skip to main content

lattice_dashboard/
section.rs

1//! The section trait and the read-only facts passed to it.
2//!
3//! A section is a pure function of [`DashboardCtx`] to a
4//! [`DashboardFragment`](crate::DashboardFragment): it performs no I/O and
5//! holds no editor state. Built-in sections are native Rust (the built-in
6//! surface stays native, like the vim grammar); plugin sections (DB.8) will
7//! satisfy the same trait through a WASM shim.
8
9use crate::fragment::DashboardFragment;
10
11/// Read-only facts a section needs to render.
12///
13/// DB.1 carries the primitives the pure sections need. Later slices grow it
14/// with the resolved-theme snapshot (DB.3/DB.4) and help/keymap lookups so
15/// the "help & bindings" section stays truthful — added when the wiring that
16/// can supply them lands, not before.
17#[derive(Debug, Clone)]
18pub struct DashboardCtx {
19    /// Pane width in cells, used by the compositor for centring (DB.4).
20    pub pane_width: usize,
21    /// Whether Nerd Font glyphs may be used (else the BMP-block fallback).
22    pub nerd_fonts: bool,
23    /// The editor version string, for the branding section.
24    pub version: String,
25}
26
27impl Default for DashboardCtx {
28    fn default() -> Self {
29        Self {
30            pane_width: 80,
31            nerd_fonts: false,
32            version: env!("CARGO_PKG_VERSION").to_string(),
33        }
34    }
35}
36
37/// A contributor of one dashboard section.
38///
39/// The registry sorts by [`order`](DashboardSection::order) (ties broken by
40/// [`id`](DashboardSection::id)) for the default layout, and looks up by
41/// `id` when the user pins an explicit order via `dashboard.sections`.
42pub trait DashboardSection: Send + Sync {
43    /// Stable identifier, used in `dashboard.sections` and (CR.4) for plugin
44    /// replace-by-id. Lowercase kebab, e.g. `"getting-started"`.
45    ///
46    /// Section ids are deliberately NOT namespaced by plugin, unlike help
47    /// topics: replacing a builtin section is a stated capability, so a
48    /// plugin registering `getting-started` is doing something supported.
49    fn id(&self) -> &str;
50
51    /// CR.2: the host-issued plugin id that contributed this section,
52    /// `None` for builtins. Provenance IS the teardown token —
53    /// [`DashboardRegistry::unregister_plugin`] removes by it.
54    ///
55    /// Defaulted, so no native section changes.
56    fn plugin_id(&self) -> Option<u64> {
57        None
58    }
59
60    /// Default sort key for the built-in layout. Lower sorts first.
61    fn order(&self) -> i32;
62
63    /// Whether this section shows when the user has not customised
64    /// `dashboard.sections`.
65    fn default_enabled(&self) -> bool {
66        true
67    }
68
69    /// Render this section's contribution.
70    fn render(&self, ctx: &DashboardCtx) -> DashboardFragment;
71}