repose_core/modifier_node.rs
1//! Persistent modifier nodes: state attached to a modifier that survives
2//! recomposition.
3//!
4//! A [`ModifierNodeElement`] describes a node; [`Modifier::node`] attaches it.
5//! During reconciliation the tree keeps the previous node when the element type
6//! and key match, and calls [`ModifierNodeElement::update`] on it instead of
7//! building a new one. Node state therefore survives the modifier being rebuilt
8//! every frame, which is what gestures, timers, and scroll physics need.
9//!
10//! [`Modifier::node`]: crate::modifier::Modifier::node
11
12use std::any::Any;
13use std::cell::{Cell, RefCell};
14use std::rc::Rc;
15
16/// A live modifier node.
17///
18/// Implementors keep their own state behind interior mutability. Lifecycle
19/// callbacks run once: `on_attach` the first time the node is painted,
20/// `on_detach` when the last handle referencing it is dropped.
21pub trait ModifierNode: Any {
22 fn on_attach(&self) {}
23 fn on_detach(&self) {}
24 /// Upcast hook used to downcast to the concrete node type.
25 fn as_any(&self) -> &dyn Any;
26}
27
28/// A description of one modifier node.
29///
30/// `create` builds the node when there is nothing to reuse. `update` copies the
31/// element's current configuration into an existing node without replacing its
32/// state, which is what makes a node persistent.
33pub trait ModifierNodeElement: Any {
34 fn create(&self) -> Rc<dyn ModifierNode>;
35 fn update(&self, node: &Rc<dyn ModifierNode>);
36 /// Extra identity beyond the element type. Two elements of the same type
37 /// with different keys never share a node.
38 fn key(&self) -> Option<u64> {
39 None
40 }
41}
42
43/// One element and its node, stored on a [`Modifier`](crate::Modifier).
44///
45/// The node is created on first use, so a modifier that never reaches a tree
46/// pays nothing for its nodes.
47pub struct ModifierNodeHandle {
48 element: Rc<dyn ModifierNodeElement>,
49 node: RefCell<Option<Rc<dyn ModifierNode>>>,
50 attached: Rc<Cell<bool>>,
51}
52
53impl ModifierNodeHandle {
54 pub fn new(element: Rc<dyn ModifierNodeElement>) -> Self {
55 Self {
56 element,
57 node: RefCell::new(None),
58 attached: Rc::new(Cell::new(false)),
59 }
60 }
61
62 /// The live node, created on first access.
63 pub fn node(&self) -> Rc<dyn ModifierNode> {
64 if let Some(node) = self.node.borrow().as_ref() {
65 return node.clone();
66 }
67 let node = self.element.create();
68 *self.node.borrow_mut() = Some(node.clone());
69 node
70 }
71
72 pub fn element(&self) -> &Rc<dyn ModifierNodeElement> {
73 &self.element
74 }
75
76 /// Whether this handle would reuse `previous`'s node.
77 pub fn same_identity(&self, previous: &Self) -> bool {
78 self.element.type_id() == previous.element.type_id()
79 && self.element.key() == previous.element.key()
80 }
81
82 /// Reuse `previous`'s node and push this element's configuration into it.
83 pub(crate) fn adopt(&mut self, previous: &Self) {
84 if !self.same_identity(previous) {
85 return;
86 }
87 let node = previous.node();
88 *self.node.borrow_mut() = Some(node.clone());
89 self.attached = previous.attached.clone();
90 self.element.update(&node);
91 }
92
93 pub(crate) fn ensure_attached(&self) {
94 if !self.attached.replace(true) {
95 self.node().on_attach();
96 }
97 }
98
99 #[doc(hidden)]
100 pub fn is_attached(&self) -> bool {
101 self.attached.get()
102 }
103}
104
105impl Clone for ModifierNodeHandle {
106 fn clone(&self) -> Self {
107 Self {
108 element: self.element.clone(),
109 node: RefCell::new(self.node.borrow().clone()),
110 attached: self.attached.clone(),
111 }
112 }
113}
114
115impl Drop for ModifierNodeHandle {
116 fn drop(&mut self) {
117 if !self.attached.get() {
118 return;
119 }
120 // `attached` is shared by every handle that adopted the same node, so
121 // the last reference is the one leaving the modifier tree. Node clones
122 // held by a frame's hit regions must not keep it attached.
123 if Rc::strong_count(&self.attached) != 1 {
124 return;
125 }
126 self.attached.set(false);
127 if let Some(node) = self.node.borrow().as_ref() {
128 node.on_detach();
129 }
130 }
131}