ferroui_base/controls/i_resource_provider.rs
1use super::{IResourceNode, ResourceHostRef};
2use crate::reactive::IDisposable;
3use crate::FerroObject;
4use std::rc::Rc;
5
6/// Represents an object that can be queried for resources but does not appear
7/// in the logical tree.
8///
9/// The interface represented by this trait is implemented by resource
10/// dictionaries, styles and the like.
11pub trait IResourceProvider: IResourceNode {
12 /// The owner of the resource provider.
13 ///
14 /// If the resource provider is, or is a child of, a resource dictionary or
15 /// style that is hosted by an element, this is that element.
16 fn owner(&self) -> Option<ResourceHostRef>;
17
18 /// Raised when the owner of the resource provider changes.
19 fn owner_changed(&self, handler: Rc<dyn Fn()>) -> Rc<dyn IDisposable>;
20
21 /// Adds an owner to the resource provider.
22 fn add_owner(&self, owner: &ResourceHostRef);
23
24 /// Removes a resource provider owner.
25 fn remove_owner(&self, owner: &ResourceHostRef);
26
27 /// The provider as an object, when it is a class instance. Used for
28 /// identity comparison and type tests.
29 fn as_object(&self) -> Option<&FerroObject> {
30 None
31 }
32}
33
34/// Identity comparison of two resource provider handles.
35pub fn resource_provider_ptr_eq(a: &Rc<dyn IResourceProvider>, b: &Rc<dyn IResourceProvider>) -> bool {
36 **a == **b
37}
38
39/// Handles compare by identity (reference equality): two handles are equal
40/// when they refer to the same object, whichever adapter they were made
41/// from.
42impl PartialEq for dyn IResourceProvider {
43 fn eq(&self, other: &Self) -> bool {
44 match (self.as_object(), other.as_object()) {
45 (Some(a), Some(b)) => std::ptr::eq(a, b),
46 (None, None) => std::ptr::addr_eq(self as *const Self, other as *const Self),
47 _ => false,
48 }
49 }
50}
51