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
use super::{style_ptr_eq, ControlTheme, IStyle, SelectorMatchResult, Style, StyleHostRef, ThemeVariant};
use crate::collections::{
    CollectionChangedHandler, FerroList, NotifyCollectionChangedAction, NotifyCollectionChangedEventArgs,
    ResetBehavior,
};
use crate::controls::{
    IResourceNode, IResourceProvider, ResourceDictionary, ResourceHostRef, ResourceKey, ResourceValue,
    WeakResourceHost,
};
use crate::property_store::FrameType;
use crate::reactive::{Disposable, IDisposable};
use crate::utilities::HandlerList;
use crate::{
    ferro_class, instantiate, FerroObject, FerroObjectImpl, FerroObjectImplExt, ObjectType, Ref, StyledElement,
    Upcast,
};
use std::cell::RefCell;
use std::rc::Rc;

/// A style that consists of a number of child styles.
#[repr(C)]
pub struct Styles {
    base: FerroObject,
    styles: FerroList<Rc<dyn IStyle>>,
    owner: RefCell<Option<WeakResourceHost>>,
    resources: RefCell<Option<Ref<ResourceDictionary>>>,
    collection_changed: HandlerList<CollectionChangedHandler<Rc<dyn IStyle>>>,
    owner_changed: HandlerList<dyn Fn()>,
}

ferro_class!(Styles: FerroObject);
crate::ferro_class_info!(Styles {
    new: Styles::new,
    interfaces: [Rc<dyn IStyle>, Rc<dyn IResourceProvider>],
});

impl FerroObjectImpl for Styles {
    fn constructed(this: &Self) {
        Self::parent_constructed(this);

        this.styles.set_reset_behavior(ResetBehavior::Remove);
        let weak = this.to_ref().downgrade();
        this.styles.add_collection_changed(Rc::new(
            move |e: &NotifyCollectionChangedEventArgs<'_, Rc<dyn IStyle>>| {
                if let Some(this) = weak.upgrade() {
                    this.on_collection_changed(e);
                }
            },
        ));
        this.styles.set_validate(Some(Rc::new(|item: &Rc<dyn IStyle>| {
            if let Some(theme) = item.as_object().and_then(|o| o.downcast_ref::<ControlTheme>()) {
                panic!(
                    "ControlTheme (for {}) cannot be added to a Styles collection.",
                    theme.target_type().map(|t| t.name()).unwrap_or_default()
                );
            }
        })));
    }
}

impl Styles {
    /// Creates the class data; see [`FerroObject::construct`].
    pub fn construct() -> Self {
        Self {
            base: FerroObject::construct(),
            styles: FerroList::new(),
            owner: RefCell::new(None),
            resources: RefCell::new(None),
            collection_changed: HandlerList::new(),
            owner_changed: HandlerList::new(),
        }
    }

    /// Creates an empty styles collection.
    pub fn new() -> Ref<Self> {
        instantiate(Self::construct())
    }

    /// Creates an empty styles collection that belongs to `owner`.
    pub fn with_owner(owner: impl Into<ResourceHostRef>) -> Ref<Self> {
        let this = Self::construct();
        *this.owner.borrow_mut() = Some(owner.into().downgrade());
        instantiate(this)
    }

    /// Raised when the collection changes. Disposing the returned handle
    /// unsubscribes.
    pub fn collection_changed(
        &self,
        handler: impl for<'a> Fn(&NotifyCollectionChangedEventArgs<'a, Rc<dyn IStyle>>) + 'static,
    ) -> Rc<dyn IDisposable> {
        let token = self.collection_changed.add(Rc::new(handler));
        let weak = self.to_ref().downgrade();
        Disposable::create(move || {
            if let Some(this) = weak.upgrade() {
                this.collection_changed.remove(token);
            }
        })
    }

    /// Raised when the owner of the collection changes.
    pub fn owner_changed(&self, handler: impl Fn() + 'static) -> Rc<dyn IDisposable> {
        self.subscribe_owner_changed(Rc::new(handler))
    }

    fn subscribe_owner_changed(&self, handler: Rc<dyn Fn()>) -> Rc<dyn IDisposable> {
        let token = self.owner_changed.add(handler);
        let weak = self.to_ref().downgrade();
        Disposable::create(move || {
            if let Some(this) = weak.upgrade() {
                this.owner_changed.remove(token);
            }
        })
    }

    /// The number of styles.
    #[inline]
    pub fn count(&self) -> usize {
        self.styles.count()
    }

    /// The owner of the collection.
    pub fn owner(&self) -> Option<ResourceHostRef> {
        let owner = self.owner.borrow().clone();
        owner.and_then(|o| o.upgrade())
    }

    fn set_owner(&self, value: Option<&ResourceHostRef>) {
        let old = self.owner();
        if old.as_ref() != value {
            *self.owner.borrow_mut() = value.map(ResourceHostRef::downgrade);
            if !self.owner_changed.is_empty() {
                for (_, handler) in self.owner_changed.snapshot().iter() {
                    handler();
                }
            }
        }
    }

    /// The dictionary of style resources.
    pub fn resources(&self) -> Ref<ResourceDictionary> {
        if let Some(resources) = &*self.resources.borrow() {
            return resources.clone();
        }
        let resources = ResourceDictionary::new();
        self.set_resources(resources.clone());
        resources
    }

    pub fn set_resources(&self, value: Ref<ResourceDictionary>) {
        let current_owner = self.owner();

        if let Some(owner) = &current_owner {
            let old = self.resources.borrow().clone();
            if let Some(old) = old {
                old.remove_owner(owner);
            }
        }

        *self.resources.borrow_mut() = Some(value.clone());

        if let Some(owner) = &current_owner {
            value.add_owner(owner);
        }
    }

    /// Whether the collection or any of its styles has resources.
    pub fn has_resources(&self) -> bool {
        if self.resources.borrow().as_ref().is_some_and(|r| r.count() > 0) {
            return true;
        }

        if self.styles.is_empty() {
            return false;
        }

        self.styles
            .snapshot()
            .iter()
            .any(|i| i.as_resource_provider().is_some_and(|provider| provider.has_resources()))
    }

    /// The style at `index`. Panics if out of range.
    pub fn get(&self, index: usize) -> Rc<dyn IStyle> {
        self.styles.get(index)
    }

    /// Replaces the style at `index`.
    ///
    /// The replacement is notified as a removal followed by an insertion.
    pub fn set(&self, index: usize, value: impl Into<Rc<dyn IStyle>>) {
        self.styles.remove_at(index);
        self.styles.insert(index, value.into());
    }

    /// A snapshot of the styles.
    #[inline]
    pub fn snapshot(&self) -> Rc<Vec<Rc<dyn IStyle>>> {
        self.styles.snapshot()
    }

    /// Tries to find a resource in the collection's resources and then in
    /// its styles, last style first.
    pub fn try_get_resource(&self, key: &ResourceKey, theme: Option<&ThemeVariant>) -> Option<ResourceValue> {
        let resources = self.resources.borrow().clone();
        if let Some(resources) = resources {
            if let Some(value) = resources.try_get_resource(key, theme) {
                return Some(value);
            }
        }

        if !self.styles.is_empty() {
            for style in self.styles.snapshot().iter().rev() {
                if let Some(value) = style.try_get_resource(key, theme) {
                    return Some(value);
                }
            }
        }

        None
    }

    pub fn add_range(&self, items: impl IntoIterator<Item = Rc<dyn IStyle>>) {
        self.styles.add_range(items)
    }

    pub fn insert_range(&self, index: usize, items: impl IntoIterator<Item = Rc<dyn IStyle>>) {
        self.styles.insert_range(index, items)
    }

    pub fn move_item(&self, old_index: usize, new_index: usize) {
        self.styles.move_item(old_index, new_index)
    }

    pub fn move_range(&self, old_index: usize, count: usize, new_index: usize) {
        self.styles.move_range(old_index, count, new_index)
    }

    pub fn remove_all(&self, items: impl IntoIterator<Item = Rc<dyn IStyle>>) {
        for item in items {
            self.remove(item);
        }
    }

    pub fn remove_range(&self, index: usize, count: usize) {
        self.styles.remove_range(index, count)
    }

    /// The index of a style, if it is in the collection.
    pub fn index_of(&self, item: impl Into<Rc<dyn IStyle>>) -> Option<usize> {
        let item = item.into();
        self.styles.snapshot().iter().position(|s| style_ptr_eq(s, &item))
    }

    pub fn insert(&self, index: usize, item: impl Into<Rc<dyn IStyle>>) {
        self.styles.insert(index, item.into())
    }

    pub fn remove_at(&self, index: usize) {
        self.styles.remove_at(index)
    }

    /// Adds a style to the collection.
    pub fn add(&self, item: impl Into<Rc<dyn IStyle>>) {
        self.styles.add(item.into())
    }

    /// Removes all styles from the collection.
    pub fn clear(&self) {
        self.styles.clear()
    }

    pub fn contains(&self, item: impl Into<Rc<dyn IStyle>>) -> bool {
        self.index_of(item).is_some()
    }

    /// Removes a style from the collection. Returns whether it was present.
    pub fn remove(&self, item: impl Into<Rc<dyn IStyle>>) -> bool {
        match self.index_of(item) {
            Some(index) => {
                self.styles.remove_at(index);
                true
            }
            None => false,
        }
    }

    /// Adds an owner to the collection and the resource providers in it.
    pub fn add_owner(&self, owner: &ResourceHostRef) {
        if self.owner().is_some() {
            panic!("The Styles already has a owner.");
        }

        self.set_owner(Some(owner));
        let resources = self.resources.borrow().clone();
        if let Some(resources) = resources {
            resources.add_owner(owner);
        }

        for child in self.styles.snapshot().iter() {
            if let Some(r) = child.as_resource_provider() {
                r.add_owner(owner);
            }
        }
    }

    /// Removes the owner of the collection and the resource providers in it.
    pub fn remove_owner(&self, owner: &ResourceHostRef) {
        if self.owner().as_ref() == Some(owner) {
            self.set_owner(None);
            let resources = self.resources.borrow().clone();
            if let Some(resources) = resources {
                resources.remove_owner(owner);
            }

            for child in self.styles.snapshot().iter() {
                if let Some(r) = child.as_resource_provider() {
                    r.remove_owner(owner);
                }
            }
        }
    }

    /// Attaches the styles of the collection (but not of nested collections)
    /// that match `target`.
    #[allow(dead_code)]
    pub(crate) fn try_attach(&self, target: &StyledElement, host: Option<&StyleHostRef>) -> SelectorMatchResult {
        let mut result = SelectorMatchResult::NeverThisType;

        for s in self.styles.snapshot().iter() {
            let Some(style) = s.as_object().and_then(|o| o.downcast_ref::<Style>()) else { continue };

            let r = style.try_attach(target, host, FrameType::Style);
            if r > result {
                result = r;
            }
        }

        result
    }

    fn internal_add(items: &[Rc<dyn IStyle>], owner: Option<&ResourceHostRef>) {
        if let Some(owner) = owner {
            for item in items {
                if let Some(provider) = item.as_resource_provider() {
                    provider.add_owner(owner);
                }
            }

            if let Some(host) = owner.as_style_host() {
                host.styles_added(items);
            }
        }
    }

    fn internal_remove(items: &[Rc<dyn IStyle>], owner: Option<&ResourceHostRef>) {
        if let Some(owner) = owner {
            for item in items {
                if let Some(provider) = item.as_resource_provider() {
                    provider.remove_owner(owner);
                }
            }

            if let Some(host) = owner.as_style_host() {
                host.styles_removed(items);
            }
        }
    }

    fn on_collection_changed(&self, e: &NotifyCollectionChangedEventArgs<'_, Rc<dyn IStyle>>) {
        if e.action == NotifyCollectionChangedAction::Reset {
            panic!("Reset should not be called on Styles.");
        }

        let current_owner = self.owner();

        match e.action {
            NotifyCollectionChangedAction::Add => Self::internal_add(e.new_items, current_owner.as_ref()),
            NotifyCollectionChangedAction::Remove => Self::internal_remove(e.old_items, current_owner.as_ref()),
            NotifyCollectionChangedAction::Replace => {
                Self::internal_remove(e.old_items, current_owner.as_ref());
                Self::internal_add(e.new_items, current_owner.as_ref());
            }
            _ => {}
        }

        if !self.collection_changed.is_empty() {
            for (_, handler) in self.collection_changed.snapshot().iter() {
                handler(e);
            }
        }
    }
}

/// The style and resource provider interfaces of a styles collection object.
struct StylesHandle(Ref<Styles>);

impl IResourceNode for StylesHandle {
    fn reference_id(&self) -> *const () {
        let object: &FerroObject = (*self.0).upcast();
        object as *const FerroObject as *const ()
    }

    fn has_resources(&self) -> bool {
        self.0.has_resources()
    }

    fn try_get_resource(&self, key: &ResourceKey, theme: Option<&ThemeVariant>) -> Option<ResourceValue> {
        self.0.try_get_resource(key, theme)
    }
}

impl IResourceProvider for StylesHandle {
    fn owner(&self) -> Option<ResourceHostRef> {
        self.0.owner()
    }

    fn owner_changed(&self, handler: Rc<dyn Fn()>) -> Rc<dyn IDisposable> {
        self.0.subscribe_owner_changed(handler)
    }

    fn add_owner(&self, owner: &ResourceHostRef) {
        self.0.add_owner(owner)
    }

    fn remove_owner(&self, owner: &ResourceHostRef) {
        self.0.remove_owner(owner)
    }

    fn as_object(&self) -> Option<&FerroObject> {
        Some((*self.0).upcast())
    }
}

impl IStyle for StylesHandle {
    fn children(&self) -> Rc<Vec<Rc<dyn IStyle>>> {
        self.0.styles.snapshot()
    }

    fn as_object(&self) -> Option<&FerroObject> {
        Some((*self.0).upcast())
    }

    fn as_resource_provider(&self) -> Option<&dyn IResourceProvider> {
        Some(self)
    }
}

/// Converts a handle of a styles collection (or a class derived from it)
/// into a style handle.
pub fn styles_as_style<T: ObjectType + Upcast<Styles>>(styles: &Ref<T>) -> Rc<dyn IStyle> {
    Rc::new(StylesHandle(styles.clone().upcast()))
}

impl From<Ref<Styles>> for Rc<dyn IStyle> {
    fn from(value: Ref<Styles>) -> Self {
        Rc::new(StylesHandle(value))
    }
}

impl From<Ref<Styles>> for Rc<dyn IResourceProvider> {
    fn from(value: Ref<Styles>) -> Self {
        Rc::new(StylesHandle(value))
    }
}

impl From<&Ref<Styles>> for Rc<dyn IResourceProvider> {
    fn from(value: &Ref<Styles>) -> Self {
        Rc::new(StylesHandle(value.clone()))
    }
}

impl From<&Ref<Styles>> for Rc<dyn IStyle> {
    fn from(value: &Ref<Styles>) -> Self {
        Rc::new(StylesHandle(value.clone()))
    }
}