Skip to main content

ferroui_controls/
column_definition.rs

1use crate::definition_base::DefinitionBaseImpl;
2use crate::{DefinitionBase, GridLength, GridUnitType};
3use ferroui_base::{
4    ferro_class, ferro_property, instantiate, FerroObjectImpl, FerroProperty, Ref, StyledProperty,
5};
6
7/// Holds a column definition for a `Grid`.
8#[repr(C)]
9pub struct ColumnDefinition {
10    base: DefinitionBase,
11}
12
13ferro_class!(ColumnDefinition: DefinitionBase);
14ferroui_base::ferro_class_info!(ColumnDefinition { new: ColumnDefinition::new });
15
16ferroui_base::ferro_impl_classes!(ColumnDefinition: FerroObjectImpl);
17
18impl DefinitionBaseImpl for ColumnDefinition {
19    fn user_size_value_cache(this: &Self) -> GridLength {
20        this.width()
21    }
22
23    fn user_min_size_value_cache(this: &Self) -> f64 {
24        this.min_width()
25    }
26
27    fn user_max_size_value_cache(this: &Self) -> f64 {
28        this.max_width()
29    }
30}
31
32ferroui_base::ferro_properties! { impl ColumnDefinition {
33    ferro_property!(
34        /// Defines the `MaxWidth` property.
35        pub fn max_width_property() -> StyledProperty<f64> {
36            FerroProperty::register::<ColumnDefinition, _>("MaxWidth", f64::INFINITY)
37        }
38    );
39
40    ferro_property!(
41        /// Defines the `MinWidth` property.
42        pub fn min_width_property() -> StyledProperty<f64> {
43            FerroProperty::register::<ColumnDefinition, _>("MinWidth", 0.0)
44        }
45    );
46
47    ferro_property!(
48        /// Defines the `Width` property.
49        pub fn width_property() -> StyledProperty<GridLength> {
50            FerroProperty::register::<ColumnDefinition, _>("Width", GridLength::new(1.0, GridUnitType::Star))
51        }
52    );
53} }
54
55impl ColumnDefinition {
56    fn static_constructor() {
57        DefinitionBase::affects_parent_measure(&[
58            Self::min_width_property().as_property(),
59            Self::max_width_property().as_property(),
60        ]);
61
62        Self::width_property()
63            .changed()
64            .add_class_handler::<DefinitionBase>(DefinitionBase::on_user_size_property_changed);
65    }
66
67    /// Creates the class data; see [`ferroui_base::FerroObject::construct`].
68    pub fn construct() -> Self {
69        Self {
70            base: DefinitionBase::construct(),
71        }
72    }
73
74    /// Creates a column definition.
75    pub fn new() -> Ref<Self> {
76        instantiate(Self::construct())
77    }
78
79    /// Creates a column definition with the given width value and unit.
80    pub fn with_value(value: f64, type_: GridUnitType) -> Ref<Self> {
81        Self::with_width(GridLength::new(value, type_))
82    }
83
84    /// Creates a column definition with the given width.
85    pub fn with_width(width: GridLength) -> Ref<Self> {
86        let result = Self::new();
87        result.set_value(Self::width_property(), width);
88        result
89    }
90
91    /// The actual calculated width of the column.
92    pub fn actual_width(&self) -> f64 {
93        match self.parent() {
94            Some(parent) => parent.get_final_column_definition_width(self.index()),
95            None => 0.0,
96        }
97    }
98
99    /// The maximum width of the column in DIPs.
100    pub fn max_width(&self) -> f64 {
101        self.get_value(Self::max_width_property())
102    }
103
104    pub fn set_max_width(&self, value: f64) {
105        self.set_value(Self::max_width_property(), value)
106    }
107
108    /// The minimum width of the column in DIPs.
109    pub fn min_width(&self) -> f64 {
110        self.get_value(Self::min_width_property())
111    }
112
113    pub fn set_min_width(&self, value: f64) {
114        self.set_value(Self::min_width_property(), value)
115    }
116
117    /// The width of the column.
118    pub fn width(&self) -> GridLength {
119        self.get_value(Self::width_property())
120    }
121
122    pub fn set_width(&self, value: GridLength) {
123        self.set_value(Self::width_property(), value)
124    }
125}