Skip to main content

adam_web_ui/inspector/
mod.rs

1//! Inspects accepted model publications without owning a mutable parsed sheet.
2//!
3//! Initialize a [`SheetHandle`] with [`crate::binding::use_sheet`] and pass it
4//! to [`SheetInspector`]. The host receives every edit outcome and chooses recovery.
5//!
6//! [`InspectorPresentation`] selects the surrounding chrome: [`InspectorPresentation::Panel`]
7//! keeps the 260px side panel with a "Cells" header for the `begin` application,
8//! while [`InspectorPresentation::Card`] drops the header and centers the rows in a
9//! card up to 24rem wide for the book and slides. In both, the rows share one flex
10//! column with a 1.5rem gap. The layout classes (`panel`, `card`, `cells`) live in
11//! `style.css` and are compiled into scoped `adam_inspector_<local>` names by the
12//! crate's build script.
13use crate::components::Label;
14use crate::{
15    binding::*,
16    components::{Card, CardContent, Separator, Theme},
17};
18use dioxus::prelude::*;
19use std::rc::Rc;
20
21include!(concat!(env!("OUT_DIR"), "/inspector-styles.rs"));
22
23#[cfg(test)]
24mod tests;
25
26/// Selects the chrome wrapping the inspector's cell rows.
27///
28/// [`Panel`](InspectorPresentation::Panel) keeps the fixed-width side panel with a
29/// "Cells" header; [`Card`](InspectorPresentation::Card) centers the rows in a
30/// headerless card. The default is [`Panel`](InspectorPresentation::Panel).
31///
32/// # Examples
33/// ```
34/// use adam_web_ui::InspectorPresentation;
35/// assert_eq!(InspectorPresentation::default(), InspectorPresentation::Panel);
36/// ```
37#[derive(Clone, Copy, PartialEq, Debug, Default)]
38pub enum InspectorPresentation {
39    /// Fixed-width side panel with a "Cells" header, used by the `begin` application.
40    #[default]
41    Panel,
42    /// Headerless card centered up to 24rem wide, used by the book and slides.
43    Card,
44}
45
46/// Renders named accepted cells with bound controls and visible capability diagnostics.
47///
48/// Numeric values retain their exact primitive codecs; derived cells remain
49/// editable. Output, relevance, forced and invalid states come from publication,
50/// not a second mutable model. Failed-model inspection is never attempted. The
51/// `presentation` prop chooses the surrounding chrome; see [`InspectorPresentation`].
52///
53/// - Complexity: O(named cells + displayed bytes) plus bound-control lookups.
54///
55/// # Examples
56/// ```
57/// use adam_web_ui::{SheetInspector, InspectorPresentation, binding::*, build_sheet};
58/// use dioxus::prelude::*;
59/// use std::rc::Rc;
60/// fn app() -> Element {
61///     let factory: ModelFactory = Rc::new(||
62///         build_sheet("sheet s { cell count: i32 = 1; }", "s.adm2"));
63///     match use_sheet(factory, "s/v1".into(), EventHandler::new(|_| {})) {
64///         Ok(sheet) => rsx! {
65///             SheetInspector { sheet, presentation: InspectorPresentation::Card }
66///         },
67///         Err(error) => rsx! { pre { "{error:?}" } },
68///     }
69/// }
70/// ```
71#[component]
72pub fn SheetInspector(
73    sheet: SheetHandle,
74    #[props(default)] presentation: InspectorPresentation,
75) -> Element {
76    let views = match sheet.views() {
77        Ok(views) => views,
78        Err(error) => return rsx! { div { role: "alert", "{error}" } },
79    };
80    let instance = sheet.instance();
81    let rows = rsx! {
82        div { class: Styles::cells,
83            for view in views {
84                div { key: "{instance}:{view.key}", class: "adam-cell-row",
85                    "data-source": view.is_source.to_string(),
86                    "data-relevant": view.flags.relevant.to_string(),
87                    "data-forced": view.flags.forced.to_string(),
88                    if !view.codec_supported {
89                        DisplayOnly { id: "adam-display-{instance}:{view.key}", view: view.clone() }
90                    } else if view.is_bool {
91                        CheckBox { bind: sheet.cell(&view.key), label: view.key.clone() }
92                    } else if view.number_kind.is_some() {
93                        if view.range.is_some() {
94                            EditSlider { bind: sheet.cell(&view.key), label: view.key.clone() }
95                        } else {
96                            EditNumber { bind: sheet.cell(&view.key), label: view.key.clone() }
97                        }
98                    } else {
99                        EditText { bind: sheet.cell(&view.key), label: view.key.clone() }
100                    }
101                    for issue in &view.issues {
102                        p { role: "alert", "{issue.message}" }
103                    }
104                }
105            }
106        }
107    };
108    rsx! { Theme {
109        match presentation {
110            InspectorPresentation::Panel => rsx! {
111                div { class: "{Styles::panel} adam-sheet-inspector",
112                    h2 { "Cells" }
113                    Separator {}
114                    {rows}
115                }
116            },
117            InspectorPresentation::Card => rsx! {
118                Card { class: "{Styles::card} adam-sheet-inspector",
119                    CardContent { {rows} }
120                }
121            },
122        }
123    } }
124}
125
126/// Renders a cell without an editing codec as a labeled, read-only value.
127///
128/// Explicit outputs show only the value; other cells add a "Display only" note
129/// because a user might otherwise expect to edit them.
130///
131/// - Complexity: O(label + value bytes).
132#[component]
133fn DisplayOnly(id: String, view: Rc<CellView>) -> Element {
134    rsx! {
135        div { class: Styles::display,
136            Label { html_for: id.clone(), "{view.key}" }
137            output { id, class: Styles::value, "{view.text}" }
138            if !view.flags.readonly {
139                small { class: Styles::note, "Display only" }
140            }
141        }
142    }
143}