vizia_core/modifiers/layout.rs
1use super::internal;
2use crate::prelude::*;
3
4/// Modifiers for changing the layout properties of a view.
5pub trait LayoutModifiers: internal::Modifiable {
6 modifier!(
7 /// Sets the layout type of the view.
8 ///
9 /// The layout type controls how a parent will position any children which have `Position::Relative`.
10 /// Accepts any value or signal with a type which can be converted into `LayoutType`.
11 ///
12 /// There are three variants:
13 /// - `LayoutType::Row` - Parent will stack its children horizontally.
14 /// - `LayoutType::Column` - (default) Parent will stack its children vertically.
15 /// - `LayoutType::Grid` - The position of children is determine by the grid properties.
16 ///
17 /// # Example
18 /// ```
19 /// # use vizia_core::prelude::*;
20 /// # let cx = &mut Context::default();
21 /// #[derive(Model, Setter)]
22 /// pub struct AppData {
23 /// layout_type: LayoutType,
24 /// }
25 ///
26 /// # AppData {
27 /// # layout_type: LayoutType::Row,
28 /// # }.build(cx);
29 ///
30 /// Element::new(cx).layout_type(LayoutType::Row); // Value of type `LayoutType`.
31 /// Element::new(cx).layout_type(AppData::layout_type); // signal of type type `LayoutType`.
32 /// ```
33 layout_type,
34 LayoutType,
35 SystemFlags::RELAYOUT
36 );
37
38 modifier!(
39 /// Sets the position type of the view.
40 ///
41 /// The position type determines how a child will be positioned within a parent.
42 ///
43 /// - `Position::Relative` - The child will be positioned relative to its siblings in a stack
44 /// (if parent layout type is `Row` or `Column`), or relative to its grid position (if parent layout type is `Grid`).
45 /// - `Position::Absolute` - The child will be positioned relative to the top-left corner of its parents bounding box
46 /// and will ignore its siblings or grid position. This is approximately equivalent to absolute positioning.
47 ///
48 /// # Example
49 /// ```
50 /// # use vizia_core::prelude::*;
51 /// # let cx = &mut Context::default();
52 /// Element::new(cx).position_type(PositionType::Absolute);
53 /// ```
54 position_type,
55 PositionType,
56 SystemFlags::RELAYOUT
57 );
58
59 modifier!(
60 /// Sets the space on the left side of the view.
61 ///
62 /// The left space, along with the right space, determines the horizontal position of a view.
63 ///
64 /// - `Units::Pixels(...)` - The left space will be a fixed number of points. This will scale with the DPI of the target display.
65 /// - `Units::Percentage(...)` - The left space will be a proportion of the parent width.
66 /// - `Units::Stretch(...)` - The left space will be a ratio of the remaining free space, see [`Units`](crate::prelude::Units).
67 /// - `Units::Auto` - The left space will be determined by the parent `padding-left`, see [`padding_left`](crate::prelude::LayoutModifiers::left).
68 ///
69 /// # Example
70 /// ```
71 /// # use vizia_core::prelude::*;
72 /// # let cx = &mut Context::default();
73 /// Element::new(cx).left(Units::Pixels(100.0));
74 /// ```
75 left,
76 Units,
77 SystemFlags::RELAYOUT
78 );
79
80 modifier!(
81 /// Sets the space on the right side of the view.
82 ///
83 /// The right space, along with the left space, determines the horizontal position of a view.
84 ///
85 /// - `Units::Pixels(...)` - The right space will be a fixed number of points. This will scale with the DPI of the target display.
86 /// - `Units::Percentage(...)` - The right space will be a proportion of the parent width.
87 /// - `Units::Stretch(...)` - The right space will be a ratio of the remaining free space, see [`Units`](crate::prelude::Units).
88 /// - `Units::Auto` - The right space will be determined by the parent `padding-left`, see [`padding_left`](crate::prelude::LayoutModifiers::left).
89 ///
90 /// # Example
91 /// ```
92 /// # use vizia_core::prelude::*;
93 /// # let cx = &mut Context::default();
94 /// Element::new(cx).right(Units::Pixels(100.0));
95 /// ```
96 right,
97 Units,
98 SystemFlags::RELAYOUT
99 );
100
101 modifier!(
102 /// Sets the space on the top side of the view.
103 ///
104 /// The top space, along with the bottom space, determines the vertical position of a view.
105 ///
106 /// - `Units::Pixels(...)` - The top space will be a fixed number of points. This will scale with the DPI of the target display.
107 /// - `Units::Percentage(...)` - The top space will be a proportion of the parent width.
108 /// - `Units::Stretch(...)` - The top space will be a ratio of the remaining free space, see [`Units`](crate::prelude::Units).
109 /// - `Units::Auto` - The top space will be determined by the parent `padding-left`, see [`padding_left`](crate::prelude::LayoutModifiers::left).
110 ///
111 /// # Example
112 /// ```
113 /// # use vizia_core::prelude::*;
114 /// # let cx = &mut Context::default();
115 /// Element::new(cx).top(Units::Pixels(100.0));
116 /// ```
117 top,
118 Units,
119 SystemFlags::RELAYOUT
120 );
121
122 modifier!(
123 /// Sets the space on the bottom side of the view.
124 ///
125 /// The bottom space, along with the top space, determines the vertical position of a view.
126 ///
127 /// - `Units::Pixels(...)` - The bottom space will be a fixed number of points. This will scale with the DPI of the target display.
128 /// - `Units::Percentage(...)` - The bottom space will be a proportion of the parent width.
129 /// - `Units::Stretch(...)` - The bottom space will be a ratio of the remaining free space, see [`Units`](crate::prelude::Units).
130 /// - `Units::Auto` - The bottom space will be determined by the parent `padding-left`, see [`padding_left`](crate::prelude::LayoutModifiers::left).
131 ///
132 /// # Example
133 /// ```
134 /// # use vizia_core::prelude::*;
135 /// # let cx = &mut Context::default();
136 /// Element::new(cx).bottom(Units::Pixels(100.0));
137 /// ```
138 bottom,
139 Units,
140 SystemFlags::RELAYOUT
141 );
142
143 /// Sets the space for all sides of the view.
144 fn space<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
145 let entity = self.entity();
146 let current = self.current();
147 self.context().with_current(current, |cx| {
148 value.set_or_bind(cx, move |cx, v| {
149 let value = v.get_value(cx).into();
150 cx.style.left.insert(entity, value);
151 cx.style.right.insert(entity, value);
152 cx.style.top.insert(entity, value);
153 cx.style.bottom.insert(entity, value);
154
155 cx.style.needs_relayout(entity);
156 });
157 });
158
159 self
160 }
161
162 modifier!(
163 /// Sets the width of the view.
164 width,
165 Units,
166 SystemFlags::RELAYOUT
167 );
168
169 modifier!(
170 /// Sets the height of the view.
171 height,
172 Units,
173 SystemFlags::RELAYOUT
174 );
175
176 modifier!(
177 /// Sets the preferred aspect ratio of the view.
178 aspect_ratio,
179 AspectRatio,
180 SystemFlags::RELAYOUT
181 );
182
183 /// Sets the width and height of the view.
184 fn size<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
185 let entity = self.entity();
186 let current = self.current();
187 self.context().with_current(current, |cx| {
188 value.set_or_bind(cx, move |cx, v| {
189 let value = v.get_value(cx).into();
190 cx.style.width.insert(entity, value);
191 cx.style.height.insert(entity, value);
192
193 cx.style.needs_relayout(entity);
194 });
195 });
196
197 self
198 }
199
200 modifier!(
201 /// Sets the space between the left side of the view and the left side of its children.
202 ///
203 /// Applies only to child views which have a `left` property set to `Auto`.
204 padding_left,
205 Units,
206 SystemFlags::RELAYOUT
207 );
208
209 modifier!(
210 /// Sets the space between the right side of the view and the right side of its children.
211 ///
212 /// Applies only to child views which have a `right` property set to `Auto`.
213 padding_right,
214 Units,
215 SystemFlags::RELAYOUT
216 );
217
218 modifier!(
219 /// Sets the space between the top side of the view and the top side of its children.
220 ///
221 /// Applies only to child views which have a `top` property set to `Auto`.
222 padding_top,
223 Units,
224 SystemFlags::RELAYOUT
225 );
226
227 modifier!(
228 /// Sets the space between the bottom side of the view and the bottom side of its children.
229 ///
230 /// Applies only to child views which have a `bottom` property set to `Auto`.
231 padding_bottom,
232 Units,
233 SystemFlags::RELAYOUT
234 );
235
236 /// Set the alignment of the view.
237 fn alignment<U: Into<Alignment>>(mut self, value: impl Res<U>) -> Self {
238 let entity = self.entity();
239 value.set_or_bind(self.context(), move |cx, v| {
240 cx.style.alignment.insert(entity, v.get_value(cx).into());
241 cx.style.system_flags |= SystemFlags::RELAYOUT;
242 cx.set_system_flags(entity, SystemFlags::RELAYOUT);
243
244 // A paragraph takes its text align from the alignment of its own view. Reflowing a
245 // view without text would only rebuild the paragraphs below it, which don't read it.
246 if cx.style.text.contains(entity) {
247 cx.style.needs_text_update(entity);
248 }
249 });
250
251 self
252 }
253
254 modifier!(
255 /// Set the text direction of the view.
256 direction,
257 Direction,
258 SystemFlags::RELAYOUT | SystemFlags::REFLOW
259 );
260
261 modifier!(
262 /// Set the wrapping behavior of the view.
263 wrap,
264 LayoutWrap,
265 SystemFlags::RELAYOUT
266 );
267
268 /// Sets the space between the vew and its children.
269 ///
270 /// The child_space works by overriding the `Auto` space properties of its children.
271 fn padding<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
272 let entity = self.entity();
273 let current = self.current();
274 self.context().with_current(current, |cx| {
275 value.set_or_bind(cx, move |cx, v| {
276 let value = v.get_value(cx).into();
277 cx.style.padding_left.insert(entity, value);
278 cx.style.padding_right.insert(entity, value);
279 cx.style.padding_top.insert(entity, value);
280 cx.style.padding_bottom.insert(entity, value);
281
282 cx.style.needs_relayout(entity);
283 });
284 });
285
286 self
287 }
288
289 modifier!(
290 /// Sets the space between the views children in the vertical direction.
291 vertical_gap,
292 Units,
293 SystemFlags::RELAYOUT
294 );
295
296 modifier!(
297 /// Sets the space between the views children in the horizontal direction.
298 horizontal_gap,
299 Units,
300 SystemFlags::RELAYOUT
301 );
302
303 /// Sets the space between the views children in both the horizontal and vertical directions.
304 fn gap<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
305 let entity = self.entity();
306 let current = self.current();
307 self.context().with_current(current, |cx| {
308 value.set_or_bind(cx, move |cx, v| {
309 let value = v.get_value(cx).into();
310 cx.style.horizontal_gap.insert(entity, value);
311 cx.style.vertical_gap.insert(entity, value);
312
313 cx.style.needs_relayout(entity);
314 });
315 });
316
317 self
318 }
319
320 modifier!(
321 /// Sets the minimum width of the view.
322 min_width,
323 Units,
324 SystemFlags::RELAYOUT
325 );
326
327 modifier!(
328 /// Sets the minimum height of the view.
329 min_height,
330 Units,
331 SystemFlags::RELAYOUT
332 );
333
334 /// Sets the minimum width and minimum height of the view.
335 fn min_size<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
336 let entity = self.entity();
337 let current = self.current();
338 self.context().with_current(current, |cx| {
339 value.set_or_bind(cx, move |cx, v| {
340 let value = v.get_value(cx).into();
341 cx.style.min_width.insert(entity, value);
342 cx.style.min_height.insert(entity, value);
343
344 cx.style.needs_relayout(entity);
345 });
346 });
347
348 self
349 }
350
351 modifier!(
352 /// Sets the maximum width of the view.
353 max_width,
354 Units,
355 SystemFlags::RELAYOUT
356 );
357
358 modifier!(
359 /// Sets the maximum height of the view.
360 max_height,
361 Units,
362 SystemFlags::RELAYOUT
363 );
364
365 /// Sets the maximum width and maximum height of the view.
366 fn max_size<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
367 let entity = self.entity();
368 let current = self.current();
369 self.context().with_current(current, |cx| {
370 value.set_or_bind(cx, move |cx, v| {
371 let value = v.get_value(cx).into();
372 cx.style.max_width.insert(entity, value);
373 cx.style.max_height.insert(entity, value);
374
375 cx.style.needs_relayout(entity);
376 });
377 });
378
379 self
380 }
381
382 modifier!(
383 /// Sets the minimum horizontal space between the children of the view.
384 min_horizontal_gap,
385 Units,
386 SystemFlags::RELAYOUT
387 );
388
389 modifier!(
390 /// Sets the minimum vertical space between the children of the view.
391 min_vertical_gap,
392 Units,
393 SystemFlags::RELAYOUT
394 );
395
396 /// Sets the minimum horizontal and minimum vertical space between the children of the view.
397 fn min_gap<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
398 let entity = self.entity();
399 let current = self.current();
400 self.context().with_current(current, |cx| {
401 value.set_or_bind(cx, move |cx, v| {
402 let value = v.get_value(cx).into();
403 cx.style.min_horizontal_gap.insert(entity, value);
404 cx.style.min_vertical_gap.insert(entity, value);
405
406 cx.style.needs_relayout(entity);
407 });
408 });
409
410 self
411 }
412
413 modifier!(
414 /// Sets the maximum horizontal space between the children of the view.
415 max_horizontal_gap,
416 Units,
417 SystemFlags::RELAYOUT
418 );
419
420 modifier!(
421 /// Sets the maximum vertical space between the children of the view.
422 max_vertical_gap,
423 Units,
424 SystemFlags::RELAYOUT
425 );
426
427 /// Sets the maximum horizontal and maximum vertical space between the children of the view.
428 fn max_gap<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
429 let entity = self.entity();
430 let current = self.current();
431 self.context().with_current(current, |cx| {
432 value.set_or_bind(cx, move |cx, v| {
433 let value = v.get_value(cx).into();
434 cx.style.max_horizontal_gap.insert(entity, value);
435 cx.style.max_vertical_gap.insert(entity, value);
436
437 cx.style.needs_relayout(entity);
438 });
439 });
440
441 self
442 }
443
444 modifier!(
445 /// Sets the grid columns for a grid layout.
446 grid_columns,
447 Vec<Units>,
448 SystemFlags::RELAYOUT
449 );
450
451 modifier!(
452 /// Sets the grid rows for a grid layout.
453 grid_rows,
454 Vec<Units>,
455 SystemFlags::RELAYOUT
456 );
457
458 fn column_start(mut self, value: impl Res<usize>) -> Self {
459 let entity = self.entity();
460 let current = self.current();
461 self.context().with_current(current, |cx| {
462 value.set_or_bind(cx, move |cx, v| {
463 let value = v.get_value(cx);
464 cx.style.column_start.insert(entity, value);
465
466 cx.style.needs_relayout(entity);
467 });
468 });
469
470 self
471 }
472
473 fn column_span(mut self, value: impl Res<usize>) -> Self {
474 let entity = self.entity();
475 let current = self.current();
476 self.context().with_current(current, |cx| {
477 value.set_or_bind(cx, move |cx, v| {
478 let value = v.get_value(cx);
479 cx.style.column_span.insert(entity, value);
480
481 cx.style.needs_relayout(entity);
482 });
483 });
484
485 self
486 }
487
488 fn row_start(mut self, value: impl Res<usize>) -> Self {
489 let entity = self.entity();
490 let current = self.current();
491 self.context().with_current(current, |cx| {
492 value.set_or_bind(cx, move |cx, v| {
493 let value = v.get_value(cx);
494 cx.style.row_start.insert(entity, value);
495
496 cx.style.needs_relayout(entity);
497 });
498 });
499
500 self
501 }
502
503 fn row_span(mut self, value: impl Res<usize>) -> Self {
504 let entity = self.entity();
505 let current = self.current();
506 self.context().with_current(current, |cx| {
507 value.set_or_bind(cx, move |cx, v| {
508 let value = v.get_value(cx);
509 cx.style.row_span.insert(entity, value);
510
511 cx.style.needs_relayout(entity);
512 });
513 });
514
515 self
516 }
517}
518
519impl<V: View> LayoutModifiers for Handle<'_, V> {}