Skip to main content

a2ui_base/model/
component_context.rs

1//! Component rendering context — passed to component implementations during build.
2
3use std::collections::HashMap;
4
5use super::components_model::SurfaceComponentsModel;
6use super::data_context::DataContext;
7use super::data_model::DataModel;
8use crate::catalog::function_api::FunctionImplementation;
9
10/// Transient context created for each component during rendering.
11///
12/// The caller is responsible for holding the RefCell borrows on DataModel
13/// and SurfaceComponentsModel for the duration of rendering.
14pub struct ComponentContext<'a> {
15    /// The component's ID.
16    pub component_id: String,
17    /// The surface ID this component belongs to.
18    pub surface_id: String,
19    /// Scoped data access for resolving dynamic values.
20    pub data_context: DataContext<'a>,
21    /// The components model (escape hatch for inspecting siblings/children).
22    pub components: &'a SurfaceComponentsModel,
23    /// The ID of the currently focused component, if any.
24    pub focused_id: Option<String>,
25    /// The index of this component within a template iteration, if applicable.
26    pub template_index: Option<usize>,
27}
28
29impl<'a> ComponentContext<'a> {
30    /// Create a component context.
31    ///
32    /// Callers should borrow `surface.data_model` and `surface.components`
33    /// before calling this and pass the references.
34    pub fn new(
35        component_id: String,
36        surface_id: String,
37        data_model: &'a DataModel,
38        components: &'a SurfaceComponentsModel,
39        functions: &'a HashMap<String, Box<dyn FunctionImplementation>>,
40        base_path: &str,
41        focused_id: Option<String>,
42    ) -> Self {
43        let data_context = if base_path.is_empty() {
44            DataContext::new(data_model, functions)
45        } else {
46            DataContext::new(data_model, functions).nested(base_path.trim_start_matches('/'))
47        };
48
49        Self {
50            component_id,
51            surface_id,
52            data_context,
53            components,
54            focused_id,
55            template_index: None,
56        }
57    }
58}