1use std::any::{Any, TypeId};
4use std::fmt;
5
6use waft_embedder::{Offset, Size, TextBaseline, TextDirection};
7use waft_foundation::{App, DiagnosticPropertiesBuilder, DoubleProperty, EnumProperty, Handle};
8use waft_painting::VerticalDirection;
9use waft_rendering::{
10 AnyRenderBox, AnyRenderObject, BaselineOffset, BoxConstraints, BoxHitTestResult, BoxParentData,
11 ContainerBoxParentData, ContainerParentData, ContainerParentDataMixin,
12 ContainerRenderObjectData, ContainerRenderObjectMixin, MainAxisAlignment, PaintingContext,
13 ParentData, PipelineOwner, RenderBox, RenderBoxContainerDefaultsMixin, RenderBoxData,
14 RenderHandle, RenderObject, RenderObjectData,
15};
16
17use crate::framework::{
18 BuildContext, KeyRef, MultiChildRenderObjectWidget, RenderObjectWidget, WidgetRef,
19};
20use crate::widgets::basic::Directionality;
21
22#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
27pub enum OverflowBarAlignment {
28 Start,
31 End,
34 Center,
36}
37
38#[derive(Debug)]
52pub struct OverflowBar {
53 pub key: Option<KeyRef>,
54 pub spacing: f64,
62 pub alignment: Option<MainAxisAlignment>,
70 pub overflow_spacing: f64,
79 pub overflow_alignment: OverflowBarAlignment,
91 pub overflow_direction: VerticalDirection,
107 pub text_direction: Option<TextDirection>,
117 pub children: Vec<WidgetRef>,
118}
119
120impl OverflowBar {
121 pub fn new() -> OverflowBar {
123 OverflowBar {
124 key: None,
125 spacing: 0.0,
126 alignment: None,
127 overflow_spacing: 0.0,
128 overflow_alignment: OverflowBarAlignment::Start,
129 overflow_direction: VerticalDirection::Down,
130 text_direction: None,
131 children: Vec::new(),
132 }
133 }
134
135 pub fn key(mut self, key: KeyRef) -> OverflowBar {
137 self.key = Some(key);
138 self
139 }
140
141 pub fn spacing(mut self, spacing: f64) -> OverflowBar {
143 self.spacing = spacing;
144 self
145 }
146
147 pub fn alignment(mut self, alignment: MainAxisAlignment) -> OverflowBar {
149 self.alignment = Some(alignment);
150 self
151 }
152
153 pub fn overflow_spacing(mut self, overflow_spacing: f64) -> OverflowBar {
155 self.overflow_spacing = overflow_spacing;
156 self
157 }
158
159 pub fn overflow_alignment(mut self, overflow_alignment: OverflowBarAlignment) -> OverflowBar {
161 self.overflow_alignment = overflow_alignment;
162 self
163 }
164
165 pub fn overflow_direction(mut self, overflow_direction: VerticalDirection) -> OverflowBar {
167 self.overflow_direction = overflow_direction;
168 self
169 }
170
171 pub fn text_direction(mut self, text_direction: TextDirection) -> OverflowBar {
173 self.text_direction = Some(text_direction);
174 self
175 }
176
177 pub fn children(mut self, children: Vec<WidgetRef>) -> OverflowBar {
179 self.children = children;
180 self
181 }
182}
183
184impl Default for OverflowBar {
185 fn default() -> OverflowBar {
186 OverflowBar::new()
187 }
188}
189
190impl RenderObjectWidget for OverflowBar {
191 type RenderObject = RenderOverflowBar;
192
193 fn key(&self) -> Option<&KeyRef> {
194 self.key.as_ref()
195 }
196
197 fn debug_fill_properties(&self, _app: &App, properties: &mut DiagnosticPropertiesBuilder) {
198 properties.add(DoubleProperty::new("spacing", self.spacing).default_value(Some(0.0)));
199 properties.add(
200 EnumProperty::<MainAxisAlignment>::new("alignment", self.alignment).default_value(None),
201 );
202 properties.add(
203 DoubleProperty::new("overflowSpacing", self.overflow_spacing).default_value(Some(0.0)),
204 );
205 properties.add(
206 EnumProperty::new("overflowAlignment", self.overflow_alignment)
207 .default_value(Some(OverflowBarAlignment::Start)),
208 );
209 properties.add(
210 EnumProperty::new("overflowDirection", self.overflow_direction)
211 .default_value(Some(VerticalDirection::Down)),
212 );
213 properties.add(
214 EnumProperty::<TextDirection>::new("textDirection", self.text_direction)
215 .default_value(None),
216 );
217 }
218
219 fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
220 let text_direction = self
221 .text_direction
222 .unwrap_or_else(|| Directionality::of(app, context));
223 RenderOverflowBar::new(
224 app,
225 self.spacing,
226 self.alignment,
227 self.overflow_spacing,
228 self.overflow_alignment,
229 self.overflow_direction,
230 text_direction,
231 )
232 .as_object()
233 }
234
235 fn update_render_object(
236 &self,
237 app: &mut App,
238 context: BuildContext,
239 render_object: RenderHandle<RenderOverflowBar>,
240 ) {
241 render_object.set_spacing(app, self.spacing);
242 render_object.set_alignment(app, self.alignment);
243 render_object.set_overflow_spacing(app, self.overflow_spacing);
244 render_object.set_overflow_alignment(app, self.overflow_alignment);
245 render_object.set_overflow_direction(app, self.overflow_direction);
246 let text_direction = self
247 .text_direction
248 .unwrap_or_else(|| Directionality::of(app, context));
249 render_object.set_text_direction(app, text_direction);
250 }
251}
252
253impl MultiChildRenderObjectWidget for OverflowBar {
254 fn children(&self) -> &[WidgetRef] {
255 &self.children
256 }
257}
258
259#[derive(Debug, Default)]
261pub struct OverflowBarParentData {
262 box_parent_data: BoxParentData,
263 container_parent_data: ContainerParentData<AnyRenderBox>,
264}
265
266impl OverflowBarParentData {
267 const fn new() -> OverflowBarParentData {
268 OverflowBarParentData {
269 box_parent_data: BoxParentData::new(),
270 container_parent_data: ContainerParentData::new(),
271 }
272 }
273}
274
275impl ParentData for OverflowBarParentData {
276 fn detach(&mut self) {
277 ContainerParentDataMixin::detach(self);
278 }
279
280 fn provide(&self, id: TypeId) -> Option<&dyn Any> {
281 if id == TypeId::of::<OverflowBarParentData>() {
282 return Some(self);
283 }
284 self.box_parent_data.provide(id)
285 }
286
287 fn provide_mut(&mut self, id: TypeId) -> Option<&mut dyn Any> {
288 if id == TypeId::of::<OverflowBarParentData>() {
289 return Some(self);
290 }
291 self.box_parent_data.provide_mut(id)
292 }
293}
294
295impl ContainerParentDataMixin for OverflowBarParentData {
296 type ChildType = AnyRenderBox;
297
298 fn container_parent_data(&self) -> &ContainerParentData<AnyRenderBox> {
299 &self.container_parent_data
300 }
301
302 fn container_parent_data_mut(&mut self) -> &mut ContainerParentData<AnyRenderBox> {
303 &mut self.container_parent_data
304 }
305}
306
307impl ContainerBoxParentData for OverflowBarParentData {
308 fn box_parent_data(&self) -> &BoxParentData {
309 &self.box_parent_data
310 }
311
312 fn box_parent_data_mut(&mut self) -> &mut BoxParentData {
313 &mut self.box_parent_data
314 }
315}
316
317impl fmt::Display for OverflowBarParentData {
318 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
319 fmt::Display::fmt(&self.box_parent_data, f)
320 }
321}
322
323pub struct RenderOverflowBar {
326 render_object: RenderObjectData,
327 render_box: RenderBoxData,
328 container: ContainerRenderObjectData<AnyRenderBox>,
329 spacing: f64,
330 alignment: Option<MainAxisAlignment>,
331 overflow_spacing: f64,
332 overflow_alignment: OverflowBarAlignment,
333 overflow_direction: VerticalDirection,
334 text_direction: TextDirection,
335}
336
337type NextChild = fn(RenderHandle<RenderOverflowBar>, &App, AnyRenderBox) -> Option<AnyRenderBox>;
339
340impl RenderOverflowBar {
341 fn new(
342 app: &mut App,
343 spacing: f64,
344 alignment: Option<MainAxisAlignment>,
345 overflow_spacing: f64,
346 overflow_alignment: OverflowBarAlignment,
347 overflow_direction: VerticalDirection,
348 text_direction: TextDirection,
349 ) -> RenderHandle<RenderOverflowBar> {
350 RenderHandle::new_box(
351 app,
352 RenderOverflowBar {
353 render_object: RenderObjectData::new(),
354 render_box: RenderBoxData::new(),
355 container: ContainerRenderObjectData::new(),
356 spacing,
357 alignment,
358 overflow_spacing,
359 overflow_alignment,
360 overflow_direction,
361 text_direction,
362 },
363 )
364 }
365
366 pub fn spacing(self: RenderHandle<Self>, app: &App) -> f64 {
368 self.get(app).spacing
369 }
370
371 pub fn set_spacing(self: RenderHandle<Self>, app: &mut App, value: f64) {
373 if self.get(app).spacing == value {
374 return;
375 }
376 self.get_mut(app).spacing = value;
377 self.mark_needs_layout(app);
378 }
379
380 pub fn alignment(self: RenderHandle<Self>, app: &App) -> Option<MainAxisAlignment> {
382 self.get(app).alignment
383 }
384
385 pub fn set_alignment(
387 self: RenderHandle<Self>,
388 app: &mut App,
389 value: Option<MainAxisAlignment>,
390 ) {
391 if self.get(app).alignment == value {
392 return;
393 }
394 self.get_mut(app).alignment = value;
395 self.mark_needs_layout(app);
396 }
397
398 pub fn overflow_spacing(self: RenderHandle<Self>, app: &App) -> f64 {
400 self.get(app).overflow_spacing
401 }
402
403 pub fn set_overflow_spacing(self: RenderHandle<Self>, app: &mut App, value: f64) {
405 if self.get(app).overflow_spacing == value {
406 return;
407 }
408 self.get_mut(app).overflow_spacing = value;
409 self.mark_needs_layout(app);
410 }
411
412 pub fn overflow_alignment(self: RenderHandle<Self>, app: &App) -> OverflowBarAlignment {
414 self.get(app).overflow_alignment
415 }
416
417 pub fn set_overflow_alignment(
419 self: RenderHandle<Self>,
420 app: &mut App,
421 value: OverflowBarAlignment,
422 ) {
423 if self.get(app).overflow_alignment == value {
424 return;
425 }
426 self.get_mut(app).overflow_alignment = value;
427 self.mark_needs_layout(app);
428 }
429
430 pub fn overflow_direction(self: RenderHandle<Self>, app: &App) -> VerticalDirection {
432 self.get(app).overflow_direction
433 }
434
435 pub fn set_overflow_direction(
437 self: RenderHandle<Self>,
438 app: &mut App,
439 value: VerticalDirection,
440 ) {
441 if self.get(app).overflow_direction == value {
442 return;
443 }
444 self.get_mut(app).overflow_direction = value;
445 self.mark_needs_layout(app);
446 }
447
448 pub fn text_direction(self: RenderHandle<Self>, app: &App) -> TextDirection {
450 self.get(app).text_direction
451 }
452
453 pub fn set_text_direction(self: RenderHandle<Self>, app: &mut App, value: TextDirection) {
455 if self.get(app).text_direction == value {
456 return;
457 }
458 self.get_mut(app).text_direction = value;
459 self.mark_needs_layout(app);
460 }
461
462 fn compute_intrinsic_height(
465 self: RenderHandle<Self>,
466 app: &mut App,
467 width: f64,
468 child_height: fn(AnyRenderBox, &mut App, f64) -> f64,
469 ) -> f64 {
470 let mut child = self.first_child(app);
471 if child.is_none() {
472 return 0.0;
473 }
474 let child_count = self.child_count(app) as f64;
475 let mut bar_width = 0.0;
476 while let Some(current) = child {
477 bar_width += current.get_min_intrinsic_width(app, f64::INFINITY);
478 child = self.child_after(app, current);
479 }
480 bar_width += self.spacing(app) * (child_count - 1.0);
481 let mut height: f64 = 0.0;
482 if bar_width > width {
483 child = self.first_child(app);
484 while let Some(current) = child {
485 height += child_height(current, app, width);
486 child = self.child_after(app, current);
487 }
488 height + self.overflow_spacing(app) * (child_count - 1.0)
489 } else {
490 child = self.first_child(app);
491 while let Some(current) = child {
492 height = height.max(child_height(current, app, width));
493 child = self.child_after(app, current);
494 }
495 height
496 }
497 }
498
499 fn compute_intrinsic_width(
501 self: RenderHandle<Self>,
502 app: &mut App,
503 child_width: fn(AnyRenderBox, &mut App, f64) -> f64,
504 ) -> f64 {
505 let mut child = self.first_child(app);
506 if child.is_none() {
507 return 0.0;
508 }
509 let mut width = 0.0;
510 while let Some(current) = child {
511 width += child_width(current, app, f64::INFINITY);
512 child = self.child_after(app, current);
513 }
514 width + self.spacing(app) * (self.child_count(app) as f64 - 1.0)
515 }
516}
517
518impl ContainerRenderObjectMixin for RenderOverflowBar {
519 type ChildType = AnyRenderBox;
520 type ParentDataType = OverflowBarParentData;
521
522 fn container_data(
523 self: RenderHandle<Self>,
524 app: &App,
525 ) -> &ContainerRenderObjectData<AnyRenderBox> {
526 &self.get(app).container
527 }
528
529 fn container_data_mut(
530 self: RenderHandle<Self>,
531 app: &mut App,
532 ) -> &mut ContainerRenderObjectData<AnyRenderBox> {
533 &mut self.get_mut(app).container
534 }
535}
536
537impl RenderBoxContainerDefaultsMixin for RenderOverflowBar {}
538
539impl RenderObject for RenderOverflowBar {
540 waft_rendering::render_object_accessors!();
541
542 fn debug_fill_properties(
543 self: RenderHandle<Self>,
544 app: &App,
545 properties: &mut DiagnosticPropertiesBuilder,
546 ) {
547 let this = self.get(app);
548 properties.add(DoubleProperty::new("spacing", Some(this.spacing)).default_value(Some(0.0)));
549 properties.add(
550 DoubleProperty::new("overflowSpacing", Some(this.overflow_spacing))
551 .default_value(Some(0.0)),
552 );
553 properties.add(
554 EnumProperty::<OverflowBarAlignment>::new(
555 "overflowAlignment",
556 Some(this.overflow_alignment),
557 )
558 .default_value(Some(OverflowBarAlignment::Start)),
559 );
560 properties.add(
561 EnumProperty::<VerticalDirection>::new(
562 "overflowDirection",
563 Some(this.overflow_direction),
564 )
565 .default_value(Some(VerticalDirection::Down)),
566 );
567 properties.add(
568 EnumProperty::<TextDirection>::new("textDirection", Some(this.text_direction))
569 .default_value(None),
570 );
571 }
572
573 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
574 let constraints = self.constraints(app);
575 let mut child = self.first_child(app);
576 if child.is_none() {
577 self.set_size(app, constraints.smallest());
578 return;
579 }
580
581 let child_constraints = constraints.loosen();
582 let mut children_width = 0.0;
583 let mut max_child_height: f64 = 0.0;
584 let mut max_child_width: f64 = 0.0;
585 while let Some(current) = child {
586 current.layout(app, child_constraints, true);
587 let child_size = current.size(app);
588 children_width += child_size.width();
589 max_child_height = max_child_height.max(child_size.height());
590 max_child_width = max_child_width.max(child_size.width());
591 child = self.child_after(app, current);
592 }
593 let _ = max_child_width;
595
596 let rtl = self.text_direction(app) == TextDirection::Rtl;
597 let child_count = self.child_count(app) as f64;
598 let spacing = self.spacing(app);
599 let actual_width = children_width + spacing * (child_count - 1.0);
600 if actual_width > constraints.max_width {
601 let down = self.overflow_direction(app) == VerticalDirection::Down;
603 child = if down {
604 self.first_child(app)
605 } else {
606 self.last_child(app)
607 };
608 let next_child: NextChild = if down {
609 |this, app, child| this.child_after(app, child)
610 } else {
611 |this, app, child| this.child_before(app, child)
612 };
613 let overflow_alignment = self.overflow_alignment(app);
614 let overflow_spacing = self.overflow_spacing(app);
615 let mut y = 0.0;
616 while let Some(current) = child {
617 let child_width = current.size(app).width();
618 let x = match overflow_alignment {
619 OverflowBarAlignment::Center => (constraints.max_width - child_width) / 2.0,
620 OverflowBarAlignment::Start => {
621 if rtl {
622 constraints.max_width - child_width
623 } else {
624 0.0
625 }
626 }
627 OverflowBarAlignment::End => {
628 if rtl {
629 0.0
630 } else {
631 constraints.max_width - child_width
632 }
633 }
634 };
635 current
636 .as_object()
637 .parent_data_of_mut::<OverflowBarParentData>(app)
638 .set_offset(Offset::new(x, y));
639 y += current.size(app).height() + overflow_spacing;
640 child = next_child(self, app, current);
641 }
642 self.set_size(
643 app,
644 constraints.constrain(Size::new(constraints.max_width, y - overflow_spacing)),
645 );
646 } else {
647 child = self.first_child(app);
649 let first_child_width = child.expect("a first child").size(app).width();
650 let alignment = self.alignment(app);
651 let overall_width = if alignment.is_none() {
652 actual_width
653 } else {
654 constraints.max_width
655 };
656 self.set_size(
657 app,
658 constraints.constrain(Size::new(overall_width, max_child_height)),
659 );
660 let size = self.size(app);
661 let mut x;
663 let mut layout_spacing = spacing;
665 match alignment {
666 None | Some(MainAxisAlignment::Start) => {
667 x = if rtl {
668 size.width() - first_child_width
669 } else {
670 0.0
671 };
672 }
673 Some(MainAxisAlignment::Center) => {
674 let half_remaining_width = (size.width() - actual_width) / 2.0;
675 x = if rtl {
676 size.width() - half_remaining_width - first_child_width
677 } else {
678 half_remaining_width
679 };
680 }
681 Some(MainAxisAlignment::End) => {
682 x = if rtl {
683 actual_width - first_child_width
684 } else {
685 size.width() - actual_width
686 };
687 }
688 Some(MainAxisAlignment::SpaceBetween) => {
689 layout_spacing = (size.width() - children_width) / (child_count - 1.0);
690 x = if rtl {
691 size.width() - first_child_width
692 } else {
693 0.0
694 };
695 }
696 Some(MainAxisAlignment::SpaceAround) => {
697 layout_spacing = if child_count > 0.0 {
698 (size.width() - children_width) / child_count
699 } else {
700 0.0
701 };
702 x = if rtl {
703 size.width() - layout_spacing / 2.0 - first_child_width
704 } else {
705 layout_spacing / 2.0
706 };
707 }
708 Some(MainAxisAlignment::SpaceEvenly) => {
709 layout_spacing = (size.width() - children_width) / (child_count + 1.0);
710 x = if rtl {
711 size.width() - layout_spacing - first_child_width
712 } else {
713 layout_spacing
714 };
715 }
716 }
717 while let Some(current) = child {
718 let child_size = current.size(app);
719 current
720 .as_object()
721 .parent_data_of_mut::<OverflowBarParentData>(app)
722 .set_offset(Offset::new(
723 x,
724 (max_child_height - child_size.height()) / 2.0,
725 ));
726 if !rtl {
731 x += child_size.width() + layout_spacing;
732 }
733 child = self.child_after(app, current);
734 if rtl && let Some(next) = child {
735 x -= next.size(app).width() + layout_spacing;
736 }
737 }
738 }
739 }
740
741 fn visit_children(
742 self: RenderHandle<Self>,
743 app: &App,
744 visitor: &mut dyn FnMut(AnyRenderObject),
745 ) {
746 ContainerRenderObjectMixin::visit_children(self, app, visitor)
747 }
748
749 fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
750 ContainerRenderObjectMixin::did_attach(self, app, owner)
751 }
752
753 fn did_detach(self: RenderHandle<Self>, app: &mut App) {
754 ContainerRenderObjectMixin::did_detach(self, app)
755 }
756
757 fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
758 ContainerRenderObjectMixin::redepth_children(self, app)
759 }
760
761 fn paint(
762 self: RenderHandle<Self>,
763 app: &mut App,
764 context: &mut PaintingContext,
765 offset: Offset,
766 ) {
767 self.default_paint(app, context, offset);
768 }
769}
770
771impl RenderBox for RenderOverflowBar {
772 waft_rendering::render_box_accessors!();
773
774 fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
775 if !child.parent_data_is::<OverflowBarParentData>(app) {
776 child.set_parent_data(app, OverflowBarParentData::new());
777 }
778 }
779
780 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
781 self.compute_intrinsic_height(app, width, |child, app, width| {
782 child.get_min_intrinsic_height(app, width)
783 })
784 }
785
786 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
787 self.compute_intrinsic_height(app, width, |child, app, width| {
788 child.get_max_intrinsic_height(app, width)
789 })
790 }
791
792 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, _height: f64) -> f64 {
793 self.compute_intrinsic_width(app, |child, app, height| {
794 child.get_min_intrinsic_width(app, height)
795 })
796 }
797
798 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, _height: f64) -> f64 {
799 self.compute_intrinsic_width(app, |child, app, height| {
800 child.get_max_intrinsic_width(app, height)
801 })
802 }
803
804 fn compute_distance_to_actual_baseline(
805 self: RenderHandle<Self>,
806 app: &mut App,
807 baseline: TextBaseline,
808 ) -> Option<f64> {
809 self.default_compute_distance_to_highest_actual_baseline(app, baseline)
810 }
811
812 fn compute_dry_baseline(
813 self: RenderHandle<Self>,
814 app: &mut App,
815 constraints: BoxConstraints,
816 baseline: TextBaseline,
817 ) -> Option<f64> {
818 let child_constraints = constraints.loosen();
819 let (next, start_child): (NextChild, Option<AnyRenderBox>) =
820 match self.overflow_direction(app) {
821 VerticalDirection::Down => (
822 |this, app, child| this.child_after(app, child),
823 self.first_child(app),
824 ),
825 VerticalDirection::Up => (
826 |this, app, child| this.child_before(app, child),
827 self.last_child(app),
828 ),
829 };
830 let overflow_spacing = self.overflow_spacing(app);
831 let mut max_child_height = 0.0;
832 let mut y = 0.0;
833 let mut children_width = 0.0;
834 let mut min_horizontal_baseline = BaselineOffset::NO_BASELINE;
835 let mut vertical_baseline = BaselineOffset::NO_BASELINE;
836 let mut child = start_child;
837 while let Some(current) = child {
838 let child_size = current.get_dry_layout(app, child_constraints);
839 let height_diff = child_size.height() - max_child_height;
840 if height_diff > 0.0 {
841 min_horizontal_baseline = min_horizontal_baseline + height_diff / 2.0;
842 max_child_height = child_size.height();
843 }
844 let baseline_offset =
845 BaselineOffset(current.get_dry_baseline(app, child_constraints, baseline));
846 if baseline_offset.offset().is_some() {
847 if vertical_baseline.offset().is_none() {
848 vertical_baseline = baseline_offset + y;
849 }
850 min_horizontal_baseline = min_horizontal_baseline
851 .min_of(baseline_offset + (max_child_height - child_size.height()));
852 }
853 y += child_size.height() + overflow_spacing;
854 children_width += child_size.width();
855 child = next(self, app, current);
856 }
857 debug_assert!(
858 vertical_baseline.offset().is_none() == min_horizontal_baseline.offset().is_none()
859 );
860 if children_width + self.spacing(app) * (self.child_count(app) as f64 - 1.0)
861 > constraints.max_width
862 {
863 vertical_baseline.offset()
864 } else {
865 min_horizontal_baseline.offset()
866 }
867 }
868
869 fn compute_dry_layout(
870 self: RenderHandle<Self>,
871 app: &mut App,
872 constraints: BoxConstraints,
873 ) -> Size {
874 let mut child = self.first_child(app);
875 if child.is_none() {
876 return constraints.smallest();
877 }
878 let child_constraints = constraints.loosen();
879 let overflow_spacing = self.overflow_spacing(app);
880 let mut children_width = 0.0;
881 let mut max_child_height: f64 = 0.0;
882 let mut y = 0.0;
883 while let Some(current) = child {
884 let child_size = current.get_dry_layout(app, child_constraints);
885 children_width += child_size.width();
886 max_child_height = max_child_height.max(child_size.height());
887 y += child_size.height() + overflow_spacing;
888 child = self.child_after(app, current);
889 }
890 let actual_width =
891 children_width + self.spacing(app) * (self.child_count(app) as f64 - 1.0);
892 if actual_width > constraints.max_width {
893 constraints.constrain(Size::new(constraints.max_width, y - overflow_spacing))
894 } else {
895 let overall_width = if self.alignment(app).is_none() {
896 actual_width
897 } else {
898 constraints.max_width
899 };
900 constraints.constrain(Size::new(overall_width, max_child_height))
901 }
902 }
903
904 fn hit_test_children(
905 self: RenderHandle<Self>,
906 app: &mut App,
907 result: &mut BoxHitTestResult<'_>,
908 position: Offset,
909 ) -> bool {
910 self.default_hit_test_children(app, result, position)
911 }
912}
913
914#[cfg(test)]
915mod tests {
916
917 use waft_embedder::Rect;
918 use waft_foundation::{AppCell, DiagnosticLevel};
919 use waft_painting::AlignmentGeometry;
920
921 use super::*;
922 use crate::framework::{GlobalKey, IntoWidget};
923 use crate::test_harness::Harness;
924 use crate::widgets::basic::{
925 Align, Center, Column, ConstrainedBox, IntrinsicHeight, IntrinsicWidth, Row, SizedBox,
926 };
927 use crate::widgets::container::Container;
928
929 fn keyed(key: &GlobalKey, width: f64, height: f64) -> WidgetRef {
930 SizedBox::new()
931 .key(key.clone().into())
932 .width(width)
933 .height(height)
934 .into_widget()
935 }
936
937 fn rect_of(app: &mut App, key: &GlobalKey) -> Rect {
938 let render_box = key
939 .current_context(app)
940 .expect("mounted")
941 .find_render_object(app)
942 .expect("a render object")
943 .as_box()
944 .expect("a box");
945 render_box.local_to_global(app, Offset::ZERO, None) & render_box.size(app)
946 }
947
948 fn assert_rects(app: &mut App, keys: &[GlobalKey; 3], expected: [Rect; 3]) {
950 let actual = [
951 rect_of(app, &keys[0]),
952 rect_of(app, &keys[1]),
953 rect_of(app, &keys[2]),
954 ];
955 assert_eq!(actual, expected);
956 }
957
958 fn mount(app: &mut App, harness: &mut Option<Harness>, tree: WidgetRef) {
959 match harness {
960 Some(harness) => {
961 harness.set_child(app, tree);
962 harness.pump(app);
963 }
964 None => {
965 let mounted = Harness::mount(app, tree);
966 mounted.pump(app);
967 *harness = Some(mounted);
968 }
969 }
970 }
971
972 fn bar_size(app: &App, harness: &Option<Harness>) -> Size {
973 let root = harness.as_ref().expect("mounted").render_root(app);
974 let mut found = None;
975 fn walk(app: &App, node: AnyRenderObject, found: &mut Option<Size>) {
976 if let Some(bar) = node.downcast::<RenderOverflowBar>(app) {
977 *found = Some(bar.size(app));
978 return;
979 }
980 node.visit_children(app, &mut |child| {
981 if found.is_none() {
982 walk(app, child, found);
983 }
984 });
985 }
986 walk(app, root.as_object(), &mut found);
987 found.expect("an overflow bar")
988 }
989
990 #[test]
992 fn the_documented_defaults() {
993 let bar = OverflowBar::new();
994 assert_eq!(bar.spacing, 0.0);
995 assert_eq!(bar.alignment, None);
996 assert_eq!(bar.overflow_spacing, 0.0);
997 assert_eq!(bar.overflow_direction, VerticalDirection::Down);
998 assert_eq!(bar.text_direction, None);
999 assert!(bar.children.is_empty());
1000 }
1001
1002 #[test]
1006 fn an_overflow_bar_dump_shows_its_layout_properties() {
1007 let cell = AppCell::new();
1008 let mut app = cell.borrow_mut();
1009 let key = GlobalKey::new();
1010 let tree = Directionality::new(
1011 TextDirection::Ltr,
1012 OverflowBar::new()
1013 .key(key.clone().into())
1014 .spacing(8.0)
1015 .alignment(MainAxisAlignment::Center)
1016 .overflow_spacing(4.0)
1017 .overflow_alignment(OverflowBarAlignment::End)
1018 .overflow_direction(VerticalDirection::Up)
1019 .children(vec![SizedBox::new().width(20.0).height(20.0).into_widget()]),
1020 )
1021 .into_widget();
1022 let harness = Harness::mount(&mut app, tree);
1023 harness.pump(&mut app);
1024 let bar = key
1025 .current_context(&app)
1026 .expect("mounted")
1027 .find_render_object(&app)
1028 .expect("a render object")
1029 .downcast::<RenderOverflowBar>(&app)
1030 .expect("an overflow bar");
1031
1032 let dump = bar
1033 .as_object()
1034 .to_string_deep_with(&app, DiagnosticLevel::Info);
1035 assert!(dump.contains("spacing: 8.0"), "{dump}");
1036 assert!(dump.contains("overflowSpacing: 4.0"), "{dump}");
1037 assert!(dump.contains("overflowAlignment: end"), "{dump}");
1038 assert!(dump.contains("overflowDirection: up"), "{dump}");
1039 assert!(dump.contains("textDirection: ltr"), "{dump}");
1040 assert!(!dump.contains(" alignment:"), "{dump}");
1041 }
1042
1043 #[test]
1045 fn an_empty_bar_takes_the_smallest_size() {
1046 let cell = AppCell::new();
1047 let mut app = cell.borrow_mut();
1048 let mut harness = None;
1049 let size = Size::new(16.0, 24.0);
1050 mount(
1051 &mut app,
1052 &mut harness,
1053 Directionality::new(
1054 TextDirection::Ltr,
1055 Center::new().child(
1056 ConstrainedBox::new(BoxConstraints::tight(size)).child(OverflowBar::new()),
1057 ),
1058 )
1059 .into_widget(),
1060 );
1061 assert_eq!(bar_size(&app, &harness), size);
1062 mount(
1063 &mut app,
1064 &mut harness,
1065 Directionality::new(TextDirection::Ltr, Center::new().child(OverflowBar::new()))
1066 .into_widget(),
1067 );
1068 assert_eq!(bar_size(&app, &harness), Size::ZERO);
1069 }
1070
1071 #[test]
1073 fn a_bar_that_fits_lays_its_children_out_in_a_centred_row() {
1074 let cell = AppCell::new();
1075 let mut app = cell.borrow_mut();
1076 let keys = [GlobalKey::new(), GlobalKey::new(), GlobalKey::new()];
1077 let mut harness = None;
1078 let mut frame = |app: &mut App, spacing: f64, text_direction: TextDirection| {
1079 let tree = Directionality::new(
1080 text_direction,
1081 Align::new().alignment(AlignmentGeometry::TOP_LEFT).child(
1082 OverflowBar::new().spacing(spacing).children(vec![
1083 keyed(&keys[0], 48.0, 48.0),
1084 keyed(&keys[1], 64.0, 64.0),
1085 keyed(&keys[2], 32.0, 32.0),
1086 ]),
1087 ),
1088 )
1089 .into_widget();
1090 mount(app, &mut harness, tree);
1091 };
1092 frame(&mut app, 0.0, TextDirection::Ltr);
1093 assert_rects(
1094 &mut app,
1095 &keys,
1096 [
1097 Rect::from_ltrb(0.0, 8.0, 48.0, 56.0),
1098 Rect::from_ltrb(48.0, 0.0, 112.0, 64.0),
1099 Rect::from_ltrb(112.0, 16.0, 144.0, 48.0),
1100 ],
1101 );
1102 frame(&mut app, 10.0, TextDirection::Ltr);
1103 assert_rects(
1104 &mut app,
1105 &keys,
1106 [
1107 Rect::from_ltrb(0.0, 8.0, 48.0, 56.0),
1108 Rect::from_ltrb(58.0, 0.0, 122.0, 64.0),
1109 Rect::from_ltrb(132.0, 16.0, 164.0, 48.0),
1110 ],
1111 );
1112 frame(&mut app, 0.0, TextDirection::Rtl);
1114 assert_rects(
1115 &mut app,
1116 &keys,
1117 [
1118 Rect::from_ltrb(96.0, 8.0, 144.0, 56.0),
1119 Rect::from_ltrb(32.0, 0.0, 96.0, 64.0),
1120 Rect::from_ltrb(0.0, 16.0, 32.0, 48.0),
1121 ],
1122 );
1123 frame(&mut app, 10.0, TextDirection::Rtl);
1124 assert_rects(
1125 &mut app,
1126 &keys,
1127 [
1128 Rect::from_ltrb(116.0, 8.0, 164.0, 56.0),
1129 Rect::from_ltrb(42.0, 0.0, 106.0, 64.0),
1130 Rect::from_ltrb(0.0, 16.0, 32.0, 48.0),
1131 ],
1132 );
1133 }
1134
1135 #[test]
1137 fn a_bar_that_overflows_lays_its_children_out_in_an_aligned_column() {
1138 let cell = AppCell::new();
1139 let mut app = cell.borrow_mut();
1140 let keys = [GlobalKey::new(), GlobalKey::new(), GlobalKey::new()];
1141 let mut harness = None;
1142 let mut frame = |app: &mut App,
1143 overflow_spacing: f64,
1144 overflow_direction: VerticalDirection,
1145 overflow_alignment: OverflowBarAlignment,
1146 text_direction: TextDirection| {
1147 let tree = Directionality::new(
1148 text_direction,
1149 Align::new().alignment(AlignmentGeometry::TOP_LEFT).child(
1150 ConstrainedBox::new(BoxConstraints::loose(Size::new(100.0, f64::INFINITY)))
1151 .child(
1152 OverflowBar::new()
1153 .overflow_spacing(overflow_spacing)
1154 .overflow_alignment(overflow_alignment)
1155 .overflow_direction(overflow_direction)
1156 .children(vec![
1157 keyed(&keys[0], 48.0, 48.0),
1158 keyed(&keys[1], 64.0, 64.0),
1159 keyed(&keys[2], 32.0, 32.0),
1160 ]),
1161 ),
1162 ),
1163 )
1164 .into_widget();
1165 mount(app, &mut harness, tree);
1166 };
1167 use OverflowBarAlignment::{Center as Middle, End, Start};
1168 use VerticalDirection::{Down, Up};
1169
1170 frame(&mut app, 0.0, Down, Start, TextDirection::Ltr);
1173 assert_rects(
1174 &mut app,
1175 &keys,
1176 [
1177 Rect::from_ltrb(0.0, 0.0, 48.0, 48.0),
1178 Rect::from_ltrb(0.0, 48.0, 64.0, 112.0),
1179 Rect::from_ltrb(0.0, 112.0, 32.0, 144.0),
1180 ],
1181 );
1182 frame(&mut app, 0.0, Down, End, TextDirection::Rtl);
1183 assert_rects(
1184 &mut app,
1185 &keys,
1186 [
1187 Rect::from_ltrb(0.0, 0.0, 48.0, 48.0),
1188 Rect::from_ltrb(0.0, 48.0, 64.0, 112.0),
1189 Rect::from_ltrb(0.0, 112.0, 32.0, 144.0),
1190 ],
1191 );
1192 frame(&mut app, 10.0, Down, Start, TextDirection::Ltr);
1194 assert_rects(
1195 &mut app,
1196 &keys,
1197 [
1198 Rect::from_ltrb(0.0, 0.0, 48.0, 48.0),
1199 Rect::from_ltrb(0.0, 58.0, 64.0, 122.0),
1200 Rect::from_ltrb(0.0, 132.0, 32.0, 164.0),
1201 ],
1202 );
1203 frame(&mut app, 0.0, Up, Start, TextDirection::Ltr);
1205 assert_rects(
1206 &mut app,
1207 &keys,
1208 [
1209 Rect::from_ltrb(0.0, 96.0, 48.0, 144.0),
1210 Rect::from_ltrb(0.0, 32.0, 64.0, 96.0),
1211 Rect::from_ltrb(0.0, 0.0, 32.0, 32.0),
1212 ],
1213 );
1214 frame(&mut app, 10.0, Up, Start, TextDirection::Ltr);
1215 assert_rects(
1216 &mut app,
1217 &keys,
1218 [
1219 Rect::from_ltrb(0.0, 116.0, 48.0, 164.0),
1220 Rect::from_ltrb(0.0, 42.0, 64.0, 106.0),
1221 Rect::from_ltrb(0.0, 0.0, 32.0, 32.0),
1222 ],
1223 );
1224 frame(&mut app, 0.0, Down, End, TextDirection::Ltr);
1226 assert_rects(
1227 &mut app,
1228 &keys,
1229 [
1230 Rect::from_ltrb(52.0, 0.0, 100.0, 48.0),
1231 Rect::from_ltrb(36.0, 48.0, 100.0, 112.0),
1232 Rect::from_ltrb(68.0, 112.0, 100.0, 144.0),
1233 ],
1234 );
1235 frame(&mut app, 0.0, Down, Start, TextDirection::Rtl);
1236 assert_rects(
1237 &mut app,
1238 &keys,
1239 [
1240 Rect::from_ltrb(52.0, 0.0, 100.0, 48.0),
1241 Rect::from_ltrb(36.0, 48.0, 100.0, 112.0),
1242 Rect::from_ltrb(68.0, 112.0, 100.0, 144.0),
1243 ],
1244 );
1245 frame(&mut app, 0.0, Down, Middle, TextDirection::Ltr);
1247 assert_rects(
1248 &mut app,
1249 &keys,
1250 [
1251 Rect::from_ltrb(26.0, 0.0, 74.0, 48.0),
1252 Rect::from_ltrb(18.0, 48.0, 82.0, 112.0),
1253 Rect::from_ltrb(34.0, 112.0, 66.0, 144.0),
1254 ],
1255 );
1256 }
1257
1258 fn intrinsic_children() -> Vec<WidgetRef> {
1259 vec![
1260 SizedBox::new().width(48.0).height(50.0).into_widget(),
1261 SizedBox::new().width(64.0).height(25.0).into_widget(),
1262 SizedBox::new().width(32.0).height(75.0).into_widget(),
1263 ]
1264 }
1265
1266 #[test]
1268 fn the_intrinsics_follow_the_row_or_the_column() {
1269 let cell = AppCell::new();
1270 let mut app = cell.borrow_mut();
1271 let mut harness = None;
1272 let bar = || {
1273 OverflowBar::new()
1274 .spacing(4.0)
1275 .overflow_spacing(8.0)
1276 .children(intrinsic_children())
1277 };
1278 for (width, expected) in [(300.0, 152.0), (150.0, 150.0)] {
1279 let tree = Directionality::new(
1280 TextDirection::Ltr,
1281 Center::new().child(
1282 Container::new()
1283 .width(width)
1284 .alignment(AlignmentGeometry::TOP_LEFT)
1285 .child(IntrinsicWidth::new().child(bar())),
1286 ),
1287 )
1288 .into_widget();
1289 mount(&mut app, &mut harness, tree);
1290 assert_eq!(bar_size(&app, &harness).width(), expected, "width {width}");
1291 }
1292 harness = None;
1293 for (width, expected) in [(300.0, 75.0), (150.0, 166.0)] {
1294 let tree = Directionality::new(
1295 TextDirection::Ltr,
1296 Center::new().child(
1297 Container::new()
1298 .width(width)
1299 .alignment(AlignmentGeometry::TOP_LEFT)
1300 .child(IntrinsicHeight::new().child(bar())),
1301 ),
1302 )
1303 .into_widget();
1304 mount(&mut app, &mut harness, tree);
1305 assert_eq!(bar_size(&app, &harness).height(), expected, "width {width}");
1306 }
1307 }
1308
1309 #[test]
1311 fn a_bar_wider_than_its_intrinsic_width_starts_its_row_at_the_start() {
1312 let cell = AppCell::new();
1313 let mut app = cell.borrow_mut();
1314 let keys = [GlobalKey::new(), GlobalKey::new(), GlobalKey::new()];
1315 let mut harness = None;
1316 for (text_direction, lefts) in [
1317 (TextDirection::Ltr, [0.0, 60.0, 130.0]),
1318 (TextDirection::Rtl, [250.0, 180.0, 100.0]),
1319 ] {
1320 let tree = Directionality::new(
1321 text_direction,
1322 SizedBox::new().width(300.0).height(200.0).child(
1325 OverflowBar::new().spacing(10.0).children(vec![
1326 keyed(&keys[0], 50.0, 50.0),
1327 keyed(&keys[1], 60.0, 50.0),
1328 keyed(&keys[2], 70.0, 50.0),
1329 ]),
1330 ),
1331 )
1332 .into_widget();
1333 mount(&mut app, &mut harness, tree);
1334 assert_eq!(bar_size(&app, &harness), Size::new(300.0, 200.0));
1335 for (key, left) in keys.iter().zip(lefts) {
1336 assert_eq!(rect_of(&mut app, key).left, left, "{text_direction:?}");
1337 }
1338 }
1339 }
1340
1341 #[test]
1343 fn an_aligned_bar_matches_a_row_with_that_main_axis_alignment() {
1344 let cell = AppCell::new();
1345 let mut app = cell.borrow_mut();
1346 let mut harness = None;
1347 for alignment in [
1348 MainAxisAlignment::Start,
1349 MainAxisAlignment::Center,
1350 MainAxisAlignment::End,
1351 MainAxisAlignment::SpaceBetween,
1352 MainAxisAlignment::SpaceAround,
1353 MainAxisAlignment::SpaceEvenly,
1354 ] {
1355 for text_direction in [TextDirection::Ltr, TextDirection::Rtl] {
1356 let bar_keys = [GlobalKey::new(), GlobalKey::new(), GlobalKey::new()];
1357 let row_keys = [GlobalKey::new(), GlobalKey::new(), GlobalKey::new()];
1358 let children = |keys: &[GlobalKey; 3]| {
1359 vec![
1360 keyed(&keys[0], 50.0, 50.0),
1361 keyed(&keys[1], 70.0, 50.0),
1362 keyed(&keys[2], 80.0, 50.0),
1363 ]
1364 };
1365 let tree = Directionality::new(
1366 text_direction,
1367 Column::new().children(vec![
1368 OverflowBar::new()
1369 .alignment(alignment)
1370 .children(children(&bar_keys))
1371 .into_widget(),
1372 Row::new()
1373 .main_axis_alignment(alignment)
1374 .children(children(&row_keys))
1375 .into_widget(),
1376 ]),
1377 )
1378 .into_widget();
1379 mount(&mut app, &mut harness, tree);
1380 assert_eq!(bar_size(&app, &harness), Size::new(300.0, 50.0));
1381 for (bar_key, row_key) in bar_keys.iter().zip(&row_keys) {
1382 let bar_rect = rect_of(&mut app, bar_key);
1383 let row_rect = rect_of(&mut app, row_key);
1384 assert_eq!(bar_rect.size(), row_rect.size());
1385 assert_eq!(
1386 (bar_rect.left, bar_rect.right),
1387 (row_rect.left, row_rect.right),
1388 "{alignment:?} {text_direction:?}"
1389 );
1390 }
1391 }
1392 }
1393 }
1394
1395 #[test]
1397 fn a_bar_at_zero_area_takes_no_space() {
1398 let cell = AppCell::new();
1399 let mut app = cell.borrow_mut();
1400 waft_painting::PaintingBinding::instance(&mut app).install_fonts(&mut app, |fonts| {
1401 fonts.add_source(valo_system_fonts::SystemFonts::load());
1402 });
1403 let mut harness = None;
1404 mount(
1405 &mut app,
1406 &mut harness,
1407 Directionality::new(
1408 TextDirection::Ltr,
1409 Center::new().child(SizedBox::shrink().child(OverflowBar::new().children(vec![
1410 crate::Text::new("X").into_widget(),
1411 crate::Text::new("Y").into_widget(),
1412 ]))),
1413 )
1414 .into_widget(),
1415 );
1416 assert_eq!(bar_size(&app, &harness), Size::ZERO);
1417 }
1418}