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
//! A widget property that is fixed, read when needed, or observed, as the
//! `p`, `p_when`, and `p_from` builders set it.
use std::sync::Arc;
use sui_reactive::Observable;
pub(crate) struct Binding<T> {
value: T,
reader: Option<Box<dyn Fn() -> T>>,
source: Option<Arc<dyn Observable<T>>>,
}
impl<T: Clone + 'static> Binding<T> {
pub(crate) fn new(value: T) -> Self {
Self {
value,
reader: None,
source: None,
}
}
/// Fix the value, dropping any reader or observable.
pub(crate) fn set(&mut self, value: T) {
self.value = value;
self.reader = None;
self.source = None;
}
/// Read the value from `reader` whenever it is needed.
pub(crate) fn set_when(&mut self, reader: impl Fn() -> T + 'static) {
self.reader = Some(Box::new(reader));
self.source = None;
}
/// Follow `source`, which the widget subscribes to through [`Self::observe`].
pub(crate) fn set_from(&mut self, source: impl Observable<T> + 'static) {
self.source = Some(Arc::new(source));
self.reader = None;
}
pub(crate) fn get(&self) -> T {
if let Some(source) = &self.source {
return source.get();
}
self.reader
.as_ref()
.map_or_else(|| self.value.clone(), |reader| reader())
}
/// Subscribe through `observe`, such as a context's `observe`, when the
/// value is observed, so the widget repaints when it changes.
pub(crate) fn observe(&self, observe: impl FnOnce(&dyn Observable<T>)) {
if let Some(source) = &self.source {
observe(source.as_ref());
}
}
/// Whether the value can change without the widget being told, which
/// rules out reusing the widget's last output.
pub(crate) fn is_live(&self) -> bool {
self.reader.is_some() || self.source.is_some()
}
/// The current value when it is read or observed, and `None` when it is
/// fixed, for widgets that keep the applied value in their own state.
pub(crate) fn live(&self) -> Option<T> {
self.is_live().then(|| self.get())
}
}