Skip to main content

cranpose_core/
callbacks.rs

1use crate::composer_context;
2use crate::{Composer, ComposerCore, RecomposeScope};
3use std::cell::RefCell;
4use std::rc::Rc;
5
6pub struct ParamState<T> {
7    pub(crate) value: Option<T>,
8}
9
10impl<T> ParamState<T> {
11    pub fn update(&mut self, new_value: &T) -> bool
12    where
13        T: PartialEq + Clone,
14    {
15        match self.value.as_mut() {
16            Some(old) if old == new_value => false,
17            Some(old) => {
18                old.clone_from(new_value);
19                true
20            }
21            None => {
22                self.value = Some(new_value.clone());
23                true
24            }
25        }
26    }
27
28    pub fn value(&self) -> Option<T>
29    where
30        T: Clone,
31    {
32        self.value.clone()
33    }
34}
35
36/// ParamSlot holds function/closure parameters by ownership (no PartialEq/Clone required).
37/// Used by the #[composable] macro to store Fn-like parameters in the slot table.
38pub struct ParamSlot<T> {
39    val: RefCell<Option<T>>,
40}
41
42impl<T> Default for ParamSlot<T> {
43    fn default() -> Self {
44        Self {
45            val: RefCell::new(None),
46        }
47    }
48}
49
50impl<T> ParamSlot<T> {
51    pub fn set(&self, v: T) {
52        *self.val.borrow_mut() = Some(v);
53    }
54
55    /// Takes the value out temporarily for a recomposition callback.
56    pub fn take(&self) -> Option<T> {
57        self.val.borrow_mut().take()
58    }
59}
60
61type CallbackCell = Rc<RefCell<Option<Box<dyn FnMut()>>>>;
62type CallbackScopeCell = Rc<RefCell<Option<RecomposeScope>>>;
63
64struct CallbackScopeGuard {
65    core: Rc<ComposerCore>,
66}
67
68impl CallbackScopeGuard {
69    fn push(composer: &Composer, scope: RecomposeScope) -> Self {
70        composer.core.scope_stack.borrow_mut().push(scope);
71        Self {
72            core: composer.clone_core(),
73        }
74    }
75}
76
77impl Drop for CallbackScopeGuard {
78    fn drop(&mut self) {
79        self.core.scope_stack.borrow_mut().pop();
80    }
81}
82
83fn with_callback_scope<R>(scope: &CallbackScopeCell, f: impl FnOnce() -> R) -> R {
84    let captured_scope = scope.borrow().clone();
85    if let Some(saved_scope) = captured_scope {
86        if let Some(composer) = composer_context::current_composer() {
87            let _scope_guard = CallbackScopeGuard::push(&composer, saved_scope);
88            return f();
89        }
90    }
91
92    f()
93}
94
95fn callback_owner_scope(composer: &Composer) -> Option<RecomposeScope> {
96    composer.core.scope_stack.borrow().last().cloned()
97}
98
99#[derive(Clone)]
100pub struct CallbackHolder {
101    rc: CallbackCell,
102    creator_scope: CallbackScopeCell,
103}
104
105impl CallbackHolder {
106    /// Create a new holder with a no-op callback so that callers can immediately invoke it.
107    pub fn new() -> Self {
108        Self::default()
109    }
110
111    /// Replace the stored callback with a new closure provided by the caller.
112    pub fn update<F>(&self, f: F)
113    where
114        F: FnMut() + 'static,
115    {
116        *self.rc.borrow_mut() = Some(Box::new(f));
117        *self.creator_scope.borrow_mut() =
118            composer_context::try_with_composer(callback_owner_scope).flatten();
119    }
120
121    /// Produce a forwarder closure that keeps the holder alive and forwards calls to it.
122    pub fn clone_rc(&self) -> impl Fn() + 'static {
123        let rc = self.rc.clone();
124        let creator_scope = self.creator_scope.clone();
125        move || {
126            with_callback_scope(&creator_scope, || {
127                if let Some(callback) = rc.borrow_mut().as_mut() {
128                    callback();
129                }
130            });
131        }
132    }
133}
134
135impl Default for CallbackHolder {
136    fn default() -> Self {
137        Self {
138            rc: Rc::new(RefCell::new(None)),
139            creator_scope: Rc::new(RefCell::new(None)),
140        }
141    }
142}
143
144/// CallbackHolder1 keeps the latest single-argument callback closure alive across recompositions.
145/// It mirrors [`CallbackHolder`] but supports callbacks that receive one argument.
146#[derive(Clone)]
147pub struct CallbackHolder1<A: 'static> {
148    #[allow(clippy::type_complexity)]
149    rc: Rc<RefCell<Option<Box<dyn FnMut(A)>>>>,
150    creator_scope: CallbackScopeCell,
151}
152
153impl<A: 'static> CallbackHolder1<A> {
154    /// Create a new holder with a no-op callback so callers can invoke it immediately.
155    pub fn new() -> Self {
156        Self::default()
157    }
158
159    /// Replace the stored callback with a new closure provided by the caller.
160    pub fn update<F>(&self, f: F)
161    where
162        F: FnMut(A) + 'static,
163    {
164        *self.rc.borrow_mut() = Some(Box::new(f));
165        *self.creator_scope.borrow_mut() =
166            composer_context::try_with_composer(callback_owner_scope).flatten();
167    }
168
169    /// Produce a forwarder closure that keeps the holder alive and forwards calls to it.
170    pub fn clone_rc(&self) -> impl Fn(A) + 'static {
171        let rc = self.rc.clone();
172        let creator_scope = self.creator_scope.clone();
173        move |arg| {
174            with_callback_scope(&creator_scope, || {
175                if let Some(callback) = rc.borrow_mut().as_mut() {
176                    callback(arg);
177                }
178            });
179        }
180    }
181}
182
183impl<A: 'static> Default for CallbackHolder1<A> {
184    fn default() -> Self {
185        Self {
186            rc: Rc::new(RefCell::new(None)),
187            creator_scope: Rc::new(RefCell::new(None)),
188        }
189    }
190}
191
192pub struct ReturnSlot<T> {
193    value: Option<T>,
194}
195
196impl<T: Clone> ReturnSlot<T> {
197    pub fn store(&mut self, value: T) {
198        self.value = Some(value);
199    }
200
201    pub fn get(&self) -> Option<T> {
202        self.value.clone()
203    }
204}
205
206impl<T> Default for ParamState<T> {
207    fn default() -> Self {
208        Self { value: None }
209    }
210}
211
212impl<T> Default for ReturnSlot<T> {
213    fn default() -> Self {
214        Self { value: None }
215    }
216}
217
218#[cfg(test)]
219mod callback_holder_tests {
220    use super::{CallbackHolder, CallbackHolder1, ParamSlot};
221    use std::cell::Cell;
222    use std::rc::Rc;
223
224    #[test]
225    fn param_slot_take_reports_absence_instead_of_panicking() {
226        let slot = ParamSlot::default();
227
228        assert_eq!(slot.take(), None);
229        slot.set(11);
230        assert_eq!(slot.take(), Some(11));
231        assert_eq!(slot.take(), None);
232    }
233
234    #[test]
235    fn callback_holder_default_forwarder_is_noop() {
236        let forwarder = CallbackHolder::new().clone_rc();
237        forwarder();
238    }
239
240    #[test]
241    fn callback_holder_forwarder_uses_latest_callback() {
242        let holder = CallbackHolder::new();
243        let total = Rc::new(Cell::new(0));
244        let forwarder = holder.clone_rc();
245
246        let first_total = Rc::clone(&total);
247        holder.update(move || first_total.set(first_total.get() + 1));
248        forwarder();
249
250        let second_total = Rc::clone(&total);
251        holder.update(move || second_total.set(second_total.get() + 10));
252        forwarder();
253
254        assert_eq!(total.get(), 11);
255    }
256
257    #[test]
258    fn callback_holder1_default_forwarder_is_noop() {
259        let forwarder = CallbackHolder1::<i32>::new().clone_rc();
260        forwarder(7);
261    }
262
263    #[test]
264    fn callback_holder1_forwarder_uses_latest_callback() {
265        let holder = CallbackHolder1::<i32>::new();
266        let total = Rc::new(Cell::new(0));
267        let forwarder = holder.clone_rc();
268
269        let first_total = Rc::clone(&total);
270        holder.update(move |value| first_total.set(first_total.get() + value));
271        forwarder(2);
272
273        let second_total = Rc::clone(&total);
274        holder.update(move |value| second_total.set(second_total.get() + value * 5));
275        forwarder(3);
276
277        assert_eq!(total.get(), 17);
278    }
279}