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