waft-widgets 0.15.0

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
//! Flutter counterpart: `widgets/icon.dart`.
//!
//! `debugCheckHasDirectionality` waits on diagnostics.

use waft_embedder::{
    BlendMode, FontVariation, FontWeight, Matrix4, Paint, Shadow, TextDirection,
    TextLeadingDistribution,
};
use waft_foundation::{
    App, DiagnosticPropertiesBuilder, DiagnosticsProperty, DoubleProperty, EnumProperty,
    StringProperty,
};
use waft_painting::{
    AlignmentGeometry, AnyColor, ColorProperty, K_DEFAULT_FONT_SIZE, TextOverflow, TextSpan,
    TextStyle,
};

use crate::framework::{BuildContext, IntoWidget, KeyRef, StatelessWidget, WidgetRef};
use crate::widgets::basic::{
    Center, Directionality, ExcludeSemantics, Semantics, SizedBox, Transform,
};
use crate::widgets::icon_data::IconData;
use crate::widgets::icon_theme::IconTheme;
use crate::widgets::icon_theme_data::describe_shadows;
use crate::widgets::media_query::MediaQuery;
use crate::widgets::text::RichText;

/// A graphical icon widget drawn with a glyph from a font described in
/// an [`IconData`] such as material's predefined `IconData`s in `Icons`.
///
/// Icons are not interactive. For an interactive icon, consider material's
/// `IconButton`.
///
/// There must be an ambient [`Directionality`] widget when using [`Icon`].
/// Typically this is introduced automatically by the `WidgetsApp` or
/// `MaterialApp`.
///
/// This widget assumes that the rendered icon is squared. Non-squared icons may
/// render incorrectly.
///
/// This example shows how to create a `Row` of [`Icon`]s in different colors and
/// sizes:
///
/// ```text
/// Row::new().children(vec![
///     Icon::new(Some(Icons::FAVORITE)).color(PINK).size(24.0).into_widget(),
///     Icon::new(Some(Icons::AUDIOTRACK)).color(GREEN).size(30.0).into_widget(),
///     Icon::new(Some(Icons::BEACH_ACCESS)).color(BLUE).size(36.0).into_widget(),
/// ])
/// ```
///
/// See also:
///
///  * `IconButton`, for interactive icons.
///  * `Icons`, for the list of available Material Icons for use with this class.
///  * [`IconTheme`], which provides ambient configuration for icons.
///  * `ImageIcon`, for showing icons from `AssetImage`s or other `ImageProvider`s.
#[derive(Debug)]
pub struct Icon {
    pub key: Option<KeyRef>,
    /// The icon to display. The available icons are described in `Icons`.
    ///
    /// The icon can be `None`, in which case the widget will render as an empty
    /// space of the specified [`size`](Self::size).
    pub icon: Option<IconData>,
    /// The size of the icon in logical pixels.
    ///
    /// Icons occupy a square with width and height equal to size.
    ///
    /// Defaults to the nearest [`IconTheme`]'s `IconThemeData::size`.
    ///
    /// If this [`Icon`] is being placed inside an `IconButton`, then use
    /// `IconButton.iconSize` instead, so that the `IconButton` can make the splash
    /// area the appropriate size as well. The `IconButton` uses an [`IconTheme`] to
    /// pass down the size to the [`Icon`].
    pub size: Option<f64>,
    /// The fill for drawing the icon.
    ///
    /// Requires the underlying icon font to support the `FILL` [`FontVariation`]
    /// axis, otherwise has no effect. Variable font filenames often indicate
    /// the supported axes. Must be between 0.0 (unfilled) and 1.0 (filled),
    /// inclusive.
    ///
    /// Can be used to convey a state transition for animation or interaction.
    ///
    /// Defaults to nearest [`IconTheme`]'s `IconThemeData::fill`.
    ///
    /// See also:
    ///  * [`weight`](Self::weight), for controlling stroke weight.
    ///  * [`grade`](Self::grade), for controlling stroke weight in a more granular way.
    ///  * [`optical_size`](Self::optical_size), for controlling optical size.
    pub fill: Option<f64>,
    /// The stroke weight for drawing the icon.
    ///
    /// Requires the underlying icon font to support the `wght` [`FontVariation`]
    /// axis, otherwise has no effect. Variable font filenames often indicate
    /// the supported axes. Must be greater than 0.
    ///
    /// Defaults to nearest [`IconTheme`]'s `IconThemeData::weight`.
    ///
    /// See also:
    ///  * [`fill`](Self::fill), for controlling fill.
    ///  * [`grade`](Self::grade), for controlling stroke weight in a more granular way.
    ///  * [`optical_size`](Self::optical_size), for controlling optical size.
    pub weight: Option<f64>,
    /// The grade (granular stroke weight) for drawing the icon.
    ///
    /// Requires the underlying icon font to support the `GRAD` [`FontVariation`]
    /// axis, otherwise has no effect. Variable font filenames often indicate
    /// the supported axes. Can be negative.
    ///
    /// Grade and [`weight`](Self::weight) both affect a symbol's stroke weight
    /// (thickness), but grade has a smaller impact on the size of the symbol.
    ///
    /// Grade is also available in some text fonts. One can match grade levels
    /// between text and symbols for a harmonious visual effect. For example, if
    /// the text font has a -25 grade value, the symbols can match it with a
    /// suitable value, say -25.
    ///
    /// Defaults to nearest [`IconTheme`]'s `IconThemeData::grade`.
    ///
    /// See also:
    ///  * [`fill`](Self::fill), for controlling fill.
    ///  * [`weight`](Self::weight), for controlling stroke weight in a less granular way.
    ///  * [`optical_size`](Self::optical_size), for controlling optical size.
    pub grade: Option<f64>,
    /// The optical size for drawing the icon.
    ///
    /// Requires the underlying icon font to support the `opsz` [`FontVariation`]
    /// axis, otherwise has no effect. Variable font filenames often indicate
    /// the supported axes. Must be greater than 0.
    ///
    /// For an icon to look the same at different sizes, the stroke weight
    /// (thickness) must change as the icon size scales. Optical size offers a way
    /// to automatically adjust the stroke weight as icon size changes.
    ///
    /// Defaults to nearest [`IconTheme`]'s `IconThemeData::optical_size`.
    ///
    /// See also:
    ///  * [`fill`](Self::fill), for controlling fill.
    ///  * [`weight`](Self::weight), for controlling stroke weight.
    ///  * [`grade`](Self::grade), for controlling stroke weight in a more granular way.
    pub optical_size: Option<f64>,
    /// The color to use when drawing the icon.
    ///
    /// Defaults to the nearest [`IconTheme`]'s `IconThemeData::color`.
    ///
    /// The color (whether specified explicitly here or obtained from the
    /// [`IconTheme`]) will be further adjusted by the nearest [`IconTheme`]'s
    /// `IconThemeData::opacity`.
    pub color: Option<AnyColor>,
    /// A list of [`Shadow`]s that will be painted underneath the icon.
    ///
    /// Multiple shadows are supported to replicate lighting from multiple light
    /// sources.
    ///
    /// Shadows must be in the same order for [`Icon`] to be considered as
    /// equivalent as order produces differing transparency.
    ///
    /// Defaults to the nearest [`IconTheme`]'s `IconThemeData::shadows`.
    pub shadows: Option<Vec<Shadow>>,
    /// Semantic label for the icon.
    ///
    /// Announced by assistive technologies (e.g TalkBack/VoiceOver).
    /// This label does not show in the UI.
    ///
    ///  * [`SemanticsProperties::label`](waft_rendering::SemanticsProperties::label), which is
    ///    set to [`semantic_label`](Self::semantic_label) in the underlying [`Semantics`]
    ///    widget.
    pub semantic_label: Option<String>,
    /// The text direction to use for rendering the icon.
    ///
    /// If this is `None`, the ambient [`Directionality`] is used instead.
    ///
    /// Some icons follow the reading direction. For example, "back" buttons point
    /// left in left-to-right environments and right in right-to-left
    /// environments. Such icons have their [`IconData::match_text_direction`] field
    /// set to true, and the [`Icon`] widget uses the
    /// [`text_direction`](Self::text_direction) to determine the orientation in which to draw
    /// the icon.
    ///
    /// This property has no effect if the [`icon`](Self::icon)'s
    /// [`IconData::match_text_direction`] field is false, but for consistency a text direction
    /// value must always be specified, either directly using this property or using
    /// [`Directionality`].
    pub text_direction: Option<TextDirection>,
    /// Whether to scale the size of this widget using the ambient [`MediaQuery`]'s
    /// `TextScaler`.
    ///
    /// This is specially useful when you have an icon associated with a text, as
    /// scaling the text without scaling the icon would result in a confusing
    /// interface.
    ///
    /// Defaults to the nearest [`IconTheme`]'s `IconThemeData::apply_text_scaling`.
    pub apply_text_scaling: Option<bool>,
    /// The [`BlendMode`] to apply to the foreground of the icon.
    ///
    /// Defaults to [`BlendMode::SrcOver`].
    pub blend_mode: Option<BlendMode>,
    /// The typeface thickness to use when painting the text (e.g., bold).
    pub font_weight: Option<FontWeight>,
}

impl Icon {
    /// Creates an icon; Dart's optional named arguments are the setters.
    pub fn new(icon: Option<IconData>) -> Icon {
        Icon {
            key: None,
            icon,
            size: None,
            fill: None,
            weight: None,
            grade: None,
            optical_size: None,
            color: None,
            shadows: None,
            semantic_label: None,
            text_direction: None,
            apply_text_scaling: None,
            blend_mode: None,
            font_weight: None,
        }
    }

    /// Dart `Icon(key:)`.
    pub fn key(mut self, key: KeyRef) -> Icon {
        self.key = Some(key);
        self
    }

    /// Dart `Icon(size:)`.
    pub fn size(mut self, size: f64) -> Icon {
        self.size = Some(size);
        self
    }

    /// Dart `Icon(fill:)`; must be between 0.0 and 1.0, inclusive.
    pub fn fill(mut self, fill: f64) -> Icon {
        debug_assert!((0.0..=1.0).contains(&fill));
        self.fill = Some(fill);
        self
    }

    /// Dart `Icon(weight:)`; must be greater than 0.
    pub fn weight(mut self, weight: f64) -> Icon {
        debug_assert!(weight > 0.0);
        self.weight = Some(weight);
        self
    }

    /// Dart `Icon(grade:)`.
    pub fn grade(mut self, grade: f64) -> Icon {
        self.grade = Some(grade);
        self
    }

    /// Dart `Icon(opticalSize:)`; must be greater than 0.
    pub fn optical_size(mut self, optical_size: f64) -> Icon {
        debug_assert!(optical_size > 0.0);
        self.optical_size = Some(optical_size);
        self
    }

    /// Dart `Icon(color:)`.
    pub fn color(mut self, color: impl Into<AnyColor>) -> Icon {
        self.color = Some(color.into());
        self
    }

    /// Dart `Icon(shadows:)`.
    pub fn shadows(mut self, shadows: Vec<Shadow>) -> Icon {
        self.shadows = Some(shadows);
        self
    }

    /// Dart `Icon(semanticLabel:)`.
    pub fn semantic_label(mut self, semantic_label: impl Into<String>) -> Icon {
        self.semantic_label = Some(semantic_label.into());
        self
    }

    /// Dart's `Semantics(label: semanticLabel)` around what the icon builds.
    fn semantics(&self) -> Semantics {
        let semantics = Semantics::new();
        match &self.semantic_label {
            Some(semantic_label) => semantics.label(semantic_label.clone()),
            None => semantics,
        }
    }

    /// Dart `Icon(textDirection:)`.
    pub fn text_direction(mut self, text_direction: TextDirection) -> Icon {
        self.text_direction = Some(text_direction);
        self
    }

    /// Dart `Icon(applyTextScaling:)`.
    pub fn apply_text_scaling(mut self, apply_text_scaling: bool) -> Icon {
        self.apply_text_scaling = Some(apply_text_scaling);
        self
    }

    /// Dart `Icon(blendMode:)`.
    pub fn blend_mode(mut self, blend_mode: BlendMode) -> Icon {
        self.blend_mode = Some(blend_mode);
        self
    }

    /// Dart `Icon(fontWeight:)`.
    pub fn font_weight(mut self, font_weight: FontWeight) -> Icon {
        self.font_weight = Some(font_weight);
        self
    }

    /// The glyph's `TextStyle`: the variable-font axes, the resolved color or foreground
    /// paint, and the metrics that centre the body in the icon's square.
    fn font_style(&self, icon: &IconData, resolved: ResolvedIcon) -> TextStyle {
        let axes = [
            ("FILL", resolved.fill),
            ("wght", resolved.weight),
            ("GRAD", resolved.grade),
            ("opsz", resolved.optical_size),
        ];
        let font_variations = axes
            .into_iter()
            .filter_map(|(axis, value)| value.map(|value| FontVariation::new(axis, value)))
            .collect();
        let mut style = TextStyle::new()
            .font_variations(font_variations)
            .inherit(false)
            .font_size(resolved.size)
            // Makes sure the font's body is vertically centered within the
            // size x size square.
            .height(1.0)
            .leading_distribution(TextLeadingDistribution::Even);
        if let Some(color) = resolved.color {
            style = style.color(color);
        }
        if let Some(font_family) = &icon.font_family {
            style = style.font_family(font_family.clone());
        }
        if let Some(font_weight) = self.font_weight {
            style = style.font_weight(font_weight);
        }
        if let Some(font_package) = &icon.font_package {
            style = style.package(font_package.clone());
        }
        if let Some(font_family_fallback) = &icon.font_family_fallback {
            style = style.font_family_fallback(font_family_fallback.clone());
        }
        if let Some(shadows) = resolved.shadows {
            style = style.shadows(shadows);
        }
        if let Some(foreground) = resolved.foreground {
            style = style.foreground(foreground);
        }
        style
    }
}

/// What `Icon.build` resolves from the widget and the ambient [`IconTheme`] before it
/// composes the glyph's [`TextStyle`]. Dart's locals, grouped so they travel together.
struct ResolvedIcon {
    size: f64,
    fill: Option<f64>,
    weight: Option<f64>,
    grade: Option<f64>,
    optical_size: Option<f64>,
    shadows: Option<Vec<Shadow>>,
    color: Option<AnyColor>,
    foreground: Option<Paint>,
}

/// Dart's `Widget.==`, which is identity: an `Icon` equals only itself, so an `Rc<Icon>`
/// compares as the Dart reference does.
impl PartialEq for Icon {
    fn eq(&self, other: &Icon) -> bool {
        std::ptr::eq(self, other)
    }
}

impl StatelessWidget for Icon {
    fn debug_fill_properties(&self, _app: &App, properties: &mut DiagnosticPropertiesBuilder) {
        properties.add(
            DiagnosticsProperty::<String>::new(
                "icon",
                self.icon.as_ref().map(|icon| format!("{icon:?}")),
            )
            .if_null("<empty>")
            .with_show_name(false),
        );
        properties.add(DoubleProperty::new("size", self.size).default_value(None));
        properties.add(DoubleProperty::new("fill", self.fill).default_value(None));
        properties.add(DoubleProperty::new("weight", self.weight).default_value(None));
        properties.add(DoubleProperty::new("grade", self.grade).default_value(None));
        properties.add(DoubleProperty::new("opticalSize", self.optical_size).default_value(None));
        properties.add(
            ColorProperty::new("color", self.color.as_ref().map(|color| color.color()))
                .default_value(None),
        );
        properties.add(
            DiagnosticsProperty::<String>::new(
                "shadows",
                self.shadows.as_deref().map(describe_shadows),
            )
            .default_value(None),
        );
        properties.add(
            StringProperty::new("semanticLabel", self.semantic_label.clone()).default_value(None),
        );
        properties.add(
            EnumProperty::<TextDirection>::new("textDirection", self.text_direction)
                .default_value(None),
        );
        properties.add(
            DiagnosticsProperty::<bool>::new("applyTextScaling", self.apply_text_scaling)
                .default_value(None),
        );
    }

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

    fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
        let text_direction = self
            .text_direction
            .unwrap_or_else(|| Directionality::of(app, context));

        let icon_theme = IconTheme::of(app, context);

        let apply_text_scaling = self
            .apply_text_scaling
            .or(icon_theme.apply_text_scaling)
            .unwrap_or(false);

        let tentative_icon_size = self.size.or(icon_theme.size).unwrap_or(K_DEFAULT_FONT_SIZE);

        let icon_size = if apply_text_scaling {
            MediaQuery::text_scaler_of(app, context).scale(tentative_icon_size)
        } else {
            tentative_icon_size
        };

        let icon_fill = self.fill.or(icon_theme.fill);
        let icon_weight = self.weight.or(icon_theme.weight);
        let icon_grade = self.grade.or(icon_theme.grade);
        let icon_optical_size = self.optical_size.or(icon_theme.optical_size);
        let icon_shadows = self.shadows.clone().or(icon_theme.shadows.clone());

        let Some(icon) = &self.icon else {
            return self
                .semantics()
                .child(SizedBox::new().width(icon_size).height(icon_size))
                .into_widget();
        };

        let icon_opacity = icon_theme.opacity.unwrap_or(1.0);
        let mut icon_color = Some(self.color.clone().unwrap_or_else(|| {
            icon_theme
                .color
                .clone()
                .expect("IconTheme::of returns a concrete theme")
        }));
        let mut foreground = None;
        if icon_opacity != 1.0 {
            let color = icon_color.as_ref().expect("the color is resolved above");
            // Dart's `withOpacity`, deprecated there too: its rounding to an 8-bit alpha is
            // part of the behaviour being ported.
            #[allow(deprecated)]
            let faded = color.with_opacity(color.opacity() * icon_opacity);
            icon_color = Some(faded.into());
        }
        if let Some(blend_mode) = self.blend_mode {
            foreground = Some(Paint {
                blend_mode,
                color: icon_color
                    .as_ref()
                    .expect("the color is resolved above")
                    .color()
                    .into(),
                ..Paint::default()
            });
            // Cannot provide both a color and a foreground.
            icon_color = None;
        }

        let font_style = self.font_style(
            icon,
            ResolvedIcon {
                size: icon_size,
                fill: icon_fill,
                weight: icon_weight,
                grade: icon_grade,
                optical_size: icon_optical_size,
                shadows: icon_shadows,
                color: icon_color,
                foreground,
            },
        );

        let code_point = char::from_u32(icon.code_point)
            .expect("an IconData code point is a Unicode scalar value");
        let mut icon_widget = RichText::new(
            TextSpan::new()
                .text(code_point.to_string())
                .style(font_style)
                .into_span(),
        )
        // Never clip.
        .overflow(TextOverflow::Visible)
        // Since we already fetched it for the assert...
        .text_direction(text_direction)
        .into_widget();

        if icon.match_text_direction {
            match text_direction {
                TextDirection::Rtl => {
                    icon_widget = Transform::new(Matrix4::scale(-1.0, 1.0))
                        .alignment(AlignmentGeometry::CENTER)
                        .transform_hit_tests(false)
                        .child(icon_widget)
                        .into_widget();
                }
                TextDirection::Ltr => {}
            }
        }

        self.semantics()
            .child(
                ExcludeSemantics::new().child(
                    SizedBox::new()
                        .width(icon_size)
                        .height(icon_size)
                        .child(Center::new().child(icon_widget)),
                ),
            )
            .into_widget()
    }
}

#[cfg(test)]
mod tests {
    use waft_embedder::Color;
    use waft_embedder::Size;
    use waft_foundation::AppCell;
    use waft_painting::{PaintingBinding, TextScaler};
    use waft_rendering::{
        AnyRenderBox, BoxConstraints, RenderBox, RenderConstrainedBox, RenderConstrainedBoxBase,
        RenderExcludeSemantics, RenderHandle, RenderObject, RenderObjectWithChildMixin,
        RenderParagraph, RenderPositionedBox, RenderSemanticsAnnotations, RenderTransform,
        SemanticsAnnotationsMixin,
    };

    use super::*;
    use crate::test_harness::Harness;
    use crate::widgets::icon_theme_data::IconThemeData;
    use crate::widgets::media_query::{MediaQuery, MediaQueryData};

    /// 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());
            });
        }
    }

    fn glyph() -> IconData {
        IconData::new(0xe900)
            .font_family("TestIcons")
            .font_package("test_icons")
    }

    fn typed_child<P, C: RenderObject>(parent: RenderHandle<P>, app: &App) -> RenderHandle<C>
    where
        P: RenderObject + RenderObjectWithChildMixin<ChildType = AnyRenderBox>,
    {
        parent
            .child(app)
            .expect("a child")
            .as_object()
            .downcast::<C>(app)
            .expect("the expected render object")
    }

    /// The `Semantics` under the root: what an `Icon` builds around everything else.
    fn semantics_under_root(
        harness: &Harness,
        app: &App,
    ) -> RenderHandle<RenderSemanticsAnnotations> {
        harness
            .render_root(app)
            .child(app)
            .expect("a child")
            .as_object()
            .downcast::<RenderSemanticsAnnotations>(app)
            .expect("Icon builds a Semantics")
    }

    /// The square inside the icon's `Semantics`, and its `ExcludeSemantics` when it has a glyph.
    fn square_under_root(harness: &Harness, app: &App) -> RenderHandle<RenderConstrainedBox> {
        let semantics = semantics_under_root(harness, app);
        let child = semantics.child(app).expect("a child").as_object();
        match child.downcast::<RenderExcludeSemantics>(app) {
            Some(exclude) => typed_child(exclude, app),
            None => child
                .downcast::<RenderConstrainedBox>(app)
                .expect("Icon builds a SizedBox"),
        }
    }

    /// Mounts `icon` under an ambient `Directionality` and `IconTheme`, and pumps a frame.
    fn mount(
        app: &mut App,
        text_direction: TextDirection,
        theme: IconThemeData,
        icon: Icon,
    ) -> Harness {
        install_fonts(app);
        let harness = Harness::mount(
            app,
            Directionality::new(text_direction, IconTheme::new(theme, icon)).into_widget(),
        );
        harness.pump(app);
        harness
    }

    #[test]
    fn an_icon_builds_a_centred_glyph_in_a_square_of_its_size() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let theme = IconThemeData::fallback().color(Color::from_argb(255, 255, 0, 0));
        let harness = mount(
            &mut app,
            TextDirection::Ltr,
            theme,
            Icon::new(Some(glyph())).size(30.0),
        );

        let square = square_under_root(&harness, &app);
        assert_eq!(
            square.additional_constraints(&app),
            BoxConstraints::tight_for(Some(30.0), Some(30.0))
        );
        assert_eq!(square.size(&app), Size::new(30.0, 30.0));

        let center = typed_child::<_, RenderPositionedBox>(square, &app);
        let paragraph = typed_child::<_, RenderParagraph>(center, &app);
        assert_eq!(
            paragraph.text(&app).to_plain_text(true, true),
            "\u{e900}",
            "the code point is the glyph's text"
        );
        let style = paragraph
            .text(&app)
            .style()
            .cloned()
            .expect("Icon always styles its span");
        assert!(!style.inherit);
        assert_eq!(style.font_size, Some(30.0));
        assert_eq!(style.height, Some(1.0));
        assert_eq!(
            style.font_family.as_deref(),
            Some("packages/test_icons/TestIcons"),
            "the icon's font package prefixes its family"
        );
        assert_eq!(
            style.color.as_ref().map(AnyColor::color),
            Some(Color::from_argb(255, 255, 0, 0)),
            "the ambient IconTheme's color"
        );
        assert_eq!(style.font_variations.as_deref().map(<[_]>::len), Some(4));
        assert!(style.foreground.is_none());
    }

    #[test]
    fn an_icon_defaults_its_size_and_axes_to_the_ambient_theme() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let theme = IconThemeData::fallback().size(18.0).weight(700.0);
        let harness = mount(
            &mut app,
            TextDirection::Ltr,
            theme,
            Icon::new(Some(glyph())),
        );
        let square = square_under_root(&harness, &app);
        assert_eq!(square.size(&app), Size::new(18.0, 18.0));

        let center = typed_child::<_, RenderPositionedBox>(square, &app);
        let paragraph = typed_child::<_, RenderParagraph>(center, &app);
        let style = paragraph
            .text(&app)
            .style()
            .cloned()
            .expect("Icon always styles its span");
        assert_eq!(style.font_size, Some(18.0));
        let weight = style
            .font_variations
            .as_ref()
            .expect("the axes are always written")
            .iter()
            .find(|variation| variation.axis == "wght")
            .expect("the theme's weight");
        assert_eq!(weight.value, 700.0);
    }

    // icon_test.dart: 'Icon with semantic label', as the annotation keeps it.
    #[test]
    fn an_icon_labels_its_semantics_and_excludes_its_glyph() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let harness = mount(
            &mut app,
            TextDirection::Ltr,
            IconThemeData::fallback(),
            Icon::new(Some(glyph())).semantic_label("a label"),
        );
        let semantics = semantics_under_root(&harness, &app);
        assert_eq!(semantics.properties(&app).label.as_deref(), Some("a label"));
        let exclude = semantics
            .child(&app)
            .expect("a child")
            .as_object()
            .downcast::<RenderExcludeSemantics>(&app)
            .expect("the glyph sits in an ExcludeSemantics");
        assert!(exclude.excluding(&app));
    }

    // icon_test.dart: 'Null icon with semantic label', as the annotation keeps it.
    #[test]
    fn an_icon_without_an_icon_data_still_labels_its_semantics() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let harness = mount(
            &mut app,
            TextDirection::Ltr,
            IconThemeData::fallback(),
            Icon::new(None).semantic_label("a label"),
        );
        let semantics = semantics_under_root(&harness, &app);
        assert_eq!(semantics.properties(&app).label.as_deref(), Some("a label"));
        assert!(
            semantics
                .child(&app)
                .expect("a child")
                .as_object()
                .downcast::<RenderConstrainedBox>(&app)
                .is_some(),
            "no ExcludeSemantics without a glyph"
        );
    }

    #[test]
    fn an_icon_without_an_icon_data_is_an_empty_square() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let harness = mount(
            &mut app,
            TextDirection::Ltr,
            IconThemeData::fallback(),
            Icon::new(None).size(42.0),
        );
        let square = square_under_root(&harness, &app);
        assert_eq!(square.size(&app), Size::new(42.0, 42.0));
        assert!(square.child(&app).is_none(), "no glyph is built");
    }

    #[test]
    fn a_direction_matching_icon_is_mirrored_in_rtl() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let mirrored = glyph().match_text_direction(true);
        let cell_ltr = AppCell::new();
        let mut app_ltr = cell_ltr.borrow_mut();
        let ltr = mount(
            &mut app_ltr,
            TextDirection::Ltr,
            IconThemeData::fallback(),
            Icon::new(Some(mirrored.clone())),
        );
        let square = square_under_root(&ltr, &app_ltr);
        let center = typed_child::<_, RenderPositionedBox>(square, &app_ltr);
        assert!(
            center
                .child(&app_ltr)
                .expect("a child")
                .as_object()
                .downcast::<RenderParagraph>(&app_ltr)
                .is_some(),
            "no transform in left-to-right"
        );

        let rtl = mount(
            &mut app,
            TextDirection::Rtl,
            IconThemeData::fallback(),
            Icon::new(Some(mirrored)),
        );
        let square = square_under_root(&rtl, &app);
        let center = typed_child::<_, RenderPositionedBox>(square, &app);
        let transform = typed_child::<_, RenderTransform>(center, &app);
        assert!(!transform.transform_hit_tests(&app));
        assert_eq!(transform.alignment(&app), Some(AlignmentGeometry::CENTER));
        typed_child::<_, RenderParagraph>(transform, &app);
    }

    #[test]
    fn apply_text_scaling_scales_the_square_with_the_media_query() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        install_fonts(&mut app);
        let media = MediaQueryData::new().text_scaler(TextScaler::linear(2.0));
        let harness = Harness::mount(
            &mut app,
            Directionality::new(
                TextDirection::Ltr,
                MediaQuery::new(
                    media,
                    IconTheme::new(
                        IconThemeData::fallback().size(10.0),
                        Icon::new(Some(glyph())).apply_text_scaling(true),
                    ),
                )
                .into_widget(),
            )
            .into_widget(),
        );
        harness.pump(&mut app);
        let square = square_under_root(&harness, &app);
        assert_eq!(
            square.size(&app),
            Size::new(20.0, 20.0),
            "the text scaler doubles the icon's size"
        );
    }

    #[test]
    fn a_blend_mode_moves_the_color_into_a_foreground_paint() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let theme = IconThemeData::fallback().color(Color::from_argb(255, 0, 0, 255));
        let harness = mount(
            &mut app,
            TextDirection::Ltr,
            theme,
            Icon::new(Some(glyph())).blend_mode(BlendMode::Multiply),
        );
        let square = square_under_root(&harness, &app);
        let center = typed_child::<_, RenderPositionedBox>(square, &app);
        let paragraph = typed_child::<_, RenderParagraph>(center, &app);
        let style = paragraph
            .text(&app)
            .style()
            .cloned()
            .expect("Icon always styles its span");
        assert!(
            style.color.is_none(),
            "a color and a foreground are exclusive"
        );
        let foreground = style.foreground.expect("the blend mode makes a paint");
        assert_eq!(foreground.blend_mode, BlendMode::Multiply);
        assert_eq!(foreground.color, Color::from_argb(255, 0, 0, 255).into());
    }

    #[test]
    fn the_theme_s_opacity_fades_the_glyph_color() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let theme = IconThemeData::fallback()
            .color(Color::from_argb(255, 0, 0, 255))
            .opacity(0.5);
        let harness = mount(
            &mut app,
            TextDirection::Ltr,
            theme,
            Icon::new(Some(glyph())),
        );
        let square = square_under_root(&harness, &app);
        let center = typed_child::<_, RenderPositionedBox>(square, &app);
        let paragraph = typed_child::<_, RenderParagraph>(center, &app);
        let style = paragraph
            .text(&app)
            .style()
            .cloned()
            .expect("Icon always styles its span");
        assert_eq!(
            style.color.as_ref().map(AnyColor::color),
            Some(Color::from_argb(128, 0, 0, 255))
        );
    }
}