1#![allow(clippy::new_ret_no_self)]
2
3use crate::artprov::IconRole;
4pub use crate::layout::AlignSelf;
5pub use crate::layout::{
6 AlignContent, AlignItems, Dimension, Display, FlexDirection, FlexWrap, JustifyContent,
7 LengthPercentage, LengthPercentageAuto, Rect, Size, Style,
8};
9use crate::{
10 Backend, Color, ContainerDirection, ContainerWrap, GuiResult, Ui, WidgetId, WidgetKind,
11};
12
13pub struct Node<'a> {
15 pub kind: WidgetKind,
16 pub text: Option<String>,
17 pub icon: Option<IconRole>,
18 pub children: Vec<Node<'a>>,
19 pub id_out: Option<&'a mut WidgetId>,
20 pub bg_color: Option<Color>,
21 pub style: Style,
22 pub direction: Option<ContainerDirection>,
23 pub wrap: Option<ContainerWrap>,
24 pub gap: Option<i32>,
25 pub padding: Option<i32>,
26 pub align_self: Option<AlignSelf>,
27 pub min_width: Option<f32>,
28 pub max_width: Option<f32>,
29 pub min_height: Option<f32>,
30 pub max_height: Option<f32>,
31 pub flex_shrink: Option<f32>,
32 pub flex_basis: Option<Dimension>,
33 pub split_ratio: Option<u8>,
34}
35
36impl<'a> Node<'a> {
37 pub fn new(kind: WidgetKind) -> Self {
38 let mut style = Style::default();
39 if matches!(kind, WidgetKind::Container | WidgetKind::Window) {
40 style.align_items = Some(AlignItems::Stretch);
41 }
42 Self {
43 kind,
44 text: None,
45 icon: None,
46 children: Vec::new(),
47 id_out: None,
48 bg_color: None,
49 style,
50 direction: None,
51 wrap: None,
52 gap: None,
53 padding: None,
54 align_self: None,
55 min_width: None,
56 max_width: None,
57 min_height: None,
58 max_height: None,
59 flex_shrink: None,
60 flex_basis: None,
61 split_ratio: None,
62 }
63 }
64
65 pub fn split_ratio(mut self, ratio: u8) -> Self {
67 self.split_ratio = Some(ratio.min(100));
68 self
69 }
70
71 pub fn text(mut self, text: impl Into<String>) -> Self {
72 self.text = Some(text.into());
73 self
74 }
75
76 pub fn icon(mut self, icon: IconRole) -> Self {
78 self.icon = Some(icon);
79 self
80 }
81
82 pub fn optional_icon(mut self, icon: Option<IconRole>) -> Self {
84 self.icon = icon;
85 self
86 }
87
88 pub fn child(mut self, child: Node<'a>) -> Self {
89 self.children.push(child);
90 self
91 }
92
93 pub fn bind_id(mut self, id_out: &'a mut WidgetId) -> Self {
94 self.id_out = Some(id_out);
95 self
96 }
97
98 pub fn bind(self, id_out: &'a mut WidgetId) -> Self {
100 self.bind_id(id_out)
101 }
102
103 pub fn mount<B: Backend>(
105 &mut self,
106 ui: &mut Ui<B>,
107 parent: Option<WidgetId>,
108 ) -> GuiResult<WidgetId> {
109 use crate::declarative::DeclarativeUi;
110 let id = ui.render(self)?;
111 if let Some(p) = parent {
112 ui.attach(p, id)?;
113 }
114 Ok(id)
115 }
116
117 pub fn bg_color(mut self, color: Color) -> Self {
118 self.bg_color = Some(color);
119 self
120 }
121
122 pub fn size(mut self, width: f32, height: f32) -> Self {
128 self.style.size.width = Dimension::length(width);
129 self.style.size.height = Dimension::length(height);
130 self
131 }
132
133 pub fn flex_direction(mut self, direction: FlexDirection) -> Self {
134 self.style.flex_direction = direction;
135 if direction == FlexDirection::Row {
136 self.direction = Some(ContainerDirection::Horizontal);
137 } else if direction == FlexDirection::Column {
138 self.direction = Some(ContainerDirection::Vertical);
139 }
140 if self.gap.is_some() {
141 let flow_gap = match direction {
142 FlexDirection::Column | FlexDirection::ColumnReverse => {
143 self.style.gap.height.to_i32_pixels()
144 }
145 FlexDirection::Row | FlexDirection::RowReverse => {
146 self.style.gap.width.to_i32_pixels()
147 }
148 };
149 self.gap = Some(flow_gap);
150 }
151 self
152 }
153
154 pub fn wrap(mut self, wrap: ContainerWrap) -> Self {
156 self.wrap = Some(wrap);
157 self.style.flex_wrap = match wrap {
158 ContainerWrap::NoWrap => FlexWrap::NoWrap,
159 ContainerWrap::Wrap => FlexWrap::Wrap,
160 ContainerWrap::WrapReverse => FlexWrap::WrapReverse,
161 };
162 self
163 }
164
165 pub fn flow_gap(self, gap: i32) -> Self {
167 let g = gap as f32;
168 self.gap(g, g)
169 }
170
171 pub fn flex_grow(mut self, grow: f32) -> Self {
172 self.style.flex_grow = grow;
173 self
174 }
175
176 pub fn flex_wrap(mut self, wrap: FlexWrap) -> Self {
179 self.style.flex_wrap = wrap;
180 self
181 }
182
183 pub fn align_items(mut self, align: AlignItems) -> Self {
185 self.style.align_items = Some(align);
186 self
187 }
188
189 pub fn justify_content(mut self, justify: JustifyContent) -> Self {
191 self.style.justify_content = Some(justify);
192 self
193 }
194
195 pub fn align_content(mut self, align: AlignContent) -> Self {
197 self.style.align_content = Some(align);
198 self
199 }
200
201 pub fn percent_size(mut self, width_pct: f32, height_pct: f32) -> Self {
202 self.style.size.width = Dimension::percent(width_pct);
203 self.style.size.height = Dimension::percent(height_pct);
204 self
205 }
206
207 pub fn gap(mut self, width: f32, height: f32) -> Self {
215 let flow_gap = match self.direction {
216 Some(ContainerDirection::Vertical) => height,
217 Some(ContainerDirection::Horizontal) => width,
218 None => {
219 if self.style.flex_direction == FlexDirection::Column {
220 height
221 } else if width > 0.0 {
222 width
223 } else {
224 height
225 }
226 }
227 };
228 self.gap = Some(flow_gap.max(0.0).round() as i32);
229 self.style.gap = Size {
230 width: LengthPercentage::length(width),
231 height: LengthPercentage::length(height),
232 };
233 self
234 }
235
236 pub fn padding(mut self, val: f32) -> Self {
242 self.padding = Some(val.max(0.0).round() as i32);
243 self.style.padding = Rect {
244 left: LengthPercentage::length(val),
245 right: LengthPercentage::length(val),
246 top: LengthPercentage::length(val),
247 bottom: LengthPercentage::length(val),
248 };
249 self
250 }
251
252 pub fn margin(mut self, val: f32) -> Self {
253 self.style.margin = Rect {
254 left: LengthPercentageAuto::length(val),
255 right: LengthPercentageAuto::length(val),
256 top: LengthPercentageAuto::length(val),
257 bottom: LengthPercentageAuto::length(val),
258 };
259 self
260 }
261
262 pub fn align_self(mut self, align: AlignSelf) -> Self {
264 self.align_self = Some(align);
265 self.style.align_self = Some(align);
266 self
267 }
268
269 pub fn min_width(mut self, width: f32) -> Self {
271 self.min_width = Some(width);
272 self.style.min_size.width = Dimension::Length(width);
273 self
274 }
275
276 pub fn max_width(mut self, width: f32) -> Self {
278 self.max_width = Some(width);
279 self.style.max_size.width = Dimension::Length(width);
280 self
281 }
282
283 pub fn min_height(mut self, height: f32) -> Self {
285 self.min_height = Some(height);
286 self.style.min_size.height = Dimension::Length(height);
287 self
288 }
289
290 pub fn max_height(mut self, height: f32) -> Self {
292 self.max_height = Some(height);
293 self.style.max_size.height = Dimension::Length(height);
294 self
295 }
296
297 pub fn min_size(mut self, width: f32, height: f32) -> Self {
299 self.min_width = Some(width);
300 self.min_height = Some(height);
301 self.style.min_size.width = Dimension::Length(width);
302 self.style.min_size.height = Dimension::Length(height);
303 self
304 }
305
306 pub fn max_size(mut self, width: f32, height: f32) -> Self {
308 self.max_width = Some(width);
309 self.max_height = Some(height);
310 self.style.max_size.width = Dimension::Length(width);
311 self.style.max_size.height = Dimension::Length(height);
312 self
313 }
314
315 pub fn flex_shrink(mut self, shrink: f32) -> Self {
317 self.flex_shrink = Some(shrink);
318 self.style.flex_shrink = shrink;
319 self
320 }
321
322 pub fn flex_basis(mut self, basis: Dimension) -> Self {
324 self.flex_basis = Some(basis);
325 self.style.flex_basis = basis;
326 self
327 }
328
329 pub fn display(mut self, display: Display) -> Self {
331 self.style.display = display;
332 self
333 }
334
335 pub fn spacer() -> Node<'static> {
338 Spacer::new()
339 }
340
341 pub fn stretch_spacer() -> Node<'static> {
343 Spacer::new()
344 }
345
346 pub fn grid() -> Node<'static> {
349 Grid::new()
350 }
351}
352
353pub struct Window;
355impl Window {
356 pub fn new<'a>(title: impl Into<String>) -> Node<'a> {
357 Node::new(WidgetKind::Window).text(title)
358 }
359}
360
361pub struct Container;
362impl Container {
363 pub fn vertical<'a>() -> Node<'a> {
364 Node::new(WidgetKind::Container).flex_direction(FlexDirection::Column)
365 }
366 pub fn horizontal<'a>() -> Node<'a> {
367 Node::new(WidgetKind::Container).flex_direction(FlexDirection::Row)
368 }
369 pub fn flow<'a>() -> Node<'a> {
371 flow_container()
372 }
373 pub fn grid<'a>() -> Node<'a> {
376 Grid::new()
377 }
378}
379
380pub fn flow_container<'a>() -> Node<'a> {
382 Node::new(WidgetKind::Container)
383 .flex_direction(FlexDirection::Row)
384 .wrap(ContainerWrap::Wrap)
385}
386
387pub struct Spacer;
389impl Spacer {
390 pub fn new<'a>() -> Node<'a> {
393 Node::new(WidgetKind::Container).flex_grow(1.0)
394 }
395
396 pub fn fixed<'a>(width: f32, height: f32) -> Node<'a> {
399 Node::new(WidgetKind::Container).size(width, height)
400 }
401}
402
403pub struct Grid;
405impl Grid {
406 pub fn new<'a>() -> Node<'a> {
408 let mut node = Node::new(WidgetKind::Container);
409 node.style.display = Display::Grid;
410 node
411 }
412}
413
414pub struct Label;
415impl Label {
416 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
417 Node::new(WidgetKind::Label).text(text)
418 }
419}
420
421pub struct Button;
422impl Button {
423 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
424 Node::new(WidgetKind::Button).text(text)
425 }
426
427 pub fn with_icon<'a>(text: impl Into<String>, icon: IconRole) -> Node<'a> {
428 Node::new(WidgetKind::Button).text(text).icon(icon)
429 }
430}
431
432pub trait DeclarativeUi {
450 fn render<'a>(&mut self, node: &mut Node<'a>) -> GuiResult<WidgetId>;
454}
455
456fn native_window_size(style: &Style) -> GuiResult<Option<(i32, i32)>> {
457 let dimension = |value: Dimension| match value {
458 Dimension::Length(length) => {
459 let rounded = f64::from(length.round());
460 if !rounded.is_finite() || rounded < 1.0 || rounded > f64::from(i32::MAX) {
461 return Err(crate::GuiError::Backend(
462 "Window length dimensions must round to positive i32 values".to_owned(),
463 ));
464 }
465 Ok(Some(rounded as i32))
466 }
467 _ => Ok(None),
468 };
469 Ok(dimension(style.size.width)?.zip(dimension(style.size.height)?))
470}
471
472impl<B: Backend> DeclarativeUi for Ui<B> {
473 fn render<'a>(&mut self, node: &mut Node<'a>) -> GuiResult<WidgetId> {
474 let window_size = if node.kind == WidgetKind::Window {
475 native_window_size(&node.style)?
476 } else {
477 None
478 };
479 let id = if let Some(ratio) = node.split_ratio {
480 let text = node.text.clone();
481 self.create_with(node.kind, |spec| {
482 spec.text = text;
483 spec.split_ratio = Some(ratio);
484 })?
485 } else {
486 self.create(node.kind, node.text.as_deref())?
487 };
488
489 if let Some((width, height)) = window_size {
490 self.set_size(id, width, height)?;
491 }
492
493 if let Some(id_out) = node.id_out.take() {
494 *id_out = id;
495 }
496
497 if let Some(color) = node.bg_color {
498 let _ = self.set_background_color(id, color);
499 }
500
501 if let Some(icon) = node.icon {
502 let _ = self.set_icon(id, Some(icon));
503 }
504
505 if let Some(dir) = node.direction {
506 let _ = self.set_container_direction(id, dir);
507 }
508
509 if let Some(wrap) = node.wrap {
510 let _ = self.set_container_wrap(id, wrap);
511 }
512
513 #[cfg(feature = "widgets-containers")]
520 let is_flow_container = matches!(node.kind, WidgetKind::Container | WidgetKind::GroupBox);
521 #[cfg(not(feature = "widgets-containers"))]
522 let is_flow_container = matches!(node.kind, WidgetKind::Container);
523 if is_flow_container {
524 let flow_gap = match node.direction {
525 Some(ContainerDirection::Vertical) => node.gap.or_else(|| {
526 let h = node.style.gap.height.to_i32_pixels();
527 if h > 0 {
528 Some(h)
529 } else {
530 None
531 }
532 }),
533 Some(ContainerDirection::Horizontal) => node.gap.or_else(|| {
534 let w = node.style.gap.width.to_i32_pixels();
535 if w > 0 {
536 Some(w)
537 } else {
538 None
539 }
540 }),
541 None => match node.style.flex_direction {
542 FlexDirection::Column | FlexDirection::ColumnReverse => {
543 node.gap.or_else(|| {
544 let h = node.style.gap.height.to_i32_pixels();
545 if h > 0 {
546 Some(h)
547 } else {
548 None
549 }
550 })
551 }
552 _ => node.gap.or_else(|| {
553 let w = node.style.gap.width.to_i32_pixels();
554 if w > 0 {
555 Some(w)
556 } else {
557 None
558 }
559 }),
560 },
561 };
562
563 let flow_padding = node.padding.or_else(|| {
564 let top = node.style.padding.top.to_i32_pixels();
565 let left = node.style.padding.left.to_i32_pixels();
566 if top > 0 {
567 Some(top)
568 } else if left > 0 {
569 Some(left)
570 } else {
571 None
572 }
573 });
574
575 if let Some(gap) = flow_gap {
576 let _ = self.set_container_gap(id, gap);
577 }
578 if let Some(pad) = flow_padding {
579 let _ = self.set_container_padding(id, pad);
580 }
581 }
582
583 if let Some(align) = node.align_self {
585 node.style.align_self = Some(align);
586 }
587 if let Some(mw) = node.min_width {
588 node.style.min_size.width = Dimension::length(mw);
589 }
590 if let Some(mw) = node.max_width {
591 node.style.max_size.width = Dimension::length(mw);
592 }
593 if let Some(mh) = node.min_height {
594 node.style.min_size.height = Dimension::length(mh);
595 }
596 if let Some(mh) = node.max_height {
597 node.style.max_size.height = Dimension::length(mh);
598 }
599 if let Some(shrink) = node.flex_shrink {
600 node.style.flex_shrink = shrink;
601 }
602 if let Some(basis) = node.flex_basis {
603 node.style.flex_basis = basis;
604 }
605
606 if node.min_width.is_some() || node.min_height.is_some() {
608 let min_w = node
609 .min_width
610 .map(|w| w.max(0.0).round() as i32)
611 .unwrap_or(0);
612 let min_h = node
613 .min_height
614 .map(|h| h.max(0.0).round() as i32)
615 .unwrap_or(0);
616 let _ = self.set_min_size(id, min_w, min_h);
617 }
618
619 let _ = self.set_layout_style(id, node.style.clone());
621
622 let auto_sized = node.style.size.width.is_auto() && node.style.size.height.is_auto();
625 #[cfg(feature = "widgets-containers")]
626 let is_flow_container = matches!(node.kind, WidgetKind::Container | WidgetKind::GroupBox);
627 #[cfg(not(feature = "widgets-containers"))]
628 let is_flow_container = matches!(node.kind, WidgetKind::Container);
629 if auto_sized && is_flow_container {
630 let style = &node.style;
631 let padding = [
632 style.padding.top.to_i32_pixels(),
633 style.padding.right.to_i32_pixels(),
634 style.padding.bottom.to_i32_pixels(),
635 style.padding.left.to_i32_pixels(),
636 ];
637 let is_asymmetric =
638 padding[0] != padding[1] || padding[0] != padding[2] || padding[0] != padding[3];
639 if is_asymmetric && padding != [0; 4] {
640 let _ = self.backend_mut().apply_flow_padding(id, padding);
641 }
642 }
643
644 for child_node in &mut node.children {
645 let child_id = self.render(child_node)?;
646 self.attach(id, child_id)?;
647 }
648 Ok(id)
649 }
650}
651
652#[cfg(feature = "widgets-text")]
653pub struct TextArea;
654#[cfg(feature = "widgets-text")]
655impl TextArea {
656 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
657 Node::new(WidgetKind::TextArea).text(text)
658 }
659}
660
661#[cfg(feature = "widgets-pickers")]
662pub struct FileChooserDialog;
663#[cfg(feature = "widgets-pickers")]
664impl FileChooserDialog {
665 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
666 Node::new(WidgetKind::FileChooserDialog).text(text)
667 }
668}
669
670#[cfg(feature = "widgets-text")]
671pub struct TextInput;
672#[cfg(feature = "widgets-text")]
673impl TextInput {
674 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
675 Node::new(WidgetKind::TextInput).text(text)
676 }
677}
678
679#[cfg(feature = "widgets-choice")]
680pub struct Checkbox;
681#[cfg(feature = "widgets-choice")]
682impl Checkbox {
683 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
684 Node::new(WidgetKind::Checkbox).text(text)
685 }
686}
687
688#[cfg(feature = "widgets-choice")]
689pub struct RadioButton;
690#[cfg(feature = "widgets-choice")]
691impl RadioButton {
692 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
693 Node::new(WidgetKind::RadioButton).text(text)
694 }
695}
696
697#[cfg(feature = "widgets-range")]
698pub struct Slider;
699#[cfg(feature = "widgets-range")]
700impl Slider {
701 pub fn new() -> Node<'static> {
702 Node::new(WidgetKind::Slider)
703 }
704}
705
706#[cfg(feature = "widgets-range")]
707pub struct SpinBox;
708#[cfg(feature = "widgets-range")]
709impl SpinBox {
710 pub fn new() -> Node<'static> {
711 Node::new(WidgetKind::SpinBox)
712 }
713}
714
715#[cfg(feature = "widgets-containers")]
716pub struct Separator;
717#[cfg(feature = "widgets-containers")]
718impl Separator {
719 pub fn new() -> Node<'static> {
720 Node::new(WidgetKind::Separator)
721 }
722}
723
724#[cfg(feature = "widgets-containers")]
725pub struct GroupBox;
726#[cfg(feature = "widgets-containers")]
727impl GroupBox {
728 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
729 Node::new(WidgetKind::GroupBox).text(text)
730 }
731}
732
733#[cfg(feature = "widgets-containers")]
734pub struct Tabs;
735#[cfg(feature = "widgets-containers")]
736impl Tabs {
737 pub fn new() -> Node<'static> {
738 Node::new(WidgetKind::Tabs)
739 }
740}
741
742#[cfg(feature = "widgets-media")]
743pub struct Image;
744#[cfg(feature = "widgets-media")]
745impl Image {
746 pub fn new() -> Node<'static> {
747 Node::new(WidgetKind::Image)
748 }
749}
750
751#[cfg(feature = "widgets-pickers")]
752pub struct DatePicker;
753#[cfg(feature = "widgets-pickers")]
754impl DatePicker {
755 pub fn new() -> Node<'static> {
756 Node::new(WidgetKind::DatePicker)
757 }
758}
759
760#[cfg(feature = "widgets-text")]
761pub struct LinkLabel;
762#[cfg(feature = "widgets-text")]
763impl LinkLabel {
764 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
765 Node::new(WidgetKind::LinkLabel).text(text)
766 }
767}
768
769#[cfg(feature = "widgets-data")]
770pub struct ListView;
771#[cfg(feature = "widgets-data")]
772impl ListView {
773 pub fn new() -> Node<'static> {
774 Node::new(WidgetKind::ListView)
775 }
776}
777
778#[cfg(feature = "widgets-data")]
779pub struct TreeView;
780#[cfg(feature = "widgets-data")]
781impl TreeView {
782 pub fn new() -> Node<'static> {
783 Node::new(WidgetKind::TreeView)
784 }
785}
786
787#[cfg(feature = "widgets-containers")]
788pub struct SplitPane;
789#[cfg(feature = "widgets-containers")]
790impl SplitPane {
791 pub fn new() -> Node<'static> {
792 Node::new(WidgetKind::SplitPane)
793 }
794
795 pub fn with_ratio(ratio: u8) -> Node<'static> {
796 Node::new(WidgetKind::SplitPane).split_ratio(ratio)
797 }
798}
799
800#[cfg(feature = "widgets-containers")]
801pub struct AlertDialog;
802#[cfg(feature = "widgets-containers")]
803impl AlertDialog {
804 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
805 Node::new(WidgetKind::AlertDialog).text(text)
806 }
807}
808
809#[cfg(feature = "widgets-canvas")]
810pub struct VulkanCanvas;
811#[cfg(feature = "widgets-canvas")]
812impl VulkanCanvas {
813 pub fn new() -> Node<'static> {
814 Node::new(WidgetKind::VulkanCanvas)
815 }
816}
817
818#[cfg(feature = "widgets-choice")]
819pub struct ComboBox;
820#[cfg(feature = "widgets-choice")]
821impl ComboBox {
822 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
823 Node::new(WidgetKind::ComboBox).text(text)
824 }
825}
826
827#[cfg(feature = "widgets-choice")]
828pub struct MultiSelect;
829#[cfg(feature = "widgets-choice")]
830impl MultiSelect {
831 pub fn new<'a>(text: impl Into<String>) -> Node<'a> {
832 Node::new(WidgetKind::MultiSelect).text(text)
833 }
834}
835
836#[cfg(feature = "widgets-range")]
837pub struct ProgressBar;
838#[cfg(feature = "widgets-range")]
839impl ProgressBar {
840 pub fn new() -> Node<'static> {
841 Node::new(WidgetKind::ProgressBar)
842 }
843}
844
845#[cfg(feature = "widgets-data")]
846pub struct DataList;
847#[cfg(feature = "widgets-data")]
848impl DataList {
849 pub fn new() -> Node<'static> {
850 Node::new(WidgetKind::DataList)
851 }
852}
853
854#[cfg(feature = "widgets-data")]
855pub struct DataGrid;
856#[cfg(feature = "widgets-data")]
857impl DataGrid {
858 pub fn new() -> Node<'static> {
859 Node::new(WidgetKind::DataGrid)
860 }
861}
862
863#[macro_export]
943macro_rules! ui {
944 ( $ui:expr => window $root:ident ( $($attrs:tt)* ) : $title:expr => { $($children:tt)* } ) => {
945 let $root = $ui
946 .create($crate::WidgetKind::Window, Some($title))
947 .expect("ui!: create Window");
948 $crate::__native_gui_ui_attrs!($ui, $root, $($attrs)*,);
949 $crate::__native_gui_ui_children!($ui, $root, $($children)*);
950 };
951 ( $ui:expr => window $root:ident : $title:expr => { $($children:tt)* } ) => {
952 let $root = $ui
953 .create($crate::WidgetKind::Window, Some($title))
954 .expect("ui!: create Window");
955 $crate::__native_gui_ui_children!($ui, $root, $($children)*);
956 };
957}
958
959#[doc(hidden)]
961#[macro_export]
962macro_rules! __native_gui_ui_children {
963 ( $ui:expr, $parent:ident ) => {};
965 ( $ui:expr, $parent:ident, ) => {};
966 ( $ui:expr, $parent:ident, , $($rest:tt)* ) => {
968 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
969 };
970 ( $ui:expr, $parent:ident, label $name:ident ( $($attrs:tt)* ) : $text:literal $($rest:tt)* ) => {
972 $crate::__native_gui_ui_leaf!($ui, $parent, Label, $name, $text, $($attrs)*);
973 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
974 };
975 ( $ui:expr, $parent:ident, button $name:ident ( $($attrs:tt)* ) : $text:literal $($rest:tt)* ) => {
976 $crate::__native_gui_ui_leaf!($ui, $parent, Button, $name, $text, $($attrs)*);
977 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
978 };
979 ( $ui:expr, $parent:ident, checkbox $name:ident ( $($attrs:tt)* ) : $text:literal $($rest:tt)* ) => {
980 $crate::__native_gui_ui_leaf!($ui, $parent, Checkbox, $name, $text, $($attrs)*);
981 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
982 };
983 ( $ui:expr, $parent:ident, text_input $name:ident ( $($attrs:tt)* ) : $text:literal $($rest:tt)* ) => {
984 $crate::__native_gui_ui_leaf!($ui, $parent, TextInput, $name, $text, $($attrs)*);
985 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
986 };
987 ( $ui:expr, $parent:ident, text_area $name:ident ( $($attrs:tt)* ) : $text:literal $($rest:tt)* ) => {
988 $crate::__native_gui_ui_leaf!($ui, $parent, TextArea, $name, $text, $($attrs)*);
989 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
990 };
991 ( $ui:expr, $parent:ident, progress_bar $name:ident ( $($attrs:tt)* ) $($rest:tt)* ) => {
993 $crate::__native_gui_ui_plain_leaf!($ui, $parent, ProgressBar, $name, $($attrs)*);
994 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
995 };
996 ( $ui:expr, $parent:ident, slider $name:ident ( $($attrs:tt)* ) $($rest:tt)* ) => {
997 $crate::__native_gui_ui_plain_leaf!($ui, $parent, Slider, $name, $($attrs)*);
998 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
999 };
1000 ( $ui:expr, $parent:ident, label $name:ident : $text:literal $($rest:tt)* ) => {
1002 let $name = $ui
1003 .create($crate::WidgetKind::Label, Some($text))
1004 .expect("ui!: create Label");
1005 $ui.attach($parent, $name).expect("ui!: attach Label");
1006 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
1007 };
1008 ( $ui:expr, $parent:ident, button $name:ident : $text:literal $($rest:tt)* ) => {
1009 let $name = $ui
1010 .create($crate::WidgetKind::Button, Some($text))
1011 .expect("ui!: create Button");
1012 $ui.attach($parent, $name).expect("ui!: attach Button");
1013 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
1014 };
1015 ( $ui:expr, $parent:ident, checkbox $name:ident : $text:literal $($rest:tt)* ) => {
1016 let $name = $ui
1017 .create($crate::WidgetKind::Checkbox, Some($text))
1018 .expect("ui!: create Checkbox");
1019 $ui.attach($parent, $name).expect("ui!: attach Checkbox");
1020 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
1021 };
1022 ( $ui:expr, $parent:ident, text_input $name:ident : $text:literal $($rest:tt)* ) => {
1023 let $name = $ui
1024 .create($crate::WidgetKind::TextInput, Some($text))
1025 .expect("ui!: create TextInput");
1026 $ui.attach($parent, $name).expect("ui!: attach TextInput");
1027 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
1028 };
1029 ( $ui:expr, $parent:ident, text_area $name:ident : $text:literal $($rest:tt)* ) => {
1030 let $name = $ui
1031 .create($crate::WidgetKind::TextArea, Some($text))
1032 .expect("ui!: create TextArea");
1033 $ui.attach($parent, $name).expect("ui!: attach TextArea");
1034 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
1035 };
1036 ( $ui:expr, $parent:ident, progress_bar $name:ident $($rest:tt)* ) => {
1038 let $name = $ui
1039 .create($crate::WidgetKind::ProgressBar, None)
1040 .expect("ui!: create ProgressBar");
1041 $ui.attach($parent, $name).expect("ui!: attach ProgressBar");
1042 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
1043 };
1044 ( $ui:expr, $parent:ident, slider $name:ident $($rest:tt)* ) => {
1045 let $name = $ui
1046 .create($crate::WidgetKind::Slider, None)
1047 .expect("ui!: create Slider");
1048 $ui.attach($parent, $name).expect("ui!: attach Slider");
1049 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
1050 };
1051 ( $ui:expr, $parent:ident, container $name:ident ( $($attrs:tt)* ) : { $($children:tt)* } $($rest:tt)* ) => {
1053 let $name = $ui
1054 .create($crate::WidgetKind::Container, None)
1055 .expect("ui!: create Container");
1056 $ui.attach($parent, $name).expect("ui!: attach Container");
1057 $crate::__native_gui_ui_attrs!($ui, $name, $($attrs)*,);
1058 $crate::__native_gui_ui_children!($ui, $name, $($children)*);
1059 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
1060 };
1061 ( $ui:expr, $parent:ident, container $name:ident : { $($children:tt)* } $($rest:tt)* ) => {
1063 let $name = $ui
1064 .create($crate::WidgetKind::Container, None)
1065 .expect("ui!: create Container");
1066 $ui.attach($parent, $name).expect("ui!: attach Container");
1067 $crate::__native_gui_ui_children!($ui, $name, $($children)*);
1068 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
1069 };
1070 ( $ui:expr, $parent:ident, any $name:ident ( $($attrs:tt)* ) : [ $kind:expr ] $($rest:tt)* ) => {
1072 let $name = $ui
1073 .create($kind, None)
1074 .expect("ui!: create widget");
1075 $ui.attach($parent, $name).expect("ui!: attach widget");
1076 $crate::__native_gui_ui_attrs!($ui, $name, $($attrs)*,);
1077 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
1078 };
1079 ( $ui:expr, $parent:ident, any $name:ident : [ $kind:expr ] $($rest:tt)* ) => {
1081 let $name = $ui
1082 .create($kind, None)
1083 .expect("ui!: create widget");
1084 $ui.attach($parent, $name).expect("ui!: attach widget");
1085 $crate::__native_gui_ui_children!($ui, $parent, $($rest)*);
1086 };
1087}
1088
1089#[doc(hidden)]
1092#[macro_export]
1093macro_rules! __native_gui_ui_leaf {
1094 ( $ui:expr, $parent:ident, $kind:ident, $name:ident, $text:expr, $($attrs:tt)* ) => {
1095 let $name = $ui
1096 .create($crate::WidgetKind::$kind, Some($text))
1097 .expect(concat!("ui!: create ", stringify!($kind)));
1098 $ui.attach($parent, $name)
1099 .expect(concat!("ui!: attach ", stringify!($kind)));
1100 $crate::__native_gui_ui_attrs!($ui, $name, $($attrs)*,);
1101 };
1102}
1103
1104#[doc(hidden)]
1107#[macro_export]
1108macro_rules! __native_gui_ui_plain_leaf {
1109 ( $ui:expr, $parent:ident, $kind:ident, $name:ident, $($attrs:tt)* ) => {
1110 let $name = $ui
1111 .create($crate::WidgetKind::$kind, None)
1112 .expect(concat!("ui!: create ", stringify!($kind)));
1113 $ui.attach($parent, $name)
1114 .expect(concat!("ui!: attach ", stringify!($kind)));
1115 $crate::__native_gui_ui_attrs!($ui, $name, $($attrs)*,);
1116 };
1117}
1118
1119#[doc(hidden)]
1123#[macro_export]
1124macro_rules! __native_gui_ui_attrs {
1125 ( $ui:expr, $name:ident, ) => {};
1126 ( $ui:expr, $name:ident, , $($rest:tt)* ) => {
1128 $crate::__native_gui_ui_attrs!($ui, $name, $($rest)*);
1129 };
1130 ( $ui:expr, $name:ident, size: ( $width:expr, $height:expr ) $($rest:tt)* ) => {
1131 $ui.set_size($name, $width, $height).expect("ui!: size");
1132 $crate::__native_gui_ui_attrs!($ui, $name, $($rest)*);
1133 };
1134 ( $ui:expr, $name:ident, tooltip: $text:literal , $($rest:tt)* ) => {
1135 $ui.set_tooltip($name, $text).expect("ui!: tooltip");
1136 $crate::__native_gui_ui_attrs!($ui, $name, $($rest)*);
1137 };
1138 ( $ui:expr, $name:ident, enabled: $enabled:expr , $($rest:tt)* ) => {
1139 $ui.set_enabled($name, $enabled).expect("ui!: enabled");
1140 $crate::__native_gui_ui_attrs!($ui, $name, $($rest)*);
1141 };
1142 ( $ui:expr, $name:ident, checked: $checked:expr , $($rest:tt)* ) => {
1143 $ui.set_checked($name, $checked).expect("ui!: checked");
1144 $crate::__native_gui_ui_attrs!($ui, $name, $($rest)*);
1145 };
1146 ( $ui:expr, $name:ident, value: $value:expr , $($rest:tt)* ) => {
1147 $ui.set_value($name, $value).expect("ui!: value");
1148 $crate::__native_gui_ui_attrs!($ui, $name, $($rest)*);
1149 };
1150 ( $ui:expr, $name:ident, direction: horizontal , $($rest:tt)* ) => {
1151 $ui.set_container_direction($name, $crate::ContainerDirection::Horizontal)
1152 .expect("ui!: direction");
1153 $crate::__native_gui_ui_attrs!($ui, $name, $($rest)*);
1154 };
1155 ( $ui:expr, $name:ident, direction: vertical , $($rest:tt)* ) => {
1156 $ui.set_container_direction($name, $crate::ContainerDirection::Vertical)
1157 .expect("ui!: direction");
1158 $crate::__native_gui_ui_attrs!($ui, $name, $($rest)*);
1159 };
1160 ( $ui:expr, $name:ident, gap: $gap:expr , $($rest:tt)* ) => {
1161 $ui.set_container_gap($name, $gap).expect("ui!: gap");
1162 $crate::__native_gui_ui_attrs!($ui, $name, $($rest)*);
1163 };
1164 ( $ui:expr, $name:ident, padding: $padding:expr , $($rest:tt)* ) => {
1165 $ui.set_container_padding($name, $padding)
1166 .expect("ui!: padding");
1167 $crate::__native_gui_ui_attrs!($ui, $name, $($rest)*);
1168 };
1169 ( $ui:expr, $name:ident, bg: $color:expr , $($rest:tt)* ) => {
1170 $ui.set_background_color($name, $color).expect("ui!: bg");
1171 $crate::__native_gui_ui_attrs!($ui, $name, $($rest)*);
1172 };
1173}
1174
1175#[macro_export]
1213macro_rules! menu {
1214 ( $ui:expr => $window:ident as $bar:ident : $($entries:tt)* ) => {
1215 let $bar = $ui.create_menu().expect("menu!: create menu bar");
1216 $crate::__native_gui_menu_entries!($ui, $bar, $($entries)*);
1217 $ui.attach_menu_to_window($window, $bar)
1218 .expect("menu!: attach menu bar to window");
1219 };
1220 ( $ui:expr => $window:ident : $($entries:tt)* ) => {
1221 let __native_gui_menu_bar = $ui.create_menu().expect("menu!: create menu bar");
1222 $crate::__native_gui_menu_entries!($ui, __native_gui_menu_bar, $($entries)*);
1223 $ui.attach_menu_to_window($window, __native_gui_menu_bar)
1224 .expect("menu!: attach menu bar to window");
1225 };
1226}
1227
1228#[doc(hidden)]
1231#[macro_export]
1232macro_rules! __native_gui_menu_entries {
1233 ( $ui:expr, $bar:ident, ) => {};
1234 ( $ui:expr, $bar:ident, , $($rest:tt)* ) => {
1235 $crate::__native_gui_menu_entries!($ui, $bar, $($rest)*);
1236 };
1237 ( $ui:expr, $bar:ident, $submenu:ident $label:literal { $($items:tt)* } $($rest:tt)* ) => {
1238 let $submenu = $ui
1239 .add_submenu($bar, $label)
1240 .expect("menu!: add submenu");
1241 $crate::__native_gui_menu_items!($ui, $submenu, $($items)*);
1242 $crate::__native_gui_menu_entries!($ui, $bar, $($rest)*);
1243 };
1244}
1245
1246#[doc(hidden)]
1249#[macro_export]
1250macro_rules! __native_gui_menu_items {
1251 ( $ui:expr, $menu:ident, ) => {};
1252 ( $ui:expr, $menu:ident, , $($rest:tt)* ) => {
1253 $crate::__native_gui_menu_items!($ui, $menu, $($rest)*);
1254 };
1255 ( $ui:expr, $menu:ident, $item:ident $label:literal @ $shortcut:literal $($rest:tt)* ) => {
1256 let $item = $ui
1257 .add_menu_item_with_shortcut($menu, $label, Some($shortcut))
1258 .expect("menu!: add menu item");
1259 $crate::__native_gui_menu_items!($ui, $menu, $($rest)*);
1260 };
1261 ( $ui:expr, $menu:ident, $item:ident $label:literal $($rest:tt)* ) => {
1262 let $item = $ui
1263 .add_menu_item($menu, $label)
1264 .expect("menu!: add menu item");
1265 $crate::__native_gui_menu_items!($ui, $menu, $($rest)*);
1266 };
1267}
1268
1269#[cfg(test)]
1270mod tests {
1271 use super::*;
1272
1273 #[test]
1274 fn declarative_window_size_reaches_backend_before_show() {
1275 let mut ui = Ui::new(crate::test_support::MockBackend::default());
1276 let mut node = Window::new("Sized").size(1100.4, 760.6);
1277 let window = ui.render(&mut node).unwrap();
1278 assert_eq!(ui.widget(window).unwrap().size, Some((1100, 761)));
1279 assert_eq!(ui.backend().created[&window].size, Some((1100, 761)));
1280 assert_eq!(ui.backend().size_writes[&window], 1);
1281 assert!(ui.backend().shown.is_empty());
1282 ui.show(window).unwrap();
1283 assert!(ui.backend().shown.contains(&window));
1284 }
1285
1286 #[test]
1287 fn declarative_window_invalid_length_does_not_create_widgets() {
1288 for invalid in [0.0, -1.0, 0.1, f32::NAN, f32::INFINITY, i32::MAX as f32] {
1289 for (width, height) in [(invalid, 760.0), (1100.0, invalid)] {
1290 let mut ui = Ui::new(crate::test_support::MockBackend::default());
1291 let mut output = WidgetId(999);
1292 let mut node = Window::new("Invalid")
1293 .size(width, height)
1294 .bind_id(&mut output);
1295 assert!(matches!(
1296 ui.render(&mut node),
1297 Err(crate::GuiError::Backend(_))
1298 ));
1299 assert_eq!(ui.widget_count(), 0);
1300 assert!(ui.backend().created.is_empty());
1301 drop(node);
1302 assert_eq!(output, WidgetId(999));
1303 }
1304 }
1305 }
1306
1307 #[test]
1308 fn declarative_window_relative_sizes_and_child_lengths_keep_layout_semantics() {
1309 let mut ui = Ui::new(crate::test_support::MockBackend::default());
1310 let mut child = WidgetId(0);
1311 let mut node =
1312 Window::new("Auto").child(Label::new("Child").size(80.0, 20.0).bind_id(&mut child));
1313 let window = ui.render(&mut node).unwrap();
1314 assert_eq!(ui.widget(window).unwrap().size, None);
1315 assert_eq!(ui.widget(child).unwrap().size, None);
1316 assert!(ui.backend().size_writes.is_empty());
1317
1318 let mut relative = Window::new("Relative").percent_size(0.8, 0.8);
1319 let relative_window = ui.render(&mut relative).unwrap();
1320 assert_eq!(ui.widget(relative_window).unwrap().size, None);
1321 assert!(ui.backend().size_writes.is_empty());
1322
1323 let mut mixed = Window::new("Mixed");
1324 mixed.style.size.width = Dimension::length(1100.0);
1325 let mixed_window = ui.render(&mut mixed).unwrap();
1326 assert_eq!(ui.widget(mixed_window).unwrap().size, None);
1327 assert!(ui.backend().size_writes.is_empty());
1328 }
1329
1330 #[test]
1331 #[cfg(feature = "widgets-data")]
1332 fn declarative_container_stretches_nested_rows_across_their_parent() {
1333 let mut ui = Ui::new(crate::test_support::MockBackend::default());
1334 let mut column_id = WidgetId(0);
1335 let mut row_id = WidgetId(0);
1336 let mut button_id = WidgetId(0);
1337 let mut grid_id = WidgetId(0);
1338 let mut node = Window::new("Nested layout").size(400.0, 300.0).child(
1339 Container::vertical()
1340 .flex_grow(1.0)
1341 .bind_id(&mut column_id)
1342 .child(
1343 Container::horizontal()
1344 .min_height(30.0)
1345 .bind_id(&mut row_id)
1346 .child(Button::new("Open").size(80.0, 30.0).bind_id(&mut button_id)),
1347 )
1348 .child(DataGrid::new().flex_grow(1.0).bind_id(&mut grid_id)),
1349 );
1350 let window = ui.render(&mut node).unwrap();
1351 ui.set_layout_root(window).unwrap();
1352 ui.compute_layout(window, 400.0, 300.0).unwrap();
1353
1354 let column = ui.layout_rect(column_id).unwrap();
1355 let row = ui.layout_rect(row_id).unwrap();
1356 let button = ui.layout_rect(button_id).unwrap();
1357 let grid = ui.layout_rect(grid_id).unwrap();
1358 assert_eq!(column.width, 400.0);
1359 assert_eq!(row.width, column.width);
1360 assert_eq!(row.height, 30.0);
1361 assert_eq!(button.width, 80.0);
1362 assert!(grid.y >= row.y + row.height, "row={row:?}, grid={grid:?}");
1363 assert_eq!(
1364 ui.layout_style(column_id).unwrap().align_items,
1365 Some(AlignItems::Stretch)
1366 );
1367 }
1368
1369 #[test]
1371 fn every_widget_kind_has_a_builder() {
1372 let built = vec![
1373 Window::new("").kind,
1374 Container::vertical().kind,
1375 Label::new("").kind,
1376 Button::new("").kind,
1377 #[cfg(feature = "widgets-choice")]
1378 Checkbox::new("").kind,
1379 #[cfg(feature = "widgets-choice")]
1380 RadioButton::new("").kind,
1381 #[cfg(feature = "widgets-range")]
1382 Slider::new().kind,
1383 #[cfg(feature = "widgets-range")]
1384 SpinBox::new().kind,
1385 #[cfg(feature = "widgets-containers")]
1386 Separator::new().kind,
1387 #[cfg(feature = "widgets-containers")]
1388 GroupBox::new("").kind,
1389 #[cfg(feature = "widgets-containers")]
1390 Tabs::new().kind,
1391 #[cfg(feature = "widgets-media")]
1392 Image::new().kind,
1393 #[cfg(feature = "widgets-pickers")]
1394 DatePicker::new().kind,
1395 #[cfg(feature = "widgets-text")]
1396 LinkLabel::new("").kind,
1397 #[cfg(feature = "widgets-data")]
1398 ListView::new().kind,
1399 #[cfg(feature = "widgets-data")]
1400 TreeView::new().kind,
1401 #[cfg(feature = "widgets-containers")]
1402 SplitPane::new().kind,
1403 #[cfg(feature = "widgets-pickers")]
1404 FileChooserDialog::new("").kind,
1405 #[cfg(feature = "widgets-containers")]
1406 AlertDialog::new("").kind,
1407 #[cfg(feature = "widgets-text")]
1408 TextInput::new("").kind,
1409 #[cfg(feature = "widgets-text")]
1410 TextArea::new("").kind,
1411 #[cfg(feature = "widgets-canvas")]
1412 VulkanCanvas::new().kind,
1413 #[cfg(feature = "widgets-choice")]
1414 ComboBox::new("").kind,
1415 #[cfg(feature = "widgets-range")]
1416 ProgressBar::new().kind,
1417 #[cfg(feature = "widgets-data")]
1418 DataList::new().kind,
1419 #[cfg(feature = "widgets-data")]
1420 DataGrid::new().kind,
1421 ];
1422
1423 let all = vec![
1424 WidgetKind::Window,
1425 WidgetKind::Container,
1426 WidgetKind::Label,
1427 WidgetKind::Button,
1428 #[cfg(feature = "widgets-choice")]
1429 WidgetKind::RadioButton,
1430 #[cfg(feature = "widgets-range")]
1431 WidgetKind::Slider,
1432 #[cfg(feature = "widgets-range")]
1433 WidgetKind::SpinBox,
1434 #[cfg(feature = "widgets-containers")]
1435 WidgetKind::Separator,
1436 #[cfg(feature = "widgets-containers")]
1437 WidgetKind::GroupBox,
1438 #[cfg(feature = "widgets-containers")]
1439 WidgetKind::Tabs,
1440 #[cfg(feature = "widgets-media")]
1441 WidgetKind::Image,
1442 #[cfg(feature = "widgets-pickers")]
1443 WidgetKind::DatePicker,
1444 #[cfg(feature = "widgets-text")]
1445 WidgetKind::LinkLabel,
1446 #[cfg(feature = "widgets-data")]
1447 WidgetKind::ListView,
1448 #[cfg(feature = "widgets-data")]
1449 WidgetKind::TreeView,
1450 #[cfg(feature = "widgets-containers")]
1451 WidgetKind::SplitPane,
1452 #[cfg(feature = "widgets-pickers")]
1453 WidgetKind::FileChooserDialog,
1454 #[cfg(feature = "widgets-containers")]
1455 WidgetKind::AlertDialog,
1456 #[cfg(feature = "widgets-text")]
1457 WidgetKind::TextInput,
1458 #[cfg(feature = "widgets-text")]
1459 WidgetKind::TextArea,
1460 #[cfg(feature = "widgets-canvas")]
1461 WidgetKind::VulkanCanvas,
1462 #[cfg(feature = "widgets-choice")]
1463 WidgetKind::Checkbox,
1464 #[cfg(feature = "widgets-choice")]
1465 WidgetKind::ComboBox,
1466 #[cfg(feature = "widgets-range")]
1467 WidgetKind::ProgressBar,
1468 #[cfg(feature = "widgets-data")]
1469 WidgetKind::DataList,
1470 #[cfg(feature = "widgets-data")]
1471 WidgetKind::DataGrid,
1472 ];
1473
1474 for kind in all {
1475 assert!(
1476 built.contains(&kind),
1477 "missing declarative builder for {kind:?}"
1478 );
1479 }
1480 }
1481
1482 #[cfg(feature = "widgets-text")]
1484 #[test]
1485 fn text_area_builder_uses_text_area_kind() {
1486 assert_eq!(TextArea::new("").kind, WidgetKind::TextArea);
1487 }
1488
1489 #[cfg(all(feature = "widgets-text", feature = "widgets-range"))]
1490 #[test]
1491 fn ui_macro_builds_attaches_and_binds_a_tree() {
1492 use crate::test_support::MockBackend;
1493 use crate::Ui;
1494
1495 let mut ui = Ui::new(MockBackend::default());
1496 crate::ui! { ui =>
1497 window root: "Demo" => {
1498 label greeting: "Hello",
1499 button go: "Click",
1500 container form: {
1501 text_input name: "Ada",
1502 progress_bar progress,
1503 },
1504 }
1505 }
1506
1507 assert_eq!(ui.widget_count(), 6);
1508 assert_eq!(
1509 ui.widget(root).map(|spec| spec.kind),
1510 Some(WidgetKind::Window)
1511 );
1512 assert_eq!(
1513 ui.widget(greeting).and_then(|spec| spec.text.as_deref()),
1514 Some("Hello")
1515 );
1516 assert_eq!(
1517 ui.widget(go).and_then(|spec| spec.text.as_deref()),
1518 Some("Click")
1519 );
1520 assert_eq!(
1521 ui.widget(form).map(|spec| spec.kind),
1522 Some(WidgetKind::Container)
1523 );
1524 assert_eq!(
1525 ui.widget(name).and_then(|spec| spec.text.as_deref()),
1526 Some("Ada")
1527 );
1528 assert_eq!(
1529 ui.widget(progress).map(|spec| spec.kind),
1530 Some(WidgetKind::ProgressBar)
1531 );
1532
1533 assert_eq!(ui.widget(greeting).and_then(|spec| spec.parent), Some(root));
1534 assert_eq!(ui.widget(go).and_then(|spec| spec.parent), Some(root));
1535 assert_eq!(ui.widget(form).and_then(|spec| spec.parent), Some(root));
1536 assert_eq!(ui.widget(name).and_then(|spec| spec.parent), Some(form));
1537 assert_eq!(ui.widget(progress).and_then(|spec| spec.parent), Some(form));
1538 }
1539
1540 #[test]
1541 #[cfg(all(feature = "widgets-containers", feature = "widgets-choice"))]
1542 fn ui_macro_supports_generic_kinds_and_empty_containers() {
1543 use crate::test_support::MockBackend;
1544 use crate::Ui;
1545
1546 let mut ui = Ui::new(MockBackend::default());
1547 crate::ui! { ui =>
1548 window root: "Demo" => {
1549 any sep: [WidgetKind::Separator],
1550 any label: [WidgetKind::Label],
1551 container empty: {},
1552 checkbox dark: "Dark mode",
1553 }
1554 }
1555
1556 assert_eq!(ui.widget_count(), 5);
1557 assert_eq!(
1558 ui.widget(sep).map(|spec| spec.kind),
1559 Some(WidgetKind::Separator)
1560 );
1561 assert_eq!(
1562 ui.widget(label).map(|spec| spec.kind),
1563 Some(WidgetKind::Label)
1564 );
1565 assert_eq!(
1566 ui.widget(empty).map(|spec| spec.kind),
1567 Some(WidgetKind::Container)
1568 );
1569 assert_eq!(
1570 ui.widget(dark).and_then(|spec| spec.text.as_deref()),
1571 Some("Dark mode")
1572 );
1573 assert_eq!(ui.widget(sep).and_then(|spec| spec.parent), Some(root));
1574 }
1575
1576 #[test]
1578 fn ui_macro_applies_attribute_groups() {
1579 use crate::test_support::MockBackend;
1580 use crate::{Color, ContainerDirection, Ui};
1581
1582 let mut ui = Ui::new(MockBackend::default());
1583 crate::ui! { ui =>
1584 window root (size: (900, 600)): "Attr demo" => {
1585 label greeting (tooltip: "Tip", size: (120, 24)): "Hello",
1586 button go (enabled: false): "Click",
1587 checkbox flag (checked: true): "Flag",
1588 progress_bar load (value: 40),
1589 container row (direction: horizontal, gap: 6, padding: 10, bg: Color::rgb(20, 30, 40)): {
1590 slider zoom (value: 7),
1591 },
1592 any badge (size: (10, 10), tooltip: "pinned"): [WidgetKind::Label],
1593 }
1594 }
1595
1596 assert_eq!(ui.widget(root).and_then(|spec| spec.size), Some((900, 600)));
1597 assert_eq!(
1598 ui.widget(greeting).and_then(|spec| spec.tooltip.as_deref()),
1599 Some("Tip")
1600 );
1601 assert_eq!(
1602 ui.widget(greeting).and_then(|spec| spec.size),
1603 Some((120, 24))
1604 );
1605 assert_eq!(ui.widget(go).and_then(|spec| spec.enabled), Some(false));
1606 assert_eq!(ui.widget(flag).and_then(|spec| spec.checked), Some(true));
1607 assert_eq!(ui.widget(load).and_then(|spec| spec.value), Some(40));
1608 assert_eq!(
1609 ui.widget(row).and_then(|spec| spec.container_direction),
1610 Some(ContainerDirection::Horizontal)
1611 );
1612 assert_eq!(
1613 ui.widget(row).and_then(|spec| spec.background_color),
1614 Some(Color::rgb(20, 30, 40))
1615 );
1616 assert_eq!(ui.widget(row).and_then(|spec| spec.container_gap), Some(6));
1617 assert_eq!(
1618 ui.widget(row).and_then(|spec| spec.container_padding),
1619 Some(10)
1620 );
1621 assert_eq!(ui.widget(zoom).and_then(|spec| spec.value), Some(7));
1622 assert_eq!(
1623 ui.widget(badge).and_then(|spec| spec.tooltip.as_deref()),
1624 Some("pinned")
1625 );
1626 assert_eq!(ui.widget(zoom).and_then(|spec| spec.parent), Some(row));
1628 assert_eq!(ui.widget(load).and_then(|spec| spec.parent), Some(root));
1629 }
1630
1631 #[test]
1634 fn ui_macro_gap_and_padding_clamp_negatives() {
1635 use crate::test_support::MockBackend;
1636 use crate::Ui;
1637
1638 let mut ui = Ui::new(MockBackend::default());
1639 crate::ui! { ui =>
1640 window root: "Clamp" => {
1641 container row (gap: -4, padding: -9): {
1642 label body: "Body",
1643 },
1644 }
1645 }
1646
1647 assert_eq!(ui.widget(row).and_then(|spec| spec.container_gap), Some(0));
1648 assert_eq!(
1649 ui.widget(row).and_then(|spec| spec.container_padding),
1650 Some(0)
1651 );
1652 }
1653
1654 #[test]
1657 fn ui_macro_accepts_optional_trailing_comma_in_attributes() {
1658 use crate::test_support::MockBackend;
1659 use crate::Ui;
1660
1661 let mut ui = Ui::new(MockBackend::default());
1662 crate::ui! { ui =>
1663 window root: "Attr demo" => {
1664 label one (tooltip: "single"): "One",
1665 label two (size: (80, 20), tooltip: "pair"): "Two",
1666 }
1667 }
1668
1669 assert_eq!(
1670 ui.widget(one).and_then(|spec| spec.tooltip.as_deref()),
1671 Some("single")
1672 );
1673 assert_eq!(ui.widget(two).and_then(|spec| spec.size), Some((80, 20)));
1674 assert_eq!(
1675 ui.widget(two).and_then(|spec| spec.tooltip.as_deref()),
1676 Some("pair")
1677 );
1678 }
1679
1680 #[test]
1683 fn menu_macro_builds_attaches_and_binds_menu_ids() {
1684 use crate::test_support::MockBackend;
1685 use crate::{MenuItemId, Ui, UiEvent};
1686
1687 let mut ui = Ui::new(MockBackend::default());
1688 crate::ui! { ui => window root: "Menus" => { label _body: "hi", } }
1689 crate::menu! { ui => root:
1690 file "File" {
1691 new "New" @ "Ctrl+N",
1692 open "Open",
1693 },
1694 edit "Edit" {
1695 _copy "Copy",
1696 },
1697 }
1698
1699 let menus = &ui.backend().menus;
1701 assert_eq!(menus.get(&file).map(Vec::len), Some(2));
1702 assert_eq!(menus.get(&edit).map(Vec::len), Some(1));
1703 assert_eq!(
1704 menus.get(&file).map(|items| items[0]),
1705 Some(MenuItemId(new.0))
1706 );
1707 assert_eq!(
1709 ui.backend().menu_shortcuts.get(&new).map(String::as_str),
1710 Some("Ctrl+N")
1711 );
1712 assert_eq!(ui.backend().menu_shortcuts.get(&open), None);
1713
1714 ui.backend_mut().push_event(UiEvent::MenuItemSelected {
1716 menu_id: file,
1717 item_id: new,
1718 });
1719 let mut seen = false;
1720 let dispatched = ui
1721 .dispatch_events(|event| {
1722 if let UiEvent::MenuItemSelected { menu_id, item_id } = event {
1723 seen = menu_id == file && item_id == new;
1724 }
1725 })
1726 .expect("dispatch should succeed");
1727 assert!(
1728 dispatched == 1 && seen,
1729 "menu! ids match the dispatched event"
1730 );
1731 }
1732
1733 #[test]
1736 fn menu_macro_binds_the_bar_when_named() {
1737 use crate::test_support::MockBackend;
1738 use crate::Ui;
1739
1740 let mut ui = Ui::new(MockBackend::default());
1741 crate::ui! { ui => window root: "Menus" => { label body: "hi", } }
1742 crate::menu! { ui => root as bar:
1743 _file "File" {
1744 _new "New",
1745 },
1746 }
1747
1748 ui.attach_context_menu(body, bar)
1749 .expect("named bar can be reused as a context menu");
1750 assert_eq!(ui.backend().context_menus.get(&body), Some(&bar));
1751 }
1752
1753 #[test]
1754 fn flex_wrap_reflows_children_on_resize() {
1755 use crate::layout::FlexWrap;
1756 use crate::test_support::MockBackend;
1757 use crate::Ui;
1758
1759 let mut ui = Ui::new(MockBackend::default());
1760 let mut chip_a = WidgetId(0);
1761 let mut chip_b = WidgetId(0);
1762 let mut chip_c = WidgetId(0);
1763 let mut chip_d = WidgetId(0);
1764 let mut app = Window::new("Wrap").child(
1765 Container::horizontal()
1766 .flex_wrap(FlexWrap::Wrap)
1767 .percent_size(1.0, 1.0)
1768 .gap(8.0, 8.0)
1769 .child(Label::new("a").size(120.0, 40.0).bind_id(&mut chip_a))
1770 .child(Label::new("b").size(120.0, 40.0).bind_id(&mut chip_b))
1771 .child(Label::new("c").size(120.0, 40.0).bind_id(&mut chip_c))
1772 .child(Label::new("d").size(120.0, 40.0).bind_id(&mut chip_d)),
1773 );
1774 let root = ui.render(&mut app).expect("render declarative tree");
1775 drop(app);
1776 let chips = [chip_a, chip_b, chip_c, chip_d];
1777
1778 let positions = |ui: &Ui<MockBackend>| -> Vec<(i32, i32)> {
1779 chips
1780 .iter()
1781 .map(|chip| {
1782 ui.widget(*chip)
1783 .and_then(|spec| spec.position)
1784 .expect("laid out chip")
1785 })
1786 .collect()
1787 };
1788
1789 ui.compute_layout(root, 260.0, 400.0)
1791 .expect("narrow layout should compute");
1792 let narrow = positions(&ui);
1793 assert_eq!(
1794 narrow[0].1, narrow[1].1,
1795 "first two chips share a row: {narrow:?}"
1796 );
1797 assert!(
1798 narrow[2].1 > narrow[1].1,
1799 "third chip wraps to a new row: {narrow:?}"
1800 );
1801 assert_eq!(
1802 narrow[2].1, narrow[3].1,
1803 "wrapped chips share the new row: {narrow:?}"
1804 );
1805
1806 ui.compute_layout(root, 700.0, 400.0)
1808 .expect("wide layout should compute");
1809 let wide = positions(&ui);
1810 assert!(
1811 wide.iter().all(|(_, y)| *y == wide[0].1),
1812 "all chips share one row when wide: {wide:?}"
1813 );
1814 }
1815
1816 #[test]
1817 fn justify_content_and_align_items_shape_the_row() {
1818 use crate::layout::{AlignItems, JustifyContent};
1819 use crate::test_support::MockBackend;
1820 use crate::Ui;
1821
1822 let mut ui = Ui::new(MockBackend::default());
1823 let mut container = WidgetId(0);
1824 let mut left = WidgetId(0);
1825 let mut right = WidgetId(0);
1826 let mut app = Window::new("Align").child(
1827 Container::horizontal()
1828 .justify_content(JustifyContent::SPACE_BETWEEN)
1829 .align_items(AlignItems::CENTER)
1830 .size(400.0, 80.0)
1831 .bind_id(&mut container)
1832 .child(Label::new("l").size(80.0, 20.0).bind_id(&mut left))
1833 .child(Label::new("r").size(80.0, 20.0).bind_id(&mut right)),
1834 );
1835 let root = ui.render(&mut app).expect("render declarative tree");
1836 drop(app);
1837 ui.compute_layout(root, 400.0, 80.0).expect("layout");
1838
1839 let position = |ui: &Ui<MockBackend>, id: WidgetId| {
1840 ui.widget(id)
1841 .and_then(|spec| spec.position)
1842 .expect("position")
1843 };
1844 let container_pos = position(&ui, container);
1845 let left_pos = position(&ui, left);
1846 let right_pos = position(&ui, right);
1847
1848 assert!(
1850 right_pos.0 - left_pos.0 >= 240,
1851 "space-between spreads the chips: left={left_pos:?} right={right_pos:?}"
1852 );
1853 assert!(
1854 left_pos.0 - container_pos.0 <= 1,
1855 "first chip starts at the container edge: {left_pos:?}"
1856 );
1857 assert!(
1859 (left_pos.1 - container_pos.1 - 30).abs() <= 1,
1860 "chips are vertically centered: left={left_pos:?} container={container_pos:?}"
1861 );
1862 assert_eq!(left_pos.1, right_pos.1, "both chips share the center line");
1863 }
1864
1865 #[test]
1869 #[cfg(feature = "widgets-containers")]
1870 fn declarative_node_forwards_gap_and_padding_to_backend_container_properties() {
1871 use crate::test_support::MockBackend;
1872 use crate::Ui;
1873
1874 let mut ui = Ui::new(MockBackend::default());
1875 let mut col_id = WidgetId(0);
1876 let mut row_id = WidgetId(0);
1877 let mut group_id = WidgetId(0);
1878 let mut plain_id = WidgetId(0);
1879 let mut zero_id = WidgetId(0);
1880
1881 let mut app = Window::new("Spacing Test").child(
1882 Container::vertical()
1883 .bind_id(&mut col_id)
1884 .padding(12.0)
1885 .gap(8.0, 8.0)
1886 .child(
1887 Container::horizontal()
1888 .bind_id(&mut row_id)
1889 .padding(6.0)
1890 .gap(14.0, 4.0)
1891 .child(Label::new("Item")),
1892 )
1893 .child(
1894 GroupBox::new("Settings")
1895 .bind_id(&mut group_id)
1896 .padding(10.0)
1897 .gap(5.0, 5.0)
1898 .child(Button::new("Save")),
1899 )
1900 .child(Container::vertical().bind_id(&mut plain_id))
1901 .child(
1902 Container::vertical()
1903 .bind_id(&mut zero_id)
1904 .padding(0.0)
1905 .gap(0.0, 0.0),
1906 ),
1907 );
1908
1909 let _ = ui
1910 .render(&mut app)
1911 .expect("render declarative spacing tree");
1912
1913 assert_eq!(
1915 ui.widget(col_id).and_then(|spec| spec.container_padding),
1916 Some(12),
1917 "declarative Node .padding() forwards to spec container_padding"
1918 );
1919 assert_eq!(
1920 ui.widget(col_id).and_then(|spec| spec.container_gap),
1921 Some(8),
1922 "declarative Node .gap() forwards to spec container_gap"
1923 );
1924
1925 assert_eq!(
1927 ui.widget(row_id).and_then(|spec| spec.container_padding),
1928 Some(6)
1929 );
1930 assert_eq!(
1931 ui.widget(row_id).and_then(|spec| spec.container_gap),
1932 Some(14)
1933 );
1934
1935 assert_eq!(
1937 ui.widget(group_id).and_then(|spec| spec.container_padding),
1938 Some(10)
1939 );
1940 assert_eq!(
1941 ui.widget(group_id).and_then(|spec| spec.container_gap),
1942 Some(5)
1943 );
1944
1945 assert_eq!(
1947 ui.widget(plain_id).and_then(|spec| spec.container_padding),
1948 None
1949 );
1950 assert_eq!(
1951 ui.widget(plain_id).and_then(|spec| spec.container_gap),
1952 None
1953 );
1954
1955 assert_eq!(
1957 ui.widget(zero_id).and_then(|spec| spec.container_padding),
1958 Some(0)
1959 );
1960 assert_eq!(
1961 ui.widget(zero_id).and_then(|spec| spec.container_gap),
1962 Some(0)
1963 );
1964 }
1965
1966 #[test]
1969 fn declarative_node_flex_direction_updates_flow_gap() {
1970 use crate::layout::FlexDirection;
1971 use crate::test_support::MockBackend;
1972 use crate::Ui;
1973
1974 let mut ui = Ui::new(MockBackend::default());
1975 let mut col_id = WidgetId(0);
1976 let mut row_id = WidgetId(0);
1977
1978 let mut app = Window::new("Direction Gap").child(
1979 Node::new(WidgetKind::Container)
1980 .gap(10.0, 20.0)
1981 .flex_direction(FlexDirection::Column)
1982 .bind_id(&mut col_id)
1983 .child(
1984 Node::new(WidgetKind::Container)
1985 .gap(10.0, 20.0)
1986 .flex_direction(FlexDirection::Row)
1987 .bind_id(&mut row_id),
1988 ),
1989 );
1990
1991 let _ = ui.render(&mut app).expect("render direction gap tree");
1992
1993 assert_eq!(
1994 ui.widget(col_id).and_then(|spec| spec.container_gap),
1995 Some(20),
1996 "column flex_direction uses height gap (20)"
1997 );
1998 assert_eq!(
1999 ui.widget(row_id).and_then(|spec| spec.container_gap),
2000 Some(10),
2001 "row flex_direction uses width gap (10)"
2002 );
2003 }
2004
2005 #[test]
2007 fn declarative_node_align_self_and_flex_properties_roundtrip() {
2008 use crate::layout::{AlignSelf, Dimension};
2009 use crate::test_support::MockBackend;
2010 use crate::Ui;
2011
2012 let mut ui = Ui::new(MockBackend::default());
2013 let mut child_id = WidgetId(0);
2014
2015 let mut app = Window::new("Flex Child Test").child(
2016 Container::horizontal().child(
2017 Button::new("Target")
2018 .bind_id(&mut child_id)
2019 .align_self(AlignSelf::END)
2020 .flex_shrink(0.25)
2021 .flex_basis(Dimension::length(80.0)),
2022 ),
2023 );
2024
2025 let _ = ui.render(&mut app).expect("render flex child tree");
2026
2027 let style = ui.layout_style(child_id).expect("layout style exists");
2028
2029 assert_eq!(
2030 style.align_self,
2031 Some(AlignSelf::END),
2032 "align_self is applied to layout style"
2033 );
2034 assert_eq!(
2035 style.flex_shrink, 0.25,
2036 "flex_shrink is applied to layout style"
2037 );
2038 assert_eq!(
2039 style.flex_basis,
2040 Dimension::length(80.0),
2041 "flex_basis is applied to layout style"
2042 );
2043 }
2044
2045 #[test]
2048 fn declarative_node_min_max_size_and_set_min_size_forwarding() {
2049 use crate::layout::Dimension;
2050 use crate::test_support::MockBackend;
2051 use crate::Ui;
2052
2053 let mut ui = Ui::new(MockBackend::default());
2054 let mut child1_id = WidgetId(0);
2055 let mut child2_id = WidgetId(0);
2056
2057 let mut app = Window::new("Size Bounds Test").child(
2058 Container::vertical()
2059 .child(
2060 Label::new("Item 1")
2061 .bind_id(&mut child1_id)
2062 .min_width(120.0)
2063 .min_height(60.0)
2064 .max_width(300.0)
2065 .max_height(200.0),
2066 )
2067 .child(
2068 Label::new("Item 2")
2069 .bind_id(&mut child2_id)
2070 .min_size(80.0, 40.0)
2071 .max_size(250.0, 150.0),
2072 ),
2073 );
2074
2075 let _ = ui.render(&mut app).expect("render size bounds tree");
2076
2077 assert_eq!(
2079 ui.min_size(child1_id),
2080 Ok((120, 60)),
2081 "child1 min size forwarded to Ui::set_min_size"
2082 );
2083 assert_eq!(
2084 ui.min_size(child2_id),
2085 Ok((80, 40)),
2086 "child2 min size forwarded to Ui::set_min_size"
2087 );
2088
2089 let s1 = ui.layout_style(child1_id).expect("s1 exists");
2091 assert_eq!(s1.min_size.width, Dimension::length(120.0));
2092 assert_eq!(s1.min_size.height, Dimension::length(60.0));
2093 assert_eq!(s1.max_size.width, Dimension::length(300.0));
2094 assert_eq!(s1.max_size.height, Dimension::length(200.0));
2095
2096 let s2 = ui.layout_style(child2_id).expect("s2 exists");
2098 assert_eq!(s2.min_size.width, Dimension::length(80.0));
2099 assert_eq!(s2.min_size.height, Dimension::length(40.0));
2100 assert_eq!(s2.max_size.width, Dimension::length(250.0));
2101 assert_eq!(s2.max_size.height, Dimension::length(150.0));
2102 }
2103
2104 #[test]
2107 fn declarative_spacer_and_grid_helpers() {
2108 use crate::layout::{Dimension, Display};
2109 use crate::test_support::MockBackend;
2110 use crate::Ui;
2111
2112 let stretch = Spacer::new();
2114 assert_eq!(stretch.kind, WidgetKind::Container);
2115 assert_eq!(stretch.style.flex_grow, 1.0);
2116 assert!(stretch.children.is_empty());
2117
2118 let node_spacer = Node::spacer();
2119 assert_eq!(node_spacer.kind, WidgetKind::Container);
2120 assert_eq!(node_spacer.style.flex_grow, 1.0);
2121
2122 let fixed_spacer = Spacer::fixed(50.0, 20.0);
2123 assert_eq!(fixed_spacer.kind, WidgetKind::Container);
2124 assert_eq!(fixed_spacer.style.size.width, Dimension::length(50.0));
2125 assert_eq!(fixed_spacer.style.size.height, Dimension::length(20.0));
2126
2127 let grid_node = Grid::new();
2128 assert_eq!(grid_node.kind, WidgetKind::Container);
2129 assert_eq!(grid_node.style.display, Display::Grid);
2130
2131 let container_grid = Container::grid();
2132 assert_eq!(container_grid.kind, WidgetKind::Container);
2133 assert_eq!(container_grid.style.display, Display::Grid);
2134
2135 let n_grid = Node::grid();
2136 assert_eq!(n_grid.kind, WidgetKind::Container);
2137 assert_eq!(n_grid.style.display, Display::Grid);
2138
2139 let mut ui = Ui::new(MockBackend::default());
2141 let mut left_btn = WidgetId(0);
2142 let mut right_btn = WidgetId(0);
2143
2144 let mut app = Window::new("Spacer Layout").child(
2145 Container::horizontal()
2146 .size(400.0, 50.0)
2147 .child(Button::new("Left").size(50.0, 30.0).bind_id(&mut left_btn))
2148 .child(Spacer::new())
2149 .child(
2150 Button::new("Right")
2151 .size(50.0, 30.0)
2152 .bind_id(&mut right_btn),
2153 ),
2154 );
2155
2156 let root = ui.render(&mut app).expect("render spacer tree");
2157 drop(app);
2158 ui.compute_layout(root, 400.0, 50.0)
2159 .expect("compute layout");
2160
2161 let left_pos = ui
2162 .widget(left_btn)
2163 .and_then(|w| w.position)
2164 .expect("left pos");
2165 let right_pos = ui
2166 .widget(right_btn)
2167 .and_then(|w| w.position)
2168 .expect("right pos");
2169
2170 assert_eq!(left_pos.0, 0, "left button starts at 0");
2171 assert!(
2172 right_pos.0 >= 350,
2173 "stretch spacer pushed right button to container end: right_x={}",
2174 right_pos.0
2175 );
2176 }
2177
2178 #[test]
2180 fn declarative_align_self_positions_child_on_cross_axis() {
2181 use crate::layout::AlignSelf;
2182 use crate::test_support::MockBackend;
2183 use crate::Ui;
2184
2185 let mut ui = Ui::new(MockBackend::default());
2186 let mut top_id = WidgetId(0);
2187 let mut bottom_id = WidgetId(0);
2188
2189 let mut app = Window::new("Cross Axis Align").child(
2190 Container::horizontal()
2191 .size(300.0, 100.0)
2192 .child(
2193 Label::new("Top")
2194 .size(60.0, 20.0)
2195 .align_self(AlignSelf::START)
2196 .bind_id(&mut top_id),
2197 )
2198 .child(
2199 Label::new("Bottom")
2200 .size(60.0, 20.0)
2201 .align_self(AlignSelf::END)
2202 .bind_id(&mut bottom_id),
2203 ),
2204 );
2205
2206 let root = ui.render(&mut app).expect("render cross axis tree");
2207 drop(app);
2208 ui.compute_layout(root, 300.0, 100.0)
2209 .expect("compute layout");
2210
2211 let top_pos = ui.widget(top_id).and_then(|w| w.position).expect("top pos");
2212 let bottom_pos = ui
2213 .widget(bottom_id)
2214 .and_then(|w| w.position)
2215 .expect("bottom pos");
2216
2217 assert_eq!(top_pos.1, 0, "align_self: START places widget at top (y=0)");
2218 assert_eq!(
2219 bottom_pos.1, 80,
2220 "align_self: END places widget at bottom (y=100-20=80)"
2221 );
2222 }
2223}