Skip to main content

cranpose_foundation/modifier/
node_element.rs

1// The typed modifier element: what makes and updates a node of a chain.
2
3use std::{any::type_name, fmt, hash::Hash};
4
5use cranpose_core::ProvidedValue;
6
7use super::{InvalidationKind, ModifierNode, NodeCapabilities};
8
9/// Strongly typed modifier elements that can create and update nodes while
10/// exposing equality/hash/inspector contracts that mirror Jetpack Compose.
11pub trait ModifierNodeElement: fmt::Debug + Hash + PartialEq + Sized + 'static {
12    /// The type of node that the element creates and updates.
13    type Node: ModifierNode;
14
15    /// Makes the element again from a node it created or last updated, for
16    /// an element whose node keeps every value of it and that asks for no
17    /// update when equal. A chain keeps no such element beside its node: it
18    /// compares a new element with the node. `None`, the default, keeps the
19    /// element.
20    const FROM_NODE: Option<fn(&Self::Node) -> Self> = None;
21
22    /// Whether `node` keeps an element equal to this one, for an element
23    /// with [`Self::FROM_NODE`]. An element whose node holds values a copy
24    /// would count references of compares them in place.
25    fn equals_node(&self, node: &Self::Node) -> bool {
26        Self::FROM_NODE.is_some_and(|from_node| from_node(node) == *self)
27    }
28
29    /// Creates a new modifier node instance for this element.
30    fn create(&self) -> Self::Node;
31
32    /// Brings an existing modifier node up to date with the element's data.
33    fn update(&self, node: &mut Self::Node);
34
35    /// Optional key used to disambiguate multiple instances of the same element type.
36    fn key(&self) -> Option<u64> {
37        None
38    }
39
40    /// Human readable name surfaced to inspector tooling.
41    fn inspector_name(&self) -> &'static str {
42        type_name::<Self>()
43    }
44
45    /// Records inspector properties for tooling.
46    fn inspector_properties(&self, _inspector: &mut dyn FnMut(&'static str, String)) {}
47
48    /// Returns the capabilities of nodes created by this element.
49    /// Override this to indicate which specialized traits the node implements.
50    fn capabilities(&self) -> NodeCapabilities {
51        NodeCapabilities::default()
52    }
53
54    /// Whether this element requires `update` to be called even if `eq` returns true.
55    ///
56    /// This is useful for elements that ignore certain fields in `eq` (e.g. closures)
57    /// to allow node reuse, but still need those fields updated in the existing node.
58    /// Defaults to `false`.
59    fn always_update(&self) -> bool {
60        false
61    }
62
63    /// Whether modifier reconciliation should request capability-wide invalidations
64    /// after updating an existing node.
65    fn auto_invalidate_on_update(&self) -> bool {
66        true
67    }
68
69    /// Optional targeted invalidation requested after updating an existing node.
70    ///
71    /// This is for nodes whose attach/remove capability is broader than the
72    /// work needed for a value-only update. For example, an offset node
73    /// participates in layout on attach but an x/y change only needs placement
74    /// data and draw output refreshed.
75    fn update_invalidation_kind(&self) -> Option<InvalidationKind> {
76        None
77    }
78
79    /// Composition locals this element hands to the content composed inside
80    /// the node it decorates.
81    ///
82    /// A modifier that stands for something its subtree lives in, the way a
83    /// window does, passes that thing down here instead of asking every
84    /// composable in between to carry it as an argument.
85    fn provided_composition_locals(&self) -> Vec<ProvidedValue> {
86        Vec::new()
87    }
88}