fui_core 0.19.0

Core library of FUI MVVM UI Framework
Documentation
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use futures_signals::signal::{Mutable, MutableLockMut, MutableLockRef, SignalExt};
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use std::sync::{Arc, RwLock};

use crate::ObservableCollection;
use crate::{spawn_local, Subscription, VecDiff};
use fui_drawing::Color;

/// Type-erased subscription that can subscribe to any Property<V> and notify
/// a target property when the source changes.
pub struct PropertySubscription {
    subscribe: Rc<dyn Fn(Rc<dyn Fn()>) -> Subscription>,
}

impl PropertySubscription {
    /// Creates a new PropertySubscription from a source Property.
    pub fn from_property<V>(property: &Property<V>) -> Self
    where
        V: 'static + Clone + PartialEq,
    {
        let property = property.clone();
        PropertySubscription {
            subscribe: Rc::new(move |notify_fn| {
                property.on_changed(move |_| {
                    notify_fn();
                })
            }),
        }
    }

    /// Subscribes to the source property and calls the notify function when it changes.
    pub fn subscribe(&self, notify_fn: Rc<dyn Fn()>) -> Subscription {
        (self.subscribe)(notify_fn)
    }
}

#[repr(transparent)]
pub struct PropertyReadGuard<'a, T> {
    pub(crate) inner: MutableLockRef<'a, T>,
}

impl<'a, T> Deref for PropertyReadGuard<'a, T> {
    type Target = T;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

#[repr(transparent)]
pub struct PropertyWriteGuard<'a, T> {
    pub(crate) inner: MutableLockMut<'a, T>,
}

impl<'a, T> Deref for PropertyWriteGuard<'a, T> {
    type Target = T;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<'a, T> DerefMut for PropertyWriteGuard<'a, T> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

pub struct Property<T> {
    data: Mutable<T>,
    bind_handles: Arc<RwLock<Vec<Subscription>>>,
}

impl<T: 'static + Clone + PartialEq> Property<T> {
    pub fn new<U: Into<T>>(val: U) -> Self {
        Property {
            data: Mutable::new(val.into()),
            bind_handles: Arc::new(RwLock::new(Vec::new())),
        }
    }

    pub fn binded_from(src_property: &Property<T>) -> Self {
        let new_property = Property {
            data: Mutable::new(src_property.get()),
            bind_handles: Arc::new(RwLock::new(Vec::new())),
        };
        new_property.bind(src_property);
        new_property
    }

    pub fn binded_c_from<TSrc: 'static + Clone + PartialEq, F: 'static + Fn(TSrc) -> T>(
        src_property: &Property<TSrc>,
        f: F,
    ) -> Self {
        let new_property = Property::new(f(src_property.get()));
        new_property.bind_c(src_property, f);
        new_property
    }

    pub fn binded_to(dst_property: &Property<T>, init_value: T) -> Self {
        let property = Property::new(init_value);
        dst_property.bind(&property);
        property
    }

    pub fn binded_c_to<TDst: 'static + Clone + PartialEq, F: 'static + Fn(T) -> TDst>(
        dst_property: &Property<TDst>,
        f: F,
        init_value: T,
    ) -> Self {
        let property = Property::new(init_value);
        dst_property.bind_c(&property, f);
        property
    }

    pub fn binded_two_way(other_property: &Property<T>) -> Self {
        other_property.clone()
    }

    pub fn binded_c_two_way<TOther, F1, F2>(
        other_property: &Property<TOther>,
        f1: F1,
        f2: F2,
    ) -> Self
    where
        TOther: 'static + Clone + PartialEq,
        F1: 'static + Fn(TOther) -> T,
        F2: 'static + Fn(T) -> TOther,
    {
        let property = Property::binded_c_from(other_property, f1);
        other_property.bind_c(&property, f2);
        property
    }

    pub fn set(&self, val: T) {
        self.data.set_neq(val);
    }

    pub fn change<F: 'static + Fn(T) -> T>(&self, f: F) {
        let val = self.data.get_cloned();
        self.data.set_neq(f(val));
    }

    pub fn get(&self) -> T {
        self.data.get_cloned()
    }

    // &T like access
    pub fn read(&self) -> PropertyReadGuard<'_, T> {
        PropertyReadGuard {
            inner: self.data.lock_ref(),
        }
    }

    // &mut T like access
    pub fn write(&self) -> PropertyWriteGuard<'_, T> {
        PropertyWriteGuard {
            inner: self.data.lock_mut(),
        }
    }

    pub fn bind(&self, src_property: &Property<T>) {
        let handle = spawn_local(src_property.data.signal_cloned().for_each({
            let data = self.data.clone();
            move |v| {
                data.set_neq(v);
                async {}
            }
        }));
        self.bind_handles
            .write()
            .unwrap()
            .push(Subscription::SpawnLocal(handle));
    }

    pub fn bind_c<TSrc: 'static + Clone + PartialEq, F: 'static + Fn(TSrc) -> T>(
        &self,
        src_property: &Property<TSrc>,
        f: F,
    ) {
        let handle = spawn_local(src_property.data.signal_cloned().for_each({
            let data = self.data.clone();
            move |v| {
                data.set_neq(f(v));
                async {}
            }
        }));
        self.bind_handles
            .write()
            .unwrap()
            .push(Subscription::SpawnLocal(handle));
    }

    /// Creates a new Property with value computed from an expression
    /// that depends on multiple source properties.
    ///
    /// The resulting property automatically tracks changes to all source properties
    /// and recomputes its value when any of them changes.
    ///
    /// # Arguments
    ///
    /// * `expr_fn` - A closure that computes the value of the resulting property.
    ///               This closure must be `'static` and typically requires `move` semantics
    ///               with cloned source properties.
    /// * `subscriptions` - A vector of `PropertySubscription` objects created from
    ///                     source properties using `PropertySubscription::from_property()`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use fui_core::{Property, PropertySubscription};
    ///
    /// let first_name = Property::new("John".to_string());
    /// let last_name = Property::new("Doe".to_string());
    ///
    /// let full_name = Property::<String>::bind_from_expr(
    ///     {
    ///         let first_name = first_name.clone();
    ///         let last_name = last_name.clone();
    ///         move || format!("{} {}", first_name.get(), last_name.get())
    ///     },
    ///     vec![
    ///         PropertySubscription::from_property(&first_name),
    ///         PropertySubscription::from_property(&last_name),
    ///     ]
    /// );
    ///
    /// assert_eq!(full_name.get(), "John Doe");
    ///
    /// first_name.set("Jane".to_string());
    /// assert_eq!(full_name.get(), "Jane Doe");
    /// ```
    pub fn bind_from_expr<F, R>(expr_fn: F, subscriptions: Vec<PropertySubscription>) -> Property<R>
    where
        R: 'static + Clone + PartialEq,
        F: 'static + Fn() -> R,
    {
        let result_prop = Property::new(expr_fn());
        let f = Rc::new(expr_fn);

        // Subscribe to each source property
        for subscription in subscriptions {
            let f = f.clone();
            let target = result_prop.clone();
            let handle = subscription.subscribe(Rc::new(move || {
                target.set(f());
            }));
            result_prop.bind_handles.write().unwrap().push(handle);
        }

        result_prop
    }

    pub fn on_changed<F: 'static + FnMut(T)>(&self, mut f: F) -> Subscription {
        Subscription::SpawnLocal(spawn_local(self.data.signal_cloned().for_each(move |v| {
            f(v);
            async {}
        })))
    }

    /// Adds a subscription to the internal bind_handles collection.
    /// This is used by the ui! macro for automatic dependency tracking.
    pub fn add_bind_subscription(&self, subscription: Subscription) {
        self.bind_handles.write().unwrap().push(subscription);
    }
}

impl<T: 'static + Clone + PartialEq> Clone for Property<T> {
    fn clone(&self) -> Self {
        Property::<T> {
            data: self.data.clone(),
            bind_handles: self.bind_handles.clone(),
        }
    }

    fn clone_from(&mut self, source: &Self) {
        self.data.clone_from(&source.data);
        self.bind_handles.clone_from(&source.bind_handles);
    }
}

///
/// Used to attribute types that can be
/// automatically converted to Property<T>.
///
pub trait IntoProperty {}

impl IntoProperty for String {}
impl IntoProperty for &String {}
impl IntoProperty for &str {}
impl IntoProperty for bool {}
impl IntoProperty for char {}
impl IntoProperty for i8 {}
impl IntoProperty for i16 {}
impl IntoProperty for i32 {}
impl IntoProperty for i64 {}
impl IntoProperty for i128 {}
impl IntoProperty for isize {}
impl IntoProperty for u8 {}
impl IntoProperty for u16 {}
impl IntoProperty for u32 {}
impl IntoProperty for u64 {}
impl IntoProperty for u128 {}
impl IntoProperty for usize {}
impl IntoProperty for f32 {}
impl IntoProperty for f64 {}
impl IntoProperty for Color {}

///
/// Allows to convert types attributed with IntoProperty to Property<T>.
///
/// Cannot do it for all types at once, because then there is a conflict
/// with binding conversions (tuples and properties used as source).
///
/// Example:
///
/// ui! { Control { int_property: 10, text_property: "My Text" }}
///
impl<T, U> From<U> for Property<T>
where
    T: 'static + Clone + PartialEq,
    U: Into<T> + IntoProperty,
{
    fn from(value: U) -> Property<T> {
        Property::new(value.into())
    }
}

///
/// Allows to easily write one-way binding.
///
/// Example:
///
/// ui! { Control { text_property: &vm.text }}
///
impl<T> From<&Property<T>> for Property<T>
where
    T: 'static + Clone + PartialEq,
{
    fn from(value: &Property<T>) -> Property<T> {
        Property::binded_from(value)
    }
}

///
/// Allows to easily write two-way bindings.
///
/// Example:
///
/// ui! { Control { text_property: &mut vm.text }}
///
impl<T> From<&mut Property<T>> for Property<T>
where
    T: 'static + Clone + PartialEq,
{
    fn from(value: &mut Property<T>) -> Property<T> {
        Property::binded_two_way(value)
    }
}

///
/// ObservableCollection for Property.
///
impl<T> ObservableCollection<T> for Property<T>
where
    T: 'static + Clone + PartialEq,
{
    fn len(&self) -> usize {
        1
    }

    fn get(&self, index: usize) -> Option<T> {
        if index == 0 {
            Some(Property::get(self))
        } else {
            None
        }
    }

    fn on_changed(&self, mut f: Box<dyn FnMut(VecDiff<T>)>) -> Option<Subscription> {
        let mut old_value = Some(self.get());
        Some(Property::on_changed(self, move |v| {
            if let Some(old) = old_value.take() {
                f(VecDiff::RemoveAt { index: 0, value: old });
            }
            old_value = Some(v.clone());
            f(VecDiff::InsertAt { index: 0, value: v });
        }))
    }
}

impl<T> ObservableCollection<T> for Property<Option<T>>
where
    T: 'static + Clone + PartialEq,
{
    fn len(&self) -> usize {
        if self.get().is_some() {
            1
        } else {
            0
        }
    }

    fn get(&self, index: usize) -> Option<T> {
        if index == 0 {
            Property::get(&self)
        } else {
            None
        }
    }

    fn on_changed(&self, mut f: Box<dyn FnMut(VecDiff<T>)>) -> Option<Subscription> {
        let mut old_value = self.get();
        Some(Property::on_changed(self, move |v| {
            f(VecDiff::Clear {
                values: old_value.take().map(|v| vec![v]).unwrap_or_default(),
            });
            old_value = v.clone();
            if let Some(v) = v {
                f(VecDiff::InsertAt { index: 0, value: v });
            }
        }))
    }
}