ferroui-base 0.2.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
use super::{IAnimatedServerObject, ServerCompositor, ServerObject, ServerValueChange};
use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};

/// Something that is notified when a render resource it depends on is
/// invalidated.
pub trait IServerRenderResourceObserver {
    fn dependency_queued_invalidate(&self, sender: &dyn IServerRenderResource);
}

/// A server-side render resource: a brush, pen, geometry, transform or
/// render data whose changes invalidate the objects that use it.
pub trait IServerRenderResource: IServerRenderResourceObserver {
    fn add_observer(&self, observer: &Rc<dyn IServerRenderResourceObserver>);
    fn remove_observer(&self, observer: &Rc<dyn IServerRenderResourceObserver>);
    fn queued_invalidate(&self);
}

/// A render resource that owns a [`ServerRenderResourceCore`].
pub trait IServerRenderResourceHost: IServerRenderResource {
    /// The compositor of the resource, while it is alive.
    fn compositor(&self) -> Option<Rc<ServerCompositor>>;

    /// The resource as a shared handle, for the invalidation queue.
    fn as_render_resource_rc(&self) -> Option<Rc<dyn IServerRenderResource>>;

    /// Called once per group of property changes, before the queued
    /// invalidation.
    fn resource_property_changed(&self) {}
}

struct ObserverEntry {
    observer: Weak<dyn IServerRenderResourceObserver>,
    count: u32,
}

/// The shared implementation of render resources: change coalescing and
/// the reference-counted observer list.
///
/// Observers are held weakly; a dropped observer is pruned the next time
/// the list is walked.
#[derive(Default)]
pub struct ServerRenderResourceCore {
    pending_invalidation: Cell<bool>,
    disposed: Cell<bool>,
    observers: RefCell<Vec<ObserverEntry>>,
}

fn same_observer(a: &Weak<dyn IServerRenderResourceObserver>, b: &Rc<dyn IServerRenderResourceObserver>) -> bool {
    std::ptr::addr_eq(a.as_ptr(), Rc::as_ptr(b))
}

impl ServerRenderResourceCore {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn is_disposed(&self) -> bool {
        self.disposed.get()
    }

    /// Assigns a property field of the resource: moves the observation of
    /// the resource from the old value to the new one and records the
    /// change. Nothing happens when the value is unchanged.
    pub fn set_value(&self, host: &dyn IServerRenderResourceHost, change: ServerValueChange<'_>) {
        if change.equal {
            return;
        }

        if self.disposed.get() {
            (change.assign)();
            return;
        }

        let observer: Option<Rc<dyn IServerRenderResourceObserver>> =
            host.as_render_resource_rc().map(|this| this as Rc<dyn IServerRenderResourceObserver>);

        if let (Some(old_child), Some(observer)) = (&change.old_resource, &observer) {
            old_child.remove_observer(observer);
        }

        (change.assign)();

        if let (Some(new_child), Some(observer)) = (&change.new_resource, &observer) {
            new_child.add_observer(observer);
        }

        self.invalidated(host);
    }

    /// Assigns a property field that holds a list of values (the
    /// `IEnumerable` branch of `SetValue`): moves the observation of the
    /// resource from the render resources of the old list to those of the
    /// new one and records the change. The lists differ by reference, which
    /// is the caller's equality test.
    pub fn set_list_value(
        &self,
        host: &dyn IServerRenderResourceHost,
        old_children: Vec<Rc<dyn IServerRenderResource>>,
        new_children: Vec<Rc<dyn IServerRenderResource>>,
        assign: &mut dyn FnMut(),
    ) {
        if self.disposed.get() {
            assign();
            return;
        }

        let observer: Option<Rc<dyn IServerRenderResourceObserver>> =
            host.as_render_resource_rc().map(|this| this as Rc<dyn IServerRenderResourceObserver>);

        if let Some(observer) = &observer {
            for child in &old_children {
                child.remove_observer(observer);
            }
        }

        assign();

        if let Some(observer) = &observer {
            for child in &new_children {
                child.add_observer(observer);
            }
        }

        self.invalidated(host);
    }

    /// Stops observing the render resource behind a property value.
    pub fn remove_observers_from_property(
        &self,
        host: &dyn IServerRenderResourceHost,
        resource: Option<&Rc<dyn IServerRenderResource>>,
    ) {
        if let (Some(resource), Some(this)) = (resource, host.as_render_resource_rc()) {
            let observer: Rc<dyn IServerRenderResourceObserver> = this;
            resource.remove_observer(&observer);
        }
    }

    /// Records that a property of the resource changed. The first change
    /// since the last queued invalidation enqueues the resource on its
    /// compositor.
    pub fn invalidated(&self, host: &dyn IServerRenderResourceHost) {
        // This is needed to avoid triggering on multiple property changes.
        if !self.pending_invalidation.get() {
            self.pending_invalidation.set(true);
            if let (Some(compositor), Some(this)) = (host.compositor(), host.as_render_resource_rc()) {
                compositor.enqueue_render_resource_for_invalidation(this);
            }
            host.resource_property_changed();
        }
    }

    pub fn dispose(&self) {
        self.disposed.set(true);
        self.observers.borrow_mut().clear();
    }

    pub fn dependency_queued_invalidate(&self, host: &dyn IServerRenderResourceHost) {
        if let (Some(compositor), Some(this)) = (host.compositor(), host.as_render_resource_rc()) {
            compositor.enqueue_render_resource_for_invalidation(this);
        }
    }

    pub fn add_observer(&self, observer: &Rc<dyn IServerRenderResourceObserver>) {
        debug_assert!(!self.disposed.get());
        if self.disposed.get() {
            return;
        }
        let mut observers = self.observers.borrow_mut();
        match observers.iter_mut().find(|e| same_observer(&e.observer, observer)) {
            Some(entry) => entry.count += 1,
            None => observers.push(ObserverEntry { observer: Rc::downgrade(observer), count: 1 }),
        }
    }

    pub fn remove_observer(&self, observer: &Rc<dyn IServerRenderResourceObserver>) {
        if self.disposed.get() {
            return;
        }
        let mut observers = self.observers.borrow_mut();
        if let Some(index) = observers.iter().position(|e| same_observer(&e.observer, observer)) {
            observers[index].count -= 1;
            if observers[index].count == 0 {
                observers.swap_remove(index);
            }
        }
    }

    pub fn queued_invalidate(&self, this: &dyn IServerRenderResource) {
        self.pending_invalidation.set(false);
        let observers: Vec<Rc<dyn IServerRenderResourceObserver>> = {
            let mut observers = self.observers.borrow_mut();
            observers.retain(|e| e.observer.strong_count() > 0);
            observers.iter().filter_map(|e| e.observer.upgrade()).collect()
        };
        for observer in observers {
            observer.dependency_queued_invalidate(this);
        }
    }

    /// The number of distinct live observers.
    pub fn observer_count(&self) -> usize {
        self.observers.borrow().iter().filter(|e| e.observer.strong_count() > 0).count()
    }
}

/// The base of the server-side render resources that are not animatable
/// (upstream `SimpleServerRenderResource`), as a part the resource classes
/// embed: the compositor, the resource core and the handle of the object as
/// a render resource.
pub struct SimpleServerRenderResource {
    compositor: Weak<ServerCompositor>,
    this: Weak<dyn IServerRenderResource>,
    core: ServerRenderResourceCore,
}

impl SimpleServerRenderResource {
    /// `this` is the object that embeds the part.
    pub fn new(compositor: &Rc<ServerCompositor>, this: Weak<dyn IServerRenderResource>) -> Self {
        Self { compositor: Rc::downgrade(compositor), this, core: ServerRenderResourceCore::new() }
    }

    pub fn compositor(&self) -> Option<Rc<ServerCompositor>> {
        self.compositor.upgrade()
    }

    pub fn core(&self) -> &ServerRenderResourceCore {
        &self.core
    }

    pub fn is_disposed(&self) -> bool {
        self.core.is_disposed()
    }

    pub fn as_render_resource_rc(&self) -> Option<Rc<dyn IServerRenderResource>> {
        self.this.upgrade()
    }
}

/// Implements the render resource contracts of a class that embeds a
/// [`SimpleServerRenderResource`] in the field `$base`: the members
/// `SimpleServerRenderResource` gives its subclasses upstream.
///
/// `SetValue` goes through the resource core, so a property that holds a
/// render resource is observed and every change invalidates the resource.
#[macro_export]
#[doc(hidden)]
macro_rules! __impl_simple_server_render_resource {
    ($ty:ty, $base:ident) => {
        impl $crate::rendering::composition::server::IServerRenderResourceObserver for $ty {
            fn dependency_queued_invalidate(
                &self,
                _sender: &dyn $crate::rendering::composition::server::IServerRenderResource,
            ) {
                self.$base.core().dependency_queued_invalidate(self);
            }
        }

        impl $crate::rendering::composition::server::IServerRenderResource for $ty {
            fn add_observer(
                &self,
                observer: &::std::rc::Rc<dyn $crate::rendering::composition::server::IServerRenderResourceObserver>,
            ) {
                self.$base.core().add_observer(observer);
            }

            fn remove_observer(
                &self,
                observer: &::std::rc::Rc<dyn $crate::rendering::composition::server::IServerRenderResourceObserver>,
            ) {
                self.$base.core().remove_observer(observer);
            }

            fn queued_invalidate(&self) {
                self.$base.core().queued_invalidate(self);
            }
        }

        impl $crate::rendering::composition::server::IServerRenderResourceHost for $ty {
            fn compositor(&self) -> Option<::std::rc::Rc<$crate::rendering::composition::server::ServerCompositor>> {
                self.$base.compositor()
            }

            fn as_render_resource_rc(
                &self,
            ) -> Option<::std::rc::Rc<dyn $crate::rendering::composition::server::IServerRenderResource>> {
                self.$base.as_render_resource_rc()
            }
        }

        impl $crate::rendering::composition::server::IServerPropertyHost for $ty {
            fn server_compositor(
                &self,
            ) -> Option<::std::rc::Rc<$crate::rendering::composition::server::ServerCompositor>> {
                self.$base.compositor()
            }

            fn set_value(
                &self,
                _property: &'static $crate::rendering::composition::server::CompositionProperty,
                change: $crate::rendering::composition::server::ServerValueChange<'_>,
            ) {
                self.$base.core().set_value(self, change);
            }
        }
    };
}

pub use crate::__impl_simple_server_render_resource as impl_simple_server_render_resource;

/// The base of the animatable server-side render resources (upstream
/// `ServerRenderResource`, a `ServerObject`), as a part the resource classes
/// embed: the animation support of the server object, the resource core and
/// the handle of the object as a render resource.
pub struct ServerRenderResource {
    object: ServerObject,
    this: Weak<dyn IServerRenderResource>,
    core: ServerRenderResourceCore,
}

impl ServerRenderResource {
    /// `owner` and `this` are the object that embeds the part.
    pub fn new(
        compositor: &Rc<ServerCompositor>,
        owner: Weak<dyn IAnimatedServerObject>,
        this: Weak<dyn IServerRenderResource>,
    ) -> Self {
        Self { object: ServerObject::new(compositor, owner), this, core: ServerRenderResourceCore::new() }
    }

    /// The embedded `ServerObject`.
    pub fn object(&self) -> &ServerObject {
        &self.object
    }

    pub fn compositor(&self) -> Option<Rc<ServerCompositor>> {
        self.object.compositor()
    }

    pub fn core(&self) -> &ServerRenderResourceCore {
        &self.core
    }

    pub fn is_disposed(&self) -> bool {
        self.core.is_disposed()
    }

    pub fn as_render_resource_rc(&self) -> Option<Rc<dyn IServerRenderResource>> {
        self.this.upgrade()
    }
}

/// Implements the render resource and animation contracts of a class that
/// embeds a [`ServerRenderResource`] in the field `$base`: the members
/// `ServerRenderResource` gives its subclasses upstream.
///
/// As upstream, `SetValue` goes through the resource core only: the
/// `SetValue` of the render resource hides the one of `ServerObject`, so a
/// direct set does not invalidate the animations that depend on the
/// property. `$get` resolves a composition property by name.
///
/// The class implements `IServerObject` itself, with `values_invalidated`
/// invalidating the resource (`self.$base.core().invalidated(self)`).
#[macro_export]
#[doc(hidden)]
macro_rules! __impl_server_render_resource {
    ($ty:ty, $base:ident, composition_property: $get:expr) => {
        $crate::rendering::composition::server::impl_server_render_resource!(@resource $ty, $base);

        impl $crate::rendering::composition::server::IServerAnimatedPropertyHost for $ty {
            fn set_animated_value(
                &self,
                property: &'static $crate::rendering::composition::server::CompositionProperty,
                current_value: $crate::rendering::composition::expressions::ExpressionVariant,
                committed_at: ::std::time::Duration,
                animation: ::std::rc::Rc<dyn $crate::rendering::composition::animations::IAnimationInstance>,
            ) {
                self.$base.object().set_animated_value(property, current_value, committed_at, animation);
            }

            fn remove_animation_for_property(
                &self,
                property: &'static $crate::rendering::composition::server::CompositionProperty,
            ) {
                self.$base.object().remove_animation_for_property(property);
            }

            fn notify_animated_value_changed(
                &self,
                _property: &'static $crate::rendering::composition::server::CompositionProperty,
            ) {
                $crate::rendering::composition::server::IServerObject::values_invalidated(self);
            }
        }

        impl $crate::rendering::composition::server::IAnimatedServerObject for $ty {
            fn server_object(&self) -> &$crate::rendering::composition::server::ServerObject {
                self.$base.object()
            }

            fn get_composition_property(
                &self,
                field_name: &str,
            ) -> Option<&'static $crate::rendering::composition::server::CompositionProperty> {
                ($get)(field_name)
            }

            fn as_server_object_dyn(&self) -> &dyn $crate::rendering::composition::server::IServerObject {
                self
            }
        }
    };
    (@resource $ty:ty, $base:ident) => {
        impl $crate::rendering::composition::server::IServerRenderResourceObserver for $ty {
            fn dependency_queued_invalidate(
                &self,
                _sender: &dyn $crate::rendering::composition::server::IServerRenderResource,
            ) {
                self.$base.core().dependency_queued_invalidate(self);
            }
        }

        impl $crate::rendering::composition::server::IServerRenderResource for $ty {
            fn add_observer(
                &self,
                observer: &::std::rc::Rc<dyn $crate::rendering::composition::server::IServerRenderResourceObserver>,
            ) {
                self.$base.core().add_observer(observer);
            }

            fn remove_observer(
                &self,
                observer: &::std::rc::Rc<dyn $crate::rendering::composition::server::IServerRenderResourceObserver>,
            ) {
                self.$base.core().remove_observer(observer);
            }

            fn queued_invalidate(&self) {
                self.$base.core().queued_invalidate(self);
            }
        }

        impl $crate::rendering::composition::server::IServerRenderResourceHost for $ty {
            fn compositor(&self) -> Option<::std::rc::Rc<$crate::rendering::composition::server::ServerCompositor>> {
                self.$base.compositor()
            }

            fn as_render_resource_rc(
                &self,
            ) -> Option<::std::rc::Rc<dyn $crate::rendering::composition::server::IServerRenderResource>> {
                self.$base.as_render_resource_rc()
            }
        }

        impl $crate::rendering::composition::server::IServerPropertyHost for $ty {
            fn server_compositor(
                &self,
            ) -> Option<::std::rc::Rc<$crate::rendering::composition::server::ServerCompositor>> {
                self.$base.compositor()
            }

            fn set_value(
                &self,
                _property: &'static $crate::rendering::composition::server::CompositionProperty,
                change: $crate::rendering::composition::server::ServerValueChange<'_>,
            ) {
                self.$base.core().set_value(self, change);
            }
        }
    };
}

pub use crate::__impl_server_render_resource as impl_server_render_resource;