Skip to main content

ferroui_base/media/
i_experimental_acrylic_material.rs

1use crate::media::ref_adapter::RefAdapter;
2use crate::media::{
3    AcrylicBackgroundSource, Color, ExperimentalAcrylicMaterial, IMutableExperimentalAcrylicMaterial,
4};
5use crate::{FerroObject, ObjectType, Ref, Upcast};
6use std::any::Any;
7use std::rc::Rc;
8
9/// Experimental interface for producing acrylic-like materials.
10///
11/// Implemented by [`ImmutableExperimentalAcrylicMaterial`](crate::media::ImmutableExperimentalAcrylicMaterial)
12/// and, through an adapter, by handles of [`ExperimentalAcrylicMaterial`].
13pub trait IExperimentalAcrylicMaterial: 'static {
14    /// The background source mode.
15    fn background_source(&self) -> AcrylicBackgroundSource;
16
17    /// The tint color of the material.
18    fn tint_color(&self) -> Color;
19
20    /// The tint opacity of the material.
21    fn tint_opacity(&self) -> f64;
22
23    /// The effective material color.
24    fn material_color(&self) -> Color;
25
26    /// The fallback color if acrylic is unavailable.
27    fn fallback_color(&self) -> Color;
28
29    /// The implementing value, for downcasts to the immutable material.
30    fn as_any(&self) -> &dyn Any;
31
32    /// The object behind the material when it is mutable.
33    fn as_object(&self) -> Option<&FerroObject> {
34        None
35    }
36
37    /// The material viewed as [`IMutableExperimentalAcrylicMaterial`], when
38    /// it is one.
39    fn as_mutable_material(&self) -> Option<&dyn IMutableExperimentalAcrylicMaterial> {
40        None
41    }
42
43    /// The identity of the material, used for reference equality.
44    #[doc(hidden)]
45    fn reference_id(&self) -> *const () {
46        self as *const Self as *const ()
47    }
48}
49
50/// Materials compare by reference.
51impl PartialEq for dyn IExperimentalAcrylicMaterial {
52    #[inline]
53    fn eq(&self, other: &Self) -> bool {
54        self.reference_id() == other.reference_id()
55    }
56}
57
58impl std::fmt::Debug for dyn IExperimentalAcrylicMaterial {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        write!(f, "IExperimentalAcrylicMaterial({})", self.tint_color())
61    }
62}
63
64impl<T: ObjectType + Upcast<ExperimentalAcrylicMaterial>> RefAdapter<T> {
65    #[inline]
66    fn material(&self) -> &ExperimentalAcrylicMaterial {
67        (*self.0).upcast()
68    }
69}
70
71impl<T: ObjectType + Upcast<ExperimentalAcrylicMaterial>> IExperimentalAcrylicMaterial for RefAdapter<T> {
72    #[inline]
73    fn background_source(&self) -> AcrylicBackgroundSource {
74        self.material().background_source()
75    }
76
77    #[inline]
78    fn tint_color(&self) -> Color {
79        self.material().effective_tint_color()
80    }
81
82    #[inline]
83    fn tint_opacity(&self) -> f64 {
84        self.material().tint_opacity()
85    }
86
87    #[inline]
88    fn material_color(&self) -> Color {
89        self.material().effective_luminosity_color()
90    }
91
92    #[inline]
93    fn fallback_color(&self) -> Color {
94        self.material().fallback_color()
95    }
96
97    fn as_any(&self) -> &dyn Any {
98        self.material()
99    }
100
101    fn as_object(&self) -> Option<&FerroObject> {
102        Some(self.object())
103    }
104
105    fn as_mutable_material(&self) -> Option<&dyn IMutableExperimentalAcrylicMaterial> {
106        Some(self)
107    }
108
109    fn reference_id(&self) -> *const () {
110        RefAdapter::reference_id(self)
111    }
112}
113
114impl<T: ObjectType + Upcast<ExperimentalAcrylicMaterial>> IMutableExperimentalAcrylicMaterial for RefAdapter<T> {
115    fn to_immutable(&self) -> Rc<dyn IExperimentalAcrylicMaterial> {
116        Rc::new(crate::media::ImmutableExperimentalAcrylicMaterial::new(self))
117    }
118}
119
120impl<T: ObjectType + Upcast<ExperimentalAcrylicMaterial>> From<Ref<T>> for Rc<dyn IExperimentalAcrylicMaterial> {
121    #[inline]
122    fn from(value: Ref<T>) -> Self {
123        Rc::new(RefAdapter(value))
124    }
125}
126
127impl<T: ObjectType + Upcast<ExperimentalAcrylicMaterial>> From<&Ref<T>> for Rc<dyn IExperimentalAcrylicMaterial> {
128    #[inline]
129    fn from(value: &Ref<T>) -> Self {
130        Rc::new(RefAdapter(value.clone()))
131    }
132}