Skip to main content

ferroui_controls/
row_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 row definition for a `Grid`.
8#[repr(C)]
9pub struct RowDefinition {
10    base: DefinitionBase,
11}
12
13ferro_class!(RowDefinition: DefinitionBase);
14ferroui_base::ferro_class_info!(RowDefinition { new: RowDefinition::new });
15
16ferroui_base::ferro_impl_classes!(RowDefinition: FerroObjectImpl);
17
18impl DefinitionBaseImpl for RowDefinition {
19    fn user_size_value_cache(this: &Self) -> GridLength {
20        this.height()
21    }
22
23    fn user_min_size_value_cache(this: &Self) -> f64 {
24        this.min_height()
25    }
26
27    fn user_max_size_value_cache(this: &Self) -> f64 {
28        this.max_height()
29    }
30}
31
32ferroui_base::ferro_properties! { impl RowDefinition {
33    ferro_property!(
34        /// Defines the `MaxHeight` property.
35        pub fn max_height_property() -> StyledProperty<f64> {
36            FerroProperty::register::<RowDefinition, _>("MaxHeight", f64::INFINITY)
37        }
38    );
39
40    ferro_property!(
41        /// Defines the `MinHeight` property.
42        pub fn min_height_property() -> StyledProperty<f64> {
43            FerroProperty::register::<RowDefinition, _>("MinHeight", 0.0)
44        }
45    );
46
47    ferro_property!(
48        /// Defines the `Height` property.
49        pub fn height_property() -> StyledProperty<GridLength> {
50            FerroProperty::register::<RowDefinition, _>("Height", GridLength::new(1.0, GridUnitType::Star))
51        }
52    );
53} }
54
55impl RowDefinition {
56    fn static_constructor() {
57        DefinitionBase::affects_parent_measure(&[
58            Self::max_height_property().as_property(),
59            Self::min_height_property().as_property(),
60        ]);
61
62        Self::height_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 row definition.
75    pub fn new() -> Ref<Self> {
76        instantiate(Self::construct())
77    }
78
79    /// Creates a row definition with the given height value and unit.
80    pub fn with_value(value: f64, type_: GridUnitType) -> Ref<Self> {
81        Self::with_height(GridLength::new(value, type_))
82    }
83
84    /// Creates a row definition with the given height.
85    pub fn with_height(height: GridLength) -> Ref<Self> {
86        let result = Self::new();
87        result.set_current_value(Self::height_property(), height);
88        result
89    }
90
91    /// The actual calculated height of the row.
92    pub fn actual_height(&self) -> f64 {
93        match self.parent() {
94            Some(parent) => parent.get_final_row_definition_height(self.index()),
95            None => 0.0,
96        }
97    }
98
99    /// The maximum height of the row in DIPs.
100    pub fn max_height(&self) -> f64 {
101        self.get_value(Self::max_height_property())
102    }
103
104    pub fn set_max_height(&self, value: f64) {
105        self.set_value(Self::max_height_property(), value)
106    }
107
108    /// The minimum height of the row in DIPs.
109    pub fn min_height(&self) -> f64 {
110        self.get_value(Self::min_height_property())
111    }
112
113    pub fn set_min_height(&self, value: f64) {
114        self.set_value(Self::min_height_property(), value)
115    }
116
117    /// The height of the row.
118    pub fn height(&self) -> GridLength {
119        self.get_value(Self::height_property())
120    }
121
122    pub fn set_height(&self, value: GridLength) {
123        self.set_value(Self::height_property(), value)
124    }
125}