ferroui-base 0.3.0

The base library of FerroUI: the property system, layout, input, styling, media, text, animation, data binding and the compositor
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Extension members for [`FerroObject`]: observables over property values
//! and property-kind-agnostic access to typed properties.

use crate::data::core::{UntypedObservableBindingExpression, ValueTypes};
use crate::data::{BindingBase, BindingExpressionBase, BindingPriority, BindingValue};
use crate::reactive::{Disposable, IDisposable, IObservable, IObserver, ObservableExt};
use crate::{
    AttachedProperty, BoxedValue, DirectProperty, DirectPropertyBase, FerroObject, FerroProperty,
    FerroPropertyChangedEventArgs, ObjectType, PropertyValue, StyledProperty, WeakRef,
};
use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};

/// Converts an observable to a binding (`source.ToBinding()`).
///
/// The values of the observable are delivered untyped: a nullable value
/// (`Option<T>`) as null or the value it holds.
pub fn to_binding<T: PropertyValue>(source: Rc<dyn IObservable<T>>) -> Rc<dyn BindingBase> {
    Rc::new(BindingAdaptor { source: source.select(|x| ValueTypes::normalize(Rc::new(x))) })
}

/// The binding [`to_binding`] returns: its instances read the values of the
/// observable with local value priority.
struct BindingAdaptor {
    source: Rc<dyn IObservable<Option<BoxedValue>>>,
}

impl BindingBase for BindingAdaptor {
    fn create_instance(
        &self,
        _target: &FerroObject,
        _target_property: Option<&'static FerroProperty>,
        _anchor: Option<&crate::Ref<FerroObject>>,
    ) -> Rc<dyn BindingExpressionBase> {
        let expression: Rc<dyn BindingExpressionBase> =
            UntypedObservableBindingExpression::new(self.source.clone(), BindingPriority::LocalValue);
        expression
    }
}

/// A property with a statically known value type: a styled, attached or
/// direct property.
///
/// This is what lets the members of [`FerroObjectExtensions`] accept any kind
/// of typed property and route to the matching [`FerroObject`] member.
pub trait TypedProperty: 'static {
    /// The value type of the property.
    type Value: PropertyValue;

    /// The untyped base of the property.
    fn untyped(&'static self) -> &'static FerroProperty;

    #[doc(hidden)]
    fn typed_get_value(&'static self, target: &FerroObject) -> Self::Value;

    #[doc(hidden)]
    fn typed_get_base_value(&'static self, target: &FerroObject) -> Option<Self::Value>;

    #[doc(hidden)]
    fn typed_bind(
        &'static self,
        target: &FerroObject,
        source: Rc<dyn IObservable<Self::Value>>,
        priority: BindingPriority,
    ) -> Rc<dyn IDisposable>;

    #[doc(hidden)]
    fn typed_bind_value(
        &'static self,
        target: &FerroObject,
        source: Rc<dyn IObservable<BindingValue<Self::Value>>>,
        priority: BindingPriority,
    ) -> Rc<dyn IDisposable>;
}

impl<T: PropertyValue> TypedProperty for StyledProperty<T> {
    type Value = T;

    fn untyped(&'static self) -> &'static FerroProperty {
        self
    }

    fn typed_get_value(&'static self, target: &FerroObject) -> T {
        target.get_value(self)
    }

    fn typed_get_base_value(&'static self, target: &FerroObject) -> Option<T> {
        target.get_base_value(self)
    }

    fn typed_bind(
        &'static self,
        target: &FerroObject,
        source: Rc<dyn IObservable<T>>,
        priority: BindingPriority,
    ) -> Rc<dyn IDisposable> {
        target.bind(self, source, priority)
    }

    fn typed_bind_value(
        &'static self,
        target: &FerroObject,
        source: Rc<dyn IObservable<BindingValue<T>>>,
        priority: BindingPriority,
    ) -> Rc<dyn IDisposable> {
        target.bind_value(self, source, priority)
    }
}

impl<T: PropertyValue> TypedProperty for AttachedProperty<T> {
    type Value = T;

    fn untyped(&'static self) -> &'static FerroProperty {
        self
    }

    fn typed_get_value(&'static self, target: &FerroObject) -> T {
        target.get_value(self)
    }

    fn typed_get_base_value(&'static self, target: &FerroObject) -> Option<T> {
        target.get_base_value(self)
    }

    fn typed_bind(
        &'static self,
        target: &FerroObject,
        source: Rc<dyn IObservable<T>>,
        priority: BindingPriority,
    ) -> Rc<dyn IDisposable> {
        target.bind(self, source, priority)
    }

    fn typed_bind_value(
        &'static self,
        target: &FerroObject,
        source: Rc<dyn IObservable<BindingValue<T>>>,
        priority: BindingPriority,
    ) -> Rc<dyn IDisposable> {
        target.bind_value(self, source, priority)
    }
}

impl<T: PropertyValue> TypedProperty for DirectPropertyBase<T> {
    type Value = T;

    fn untyped(&'static self) -> &'static FerroProperty {
        self
    }

    fn typed_get_value(&'static self, target: &FerroObject) -> T {
        target.get_direct_value(self)
    }

    fn typed_get_base_value(&'static self, target: &FerroObject) -> Option<T> {
        Some(target.get_direct_value(self))
    }

    fn typed_bind(
        &'static self,
        target: &FerroObject,
        source: Rc<dyn IObservable<T>>,
        _priority: BindingPriority,
    ) -> Rc<dyn IDisposable> {
        target.bind_direct(self, source)
    }

    fn typed_bind_value(
        &'static self,
        target: &FerroObject,
        source: Rc<dyn IObservable<BindingValue<T>>>,
        _priority: BindingPriority,
    ) -> Rc<dyn IDisposable> {
        target.bind_direct_value(self, source)
    }
}

impl<TOwner: ObjectType, T: PropertyValue> TypedProperty for DirectProperty<TOwner, T> {
    type Value = T;

    fn untyped(&'static self) -> &'static FerroProperty {
        self
    }

    fn typed_get_value(&'static self, target: &FerroObject) -> T {
        target.get_direct_value(self)
    }

    fn typed_get_base_value(&'static self, target: &FerroObject) -> Option<T> {
        Some(target.get_direct_value(self))
    }

    fn typed_bind(
        &'static self,
        target: &FerroObject,
        source: Rc<dyn IObservable<T>>,
        _priority: BindingPriority,
    ) -> Rc<dyn IDisposable> {
        target.bind_direct(self, source)
    }

    fn typed_bind_value(
        &'static self,
        target: &FerroObject,
        source: Rc<dyn IObservable<BindingValue<T>>>,
        _priority: BindingPriority,
    ) -> Rc<dyn IDisposable> {
        target.bind_direct_value(self, source)
    }
}

/// Extension members for [`FerroObject`].
pub trait FerroObjectExtensions {
    /// Gets an observable for a property.
    ///
    /// The observable fires with the current value of the property on the
    /// object immediately upon subscription, and then every time the value
    /// changes. It holds a weak reference to the object.
    fn get_observable<P: TypedProperty>(&self, property: &'static P) -> Rc<dyn IObservable<P::Value>>;

    /// Gets an observable for a property, projecting each value with
    /// `converter`.
    fn get_observable_with<P: TypedProperty, TResult: PropertyValue>(
        &self,
        property: &'static P,
        converter: impl Fn(P::Value) -> TResult + 'static,
    ) -> Rc<dyn IObservable<TResult>>;

    /// Gets an observable of untyped values for a property.
    fn get_observable_untyped(&self, property: &'static FerroProperty) -> Rc<dyn IObservable<BoxedValue>>;

    /// Gets an observable of untyped values for a property, projecting each
    /// value with `converter`.
    fn get_observable_untyped_with<TResult: PropertyValue>(
        &self,
        property: &'static FerroProperty,
        converter: impl Fn(BoxedValue) -> TResult + 'static,
    ) -> Rc<dyn IObservable<TResult>>;

    /// Gets an observable of binding values for a property.
    fn get_binding_observable<P: TypedProperty>(
        &self,
        property: &'static P,
    ) -> Rc<dyn IObservable<BindingValue<P::Value>>>;

    /// Gets an observable of binding values for a property, projecting each
    /// value with `converter`.
    fn get_binding_observable_with<P: TypedProperty, TResult: PropertyValue>(
        &self,
        property: &'static P,
        converter: impl Fn(P::Value) -> TResult + 'static,
    ) -> Rc<dyn IObservable<BindingValue<TResult>>>;

    /// Gets an observable of untyped binding values for a property.
    fn get_binding_observable_untyped(
        &self,
        property: &'static FerroProperty,
    ) -> Rc<dyn IObservable<BindingValue<BoxedValue>>>;

    /// Gets an observable of untyped binding values for a property,
    /// projecting each value with `converter`.
    fn get_binding_observable_untyped_with<TResult: PropertyValue>(
        &self,
        property: &'static FerroProperty,
        converter: impl Fn(BoxedValue) -> TResult + 'static,
    ) -> Rc<dyn IObservable<BindingValue<TResult>>>;

    /// Gets an observable that listens for property changed events of a
    /// property on this object. It fires each time the property changes and
    /// does not fire with the current value on subscription.
    fn get_property_changed_observable(&self, property: &'static FerroProperty) -> ObjectPropertyChangedObservable;

    /// Binds a typed property of any kind to an observable. The priority is
    /// ignored for direct properties.
    fn bind_typed<P: TypedProperty>(
        &self,
        property: &'static P,
        source: Rc<dyn IObservable<P::Value>>,
        priority: BindingPriority,
    ) -> Rc<dyn IDisposable>;

    /// Binds a typed property of any kind to an observable of binding values.
    /// The priority is ignored for direct properties.
    fn bind_typed_value<P: TypedProperty>(
        &self,
        property: &'static P,
        source: Rc<dyn IObservable<BindingValue<P::Value>>>,
        priority: BindingPriority,
    ) -> Rc<dyn IDisposable>;

    /// Gets the value of a typed property of any kind.
    fn get_typed_value<P: TypedProperty>(&self, property: &'static P) -> P::Value;

    /// Gets the base value of a typed property of any kind: the value
    /// excluding animated values. For direct properties this is the value.
    fn get_typed_base_value<P: TypedProperty>(&self, property: &'static P) -> Option<P::Value>;

    /// Gets the base value of a property as an untyped value; the unset
    /// marker when there is none.
    fn get_base_value_untyped(&self, property: &'static FerroProperty) -> BoxedValue;
}

impl FerroObjectExtensions for FerroObject {
    fn get_observable<P: TypedProperty>(&self, property: &'static P) -> Rc<dyn IObservable<P::Value>> {
        PropertyObservable::create(
            self,
            property.untyped(),
            move |o| property.typed_get_value(o),
            |a, b| a == b,
            true,
        )
    }

    fn get_observable_with<P: TypedProperty, TResult: PropertyValue>(
        &self,
        property: &'static P,
        converter: impl Fn(P::Value) -> TResult + 'static,
    ) -> Rc<dyn IObservable<TResult>> {
        PropertyObservable::create(
            self,
            property.untyped(),
            move |o| converter(property.typed_get_value(o)),
            |a, b| a == b,
            true,
        )
    }

    fn get_observable_untyped(&self, property: &'static FerroProperty) -> Rc<dyn IObservable<BoxedValue>> {
        PropertyObservable::create(self, property, move |o| o.get_value_untyped(property), |a, b| a == b, true)
    }

    fn get_observable_untyped_with<TResult: PropertyValue>(
        &self,
        property: &'static FerroProperty,
        converter: impl Fn(BoxedValue) -> TResult + 'static,
    ) -> Rc<dyn IObservable<TResult>> {
        PropertyObservable::create(
            self,
            property,
            move |o| converter(o.get_value_untyped(property)),
            |a, b| a == b,
            true,
        )
    }

    fn get_binding_observable<P: TypedProperty>(
        &self,
        property: &'static P,
    ) -> Rc<dyn IObservable<BindingValue<P::Value>>> {
        PropertyObservable::create(
            self,
            property.untyped(),
            move |o| BindingValue::new(property.typed_get_value(o)),
            |a, b| a == b,
            false,
        )
    }

    fn get_binding_observable_with<P: TypedProperty, TResult: PropertyValue>(
        &self,
        property: &'static P,
        converter: impl Fn(P::Value) -> TResult + 'static,
    ) -> Rc<dyn IObservable<BindingValue<TResult>>> {
        PropertyObservable::create(
            self,
            property.untyped(),
            move |o| BindingValue::new(converter(property.typed_get_value(o))),
            |a, b| a == b,
            false,
        )
    }

    fn get_binding_observable_untyped(
        &self,
        property: &'static FerroProperty,
    ) -> Rc<dyn IObservable<BindingValue<BoxedValue>>> {
        PropertyObservable::create(
            self,
            property,
            move |o| BindingValue::new(o.get_value_untyped(property)),
            |a, b| a == b,
            false,
        )
    }

    fn get_binding_observable_untyped_with<TResult: PropertyValue>(
        &self,
        property: &'static FerroProperty,
        converter: impl Fn(BoxedValue) -> TResult + 'static,
    ) -> Rc<dyn IObservable<BindingValue<TResult>>> {
        PropertyObservable::create(
            self,
            property,
            move |o| BindingValue::new(converter(o.get_value_untyped(property))),
            |a, b| a == b,
            false,
        )
    }

    fn get_property_changed_observable(&self, property: &'static FerroProperty) -> ObjectPropertyChangedObservable {
        ObjectPropertyChangedObservable { target: self.to_weak(), property }
    }

    fn bind_typed<P: TypedProperty>(
        &self,
        property: &'static P,
        source: Rc<dyn IObservable<P::Value>>,
        priority: BindingPriority,
    ) -> Rc<dyn IDisposable> {
        property.typed_bind(self, source, priority)
    }

    fn bind_typed_value<P: TypedProperty>(
        &self,
        property: &'static P,
        source: Rc<dyn IObservable<BindingValue<P::Value>>>,
        priority: BindingPriority,
    ) -> Rc<dyn IDisposable> {
        property.typed_bind_value(self, source, priority)
    }

    fn get_typed_value<P: TypedProperty>(&self, property: &'static P) -> P::Value {
        property.typed_get_value(self)
    }

    fn get_typed_base_value<P: TypedProperty>(&self, property: &'static P) -> Option<P::Value> {
        property.typed_get_base_value(self)
    }

    fn get_base_value_untyped(&self, property: &'static FerroProperty) -> BoxedValue {
        property.routes().route_get_base_value(self)
    }
}

/// The change notifications of one property on one object.
///
/// Returned by
/// [`get_property_changed_observable`](FerroObjectExtensions::get_property_changed_observable).
/// Change notifications borrow their values, so handlers are closures rather
/// than observers. The object is held weakly.
#[derive(Clone)]
pub struct ObjectPropertyChangedObservable {
    target: WeakRef<FerroObject>,
    property: &'static FerroProperty,
}

impl ObjectPropertyChangedObservable {
    /// Subscribes to changes of the property on the object. Disposing the
    /// returned handle unsubscribes.
    pub fn subscribe(&self, handler: impl Fn(&FerroPropertyChangedEventArgs<'_>) + 'static) -> Rc<dyn IDisposable> {
        let property = self.property;
        match self.target.upgrade() {
            Some(target) => target.property_changed(move |e| {
                if e.property() == property {
                    handler(e);
                }
            }),
            None => Disposable::empty(),
        }
    }

    /// Subscribes a handler that is only invoked when the object is of class
    /// `TTarget` (or a class derived from it).
    pub fn add_class_handler<TTarget: ObjectType>(
        &self,
        handler: impl Fn(&TTarget, &FerroPropertyChangedEventArgs<'_>) + 'static,
    ) -> Rc<dyn IDisposable> {
        self.subscribe(move |e| {
            if let Some(target) = e.sender().downcast_ref::<TTarget>() {
                handler(target, e);
            }
        })
    }
}

/// An observable over the value of a property on an object.
///
/// Listens to the object's change notifications while it has observers, and
/// replays the current value to each new observer.
struct PropertyObservable<TResult: Clone + 'static> {
    this: Weak<PropertyObservable<TResult>>,
    target: WeakRef<FerroObject>,
    property: &'static FerroProperty,
    read: Box<dyn Fn(&FerroObject) -> TResult>,
    same: fn(&TResult, &TResult) -> bool,
    /// Whether the cached value is dropped when the last observer leaves.
    clear_value_on_deinitialize: bool,
    value: RefCell<Option<TResult>>,
    observers: RefCell<Vec<(u64, Rc<dyn IObserver<TResult>>)>>,
    next_id: Cell<u64>,
    subscription: RefCell<Option<Rc<dyn IDisposable>>>,
}

impl<TResult: Clone + 'static> PropertyObservable<TResult> {
    fn create(
        target: &FerroObject,
        property: &'static FerroProperty,
        read: impl Fn(&FerroObject) -> TResult + 'static,
        same: fn(&TResult, &TResult) -> bool,
        clear_value_on_deinitialize: bool,
    ) -> Rc<dyn IObservable<TResult>> {
        Rc::new_cyclic(|this| Self {
            this: this.clone(),
            target: target.to_weak(),
            property,
            read: Box::new(read),
            same,
            clear_value_on_deinitialize,
            value: RefCell::new(None),
            observers: RefCell::new(Vec::new()),
            next_id: Cell::new(0),
            subscription: RefCell::new(None),
        })
    }

    fn initialize(&self) {
        let Some(target) = self.target.upgrade() else { return };
        let value = (self.read)(&target);
        *self.value.borrow_mut() = Some(value);
        // The handler keeps the observable alive for as long as it has
        // observers, even if the caller dropped its own reference.
        let Some(this) = self.this.upgrade() else { return };
        let subscription = target.property_changed(move |e| this.property_changed(e));
        *self.subscription.borrow_mut() = Some(subscription);
    }

    fn deinitialize(&self) {
        let subscription = self.subscription.take();
        if let Some(subscription) = subscription {
            subscription.dispose();
        }
        if self.clear_value_on_deinitialize {
            *self.value.borrow_mut() = None;
        }
    }

    fn property_changed(&self, e: &FerroPropertyChangedEventArgs<'_>) {
        if e.property() != self.property {
            return;
        }
        let new_value = (self.read)(e.sender());
        let changed = match &*self.value.borrow() {
            Some(current) => !(self.same)(&new_value, current),
            None => true,
        };
        if changed {
            *self.value.borrow_mut() = Some(new_value.clone());
            let observers: Vec<Rc<dyn IObserver<TResult>>> =
                self.observers.borrow().iter().map(|(_, o)| o.clone()).collect();
            match observers.len() {
                0 => {}
                1 => observers[0].on_next(new_value),
                _ => {
                    for observer in observers {
                        observer.on_next(new_value.clone());
                    }
                }
            }
        }
    }

    fn remove(&self, id: u64) {
        let now_empty = {
            let mut observers = self.observers.borrow_mut();
            let before = observers.len();
            observers.retain(|(i, _)| *i != id);
            before != observers.len() && observers.is_empty()
        };
        if now_empty {
            self.deinitialize();
        }
    }
}

impl<TResult: Clone + 'static> IObservable<TResult> for PropertyObservable<TResult> {
    fn subscribe(&self, observer: Rc<dyn IObserver<TResult>>) -> Rc<dyn IDisposable> {
        let id = self.next_id.get();
        self.next_id.set(id + 1);
        let first = {
            let mut observers = self.observers.borrow_mut();
            observers.push((id, observer.clone()));
            observers.len() == 1
        };
        if first {
            self.initialize();
        }
        let current = self.value.borrow().clone();
        if let Some(value) = current {
            observer.on_next(value);
        }
        match self.this.upgrade() {
            Some(this) => Disposable::create(move || this.remove(id)),
            None => Disposable::empty(),
        }
    }
}