1use std::any::{Any, TypeId};
5use std::fmt::{self, Debug};
6use std::rc::Rc;
7
8use waft_embedder::{Clip, Offset, Rect, Size, TextBaseline, TextDirection, lerp_double};
9use waft_foundation::{
10 App, DiagnosticPropertiesBuilder, DiagnosticsNode, DiagnosticsProperty, DiagnosticsTreeStyle,
11 EnumProperty, Handle, IntProperty,
12};
13use waft_painting::{Alignment, AlignmentGeometry};
14
15use crate::box_::{
16 AnyRenderBox, BaselineOffset, BoxConstraints, BoxHitTestResult, BoxParentData,
17 ContainerBoxParentData, RenderBox, RenderBoxContainerDefaultsMixin, RenderBoxData,
18};
19use crate::layer::{ClipRectLayer, LayerHandle};
20use crate::layout_helper::ChildLayoutHelper;
21use crate::object::{
22 AnyRenderObject, ContainerParentData, ContainerParentDataMixin, ContainerRenderObjectData,
23 ContainerRenderObjectMixin, ParentData, RenderHandle, RenderObject, RenderObjectData,
24};
25use crate::painting_context::PaintingContext;
26use crate::pipeline_owner::PipelineOwner;
27
28#[derive(Clone, Copy, PartialEq)]
36pub struct RelativeRect {
37 pub left: f64,
41
42 pub top: f64,
46
47 pub right: f64,
51
52 pub bottom: f64,
56}
57
58impl RelativeRect {
59 pub const fn from_ltrb(left: f64, top: f64, right: f64, bottom: f64) -> RelativeRect {
61 RelativeRect {
62 left,
63 top,
64 right,
65 bottom,
66 }
67 }
68
69 pub fn from_size(rect: Rect, container: Size) -> RelativeRect {
74 RelativeRect {
75 left: rect.left,
76 top: rect.top,
77 right: container.width() - rect.right,
78 bottom: container.height() - rect.bottom,
79 }
80 }
81
82 pub fn from_rect(rect: Rect, container: Rect) -> RelativeRect {
95 RelativeRect {
96 left: rect.left - container.left,
97 top: rect.top - container.top,
98 right: container.right - rect.right,
99 bottom: container.bottom - rect.bottom,
100 }
101 }
102
103 pub fn from_directional(
111 text_direction: TextDirection,
112 start: f64,
113 top: f64,
114 end: f64,
115 bottom: f64,
116 ) -> RelativeRect {
117 let (left, right) = match text_direction {
118 TextDirection::Rtl => (end, start),
119 TextDirection::Ltr => (start, end),
120 };
121 RelativeRect::from_ltrb(left, top, right, bottom)
122 }
123
124 pub const FILL: RelativeRect = RelativeRect::from_ltrb(0.0, 0.0, 0.0, 0.0);
126
127 pub fn has_insets(&self) -> bool {
133 self.left > 0.0 || self.top > 0.0 || self.right > 0.0 || self.bottom > 0.0
134 }
135
136 pub fn shift(&self, offset: Offset) -> RelativeRect {
138 RelativeRect::from_ltrb(
139 self.left + offset.dx(),
140 self.top + offset.dy(),
141 self.right - offset.dx(),
142 self.bottom - offset.dy(),
143 )
144 }
145
146 pub fn inflate(&self, delta: f64) -> RelativeRect {
148 RelativeRect::from_ltrb(
149 self.left - delta,
150 self.top - delta,
151 self.right - delta,
152 self.bottom - delta,
153 )
154 }
155
156 pub fn deflate(&self, delta: f64) -> RelativeRect {
158 self.inflate(-delta)
159 }
160
161 pub fn intersect(&self, other: RelativeRect) -> RelativeRect {
163 RelativeRect::from_ltrb(
164 self.left.max(other.left),
165 self.top.max(other.top),
166 self.right.max(other.right),
167 self.bottom.max(other.bottom),
168 )
169 }
170
171 pub fn to_rect(&self, container: Rect) -> Rect {
178 Rect::from_ltrb(
179 self.left,
180 self.top,
181 container.width() - self.right,
182 container.height() - self.bottom,
183 )
184 }
185
186 pub fn to_size(&self, container: Size) -> Size {
192 Size::new(
193 container.width() - self.left - self.right,
194 container.height() - self.top - self.bottom,
195 )
196 }
197
198 pub fn lerp(a: Option<RelativeRect>, b: Option<RelativeRect>, t: f64) -> Option<RelativeRect> {
202 match (a, b) {
203 (None, None) => None,
204 (None, Some(b)) => Some(RelativeRect::from_ltrb(
205 b.left * t,
206 b.top * t,
207 b.right * t,
208 b.bottom * t,
209 )),
210 (Some(a), None) => {
211 let k = 1.0 - t;
212 Some(RelativeRect::from_ltrb(
213 a.left * k,
214 a.top * k,
215 a.right * k,
216 a.bottom * k,
217 ))
218 }
219 (Some(a), Some(b)) => Some(RelativeRect::from_ltrb(
220 lerp_double(Some(a.left), Some(b.left), t).unwrap(),
221 lerp_double(Some(a.top), Some(b.top), t).unwrap(),
222 lerp_double(Some(a.right), Some(b.right), t).unwrap(),
223 lerp_double(Some(a.bottom), Some(b.bottom), t).unwrap(),
224 )),
225 }
226 }
227}
228
229impl Debug for RelativeRect {
230 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
231 write!(
232 f,
233 "RelativeRect::from_ltrb({:.1}, {:.1}, {:.1}, {:.1})",
234 self.left, self.top, self.right, self.bottom
235 )
236 }
237}
238
239#[derive(Debug)]
241pub struct StackParentData {
242 box_parent_data: BoxParentData,
243 container_parent_data: ContainerParentData<AnyRenderBox>,
244
245 pub top: Option<f64>,
247
248 pub right: Option<f64>,
250
251 pub bottom: Option<f64>,
253
254 pub left: Option<f64>,
256
257 pub width: Option<f64>,
261
262 pub height: Option<f64>,
266}
267
268impl StackParentData {
269 pub const fn new() -> StackParentData {
271 StackParentData {
272 box_parent_data: BoxParentData::new(),
273 container_parent_data: ContainerParentData::new(),
274 top: None,
275 right: None,
276 bottom: None,
277 left: None,
278 width: None,
279 height: None,
280 }
281 }
282
283 pub fn rect(&self) -> RelativeRect {
289 RelativeRect::from_ltrb(
290 self.left.expect("left is set"),
291 self.top.expect("top is set"),
292 self.right.expect("right is set"),
293 self.bottom.expect("bottom is set"),
294 )
295 }
296
297 pub fn set_rect(&mut self, value: RelativeRect) {
299 self.top = Some(value.top);
300 self.right = Some(value.right);
301 self.bottom = Some(value.bottom);
302 self.left = Some(value.left);
303 }
304
305 pub fn is_positioned(&self) -> bool {
311 self.top.is_some()
312 || self.right.is_some()
313 || self.bottom.is_some()
314 || self.left.is_some()
315 || self.width.is_some()
316 || self.height.is_some()
317 }
318
319 pub fn positioned_child_constraints(&self, stack_size: Size) -> BoxConstraints {
325 debug_assert!(self.is_positioned());
326 let width = match (self.left, self.right) {
327 (Some(left), Some(right)) => Some(stack_size.width() - right - left),
328 _ => self.width,
329 };
330 let height = match (self.top, self.bottom) {
331 (Some(top), Some(bottom)) => Some(stack_size.height() - bottom - top),
332 _ => self.height,
333 };
334 debug_assert!(height.is_none_or(|height| !height.is_nan()));
335 debug_assert!(width.is_none_or(|width| !width.is_nan()));
336 BoxConstraints::tight_for(
337 width.map(|width| width.max(0.0)),
338 height.map(|height| height.max(0.0)),
339 )
340 }
341}
342
343impl Default for StackParentData {
344 fn default() -> StackParentData {
345 StackParentData::new()
346 }
347}
348
349impl ParentData for StackParentData {
350 fn detach(&mut self) {
351 ContainerParentDataMixin::detach(self);
352 }
353
354 fn provide(&self, id: TypeId) -> Option<&dyn Any> {
355 if id == TypeId::of::<StackParentData>() {
356 return Some(self);
357 }
358 self.box_parent_data.provide(id)
359 }
360
361 fn provide_mut(&mut self, id: TypeId) -> Option<&mut dyn Any> {
362 if id == TypeId::of::<StackParentData>() {
363 return Some(self);
364 }
365 self.box_parent_data.provide_mut(id)
366 }
367}
368
369impl ContainerParentDataMixin for StackParentData {
370 type ChildType = AnyRenderBox;
371
372 fn container_parent_data(&self) -> &ContainerParentData<AnyRenderBox> {
373 &self.container_parent_data
374 }
375
376 fn container_parent_data_mut(&mut self) -> &mut ContainerParentData<AnyRenderBox> {
377 &mut self.container_parent_data
378 }
379}
380
381impl ContainerBoxParentData for StackParentData {
382 fn box_parent_data(&self) -> &BoxParentData {
383 &self.box_parent_data
384 }
385
386 fn box_parent_data_mut(&mut self) -> &mut BoxParentData {
387 &mut self.box_parent_data
388 }
389}
390
391impl fmt::Display for StackParentData {
392 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
393 let mut values = Vec::new();
394 for (name, value) in [
395 ("top", self.top),
396 ("right", self.right),
397 ("bottom", self.bottom),
398 ("left", self.left),
399 ("width", self.width),
400 ("height", self.height),
401 ] {
402 if let Some(value) = value {
403 values.push(format!("{name}={value}"));
404 }
405 }
406 if values.is_empty() {
407 values.push("not positioned".to_string());
408 }
409 values.push(self.box_parent_data.to_string());
410 f.write_str(&values.join("; "))
411 }
412}
413
414#[derive(Clone, Copy, Debug, PartialEq, Eq)]
416pub enum StackFit {
417 Loose,
423
424 Expand,
431
432 Passthrough,
435}
436
437pub struct RenderStackData {
439 has_visual_overflow: bool,
440 alignment: AlignmentGeometry,
441 text_direction: Option<TextDirection>,
442 fit: StackFit,
443 clip_behavior: Clip,
444 resolved_alignment_cache: Option<Alignment>,
445 clip_rect_layer: LayerHandle<Handle<ClipRectLayer>>,
446}
447
448impl RenderStackData {
449 pub fn new() -> RenderStackData {
451 RenderStackData {
452 has_visual_overflow: false,
453 alignment: AlignmentGeometry::TOP_START,
454 text_direction: None,
455 fit: StackFit::Loose,
456 clip_behavior: Clip::HardEdge,
457 resolved_alignment_cache: None,
458 clip_rect_layer: LayerHandle::new(),
459 }
460 }
461}
462
463impl Default for RenderStackData {
464 fn default() -> RenderStackData {
465 RenderStackData::new()
466 }
467}
468
469pub fn get_intrinsic_dimension(
473 app: &mut App,
474 first_child: Option<AnyRenderBox>,
475 mut main_child_size_getter: impl FnMut(&mut App, AnyRenderBox) -> f64,
476) -> f64 {
477 let mut extent: f64 = 0.0;
478 let mut child = first_child;
479 while let Some(current) = child {
480 let child_parent_data = current.as_object().parent_data_of::<StackParentData>(app);
481 let (is_positioned, next_sibling) = (
482 child_parent_data.is_positioned(),
483 child_parent_data.next_sibling(),
484 );
485 if !is_positioned {
486 extent = extent.max(main_child_size_getter(app, current));
487 }
488 child = next_sibling;
489 }
490 extent
491}
492
493pub fn layout_positioned_child(
498 app: &mut App,
499 child: AnyRenderBox,
500 size: Size,
501 alignment: Alignment,
502) -> bool {
503 let child_constraints = {
504 let child_parent_data = child.as_object().parent_data_of::<StackParentData>(app);
505 debug_assert!(child_parent_data.is_positioned());
506 child_parent_data.positioned_child_constraints(size)
507 };
508 child.layout(app, child_constraints, true);
509
510 let child_size = child.size(app);
511 let child_parent_data = child.as_object().parent_data_of::<StackParentData>(app);
512 let x = match (child_parent_data.left, child_parent_data.right) {
513 (Some(left), _) => left,
514 (None, Some(right)) => size.width() - right - child_size.width(),
515 (None, None) => alignment.along_offset(size - child_size).dx(),
516 };
517 let y = match (child_parent_data.top, child_parent_data.bottom) {
518 (Some(top), _) => top,
519 (None, Some(bottom)) => size.height() - bottom - child_size.height(),
520 (None, None) => alignment.along_offset(size - child_size).dy(),
521 };
522
523 child
524 .parent_data_of_mut::<StackParentData>(app)
525 .set_offset(Offset::new(x, y));
526 x < 0.0
527 || x + child_size.width() > size.width()
528 || y < 0.0
529 || y + child_size.height() > size.height()
530}
531
532fn baseline_for_child(
534 app: &mut App,
535 child: AnyRenderBox,
536 stack_size: Size,
537 non_positioned_child_constraints: BoxConstraints,
538 alignment: Alignment,
539 baseline: TextBaseline,
540) -> Option<f64> {
541 let child_parent_data = child.as_object().parent_data_of::<StackParentData>(app);
542 let child_constraints = if child_parent_data.is_positioned() {
543 child_parent_data.positioned_child_constraints(stack_size)
544 } else {
545 non_positioned_child_constraints
546 };
547 let (top, bottom) = (child_parent_data.top, child_parent_data.bottom);
548 let baseline_offset = child.get_dry_baseline(app, child_constraints, baseline)?;
549 let y = match (top, bottom) {
550 (Some(top), _) => top,
551 (None, Some(bottom)) => {
552 stack_size.height() - bottom - child.get_dry_layout(app, child_constraints).height()
553 }
554 (None, None) => alignment
555 .along_offset(stack_size - child.get_dry_layout(app, child_constraints))
556 .dy(),
557 };
558 Some(baseline_offset + y)
559}
560
561pub trait RenderStackBase:
587 RenderBoxContainerDefaultsMixin<ParentDataType = StackParentData>
588{
589 fn stack_data(self: RenderHandle<Self>, app: &App) -> &RenderStackData;
591
592 fn stack_data_mut(self: RenderHandle<Self>, app: &mut App) -> &mut RenderStackData;
594
595 fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
600 if !child.parent_data_is::<StackParentData>(app) {
601 child.set_parent_data(app, StackParentData::new());
602 }
603 }
604
605 fn resolved_alignment(self: RenderHandle<Self>, app: &mut App) -> Alignment {
608 if let Some(cached) = self.stack_data(app).resolved_alignment_cache {
609 return cached;
610 }
611 let resolved = self.alignment(app).resolve(self.text_direction(app));
612 self.stack_data_mut(app).resolved_alignment_cache = Some(resolved);
613 resolved
614 }
615
616 fn mark_need_resolution(self: RenderHandle<Self>, app: &mut App) {
618 self.stack_data_mut(app).resolved_alignment_cache = None;
619 self.mark_needs_layout(app);
620 }
621
622 fn alignment(self: RenderHandle<Self>, app: &App) -> AlignmentGeometry {
636 self.stack_data(app).alignment
637 }
638
639 fn set_alignment(self: RenderHandle<Self>, app: &mut App, value: AlignmentGeometry) {
641 if self.stack_data(app).alignment == value {
642 return;
643 }
644 self.stack_data_mut(app).alignment = value;
645 self.mark_need_resolution(app);
646 }
647
648 fn text_direction(self: RenderHandle<Self>, app: &App) -> Option<TextDirection> {
653 self.stack_data(app).text_direction
654 }
655
656 fn set_text_direction(self: RenderHandle<Self>, app: &mut App, value: Option<TextDirection>) {
658 if self.stack_data(app).text_direction == value {
659 return;
660 }
661 self.stack_data_mut(app).text_direction = value;
662 self.mark_need_resolution(app);
663 }
664
665 fn fit(self: RenderHandle<Self>, app: &App) -> StackFit {
670 self.stack_data(app).fit
671 }
672
673 fn set_fit(self: RenderHandle<Self>, app: &mut App, value: StackFit) {
675 if self.stack_data(app).fit != value {
676 self.stack_data_mut(app).fit = value;
677 self.mark_needs_layout(app);
678 }
679 }
680
681 fn clip_behavior(self: RenderHandle<Self>, app: &App) -> Clip {
690 self.stack_data(app).clip_behavior
691 }
692
693 fn set_clip_behavior(self: RenderHandle<Self>, app: &mut App, value: Clip) {
695 if value != self.stack_data(app).clip_behavior {
696 self.stack_data_mut(app).clip_behavior = value;
697 self.mark_needs_paint(app);
698 }
699 }
700
701 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
703 let first_child = self.first_child(app);
704 get_intrinsic_dimension(app, first_child, |app, child| {
705 child.get_min_intrinsic_width(app, height)
706 })
707 }
708
709 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
711 let first_child = self.first_child(app);
712 get_intrinsic_dimension(app, first_child, |app, child| {
713 child.get_max_intrinsic_width(app, height)
714 })
715 }
716
717 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
719 let first_child = self.first_child(app);
720 get_intrinsic_dimension(app, first_child, |app, child| {
721 child.get_min_intrinsic_height(app, width)
722 })
723 }
724
725 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
727 let first_child = self.first_child(app);
728 get_intrinsic_dimension(app, first_child, |app, child| {
729 child.get_max_intrinsic_height(app, width)
730 })
731 }
732
733 fn compute_distance_to_actual_baseline(
735 self: RenderHandle<Self>,
736 app: &mut App,
737 baseline: TextBaseline,
738 ) -> Option<f64> {
739 self.default_compute_distance_to_highest_actual_baseline(app, baseline)
740 }
741
742 fn compute_dry_baseline(
744 self: RenderHandle<Self>,
745 app: &mut App,
746 constraints: BoxConstraints,
747 baseline: TextBaseline,
748 ) -> Option<f64> {
749 let non_positioned_child_constraints = match self.fit(app) {
750 StackFit::Loose => constraints.loosen(),
751 StackFit::Expand => BoxConstraints::tight(constraints.biggest()),
752 StackFit::Passthrough => constraints,
753 };
754
755 let alignment = self.resolved_alignment(app);
756 let size = self.as_box().get_dry_layout(app, constraints);
757
758 let mut baseline_offset = BaselineOffset::NO_BASELINE;
759 let mut child = self.first_child(app);
760 while let Some(current) = child {
761 baseline_offset = baseline_offset.min_of(BaselineOffset(baseline_for_child(
762 app,
763 current,
764 size,
765 non_positioned_child_constraints,
766 alignment,
767 baseline,
768 )));
769 child = self.child_after(app, current);
770 }
771 baseline_offset.offset()
772 }
773
774 fn compute_dry_layout(
776 self: RenderHandle<Self>,
777 app: &mut App,
778 constraints: BoxConstraints,
779 ) -> Size {
780 self.compute_size(app, constraints, ChildLayoutHelper::dry_layout_child)
781 }
782
783 fn compute_size(
786 self: RenderHandle<Self>,
787 app: &mut App,
788 constraints: BoxConstraints,
789 mut layout_child: impl FnMut(&mut App, AnyRenderBox, BoxConstraints) -> Size,
790 ) -> Size {
791 let mut has_non_positioned_children = false;
792 if self.child_count(app) == 0 {
793 return if constraints.biggest().is_finite() {
794 constraints.biggest()
795 } else {
796 constraints.smallest()
797 };
798 }
799
800 let mut width = constraints.min_width;
801 let mut height = constraints.min_height;
802
803 let non_positioned_constraints = match self.fit(app) {
804 StackFit::Loose => constraints.loosen(),
805 StackFit::Expand => BoxConstraints::tight(constraints.biggest()),
806 StackFit::Passthrough => constraints,
807 };
808
809 let mut child = self.first_child(app);
810 while let Some(current) = child {
811 let is_positioned = current
812 .as_object()
813 .parent_data_of::<StackParentData>(app)
814 .is_positioned();
815
816 if !is_positioned {
817 has_non_positioned_children = true;
818
819 let child_size = layout_child(app, current, non_positioned_constraints);
820
821 width = width.max(child_size.width());
822 height = height.max(child_size.height());
823 }
824
825 child = self.child_after(app, current);
826 }
827
828 let size = if has_non_positioned_children {
829 let size = Size::new(width, height);
830 debug_assert_eq!(size.width(), constraints.constrain_width(width));
831 debug_assert_eq!(size.height(), constraints.constrain_height(height));
832 size
833 } else {
834 constraints.biggest()
835 };
836
837 debug_assert!(
838 size.is_finite(),
839 "A Stack requires bounded constraints from its parent. This error commonly occurs \
840 when a Stack is placed inside a widget like Column, ListView, or other widgets that \
841 do not constrain their children. To fix this, wrap the Stack in a widget that \
842 provides finite height and width constraints, such as a SizedBox or ConstrainedBox. \
843 Use Expanded only if the parent is a Flex widget like Row or Column."
844 );
845 size
846 }
847
848 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
850 let constraints = self.constraints(app);
851 self.stack_data_mut(app).has_visual_overflow = false;
852
853 let size = self.compute_size(app, constraints, ChildLayoutHelper::layout_child);
854 self.set_size(app, size);
855
856 let resolved_alignment = self.resolved_alignment(app);
857 let mut child = self.first_child(app);
858 while let Some(current) = child {
859 let is_positioned = current
860 .as_object()
861 .parent_data_of::<StackParentData>(app)
862 .is_positioned();
863
864 if !is_positioned {
865 let offset = resolved_alignment.along_offset(size - current.size(app));
866 current
867 .parent_data_of_mut::<StackParentData>(app)
868 .set_offset(offset);
869 } else {
870 let overflowed = layout_positioned_child(app, current, size, resolved_alignment);
871 self.stack_data_mut(app).has_visual_overflow |= overflowed;
872 }
873
874 child = self.child_after(app, current);
875 }
876 }
877
878 fn paint_stack(
884 self: RenderHandle<Self>,
885 app: &mut App,
886 context: &mut PaintingContext,
887 offset: Offset,
888 ) {
889 self.default_paint(app, context, offset);
890 }
891
892 fn paint(
894 self: RenderHandle<Self>,
895 app: &mut App,
896 context: &mut PaintingContext,
897 offset: Offset,
898 ) {
899 let clip_behavior = self.clip_behavior(app);
900 if clip_behavior != Clip::None && self.stack_data(app).has_visual_overflow {
901 let size = self.size(app);
902 let old = self.stack_data(app).clip_rect_layer.layer();
903 let layer = context.push_clip_rect(
904 app,
905 self.as_object().needs_compositing(app),
906 offset,
907 Offset::ZERO & size,
908 |app, context, offset| self.paint_stack(app, context, offset),
909 clip_behavior,
910 old,
911 );
912 LayerHandle::set_layer(
913 app,
914 |app| &mut self.stack_data_mut(app).clip_rect_layer,
915 layer,
916 );
917 } else {
918 LayerHandle::set_layer(
919 app,
920 |app| &mut self.stack_data_mut(app).clip_rect_layer,
921 None,
922 );
923 self.paint_stack(app, context, offset);
924 }
925 }
926
927 fn dispose(self: RenderHandle<Self>, app: &mut App) {
929 LayerHandle::set_layer(
930 app,
931 |app| &mut self.stack_data_mut(app).clip_rect_layer,
932 None,
933 );
934 crate::object::RenderObjectBase::dispose(self, app);
935 }
936
937 fn describe_approximate_paint_clip(
940 self: RenderHandle<Self>,
941 app: &App,
942 _child: AnyRenderObject,
943 ) -> Option<Rect> {
944 match self.clip_behavior(app) {
945 Clip::None => None,
946 Clip::HardEdge | Clip::AntiAlias | Clip::AntiAliasWithSaveLayer => self
947 .stack_data(app)
948 .has_visual_overflow
949 .then(|| Offset::ZERO & self.size(app)),
950 }
951 }
952}
953
954pub struct RenderStack {
956 render_object: RenderObjectData,
957 render_box: RenderBoxData,
958 container: ContainerRenderObjectData<AnyRenderBox>,
959 stack: RenderStackData,
960}
961
962impl RenderStack {
963 pub fn new(app: &mut App) -> RenderHandle<RenderStack> {
969 RenderHandle::new_box(
970 app,
971 RenderStack {
972 render_object: RenderObjectData::new(),
973 render_box: RenderBoxData::new(),
974 container: ContainerRenderObjectData::new(),
975 stack: RenderStackData::new(),
976 },
977 )
978 }
979}
980
981impl ContainerRenderObjectMixin for RenderStack {
982 type ChildType = AnyRenderBox;
983 type ParentDataType = StackParentData;
984
985 fn container_data(
986 self: RenderHandle<Self>,
987 app: &App,
988 ) -> &ContainerRenderObjectData<AnyRenderBox> {
989 &self.get(app).container
990 }
991
992 fn container_data_mut(
993 self: RenderHandle<Self>,
994 app: &mut App,
995 ) -> &mut ContainerRenderObjectData<AnyRenderBox> {
996 &mut self.get_mut(app).container
997 }
998}
999
1000impl RenderBoxContainerDefaultsMixin for RenderStack {}
1001
1002impl RenderStackBase for RenderStack {
1003 fn stack_data(self: RenderHandle<Self>, app: &App) -> &RenderStackData {
1004 &self.get(app).stack
1005 }
1006
1007 fn stack_data_mut(self: RenderHandle<Self>, app: &mut App) -> &mut RenderStackData {
1008 &mut self.get_mut(app).stack
1009 }
1010}
1011
1012impl RenderObject for RenderStack {
1013 crate::render_object_accessors!();
1014
1015 fn debug_fill_properties(
1016 self: RenderHandle<Self>,
1017 app: &App,
1018 properties: &mut DiagnosticPropertiesBuilder,
1019 ) {
1020 let data = RenderStackBase::stack_data(self, app);
1021 properties.add(DiagnosticsProperty::new("alignment", data.alignment));
1022 properties.add(EnumProperty::<TextDirection>::new(
1023 "textDirection",
1024 data.text_direction,
1025 ));
1026 properties.add(EnumProperty::<StackFit>::new("fit", data.fit));
1027 properties.add(
1028 EnumProperty::<Clip>::new("clipBehavior", Some(data.clip_behavior))
1029 .default_value(Some(Clip::HardEdge)),
1030 );
1031 }
1032
1033 fn debug_describe_children(
1034 self: RenderHandle<Self>,
1035 app: &App,
1036 ) -> Vec<Rc<dyn DiagnosticsNode>> {
1037 ContainerRenderObjectMixin::debug_describe_children(self, app)
1038 }
1039
1040 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
1041 RenderStackBase::perform_layout(self, app)
1042 }
1043
1044 fn visit_children(
1045 self: RenderHandle<Self>,
1046 app: &App,
1047 visitor: &mut dyn FnMut(AnyRenderObject),
1048 ) {
1049 ContainerRenderObjectMixin::visit_children(self, app, visitor)
1050 }
1051
1052 fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
1053 ContainerRenderObjectMixin::did_attach(self, app, owner)
1054 }
1055
1056 fn did_detach(self: RenderHandle<Self>, app: &mut App) {
1057 ContainerRenderObjectMixin::did_detach(self, app)
1058 }
1059
1060 fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
1061 ContainerRenderObjectMixin::redepth_children(self, app)
1062 }
1063
1064 fn paint(
1065 self: RenderHandle<Self>,
1066 app: &mut App,
1067 context: &mut PaintingContext,
1068 offset: Offset,
1069 ) {
1070 RenderStackBase::paint(self, app, context, offset)
1071 }
1072
1073 fn dispose(self: RenderHandle<Self>, app: &mut App) {
1074 RenderStackBase::dispose(self, app)
1075 }
1076
1077 fn describe_approximate_paint_clip(
1078 self: RenderHandle<Self>,
1079 app: &App,
1080 child: AnyRenderObject,
1081 ) -> Option<Rect> {
1082 RenderStackBase::describe_approximate_paint_clip(self, app, child)
1083 }
1084}
1085
1086impl RenderBox for RenderStack {
1087 crate::render_box_accessors!();
1088
1089 fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
1090 RenderStackBase::setup_parent_data(self, app, child)
1091 }
1092
1093 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
1094 RenderStackBase::compute_min_intrinsic_width(self, app, height)
1095 }
1096
1097 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
1098 RenderStackBase::compute_max_intrinsic_width(self, app, height)
1099 }
1100
1101 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
1102 RenderStackBase::compute_min_intrinsic_height(self, app, width)
1103 }
1104
1105 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
1106 RenderStackBase::compute_max_intrinsic_height(self, app, width)
1107 }
1108
1109 fn compute_distance_to_actual_baseline(
1110 self: RenderHandle<Self>,
1111 app: &mut App,
1112 baseline: TextBaseline,
1113 ) -> Option<f64> {
1114 RenderStackBase::compute_distance_to_actual_baseline(self, app, baseline)
1115 }
1116
1117 fn compute_dry_baseline(
1118 self: RenderHandle<Self>,
1119 app: &mut App,
1120 constraints: BoxConstraints,
1121 baseline: TextBaseline,
1122 ) -> Option<f64> {
1123 RenderStackBase::compute_dry_baseline(self, app, constraints, baseline)
1124 }
1125
1126 fn compute_dry_layout(
1127 self: RenderHandle<Self>,
1128 app: &mut App,
1129 constraints: BoxConstraints,
1130 ) -> Size {
1131 RenderStackBase::compute_dry_layout(self, app, constraints)
1132 }
1133
1134 fn hit_test_children(
1135 self: RenderHandle<Self>,
1136 app: &mut App,
1137 result: &mut BoxHitTestResult<'_>,
1138 position: Offset,
1139 ) -> bool {
1140 self.default_hit_test_children(app, result, position)
1141 }
1142}
1143
1144pub struct RenderIndexedStack {
1150 render_object: RenderObjectData,
1151 render_box: RenderBoxData,
1152 container: ContainerRenderObjectData<AnyRenderBox>,
1153 stack: RenderStackData,
1154 index: Option<i32>,
1155}
1156
1157impl RenderIndexedStack {
1158 pub fn new(app: &mut App) -> RenderHandle<RenderIndexedStack> {
1162 RenderHandle::new_box(
1163 app,
1164 RenderIndexedStack {
1165 render_object: RenderObjectData::new(),
1166 render_box: RenderBoxData::new(),
1167 container: ContainerRenderObjectData::new(),
1168 stack: RenderStackData::new(),
1169 index: Some(0),
1170 },
1171 )
1172 }
1173
1174 pub fn index(self: RenderHandle<Self>, app: &App) -> Option<i32> {
1176 self.get(app).index
1177 }
1178
1179 pub fn set_index(self: RenderHandle<Self>, app: &mut App, value: Option<i32>) {
1181 if self.get(app).index != value {
1182 self.get_mut(app).index = value;
1183 self.mark_needs_layout(app);
1184 }
1185 }
1186
1187 fn child_at_index(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1188 let index = self.index(app)?;
1189 let mut child = self.first_child(app);
1190 let mut i = 0;
1191 while i < index
1192 && let Some(current) = child
1193 {
1194 child = self.child_after(app, current);
1195 i += 1;
1196 }
1197 debug_assert!(self.first_child(app).is_none() || child.is_some());
1198 child
1199 }
1200}
1201
1202impl ContainerRenderObjectMixin for RenderIndexedStack {
1203 type ChildType = AnyRenderBox;
1204 type ParentDataType = StackParentData;
1205
1206 fn container_data(
1207 self: RenderHandle<Self>,
1208 app: &App,
1209 ) -> &ContainerRenderObjectData<AnyRenderBox> {
1210 &self.get(app).container
1211 }
1212
1213 fn container_data_mut(
1214 self: RenderHandle<Self>,
1215 app: &mut App,
1216 ) -> &mut ContainerRenderObjectData<AnyRenderBox> {
1217 &mut self.get_mut(app).container
1218 }
1219}
1220
1221impl RenderBoxContainerDefaultsMixin for RenderIndexedStack {}
1222
1223impl RenderStackBase for RenderIndexedStack {
1224 fn stack_data(self: RenderHandle<Self>, app: &App) -> &RenderStackData {
1225 &self.get(app).stack
1226 }
1227
1228 fn stack_data_mut(self: RenderHandle<Self>, app: &mut App) -> &mut RenderStackData {
1229 &mut self.get_mut(app).stack
1230 }
1231
1232 fn paint_stack(
1233 self: RenderHandle<Self>,
1234 app: &mut App,
1235 context: &mut PaintingContext,
1236 offset: Offset,
1237 ) {
1238 let Some(displayed_child) = self.child_at_index(app) else {
1239 return;
1240 };
1241 let child_offset = displayed_child
1242 .as_object()
1243 .parent_data_of::<StackParentData>(app)
1244 .offset();
1245 context.paint_child(app, displayed_child.as_object(), child_offset + offset);
1246 }
1247}
1248
1249impl RenderObject for RenderIndexedStack {
1250 crate::render_object_accessors!();
1251
1252 fn debug_fill_properties(
1253 self: RenderHandle<Self>,
1254 app: &App,
1255 properties: &mut DiagnosticPropertiesBuilder,
1256 ) {
1257 properties.add(IntProperty::<i32>::new("index", self.get(app).index));
1258 }
1259
1260 fn debug_describe_children(
1261 self: RenderHandle<Self>,
1262 app: &App,
1263 ) -> Vec<Rc<dyn DiagnosticsNode>> {
1264 let index = self.index(app);
1265 let mut children = Vec::new();
1266 let mut i = 0;
1267 let mut child = self.first_child(app);
1268 while let Some(current) = child {
1269 let mut node = current
1270 .as_object()
1271 .to_diagnostics_node(app, format!("child {}", i + 1));
1272 if Some(i) != index {
1273 node = node.with_style(DiagnosticsTreeStyle::Offstage);
1274 }
1275 children.push(Rc::new(node) as Rc<dyn DiagnosticsNode>);
1276 child = self.child_after(app, current);
1277 i += 1;
1278 }
1279 children
1280 }
1281
1282 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
1283 RenderStackBase::perform_layout(self, app)
1284 }
1285
1286 fn visit_children(
1287 self: RenderHandle<Self>,
1288 app: &App,
1289 visitor: &mut dyn FnMut(AnyRenderObject),
1290 ) {
1291 ContainerRenderObjectMixin::visit_children(self, app, visitor)
1292 }
1293
1294 fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
1295 ContainerRenderObjectMixin::did_attach(self, app, owner)
1296 }
1297
1298 fn did_detach(self: RenderHandle<Self>, app: &mut App) {
1299 ContainerRenderObjectMixin::did_detach(self, app)
1300 }
1301
1302 fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
1303 ContainerRenderObjectMixin::redepth_children(self, app)
1304 }
1305
1306 fn paint(
1307 self: RenderHandle<Self>,
1308 app: &mut App,
1309 context: &mut PaintingContext,
1310 offset: Offset,
1311 ) {
1312 RenderStackBase::paint(self, app, context, offset)
1313 }
1314
1315 fn dispose(self: RenderHandle<Self>, app: &mut App) {
1316 RenderStackBase::dispose(self, app)
1317 }
1318
1319 fn describe_approximate_paint_clip(
1320 self: RenderHandle<Self>,
1321 app: &App,
1322 child: AnyRenderObject,
1323 ) -> Option<Rect> {
1324 RenderStackBase::describe_approximate_paint_clip(self, app, child)
1325 }
1326}
1327
1328impl RenderBox for RenderIndexedStack {
1329 crate::render_box_accessors!();
1330
1331 fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
1332 RenderStackBase::setup_parent_data(self, app, child)
1333 }
1334
1335 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
1336 RenderStackBase::compute_min_intrinsic_width(self, app, height)
1337 }
1338
1339 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
1340 RenderStackBase::compute_max_intrinsic_width(self, app, height)
1341 }
1342
1343 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
1344 RenderStackBase::compute_min_intrinsic_height(self, app, width)
1345 }
1346
1347 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
1348 RenderStackBase::compute_max_intrinsic_height(self, app, width)
1349 }
1350
1351 fn compute_distance_to_actual_baseline(
1353 self: RenderHandle<Self>,
1354 app: &mut App,
1355 baseline: TextBaseline,
1356 ) -> Option<f64> {
1357 let displayed_child = self.child_at_index(app)?;
1358 let child_offset = displayed_child
1359 .as_object()
1360 .parent_data_of::<StackParentData>(app)
1361 .offset();
1362 let offset = BaselineOffset(displayed_child.get_distance_to_actual_baseline(app, baseline))
1363 + child_offset.dy();
1364 offset.offset()
1365 }
1366
1367 fn compute_dry_baseline(
1369 self: RenderHandle<Self>,
1370 app: &mut App,
1371 constraints: BoxConstraints,
1372 baseline: TextBaseline,
1373 ) -> Option<f64> {
1374 let displayed_child = self.child_at_index(app)?;
1375 let non_positioned_child_constraints = match self.fit(app) {
1376 StackFit::Loose => constraints.loosen(),
1377 StackFit::Expand => BoxConstraints::tight(constraints.biggest()),
1378 StackFit::Passthrough => constraints,
1379 };
1380
1381 let alignment = self.resolved_alignment(app);
1382 let size = self.as_box().get_dry_layout(app, constraints);
1383
1384 baseline_for_child(
1385 app,
1386 displayed_child,
1387 size,
1388 non_positioned_child_constraints,
1389 alignment,
1390 baseline,
1391 )
1392 }
1393
1394 fn compute_dry_layout(
1395 self: RenderHandle<Self>,
1396 app: &mut App,
1397 constraints: BoxConstraints,
1398 ) -> Size {
1399 RenderStackBase::compute_dry_layout(self, app, constraints)
1400 }
1401
1402 fn hit_test_children(
1403 self: RenderHandle<Self>,
1404 app: &mut App,
1405 result: &mut BoxHitTestResult<'_>,
1406 position: Offset,
1407 ) -> bool {
1408 let Some(displayed_child) = self.child_at_index(app) else {
1409 return false;
1410 };
1411 let child_offset = displayed_child
1412 .as_object()
1413 .parent_data_of::<StackParentData>(app)
1414 .offset();
1415 result.add_with_paint_offset(Some(child_offset), position, |result, transformed| {
1416 debug_assert_eq!(transformed, position - child_offset);
1417 displayed_child.hit_test(app, result, transformed)
1418 })
1419 }
1420}
1421
1422#[cfg(test)]
1423mod tests {
1424 use std::cell::Cell;
1425 use std::rc::Rc;
1426 use waft_foundation::AppCell;
1427
1428 use super::*;
1429 use crate::flex::{MainAxisSize, RenderFlex};
1430 use crate::proxy_box::RenderConstrainedBox;
1431 use crate::shifted_box::RenderPadding;
1432 use crate::testing::{EnginePhase, layout_with, painted_ops, pump_frame};
1433 use waft_embedder::valo::Op;
1434 use waft_painting::EdgeInsetsGeometry;
1435
1436 struct SizedTestBox {
1438 render_object: RenderObjectData,
1439 render_box: RenderBoxData,
1440 preferred_size: Size,
1441 paints: Rc<Cell<u32>>,
1442 }
1443
1444 impl RenderObject for SizedTestBox {
1445 crate::render_object_accessors!();
1446
1447 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
1448 let preferred_size = self.get(app).preferred_size;
1449 let size = self.constraints(app).constrain(preferred_size);
1450 self.set_size(app, size);
1451 }
1452
1453 fn paint(
1454 self: RenderHandle<Self>,
1455 app: &mut App,
1456 _context: &mut PaintingContext,
1457 _offset: Offset,
1458 ) {
1459 let paints = self.get(app).paints.clone();
1460 paints.set(paints.get() + 1);
1461 }
1462 }
1463
1464 impl RenderBox for SizedTestBox {
1465 crate::render_box_accessors!();
1466 }
1467
1468 struct BaselineTestBox {
1471 render_object: RenderObjectData,
1472 render_box: RenderBoxData,
1473 preferred_size: Size,
1474 baseline: f64,
1475 }
1476
1477 impl RenderObject for BaselineTestBox {
1478 crate::render_object_accessors!();
1479
1480 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
1481 let preferred_size = self.get(app).preferred_size;
1482 let size = self.constraints(app).constrain(preferred_size);
1483 self.set_size(app, size);
1484 }
1485 }
1486
1487 impl RenderBox for BaselineTestBox {
1488 crate::render_box_accessors!();
1489
1490 fn compute_dry_layout(
1491 self: RenderHandle<Self>,
1492 app: &mut App,
1493 constraints: BoxConstraints,
1494 ) -> Size {
1495 constraints.constrain(self.get(app).preferred_size)
1496 }
1497
1498 fn compute_distance_to_actual_baseline(
1499 self: RenderHandle<Self>,
1500 app: &mut App,
1501 _baseline: TextBaseline,
1502 ) -> Option<f64> {
1503 Some(self.get(app).baseline)
1504 }
1505
1506 fn compute_dry_baseline(
1507 self: RenderHandle<Self>,
1508 app: &mut App,
1509 _constraints: BoxConstraints,
1510 _baseline: TextBaseline,
1511 ) -> Option<f64> {
1512 Some(self.get(app).baseline)
1513 }
1514 }
1515
1516 fn baseline_box(app: &mut App, size: Size, baseline: f64) -> AnyRenderBox {
1517 RenderHandle::new_box(
1518 app,
1519 BaselineTestBox {
1520 render_object: RenderObjectData::new(),
1521 render_box: RenderBoxData::new(),
1522 preferred_size: size,
1523 baseline,
1524 },
1525 )
1526 .as_box()
1527 }
1528
1529 fn dry_and_actual_baseline(
1531 app: &mut App,
1532 stack: AnyRenderBox,
1533 constraints: BoxConstraints,
1534 ) -> (Option<f64>, Option<f64>) {
1535 let dry = stack.get_dry_baseline(app, constraints, TextBaseline::Alphabetic);
1536 stack.layout(app, constraints, true);
1537 let actual = stack.get_distance_to_baseline(app, TextBaseline::Alphabetic, true);
1538 (dry, actual)
1539 }
1540
1541 fn sized_box(app: &mut App, size: Size) -> (AnyRenderBox, Rc<Cell<u32>>) {
1542 let paints = Rc::new(Cell::new(0));
1543 let box_ = RenderHandle::new_box(
1544 app,
1545 SizedTestBox {
1546 render_object: RenderObjectData::new(),
1547 render_box: RenderBoxData::new(),
1548 preferred_size: size,
1549 paints: paints.clone(),
1550 },
1551 )
1552 .as_box();
1553 (box_, paints)
1554 }
1555
1556 fn picture_has_clip(app: &mut App, render: AnyRenderObject) -> bool {
1558 painted_ops(app, render)
1559 .iter()
1560 .any(|op| matches!(op, Op::ClipPath { .. }))
1561 }
1562
1563 #[test]
1568 fn stack_intrinsics_are_the_largest_non_positioned_child() {
1569 let cell = AppCell::new();
1570 let mut app = cell.borrow_mut();
1571 let first =
1572 RenderConstrainedBox::new(&mut app, BoxConstraints::tight(Size::new(30.0, 40.0)), None);
1573 let second =
1574 RenderConstrainedBox::new(&mut app, BoxConstraints::tight(Size::new(50.0, 20.0)), None);
1575 let positioned = RenderConstrainedBox::new(
1576 &mut app,
1577 BoxConstraints::tight(Size::new(200.0, 200.0)),
1578 None,
1579 );
1580 let stack = RenderStack::new(&mut app);
1581 stack.set_text_direction(&mut app, Some(TextDirection::Ltr));
1582 stack.add_all(
1583 &mut app,
1584 Some(vec![first.as_box(), second.as_box(), positioned.as_box()]),
1585 );
1586 positioned
1587 .as_box()
1588 .parent_data_of_mut::<StackParentData>(&mut app)
1589 .left = Some(0.0);
1590
1591 let box_ = stack.as_box();
1592 assert_eq!(box_.get_min_intrinsic_width(&mut app, 0.0), 50.0);
1593 assert_eq!(box_.get_max_intrinsic_width(&mut app, 0.0), 50.0);
1594 assert_eq!(box_.get_min_intrinsic_height(&mut app, 0.0), 40.0);
1595 assert_eq!(box_.get_max_intrinsic_height(&mut app, 0.0), 40.0);
1596 }
1597
1598 #[test]
1601 fn dry_layout_matches_layout_for_a_padding_in_a_flex_in_a_stack() {
1602 let cell = AppCell::new();
1603 let mut app = cell.borrow_mut();
1604 let leaf =
1605 RenderConstrainedBox::new(&mut app, BoxConstraints::tight(Size::new(40.0, 20.0)), None);
1606 let padding = RenderPadding::new(
1607 &mut app,
1608 EdgeInsetsGeometry::all(5.0),
1609 None,
1610 Some(leaf.as_box()),
1611 );
1612 let row = RenderFlex::new(&mut app);
1613 row.set_text_direction(&mut app, Some(TextDirection::Ltr));
1614 row.set_main_axis_size(&mut app, MainAxisSize::Min);
1615 row.add_all(&mut app, Some(vec![padding.as_box()]));
1616 let stack = RenderStack::new(&mut app);
1617 stack.set_text_direction(&mut app, Some(TextDirection::Ltr));
1618 stack.add_all(&mut app, Some(vec![row.as_box()]));
1619
1620 let constraints = BoxConstraints::new().max_width(300.0).max_height(300.0);
1621 let dry = stack.as_box().get_dry_layout(&mut app, constraints);
1622 stack.layout(&mut app, constraints, false);
1623 assert_eq!(dry, Size::new(50.0, 30.0));
1624 assert_eq!(dry, stack.size(&app));
1625 assert_eq!(row.size(&app), Size::new(50.0, 30.0));
1626 }
1627
1628 #[test]
1629 fn positioned_child_is_laid_out_by_its_rect() {
1630 let cell = AppCell::new();
1631 let mut app = cell.borrow_mut();
1632 let (child, _) = sized_box(&mut app, Size::new(10.0, 10.0));
1633 let stack = RenderStack::new(&mut app);
1634 stack.set_text_direction(&mut app, Some(TextDirection::Ltr));
1635 stack.add(&mut app, child);
1636 child
1637 .parent_data_of_mut::<StackParentData>(&mut app)
1638 .set_rect(RelativeRect::from_ltrb(10.0, 20.0, 30.0, 40.0));
1639
1640 stack.layout(
1641 &mut app,
1642 BoxConstraints::tight(Size::new(100.0, 100.0)),
1643 false,
1644 );
1645
1646 assert_eq!(stack.size(&app), Size::new(100.0, 100.0));
1648 assert_eq!(child.size(&app), Size::new(60.0, 40.0));
1649 assert_eq!(child.box_parent_data(&app).offset, Offset::new(10.0, 20.0));
1650 }
1651
1652 #[test]
1655 fn non_positioned_children_are_sized_by_the_fit() {
1656 let cases = [
1657 (
1658 StackFit::Loose,
1659 Size::new(20.0, 20.0),
1660 Size::new(50.0, 50.0),
1661 ),
1662 (
1663 StackFit::Expand,
1664 Size::new(100.0, 100.0),
1665 Size::new(100.0, 100.0),
1666 ),
1667 (
1668 StackFit::Passthrough,
1669 Size::new(50.0, 50.0),
1670 Size::new(50.0, 50.0),
1671 ),
1672 ];
1673 for (fit, child_size, stack_size) in cases {
1674 let cell = AppCell::new();
1675 let mut app = cell.borrow_mut();
1676 let (child, _) = sized_box(&mut app, Size::new(20.0, 20.0));
1677 let stack = RenderStack::new(&mut app);
1678 stack.set_text_direction(&mut app, Some(TextDirection::Ltr));
1679 stack.set_fit(&mut app, fit);
1680 stack.add(&mut app, child);
1681
1682 stack.layout(
1683 &mut app,
1684 BoxConstraints {
1685 min_width: 50.0,
1686 max_width: 100.0,
1687 min_height: 50.0,
1688 max_height: 100.0,
1689 },
1690 false,
1691 );
1692
1693 assert_eq!(child.size(&app), child_size, "{fit:?}");
1694 assert_eq!(stack.size(&app), stack_size, "{fit:?}");
1695 }
1696 }
1697
1698 #[test]
1701 fn indexed_stack_paints_one_child() {
1702 let cell = AppCell::new();
1703 let mut app = cell.borrow_mut();
1704 let (first, first_paints) = sized_box(&mut app, Size::new(20.0, 20.0));
1705 let (second, second_paints) = sized_box(&mut app, Size::new(30.0, 30.0));
1706 let stack = RenderIndexedStack::new(&mut app);
1707 stack.set_text_direction(&mut app, Some(TextDirection::Ltr));
1708 stack.add_all(&mut app, Some(vec![first, second]));
1709 stack.set_index(&mut app, Some(1));
1710 layout_with(
1711 &mut app,
1712 stack.as_box(),
1713 Some(BoxConstraints::tight(Size::new(100.0, 100.0))),
1714 AlignmentGeometry::CENTER,
1715 EnginePhase::Paint,
1716 );
1717
1718 assert_eq!(first.size(&app), Size::new(20.0, 20.0));
1720 assert_eq!(second.size(&app), Size::new(30.0, 30.0));
1721 assert_eq!(first_paints.get(), 0);
1722 assert_eq!(second_paints.get(), 1);
1723
1724 stack.set_index(&mut app, Some(0));
1725 pump_frame(&mut app, EnginePhase::Paint);
1726 assert_eq!(first_paints.get(), 1);
1727 assert_eq!(second_paints.get(), 1);
1728 }
1729
1730 #[test]
1732 fn overflowing_positioned_child_is_clipped() {
1733 for (clip_behavior, clips) in [(Clip::None, false), (Clip::HardEdge, true)] {
1734 let cell = AppCell::new();
1735 let mut app = cell.borrow_mut();
1736 let (child, _) = sized_box(&mut app, Size::new(10.0, 10.0));
1737 let stack = RenderStack::new(&mut app);
1738 stack.set_text_direction(&mut app, Some(TextDirection::Ltr));
1739 stack.set_clip_behavior(&mut app, clip_behavior);
1740 stack.add(&mut app, child);
1741 child.parent_data_of_mut::<StackParentData>(&mut app).left = Some(-10.0);
1742 layout_with(
1743 &mut app,
1744 stack.as_box(),
1745 Some(BoxConstraints::tight(Size::new(100.0, 100.0))),
1746 AlignmentGeometry::CENTER,
1747 EnginePhase::Paint,
1748 );
1749
1750 let clipped = picture_has_clip(&mut app, stack.as_object());
1751 assert_eq!(clipped, clips, "{clip_behavior:?}");
1752 }
1753 }
1754
1755 #[test]
1758 fn a_stack_baseline_is_its_highest_child_baseline() {
1759 let cell = AppCell::new();
1760 let mut app = cell.borrow_mut();
1761 let short = baseline_box(&mut app, Size::new(40.0, 20.0), 15.0);
1762 let tall = baseline_box(&mut app, Size::new(40.0, 60.0), 50.0);
1763 let pinned = baseline_box(&mut app, Size::new(40.0, 20.0), 15.0);
1764 let stack = RenderStack::new(&mut app);
1765 stack.set_text_direction(&mut app, Some(TextDirection::Ltr));
1766 stack.set_alignment(&mut app, AlignmentGeometry::from(Alignment::CENTER));
1767 stack.add_all(&mut app, Some(vec![short, tall, pinned]));
1768 pinned
1769 .parent_data_of_mut::<StackParentData>(&mut app)
1770 .bottom = Some(0.0);
1771
1772 let constraints = BoxConstraints::new().max_width(100.0).max_height(100.0);
1773 assert_eq!(
1775 dry_and_actual_baseline(&mut app, stack.as_box(), constraints),
1776 (Some(35.0), Some(35.0))
1777 );
1778 }
1779
1780 #[test]
1782 fn an_indexed_stack_baseline_is_its_displayed_child_baseline() {
1783 let cell = AppCell::new();
1784 let mut app = cell.borrow_mut();
1785 let short = baseline_box(&mut app, Size::new(40.0, 20.0), 15.0);
1786 let tall = baseline_box(&mut app, Size::new(40.0, 60.0), 50.0);
1787 let stack = RenderIndexedStack::new(&mut app);
1788 stack.set_text_direction(&mut app, Some(TextDirection::Ltr));
1789 stack.set_alignment(&mut app, AlignmentGeometry::from(Alignment::CENTER));
1790 stack.add_all(&mut app, Some(vec![short, tall]));
1791 stack.set_index(&mut app, Some(0));
1792
1793 let constraints = BoxConstraints::new().max_width(100.0).max_height(100.0);
1794 assert_eq!(
1795 dry_and_actual_baseline(&mut app, stack.as_box(), constraints),
1796 (Some(35.0), Some(35.0))
1797 );
1798 }
1799}