waft-widgets 0.10.1

Widget framework and widgets for Waft
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
//! Flutter counterpart: `widgets/widget_span.dart`.
//!
//! The `Semantics` wrapper that tags each inline widget with its placeholder index waits with
//! the rest of semantics.

use std::any::Any;
use std::fmt;
use std::rc::Rc;

use waft_embedder::{
    Matrix4, Offset, ParagraphBuilder, PlaceholderAlignment, Size, TextBaseline, TextPosition,
};
use waft_foundation::{App, DiagnosticPropertiesBuilder, DiagnosticsProperty};
use waft_painting::{
    Accumulator, InlineSpan, InlineSpanRef, K_DEFAULT_FONT_SIZE, PLACEHOLDER_CODE_UNIT,
    PlaceholderDimensions, PlaceholderSpan, RenderComparison, TextScaler, TextStyle,
    inline_span_debug_fill_properties, placeholder_span_debug_fill_properties, same_span,
};
use waft_rendering::{
    AnyRenderBox, AnyRenderObject, BoxConstraints, BoxHitTestResult, ContainerLayer,
    PaintingContext, RenderBox, RenderBoxData, RenderHandle, RenderObject, RenderObjectData,
    RenderObjectWithChildData, RenderObjectWithChildMixin, TextParentData, TransformLayer,
};

use crate::framework::{
    BuildContext, IntoWidget, KeyRef, ParentDataWidget, RenderObjectWidget,
    SingleChildRenderObjectWidget, WidgetRef,
};

/// An immutable widget that is embedded inline within text.
///
/// The [`child`](Self::child) property is the widget that will be embedded. Children are
/// constrained by the width of the paragraph.
///
/// The [`child`](Self::child) property may contain its own `Widget` children (if applicable),
/// including `Text` and `RichText` widgets which may include additional [`WidgetSpan`]s.
/// Child `Text` and `RichText` widgets will be laid out independently and occupy a rectangular
/// space in the parent text layout.
///
/// [`WidgetSpan`]s will be ignored when passed into a [`TextPainter`](waft_painting::TextPainter)
/// directly. To properly layout and paint the [`WidgetSpan`], [`WidgetSpan`] should be passed
/// into a `Text::rich` widget.
///
/// ```text
/// Text::rich(
///     TextSpan::new()
///         .children(vec![
///             TextSpan::new().text("Flutter is").into_span(),
///             WidgetSpan::new(SizedBox::new().width(120.0).height(50.0).child(Card::new(...)))
///                 .into_span(),
///             TextSpan::new().text("the best!").into_span(),
///         ])
///         .into_span(),
/// )
/// ```
///
/// [`WidgetSpan`] contributes the semantics of the [`WidgetSpan::child`] to the semantics
/// tree.
///
/// See also:
///
///  * `TextSpan`, a node that represents text in an [`InlineSpan`] tree.
///  * `Text`, a widget for showing uniformly-styled text.
///  * `RichText`, a widget for finer control of text rendering.
///  * [`TextPainter`](waft_painting::TextPainter), a class for painting [`InlineSpan`]
///    objects on a `Canvas`.
///
/// Dart's named constructor arguments are the fluent setters; the constructor's assert runs
/// in [`into_span`](Self::into_span), once every setter has had its say.
#[derive(Clone)]
pub struct WidgetSpan {
    /// The widget to embed inline within text.
    pub child: WidgetRef,
    /// How the placeholder aligns vertically with the text.
    pub alignment: PlaceholderAlignment,
    /// The [`TextBaseline`] to align against when using [`PlaceholderAlignment::Baseline`],
    /// [`PlaceholderAlignment::AboveBaseline`], and [`PlaceholderAlignment::BelowBaseline`].
    pub baseline: Option<TextBaseline>,
    /// The [`TextStyle`] to apply to this span; only the decoration, foreground, background,
    /// and spacing options are used.
    pub style: Option<TextStyle>,
}

impl WidgetSpan {
    /// Creates a [`WidgetSpan`] with the given values.
    ///
    /// The [`child`](Self::child) property is the widget that will be embedded. Children are
    /// constrained by the width of the paragraph.
    ///
    /// The [`child`](Self::child) property may contain its own `Widget` children (if
    /// applicable), including `Text` and `RichText` widgets which may include additional
    /// [`WidgetSpan`]s. Child `Text` and `RichText` widgets will be laid out independently and
    /// occupy a rectangular space in the parent text layout.
    ///
    /// A [`TextStyle`] may be provided with the [`style`](Self::style) property, but only the
    /// decoration, foreground, background, and spacing options will be used.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn new<K>(child: impl IntoWidget<K>) -> WidgetSpan {
        WidgetSpan {
            child: child.into_widget(),
            alignment: PlaceholderAlignment::Bottom,
            baseline: None,
            style: None,
        }
    }

    /// Dart `WidgetSpan(alignment:)`.
    pub fn alignment(mut self, alignment: PlaceholderAlignment) -> WidgetSpan {
        self.alignment = alignment;
        self
    }

    /// Dart `WidgetSpan(baseline:)`.
    pub fn baseline(mut self, baseline: TextBaseline) -> WidgetSpan {
        self.baseline = Some(baseline);
        self
    }

    /// Dart `WidgetSpan(style:)`.
    pub fn style(mut self, style: TextStyle) -> WidgetSpan {
        self.style = Some(style);
        self
    }

    /// The span as a shared [`InlineSpanRef`].
    ///
    /// A baseline alignment needs a [`baseline`](Self::baseline).
    pub fn into_span(self) -> InlineSpanRef {
        debug_assert!(
            self.baseline.is_some()
                || !matches!(
                    self.alignment,
                    PlaceholderAlignment::AboveBaseline
                        | PlaceholderAlignment::BelowBaseline
                        | PlaceholderAlignment::Baseline
                )
        );
        std::rc::Rc::new(self)
    }

    /// Helper function for extracting [`WidgetSpan`]s in preorder, from the given
    /// [`InlineSpan`] as a list of widgets.
    ///
    /// The `text_scaler` is the scaling strategy for scaling the content.
    ///
    /// This function is used by `EditableText` and `RichText` so calling it directly is rarely
    /// necessary.
    pub fn extract_from_inline_span(
        span: &dyn InlineSpan,
        text_scaler: &TextScaler,
    ) -> Vec<WidgetRef> {
        let mut widgets = Vec::new();
        // _kEngineDefaultFontSize is the default font size to use when none of the
        // ancestor spans specifies one.
        let mut font_size_stack = vec![K_DEFAULT_FONT_SIZE];
        visit_subtree(span, text_scaler, &mut font_size_stack, &mut widgets);
        widgets
    }
}

/// `extract_from_inline_span`'s `visitSubtree`. This assumes an [`InlineSpan`] tree's logical
/// order is equivalent to preorder.
fn visit_subtree(
    span: &dyn InlineSpan,
    text_scaler: &TextScaler,
    font_size_stack: &mut Vec<f64>,
    widgets: &mut Vec<WidgetRef>,
) -> bool {
    let font_size_to_push = span
        .style()
        .and_then(|style| style.font_size)
        .filter(|size| Some(size) != font_size_stack.last());
    if let Some(size) = font_size_to_push {
        font_size_stack.push(size);
    }
    if let Some(widget_span) = span.as_any().downcast_ref::<WidgetSpan>() {
        let font_size = *font_size_stack.last().expect("never empty");
        let text_scale_factor = if font_size == 0.0 {
            0.0
        } else {
            text_scaler.scale(font_size) / font_size
        };
        widgets.push(
            WidgetSpanParentData {
                span: widget_span.clone(),
                child: AutoScaleInlineWidget {
                    span: widget_span.clone(),
                    text_scale_factor,
                    child: widget_span.child.clone(),
                }
                .into_widget(),
            }
            .into_widget(),
        );
    }
    debug_assert!(
        span.as_any().is::<WidgetSpan>() || span.as_placeholder_span().is_none(),
        "{span:?} is a PlaceholderSpan but not a WidgetSpan subclass. This is currently not \
         supported."
    );
    span.visit_direct_children(&mut |child| {
        visit_subtree(&**child, text_scaler, font_size_stack, widgets)
    });
    if let Some(pushed) = font_size_to_push {
        let popped_font_size = font_size_stack.pop();
        debug_assert!(!font_size_stack.is_empty());
        debug_assert_eq!(popped_font_size, Some(pushed));
    }
    true
}

impl fmt::Debug for WidgetSpan {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut s = f.debug_struct("WidgetSpan");
        if let Some(style) = &self.style {
            s.field("style", style);
        }
        s.field("alignment", &self.alignment)
            .field("baseline", &self.baseline)
            .field("widget", &self.child)
            .finish()
    }
}

impl PlaceholderSpan for WidgetSpan {
    fn alignment(&self) -> PlaceholderAlignment {
        self.alignment
    }

    fn baseline(&self) -> Option<TextBaseline> {
        self.baseline
    }
}

impl InlineSpan for WidgetSpan {
    fn to_string_short(&self) -> String {
        waft_foundation::describe_identity("WidgetSpan", self as *const WidgetSpan as usize as u64)
    }

    fn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder) {
        inline_span_debug_fill_properties(self.style(), properties);
        placeholder_span_debug_fill_properties(self, properties);
        properties.add(DiagnosticsProperty::<String>::new(
            "widget",
            Some("Instance of 'Widget'".to_string()),
        ));
    }

    fn style(&self) -> Option<&TextStyle> {
        self.style.as_ref()
    }

    /// Adds a placeholder box to the paragraph builder if a size has been calculated for the
    /// widget.
    ///
    /// Sizes are provided through `dimensions`, which should contain a 1:1 in-order mapping
    /// of widget to laid-out dimensions. If no such dimension is provided, the widget will be
    /// skipped.
    ///
    /// The `text_scaler` will be applied to the laid-out size of the widget.
    fn build(
        &self,
        builder: &mut ParagraphBuilder,
        text_scaler: &TextScaler,
        dimensions: Option<&[PlaceholderDimensions]>,
    ) {
        debug_assert!(InlineSpan::debug_assert_is_valid(self));
        let dimensions = dimensions.expect("a WidgetSpan is built with its dimensions");
        let has_style = self.style.is_some();
        if let Some(style) = &self.style {
            builder.push_style(style.get_text_style_with(text_scaler));
        }
        debug_assert!((builder.placeholder_count() as usize) < dimensions.len());
        let current_dimensions = dimensions[builder.placeholder_count() as usize];
        builder.add_placeholder(
            current_dimensions.size.width(),
            current_dimensions.size.height(),
            self.alignment,
            1.0,
            current_dimensions.baseline_offset,
            current_dimensions.baseline,
        );
        if has_style {
            builder.pop();
        }
    }

    /// Calls `visitor` on this [`WidgetSpan`]. There are no children spans to walk.
    fn visit_children(self: Rc<Self>, visitor: &mut dyn FnMut(&InlineSpanRef) -> bool) -> bool {
        visitor(&(self as InlineSpanRef))
    }

    fn visit_direct_children(&self, _visitor: &mut dyn FnMut(&InlineSpanRef) -> bool) -> bool {
        true
    }

    /// [`WidgetSpan`]s are not hit-test targets: this always returns `None`.
    fn get_span_for_position(self: Rc<Self>, _position: TextPosition) -> Option<InlineSpanRef> {
        debug_assert!(InlineSpan::debug_assert_is_valid(&*self));
        None
    }

    fn get_span_for_position_visitor(
        self: Rc<Self>,
        position: TextPosition,
        offset: &mut Accumulator,
    ) -> Option<InlineSpanRef> {
        if position.offset == offset.value() {
            return Some(self);
        }
        offset.increment(1);
        None
    }

    fn compute_to_plain_text(
        &self,
        buffer: &mut String,
        include_semantics_labels: bool,
        include_placeholders: bool,
    ) {
        PlaceholderSpan::compute_to_plain_text(
            self,
            buffer,
            include_semantics_labels,
            include_placeholders,
        );
    }

    fn code_unit_at_visitor(&self, index: i32, offset: &mut Accumulator) -> Option<u16> {
        let local_offset = index - offset.value();
        debug_assert!(local_offset >= 0);
        offset.increment(1);
        (local_offset == 0).then_some(PLACEHOLDER_CODE_UNIT)
    }

    /// [`WidgetSpan`]s are always valid as asserts prevent invalid [`WidgetSpan`]s from being
    /// constructed.
    fn debug_assert_is_valid(&self) -> bool {
        true
    }

    fn compare_to(&self, other: &dyn InlineSpan) -> RenderComparison {
        if same_span(self, other) {
            return RenderComparison::Identical;
        }
        let Some(typed_other) = other.as_any().downcast_ref::<WidgetSpan>() else {
            return RenderComparison::Layout;
        };
        if self.style.is_none() != typed_other.style.is_none() {
            return RenderComparison::Layout;
        }
        if !std::rc::Rc::ptr_eq(&self.child, &typed_other.child)
            || self.alignment != typed_other.alignment
        {
            return RenderComparison::Layout;
        }
        let mut result = RenderComparison::Identical;
        if let (Some(style), Some(other_style)) = (&self.style, &typed_other.style) {
            let candidate = style.compare_to(other_style);
            if candidate > result {
                result = candidate;
            }
            if result == RenderComparison::Layout {
                return result;
            }
        }
        result
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_placeholder_span(&self) -> Option<&dyn PlaceholderSpan> {
        Some(self)
    }

    fn eq_span(&self, other: &dyn InlineSpan) -> bool {
        if same_span(self, other) {
            return true;
        }
        let Some(other) = other.as_any().downcast_ref::<WidgetSpan>() else {
            return false;
        };
        other.style == self.style
            && std::rc::Rc::ptr_eq(&other.child, &self.child)
            && other.alignment == self.alignment
            && other.baseline == self.baseline
    }
}

/// A `ParentDataWidget` that sets [`TextParentData::span`].
#[derive(Debug)]
struct WidgetSpanParentData {
    span: WidgetSpan,
    child: WidgetRef,
}

impl ParentDataWidget for WidgetSpanParentData {
    type ParentData = TextParentData;

    fn child(&self) -> &WidgetRef {
        &self.child
    }

    fn apply_parent_data(&self, app: &mut App, render_object: AnyRenderObject) {
        let parent_data = render_object.parent_data_of_mut::<TextParentData>(app);
        parent_data.span = Some(std::rc::Rc::new(self.span.clone()));
    }
}

// A RenderObjectWidget that automatically applies text scaling on inline
// widgets.
//
// TODO(LongCatIsLooong): this shouldn't happen automatically, at least there
// should be a way to opt out: https://github.com/flutter/flutter/issues/126962
#[derive(Debug)]
struct AutoScaleInlineWidget {
    span: WidgetSpan,
    text_scale_factor: f64,
    child: WidgetRef,
}

impl RenderObjectWidget for AutoScaleInlineWidget {
    type RenderObject = RenderScaledInlineWidget;

    fn key(&self) -> Option<&KeyRef> {
        None
    }

    fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
        RenderScaledInlineWidget::new(
            app,
            self.span.alignment,
            self.span.baseline,
            self.text_scale_factor,
        )
        .as_object()
    }

    fn update_render_object(
        &self,
        app: &mut App,
        _context: BuildContext,
        render_object: RenderHandle<RenderScaledInlineWidget>,
    ) {
        render_object.set_alignment(app, self.span.alignment);
        render_object.set_baseline(app, self.span.baseline);
        render_object.set_scale(app, self.text_scale_factor);
    }
}

impl SingleChildRenderObjectWidget for AutoScaleInlineWidget {
    fn child(&self) -> Option<&WidgetRef> {
        Some(&self.child)
    }
}

/// The render box of an inline widget: its child laid out at the paragraph's width and
/// scaled by the text scale factor.
struct RenderScaledInlineWidget {
    render_object: RenderObjectData,
    render_box: RenderBoxData,
    child: RenderObjectWithChildData<AnyRenderBox>,
    scale: f64,
    alignment: PlaceholderAlignment,
    baseline: Option<TextBaseline>,
}

impl RenderScaledInlineWidget {
    fn new(
        app: &mut App,
        alignment: PlaceholderAlignment,
        baseline: Option<TextBaseline>,
        scale: f64,
    ) -> RenderHandle<Self> {
        RenderHandle::new_box(
            app,
            RenderScaledInlineWidget {
                render_object: RenderObjectData::new(),
                render_box: RenderBoxData::new(),
                child: RenderObjectWithChildData::new(),
                scale,
                alignment,
                baseline,
            },
        )
    }

    fn scale(self: RenderHandle<Self>, app: &App) -> f64 {
        self.get(app).scale
    }

    fn set_scale(self: RenderHandle<Self>, app: &mut App, value: f64) {
        if value == self.get(app).scale {
            return;
        }
        debug_assert!(value > 0.0);
        debug_assert!(value.is_finite());
        self.get_mut(app).scale = value;
        self.as_object().mark_needs_layout(app);
    }

    fn set_alignment(self: RenderHandle<Self>, app: &mut App, value: PlaceholderAlignment) {
        if self.get(app).alignment == value {
            return;
        }
        self.get_mut(app).alignment = value;
        self.as_object().mark_needs_layout(app);
    }

    fn set_baseline(self: RenderHandle<Self>, app: &mut App, value: Option<TextBaseline>) {
        if value == self.get(app).baseline {
            return;
        }
        self.get_mut(app).baseline = value;
        self.as_object().mark_needs_layout(app);
    }

    /// `Matrix4.diagonal3Values(scale, scale, 1.0)`.
    fn scale_matrix(self: RenderHandle<Self>, app: &App) -> Matrix4 {
        let scale = self.scale(app) as f32;
        Matrix4::scale(scale, scale)
    }
}

impl RenderObjectWithChildMixin for RenderScaledInlineWidget {
    type ChildType = AnyRenderBox;

    fn child_data(self: RenderHandle<Self>, app: &App) -> &RenderObjectWithChildData<AnyRenderBox> {
        &self.get(app).child
    }

    fn child_data_mut(
        self: RenderHandle<Self>,
        app: &mut App,
    ) -> &mut RenderObjectWithChildData<AnyRenderBox> {
        &mut self.get_mut(app).child
    }
}

impl RenderObject for RenderScaledInlineWidget {
    waft_rendering::render_object_accessors!();

    fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
        let Some(child) = self.child(app) else {
            return;
        };
        let constraints = self.constraints(app);
        debug_assert!(!constraints.has_bounded_height());
        let scale = self.scale(app);
        // Only constrain the width to the maximum width of the paragraph.
        // Leave height unconstrained, which will overflow if expanded past.
        child.layout(
            app,
            BoxConstraints::new().max_width(constraints.max_width / scale),
            true,
        );
        let size = constraints.constrain(child.size(app) * scale);
        self.set_size(app, size);
    }

    fn paint(
        self: RenderHandle<Self>,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
    ) {
        let Some(child) = self.child(app) else {
            self.as_object().set_layer(app, None);
            return;
        };
        if self.scale(app) == 1.0 {
            context.paint_child(app, child.as_object(), offset);
            self.as_object().set_layer(app, None);
            return;
        }
        let old_layer = self.as_object().layer_as::<TransformLayer>(app);
        let layer = context.push_transform(
            app,
            self.as_object().needs_compositing(app),
            offset,
            self.scale_matrix(app),
            |app, context, offset| context.paint_child(app, child.as_object(), offset),
            old_layer,
        );
        self.as_object()
            .set_layer(app, layer.map(|layer| layer.as_container_layer()));
    }

    fn visit_children(
        self: RenderHandle<Self>,
        app: &App,
        visitor: &mut dyn FnMut(AnyRenderObject),
    ) {
        if let Some(child) = self.child(app) {
            visitor(child.as_object());
        }
    }
}

impl RenderBox for RenderScaledInlineWidget {
    waft_rendering::render_box_accessors!();

    fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        let scale = self.scale(app);
        self.child(app).map_or(0.0, |child| {
            child.get_max_intrinsic_height(app, width / scale)
        }) * scale
    }

    fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        let scale = self.scale(app);
        self.child(app).map_or(0.0, |child| {
            child.get_max_intrinsic_width(app, height / scale)
        }) * scale
    }

    fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        let scale = self.scale(app);
        self.child(app).map_or(0.0, |child| {
            child.get_min_intrinsic_height(app, width / scale)
        }) * scale
    }

    fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        let scale = self.scale(app);
        self.child(app).map_or(0.0, |child| {
            child.get_min_intrinsic_width(app, height / scale)
        }) * scale
    }

    fn compute_distance_to_actual_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        baseline: TextBaseline,
    ) -> Option<f64> {
        let scale = self.scale(app);
        self.child(app)
            .and_then(|child| child.get_distance_to_actual_baseline(app, baseline))
            .map(|child_baseline| scale * child_baseline)
    }

    fn compute_dry_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
        baseline: TextBaseline,
    ) -> Option<f64> {
        let scale = self.scale(app);
        let distance = self.child(app)?.get_dry_baseline(
            app,
            BoxConstraints::new().max_width(constraints.max_width / scale),
            baseline,
        );
        distance.map(|distance| scale * distance)
    }

    fn compute_dry_layout(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
    ) -> Size {
        debug_assert!(!constraints.has_bounded_height());
        let scale = self.scale(app);
        let unscaled_size = self.child(app).map_or(Size::ZERO, |child| {
            child.get_dry_layout(
                app,
                BoxConstraints::new().max_width(constraints.max_width / scale),
            )
        });
        constraints.constrain(unscaled_size * scale)
    }

    fn apply_paint_transform(
        self: RenderHandle<Self>,
        app: &App,
        _child: AnyRenderObject,
        transform: &mut Matrix4,
    ) {
        *transform = transform.then(&self.scale_matrix(app));
    }

    fn hit_test_children(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        let Some(child) = self.child(app) else {
            return false;
        };
        let transform = self.scale_matrix(app);
        result.add_with_paint_transform(Some(transform), position, |result, transformed| {
            child.hit_test(app, result, transformed)
        })
    }
}

#[cfg(test)]
mod tests {
    use waft_embedder::Color;
    use waft_embedder::{TextDirection, TextLeadingDistribution};
    use waft_foundation::AppCell;
    use waft_gestures::HitTestResult;
    use waft_painting::{PaintingBinding, TextOverflow, TextSpan};
    use waft_rendering::{RenderColoredBox, RenderIntrinsicHeight, RenderParagraph};

    use super::*;
    use crate::test_harness::Harness;
    use crate::widgets::basic::{
        Center, ColoredBox, Directionality, IntrinsicHeight, Row, SizedBox,
    };
    use crate::widgets::text::{RichText, Text};

    /// What the shell does at start-up: the app-wide fonts, here the OS fonts.
    fn install_fonts(app: &mut App) {
        let binding = PaintingBinding::instance(app);
        if !binding.has_fonts(app) {
            binding.install_fonts(app, |fonts| {
                fonts.add_source(valo_system_fonts::SystemFonts::load());
            });
        }
    }

    /// Mounts `child` under a left-to-right `Directionality` with fonts installed, and pumps
    /// the first frame.
    fn mount(app: &mut App, child: WidgetRef) -> Harness {
        install_fonts(app);
        let harness = Harness::mount(
            app,
            Directionality::new(TextDirection::Ltr, child).into_widget(),
        );
        harness.pump(app);
        harness
    }

    /// The only child of `parent` in the render tree.
    fn only_child(app: &App, parent: AnyRenderObject) -> AnyRenderObject {
        let mut children = Vec::new();
        parent.visit_children(app, &mut |child| children.push(child));
        assert_eq!(children.len(), 1, "one child");
        children[0]
    }

    fn paragraph_under(app: &App, harness: &Harness) -> RenderHandle<RenderParagraph> {
        only_child(app, harness.render_root(app).as_object())
            .downcast::<RenderParagraph>(app)
            .expect("a RenderParagraph")
    }

    fn sized_box(width: f64, height: f64) -> WidgetRef {
        SizedBox::new().width(width).height(height).into_widget()
    }

    fn text(text: &str) -> InlineSpanRef {
        TextSpan::new().text(text).into_span()
    }

    #[test]
    fn a_widget_span_lays_its_child_out_between_the_text() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let span = TextSpan::new()
            .children(vec![
                text("ab"),
                WidgetSpan::new(sized_box(30.0, 20.0)).into_span(),
                text("cd"),
            ])
            .into_span();
        let harness = mount(
            &mut app,
            Center::new().child(Text::rich(span)).into_widget(),
        );
        let paragraph = only_child(&app, harness.render_root(&app).as_object())
            .downcast::<waft_rendering::RenderPositionedBox>(&app)
            .map(|center| only_child(&app, center.as_object()))
            .and_then(|child| child.downcast::<RenderParagraph>(&app))
            .expect("a RenderParagraph under the Center");
        assert_eq!(
            paragraph.text(&app).to_plain_text(true, true),
            "ab\u{FFFC}cd"
        );
        let inline = only_child(&app, paragraph.as_object());
        let parent_data = inline.parent_data_of::<TextParentData>(&app);
        assert!(
            parent_data.span.is_some(),
            "the parent data widget set the span"
        );
        let offset = parent_data.offset().expect("laid out");
        assert!(offset.dx() > 0.0, "after \"ab\"");
        let inline = inline.as_box().expect("a box");
        assert_eq!(inline.size(&app), Size::new(30.0, 20.0));
        let width = paragraph.size(&app).width();
        assert!(width > offset.dx() + 30.0, "\"cd\" follows the widget");
        assert!(paragraph.size(&app).height() >= 20.0);
    }

    // Regression test for https://github.com/flutter/flutter/issues/35869
    #[test]
    fn inline_widgets_works_with_ellipsis() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let truncated =
            RichText::new(text("widget should be truncated")).text_direction(TextDirection::Rtl);
        let span = TextSpan::new()
            .children(vec![
                text("a very very very very very very very very very very long line"),
                WidgetSpan::new(SizedBox::new().width(20.0).height(40.0).child(truncated))
                    .into_span(),
            ])
            .into_span();
        let harness = mount(
            &mut app,
            Text::rich(span)
                .max_lines(1)
                .overflow(TextOverflow::Ellipsis)
                .into_widget(),
        );
        let paragraph = paragraph_under(&app, &harness);
        let inline = only_child(&app, paragraph.as_object());
        assert_eq!(
            inline.parent_data_of::<TextParentData>(&app).offset(),
            None,
            "ellipsized away"
        );
    }

    #[test]
    fn a_hit_on_an_inline_widget_reaches_it_and_a_hit_on_the_text_lands_on_its_span() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let opaque = ColoredBox::new(Color::from_argb(255, 0, 0, 255)).child(sized_box(30.0, 20.0));
        let span = TextSpan::new()
            .children(vec![text("ab"), WidgetSpan::new(opaque).into_span()])
            .into_span();
        let harness = mount(
            &mut app,
            Center::new().child(Text::rich(span)).into_widget(),
        );
        let center = only_child(&app, harness.render_root(&app).as_object());
        let paragraph = only_child(&app, center)
            .downcast::<RenderParagraph>(&app)
            .expect("a RenderParagraph");
        let inline = only_child(&app, paragraph.as_object());
        let offset = inline
            .parent_data_of::<TextParentData>(&app)
            .offset()
            .expect("laid out");

        let mut result = HitTestResult::new();
        let hit = paragraph.as_box().hit_test(
            &mut app,
            &mut BoxHitTestResult::wrap(&mut result),
            offset + Offset::new(15.0, 10.0),
        );
        assert!(hit);
        assert!(
            only_child(&app, inline)
                .downcast::<RenderColoredBox>(&app)
                .is_some()
        );
        assert_eq!(
            result.path().len(),
            3,
            "the colored box, the scaled inline box and the paragraph"
        );

        let mut result = HitTestResult::new();
        paragraph.as_box().hit_test(
            &mut app,
            &mut BoxHitTestResult::wrap(&mut result),
            Offset::new(1.0, offset.dy() + 10.0),
        );
        assert_eq!(result.path().len(), 2, "the span and the paragraph");
        let target: &dyn std::any::Any = result.path()[0].target();
        assert_eq!(
            target
                .downcast_ref::<TextSpan>()
                .and_then(|span| span.text.as_deref()),
            Some("ab")
        );
    }

    // Regression test for https://github.com/flutter/flutter/issues/59316
    #[test]
    fn inline_widgets_works_with_text_scaler() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let rich = |scale: f64| {
            let span = TextSpan::new()
                .children(vec![WidgetSpan::new(sized_box(30.0, 20.0)).into_span()])
                .into_span();
            Center::new()
                .child(Text::rich(span).text_scaler(TextScaler::linear(scale)))
                .into_widget()
        };
        let harness = mount(&mut app, rich(1.0));
        let inline_size = |app: &App| {
            let center = only_child(app, harness.render_root(app).as_object());
            let paragraph = only_child(app, center);
            only_child(app, paragraph)
                .as_box()
                .expect("a box")
                .size(app)
        };
        assert_eq!(inline_size(&app), Size::new(30.0, 20.0));

        harness.set_child(
            &mut app,
            Directionality::new(TextDirection::Ltr, rich(2.0)).into_widget(),
        );
        harness.pump(&mut app);
        assert_eq!(inline_size(&app), Size::new(60.0, 40.0));
    }

    #[test]
    fn widget_span_calculate_correct_intrinsic_heights() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let line = TextStyle::new()
            .height(1.0)
            .font_size(16.0)
            .leading_distribution(TextLeadingDistribution::Even);
        let span = TextSpan::new()
            .children(vec![
                TextSpan::new()
                    .text("Start\n")
                    .style(line.clone())
                    .into_span(),
                WidgetSpan::new(Row::new().children([sized_box(16.0, 16.0)])).into_span(),
                TextSpan::new().text("End").style(line).into_span(),
            ])
            .into_span();
        let harness = mount(
            &mut app,
            Center::new()
                .child(IntrinsicHeight::new().child(RichText::new(span)))
                .into_widget(),
        );
        let center = only_child(&app, harness.render_root(&app).as_object());
        let intrinsic = only_child(&app, center)
            .downcast::<RenderIntrinsicHeight>(&app)
            .expect("a RenderIntrinsicHeight");
        assert_eq!(intrinsic.size(&app).height(), 3.0 * 16.0);
    }

    /// `paragraph_test.dart`'s `TestSelectionRegistrar`.
    struct TestSelectionRegistrar {
        selectables: Vec<waft_rendering::AnySelectable>,
    }

    impl waft_rendering::SelectionRegistrar for TestSelectionRegistrar {
        fn add(
            self: waft_foundation::Handle<Self>,
            app: &mut App,
            selectable: waft_rendering::AnySelectable,
        ) {
            app.get_mut(self).selectables.push(selectable);
        }

        fn remove(
            self: waft_foundation::Handle<Self>,
            app: &mut App,
            selectable: waft_rendering::AnySelectable,
        ) {
            app.get_mut(self).selectables.retain(|s| *s != selectable);
        }
    }

    // paragraph_test.dart 'can handle select all when contains widget span': a rendering case,
    // here because `WidgetSpan` is a widgets type.
    #[test]
    fn can_handle_select_all_when_contains_widget_span() {
        use waft_rendering::{ContainerRenderObjectMixin as _, SelectionEvent, SelectionStatus};

        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let registrar = app.create(TestSelectionRegistrar {
            selectables: Vec::new(),
        });
        let render_box = RenderParagraph::new(&mut app, text("widget"), TextDirection::Ltr, None);
        let widget_span = WidgetSpan::new(Text::new("widget"));
        let paragraph = RenderParagraph::new(
            &mut app,
            TextSpan::new()
                .children(vec![
                    text("before the span"),
                    widget_span.clone().into_span(),
                    text("after the span"),
                ])
                .into_span(),
            TextDirection::Ltr,
            None,
        );
        paragraph.set_registrar(&mut app, Some(registrar.erase()));
        paragraph.add(&mut app, render_box.as_box());
        // `_applyParentData`.
        render_box
            .as_object()
            .parent_data_of_mut::<TextParentData>(&mut app)
            .span = Some(std::rc::Rc::new(widget_span));
        waft_rendering::testing::layout(&mut app, paragraph.as_box());
        // The widget span will register to the selection container without going
        // through the render paragraph.
        let selectables = app.get(&registrar).selectables.clone();
        assert_eq!(selectables.len(), 2);
        for segment in selectables {
            segment.dispatch_selection_event(&mut app, &SelectionEvent::SelectAll);
            let geometry = segment.value(&app);
            assert!(geometry.has_content);
            assert_eq!(geometry.status, SelectionStatus::Uncollapsed);
        }
    }
}