Skip to main content

waft_winui/controls/grid/
definition.rs

1//! `RowDefinition`, `ColumnDefinition` and `GridLength` (`dxaml/xcp/core/inc/GridDefinitions.h`, `GridLength.cpp`): what a caller declares, and the per-definition working state `CDefinitionBase` keeps between measure and arrange.
2
3/// XAML `GridUnitType`.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum GridUnitType {
6    /// Sized to the content.
7    Auto,
8    /// A fixed length.
9    Pixel,
10    /// A weighted share of the remaining space.
11    Star,
12}
13
14/// XAML `GridLength`: `Auto`, a pixel value, or `n*`.
15#[derive(Clone, Copy, Debug, PartialEq)]
16pub struct GridLength {
17    pub value: f64,
18    pub unit: GridUnitType,
19}
20
21impl GridLength {
22    /// XAML `Auto`. The value is `XGRIDLENGTH::Default()`, 1.
23    pub const AUTO: GridLength = GridLength {
24        value: 1.0,
25        unit: GridUnitType::Auto,
26    };
27
28    /// XAML `*`.
29    pub const STAR: GridLength = GridLength::star(1.0);
30
31    /// XAML `Width="40"`.
32    pub const fn pixel(value: f64) -> GridLength {
33        GridLength {
34            value,
35            unit: GridUnitType::Pixel,
36        }
37    }
38
39    /// XAML `Width="2*"`.
40    pub const fn star(value: f64) -> GridLength {
41        GridLength {
42            value,
43            unit: GridUnitType::Star,
44        }
45    }
46}
47
48/// XAML `RowDefinition`: `Height` (default `*`), `MinHeight`, `MaxHeight`.
49#[derive(Clone, Copy, Debug, PartialEq)]
50pub struct RowDefinition {
51    pub height: GridLength,
52    pub min_height: f64,
53    pub max_height: f64,
54}
55
56impl RowDefinition {
57    pub const fn new(height: GridLength) -> RowDefinition {
58        RowDefinition {
59            height,
60            min_height: 0.0,
61            max_height: f64::INFINITY,
62        }
63    }
64
65    /// XAML `MinHeight`.
66    pub const fn min_height(mut self, min_height: f64) -> RowDefinition {
67        self.min_height = min_height;
68        self
69    }
70
71    /// XAML `MaxHeight`.
72    pub const fn max_height(mut self, max_height: f64) -> RowDefinition {
73        self.max_height = max_height;
74        self
75    }
76
77    pub(crate) fn definition(&self) -> Definition {
78        Definition::new(self.height, self.min_height, self.max_height)
79    }
80}
81
82impl Default for RowDefinition {
83    fn default() -> RowDefinition {
84        RowDefinition::new(GridLength::STAR)
85    }
86}
87
88/// XAML `ColumnDefinition`: `Width` (default `*`), `MinWidth`, `MaxWidth`.
89#[derive(Clone, Copy, Debug, PartialEq)]
90pub struct ColumnDefinition {
91    pub width: GridLength,
92    pub min_width: f64,
93    pub max_width: f64,
94}
95
96impl ColumnDefinition {
97    pub const fn new(width: GridLength) -> ColumnDefinition {
98        ColumnDefinition {
99            width,
100            min_width: 0.0,
101            max_width: f64::INFINITY,
102        }
103    }
104
105    /// XAML `MinWidth`.
106    pub const fn min_width(mut self, min_width: f64) -> ColumnDefinition {
107        self.min_width = min_width;
108        self
109    }
110
111    /// XAML `MaxWidth`.
112    pub const fn max_width(mut self, max_width: f64) -> ColumnDefinition {
113        self.max_width = max_width;
114        self
115    }
116
117    pub(crate) fn definition(&self) -> Definition {
118        Definition::new(self.width, self.min_width, self.max_width)
119    }
120}
121
122impl Default for ColumnDefinition {
123    fn default() -> ColumnDefinition {
124        ColumnDefinition::new(GridLength::STAR)
125    }
126}
127
128/// `CDefinitionBase`: the user values of a row or column and the sizes the layout passes compute for it.
129#[derive(Clone, Copy, Debug, PartialEq)]
130pub(crate) struct Definition {
131    pub user_size: GridLength,
132    pub user_min_size: f64,
133    pub user_max_size: f64,
134    pub effective_min_size: f64,
135    pub measure_arrange_size: f64,
136    pub size_cache: f64,
137    pub final_offset: f64,
138    pub effective_unit_type: GridUnitType,
139}
140
141impl Definition {
142    pub fn new(user_size: GridLength, user_min_size: f64, user_max_size: f64) -> Definition {
143        Definition {
144            user_size,
145            user_min_size,
146            user_max_size,
147            effective_min_size: 0.0,
148            measure_arrange_size: 0.0,
149            size_cache: 0.0,
150            final_offset: 0.0,
151            effective_unit_type: GridUnitType::Auto,
152        }
153    }
154
155    pub fn user_size_type(&self) -> GridUnitType {
156        self.user_size.unit
157    }
158
159    pub fn is_auto(&self) -> bool {
160        self.user_size.unit == GridUnitType::Auto
161    }
162
163    /// `GetPreferredSize`.
164    pub fn preferred_size(&self) -> f64 {
165        if self.effective_unit_type != GridUnitType::Auto
166            && self.effective_min_size < self.measure_arrange_size
167        {
168            self.measure_arrange_size
169        } else {
170            self.effective_min_size
171        }
172    }
173
174    /// `UpdateEffectiveMinSize`.
175    pub fn update_effective_min_size(&mut self, new_value: f64) {
176        self.effective_min_size = self.effective_min_size.max(new_value);
177    }
178}