Skip to main content

ferroui_base/media/
radial_gradient_brush.rs

1use crate::media::immutable::ImmutableRadialGradientBrush;
2use crate::media::{BrushImpl, BrushImplExt, GradientBrush, GradientBrushImpl, IImmutableBrush, ServerBrushFactory};
3use crate::rendering::composition::generated::ServerCompositionSimpleRadialGradientBrushProps;
4use crate::rendering::composition::server::ServerCompositionSimpleRadialGradientBrush;
5use crate::rendering::composition::transport::BatchStreamWriter;
6use crate::rendering::composition::Compositor;
7use crate::{
8    ferro_class, ferro_impl_classes, ferro_property, instantiate, FerroObjectImpl, FerroProperty, Ref, RelativePoint, RelativeScalar,
9    StyledProperty,
10};
11use std::rc::Rc;
12
13/// Paints an area with a radial gradient.
14#[repr(C)]
15pub struct RadialGradientBrush {
16    base: GradientBrush,
17}
18
19ferro_class!(RadialGradientBrush: GradientBrush);
20crate::ferro_class_info!(RadialGradientBrush { new: RadialGradientBrush::new });
21ferro_impl_classes!(RadialGradientBrush: FerroObjectImpl);
22
23impl BrushImpl for RadialGradientBrush {
24    fn factory(_this: &Self) -> Option<ServerBrushFactory> {
25        Some(|c| ServerCompositionSimpleRadialGradientBrush::new(c))
26    }
27
28    fn serialize_changes(this: &Self, c: &Compositor, writer: &mut BatchStreamWriter<'_>) {
29        Self::parent_serialize_changes(this, c, writer);
30        ServerCompositionSimpleRadialGradientBrushProps::serialize_all_changes(writer, this.center(),
31            this.gradient_origin(),
32            this.radius_x(),
33            this.radius_y(),
34        );
35    }
36}
37
38impl GradientBrushImpl for RadialGradientBrush {
39    fn to_immutable(this: &Self) -> Rc<dyn IImmutableBrush> {
40        Rc::new(ImmutableRadialGradientBrush::from_brush(this))
41    }
42}
43
44crate::ferro_properties! { impl RadialGradientBrush {
45    ferro_property!(pub fn center_property() -> StyledProperty<RelativePoint> {
46        FerroProperty::register::<RadialGradientBrush, _>("Center", RelativePoint::CENTER)
47    });
48
49    ferro_property!(pub fn gradient_origin_property() -> StyledProperty<RelativePoint> {
50        FerroProperty::register::<RadialGradientBrush, _>("GradientOrigin", RelativePoint::CENTER)
51    });
52
53    ferro_property!(pub fn radius_x_property() -> StyledProperty<RelativeScalar> {
54        FerroProperty::register::<RadialGradientBrush, _>("RadiusX", RelativeScalar::MIDDLE)
55    });
56
57    ferro_property!(pub fn radius_y_property() -> StyledProperty<RelativeScalar> {
58        FerroProperty::register::<RadialGradientBrush, _>("RadiusY", RelativeScalar::MIDDLE)
59    });
60} }
61
62impl RadialGradientBrush {
63    /// Creates the class data.
64    pub fn construct() -> Self {
65        Self { base: GradientBrush::construct() }
66    }
67
68    pub fn new() -> Ref<Self> {
69        instantiate(Self::construct())
70    }
71
72    /// The start point for the gradient.
73    pub fn center(&self) -> RelativePoint {
74        self.get_value(Self::center_property())
75    }
76
77    pub fn set_center(&self, value: RelativePoint) {
78        self.set_value(Self::center_property(), value)
79    }
80
81    /// The location of the two-dimensional focal point that defines the beginning of the gradient.
82    pub fn gradient_origin(&self) -> RelativePoint {
83        self.get_value(Self::gradient_origin_property())
84    }
85
86    pub fn set_gradient_origin(&self, value: RelativePoint) {
87        self.set_value(Self::gradient_origin_property(), value)
88    }
89
90    /// The horizontal radius of the outermost circle of the radial gradient.
91    pub fn radius_x(&self) -> RelativeScalar {
92        self.get_value(Self::radius_x_property())
93    }
94
95    pub fn set_radius_x(&self, value: RelativeScalar) {
96        self.set_value(Self::radius_x_property(), value)
97    }
98
99    /// The vertical radius of the outermost circle of the radial gradient.
100    pub fn radius_y(&self) -> RelativeScalar {
101        self.get_value(Self::radius_y_property())
102    }
103
104    pub fn set_radius_y(&self, value: RelativeScalar) {
105        self.set_value(Self::radius_y_property(), value)
106    }
107}