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
//! Values a view shows: fixed, or computed from signals and kept current.
//!
//! Wherever a view or modifier takes `impl IntoValue<T>`, it takes either a
//! plain value or a closure that reads signals:
//!
//! ```
//! use ferrokit::*;
//!
//! let done = Signal::new(false);
//! let fixed = text("Packing list").color(Color::Secondary);
//! let live = text(move || if done.get() { "All packed".to_string() } else { "Still packing".to_string() })
//! .color(move || if done.get() { Color::Green } else { Color::Primary });
//! # let _ = (fixed, live);
//! ```
//!
//! A closure runs again whenever a signal it read changes, and the view
//! updates in place, as SwiftUI's views follow their state.
use std::rc::Rc;
/// A value a view shows: fixed, or computed from signals (see the module
/// docs). Made by [`IntoValue`].
pub enum Value<T> {
/// The same value always.
Fixed(T),
/// Computed again when the signals it reads change.
Computed(Rc<dyn Fn() -> T>),
}
impl<T: Clone> Clone for Value<T> {
fn clone(&self) -> Self {
match self {
Value::Fixed(v) => Value::Fixed(v.clone()),
Value::Computed(f) => Value::Computed(f.clone()),
}
}
}
impl<T: Clone> Value<T> {
/// The value now (reading its signals, inside an effect, subscribes it).
pub fn get(&self) -> T {
match self {
Value::Fixed(v) => v.clone(),
Value::Computed(f) => f(),
}
}
/// Whether it never changes.
pub fn is_fixed(&self) -> bool {
matches!(self, Value::Fixed(_))
}
}
/// What a view or modifier taking a value accepts: the value itself, or a
/// closure computing it from signals.
pub trait IntoValue<T> {
/// The [`Value`].
fn into_value(self) -> Value<T>;
}
impl<T, F: Fn() -> T + 'static> IntoValue<T> for F {
fn into_value(self) -> Value<T> {
Value::Computed(Rc::new(self))
}
}
macro_rules! fixed {
($($t:ty),*) => {$(
impl IntoValue<$t> for $t {
fn into_value(self) -> Value<$t> {
Value::Fixed(self)
}
}
)*};
}
fixed!(crate::Color, f64, bool, String, Option<f64>);
impl IntoValue<String> for &str {
fn into_value(self) -> Value<String> {
Value::Fixed(self.to_string())
}
}
impl IntoValue<String> for &String {
fn into_value(self) -> Value<String> {
Value::Fixed(self.clone())
}
}