1use crate::{TypeStyle, generated::*};
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum Direction {
7 #[default]
9 Ltr,
10 Rtl,
12}
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum WidthClass {
17 Compact,
19 Medium,
21 Expanded,
23 Large,
25 ExtraLarge,
27}
28
29impl WidthClass {
30 pub fn from_width(width: f32) -> Self {
32 if !width.is_finite() {
33 return Self::Compact;
34 }
35 if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_EXTRA_LARGE {
36 Self::ExtraLarge
37 } else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_LARGE {
38 Self::Large
39 } else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_EXPANDED {
40 Self::Expanded
41 } else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_MEDIUM {
42 Self::Medium
43 } else {
44 Self::Compact
45 }
46 }
47}
48
49#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
51#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
52pub enum Density {
53 Compact,
55 #[default]
57 Standard,
58 Comfortable,
60}
61
62impl Density {
63 pub fn visual_size(self, baseline: f32) -> f32 {
67 let delta = match self {
68 Self::Compact => MD_SYS_LAYOUT_DENSITY_COMPACT,
69 Self::Standard => MD_SYS_LAYOUT_DENSITY_STANDARD,
70 Self::Comfortable => MD_SYS_LAYOUT_DENSITY_COMFORTABLE,
71 };
72 if !baseline.is_finite() || baseline <= 0.0 {
73 return 0.0;
74 }
75 (baseline + delta).max(0.0)
76 }
77
78 pub fn interactive_size(self, baseline: f32) -> f32 {
80 self.visual_size(baseline)
81 .max(MD_SYS_LAYOUT_MINIMUM_INTERACTIVE_SIZE)
82 }
83}
84
85#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
87#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
88pub struct LayoutPolicy {
89 pub density: Density,
91 pub direction: Direction,
93}
94
95impl LayoutPolicy {
96 pub fn scale_text(style: TypeStyle, font_scale: f32) -> TypeStyle {
99 if !font_scale.is_finite() || font_scale <= 0.0 {
100 return style;
101 }
102 let scaled = TypeStyle {
103 size: style.size * font_scale,
104 line_height: style.line_height * font_scale,
105 tracking: style.tracking * font_scale,
106 ..style
107 };
108 if [scaled.size, scaled.line_height, scaled.tracking]
109 .into_iter()
110 .all(f32::is_finite)
111 && scaled.size > 0.0
112 && scaled.line_height > 0.0
113 {
114 scaled
115 } else {
116 style
117 }
118 }
119
120 pub const fn horizontal<T: Copy>(self, start: T, end: T) -> (T, T) {
122 match self.direction {
123 Direction::Ltr => (start, end),
124 Direction::Rtl => (end, start),
125 }
126 }
127}
128
129pub const fn non_negative_f32(value: f32) -> f32 {
133 if !value.is_finite() || value <= 0.0 {
134 0.0
135 } else {
136 value
137 }
138}
139
140#[derive(Clone, Copy, Debug, Default, PartialEq)]
144#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
145pub struct LogicalEdges {
146 pub top: f32,
148 pub bottom: f32,
150 pub start: f32,
152 pub end: f32,
154}
155
156impl LogicalEdges {
157 pub const ZERO: Self = Self {
159 top: 0.0,
160 bottom: 0.0,
161 start: 0.0,
162 end: 0.0,
163 };
164
165 pub const fn all(value: f32) -> Self {
167 let value = non_negative_f32(value);
168 Self {
169 top: value,
170 bottom: value,
171 start: value,
172 end: value,
173 }
174 }
175
176 pub const fn symmetric(horizontal: f32, vertical: f32) -> Self {
178 let horizontal = non_negative_f32(horizontal);
179 let vertical = non_negative_f32(vertical);
180 Self {
181 top: vertical,
182 bottom: vertical,
183 start: horizontal,
184 end: horizontal,
185 }
186 }
187
188 pub const fn new(top: f32, end: f32, bottom: f32, start: f32) -> Self {
190 Self {
191 top: non_negative_f32(top),
192 bottom: non_negative_f32(bottom),
193 start: non_negative_f32(start),
194 end: non_negative_f32(end),
195 }
196 }
197
198 pub const fn top(mut self, value: f32) -> Self {
200 self.top = non_negative_f32(value);
201 self
202 }
203
204 pub const fn bottom(mut self, value: f32) -> Self {
206 self.bottom = non_negative_f32(value);
207 self
208 }
209
210 pub const fn start(mut self, value: f32) -> Self {
212 self.start = non_negative_f32(value);
213 self
214 }
215
216 pub const fn end(mut self, value: f32) -> Self {
218 self.end = non_negative_f32(value);
219 self
220 }
221
222 pub const fn horizontal(mut self, value: f32) -> Self {
224 let value = non_negative_f32(value);
225 self.start = value;
226 self.end = value;
227 self
228 }
229
230 pub const fn vertical(mut self, value: f32) -> Self {
232 let value = non_negative_f32(value);
233 self.top = value;
234 self.bottom = value;
235 self
236 }
237
238 pub const fn sanitize(self) -> Self {
240 Self {
241 top: non_negative_f32(self.top),
242 bottom: non_negative_f32(self.bottom),
243 start: non_negative_f32(self.start),
244 end: non_negative_f32(self.end),
245 }
246 }
247
248 pub const fn resolve(self, direction: Direction) -> [f32; 4] {
250 let clean = self.sanitize();
251 let (left, right) = match direction {
252 Direction::Ltr => (clean.start, clean.end),
253 Direction::Rtl => (clean.end, clean.start),
254 };
255 [clean.top, right, clean.bottom, left]
256 }
257}