Skip to main content

ferroui_base/styling/
setter_base.rs

1use super::{ISetterInstance, StyleInstance};
2use crate::property_store::IValueEntry;
3use crate::StyledElement;
4use std::rc::Rc;
5
6/// Represents the base class for value setters.
7pub trait SetterBase: 'static {
8    /// Instances a setter on a control.
9    #[doc(hidden)]
10    fn instance(self: Rc<Self>, style_instance: &Rc<StyleInstance>, target: &StyledElement) -> SetterInstance;
11
12    /// The implementing value, for casting to its concrete type
13    /// (`as_any()?.downcast_ref::<T>()`): the equivalent of `is`/`as` on the
14    /// contract. `None` for an implementation that does not expose itself.
15    fn as_any(&self) -> Option<&dyn std::any::Any> {
16        None
17    }
18}
19
20/// The result of instancing a setter on a control.
21pub struct SetterInstance {
22    pub(crate) kind: SetterInstanceKind,
23    /// True when the instance is the setter itself, in which case the style
24    /// instance holding it can be shared between controls.
25    pub(crate) is_setter: bool,
26}
27
28pub(crate) enum SetterInstanceKind {
29    /// An entry in the value frame of the style instance.
30    Entry(Rc<dyn IValueEntry>),
31    /// A setter that was applied directly to the control.
32    Other(Rc<dyn ISetterInstance>),
33}
34
35/// Handles compare by identity (reference equality), so that they can be
36/// held in property and untyped values.
37impl PartialEq for dyn SetterBase {
38    #[inline]
39    fn eq(&self, other: &Self) -> bool {
40        std::ptr::addr_eq(self as *const Self, other as *const Self)
41    }
42}