Skip to main content

ferroui_base/media/effects/
i_blur_effect.rs

1use crate::media::effects::{BlurEffect, Effect, IEffect, IImmutableEffect};
2use crate::media::ref_adapter::RefAdapter;
3use crate::{ObjectType, Upcast};
4use std::any::Any;
5use std::rc::Rc;
6
7/// An effect that blurs its content.
8pub trait IBlurEffect: IEffect {
9    /// The blur radius.
10    fn radius(&self) -> f64;
11}
12
13/// An immutable blur effect.
14#[derive(Clone, Copy, Debug)]
15pub struct ImmutableBlurEffect {
16    radius: f64,
17}
18
19impl ImmutableBlurEffect {
20    pub fn new(radius: f64) -> Self {
21        Self { radius }
22    }
23
24    /// The blur radius.
25    #[inline]
26    pub fn radius(&self) -> f64 {
27        self.radius
28    }
29}
30
31impl IEffect for ImmutableBlurEffect {
32    fn as_any(&self) -> &dyn Any {
33        self
34    }
35
36    fn as_blur_effect(&self) -> Option<&dyn IBlurEffect> {
37        Some(self)
38    }
39
40    fn as_immutable_effect(&self) -> Option<&dyn IImmutableEffect> {
41        Some(self)
42    }
43
44    fn into_immutable_effect(self: Rc<Self>) -> Option<std::sync::Arc<dyn IImmutableEffect>> {
45        Some(std::sync::Arc::new(*self))
46    }
47}
48
49impl IBlurEffect for ImmutableBlurEffect {
50    #[inline]
51    fn radius(&self) -> f64 {
52        self.radius
53    }
54}
55
56impl IImmutableEffect for ImmutableBlurEffect {
57    fn equals(&self, other: Option<&dyn IEffect>) -> bool {
58        other.and_then(|other| other.as_blur_effect()).is_some_and(|blur| blur.radius() == self.radius)
59    }
60}
61
62impl<T: ObjectType + Upcast<Effect>> IBlurEffect for RefAdapter<T> {
63    #[inline]
64    fn radius(&self) -> f64 {
65        self.class::<BlurEffect>().radius()
66    }
67}