waft-material 0.16.0

Material Design 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
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
//! Flutter counterpart: `material/magnifier.dart`.

use std::rc::Rc;
use std::time::Duration;

use waft_embedder::{BlurStyle, Clip, Color, Offset, Radius, Size, TargetPlatform, clamp_double};
use waft_foundation::{
    App, Handle, Listenable, Listener, Ref, Timer, ValueListenable, ValueNotifier,
};
use waft_painting::{BorderRadius, BorderSide, BoxShadow, RoundedRectangleBorder};
use waft_widgets::{
    AnimatedPositioned, BuildContext, ColoredBox, IntoWidget, KeyRef, MagnifierBuilder,
    MagnifierController, MagnifierDecoration, MagnifierInfo, MediaQuery, RawMagnifier, State,
    StateData, StatefulWidget, StatelessWidget, TextMagnifierConfiguration, WidgetRef,
};

use waft_cupertino::CupertinoTextMagnifier;

/// A [`Magnifier`] positioned by rules dictated by the native Android magnifier.
///
/// The positioning rules are based on [`magnifier_info`](Self::magnifier_info), as follows:
///
/// - The loupe tracks the gesture's _x_ coordinate, clamping to the beginning
///   and end of the currently editing line.
///
/// - The focal point never contains anything out of the bounds of the text
///   field or other widget being magnified (the [`MagnifierInfo::field_bounds`]).
///
/// - The focal point always remains aligned with the _y_ coordinate of the touch.
///
/// - The loupe always remains on the screen.
///
/// - When the line targeted by the touch's _y_ coordinate changes, the position
///   is animated over [`JUMP_BETWEEN_LINES_ANIMATION_DURATION`](Self::JUMP_BETWEEN_LINES_ANIMATION_DURATION).
///
/// This behavior was based on the Android 12 source code, where possible, and
/// on eyeballing a Pixel 6 running Android 12 otherwise.
#[derive(Debug)]
pub struct TextMagnifier {
    pub key: Option<KeyRef>,
    /// The current status of the user's touch.
    ///
    /// As the value of the [`magnifier_info`](Self::magnifier_info) changes, the position of the
    /// loupe is adjusted automatically, according to the rules described in the
    /// [`TextMagnifier`] class description.
    pub magnifier_info: Ref<ValueNotifier<MagnifierInfo>>,
}

impl TextMagnifier {
    /// Creates a [`TextMagnifier`].
    ///
    /// The `magnifier_info` must be provided, and must be updated with new values
    /// as the user's touch changes.
    pub fn new(magnifier_info: Ref<ValueNotifier<MagnifierInfo>>) -> TextMagnifier {
        TextMagnifier {
            key: None,
            magnifier_info,
        }
    }

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

    /// Dart's `adaptiveMagnifierConfiguration`: a [`TextMagnifierConfiguration`] that returns a
    /// [`CupertinoTextMagnifier`] on iOS, [`TextMagnifier`] on Android, and `None` on all other
    /// platforms, and shows the editing handles only on iOS.
    ///
    /// Every call returns a configuration with the same builder, so two of them are the same
    /// configuration to a widget that compares them.
    pub fn adaptive_magnifier_configuration(app: &App) -> TextMagnifierConfiguration {
        thread_local! {
            static MAGNIFIER_BUILDER: MagnifierBuilder = Rc::new(
                |app: &mut App,
                 _context: BuildContext,
                 controller: Ref<MagnifierController>,
                 magnifier_info: Ref<ValueNotifier<MagnifierInfo>>| {
                    match app.platform().target_platform() {
                        TargetPlatform::IOS => Some(
                            CupertinoTextMagnifier::new(controller, magnifier_info).into_widget(),
                        ),
                        TargetPlatform::Android => {
                            Some(TextMagnifier::new(magnifier_info).into_widget())
                        }
                        TargetPlatform::Fuchsia
                        | TargetPlatform::Linux
                        | TargetPlatform::MacOS
                        | TargetPlatform::Windows => None,
                    }
                },
            );
        }
        TextMagnifierConfiguration::new()
            .should_display_handles_in_magnifier(
                app.platform().target_platform() == TargetPlatform::IOS,
            )
            .with_magnifier_builder(MAGNIFIER_BUILDER.with(MagnifierBuilder::clone))
    }

    /// The duration that the position is animated if [`TextMagnifier`] just switched
    /// between lines.
    pub const JUMP_BETWEEN_LINES_ANIMATION_DURATION: Duration = Duration::from_millis(70);
}

impl StatefulWidget for TextMagnifier {
    type State = TextMagnifierState;

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

    fn create_state(&self) -> TextMagnifierState {
        TextMagnifierState {
            state: StateData::new(),
            magnifier_position: None,
            position_should_be_animated_timer: None,
            extra_focal_point_offset: Offset::ZERO,
        }
    }
}

/// Dart's `_TextMagnifierState`.
pub struct TextMagnifierState {
    state: StateData<TextMagnifier>,
    // Should _only_ be null on construction. This is because of the animation logic.
    //
    // Animations are added when `last_build_y != current_build_y`. This condition
    // is true on the initial render, which would mean that the initial
    // build would be animated - this is undesired. Thus, this is null for the
    // first frame and the condition becomes `magnifier_position != null && last_build_y != this_build_y`.
    magnifier_position: Option<Offset>,
    // A timer that unsets itself after an animation duration.
    // If the timer exists, then the magnifier animates its position -
    // if this timer does not exist, the magnifier tracks the gesture (with respect
    // to the positioning rules) directly.
    position_should_be_animated_timer: Option<Timer>,
    extra_focal_point_offset: Offset,
}

impl TextMagnifierState {
    fn position_should_be_animated(self: Handle<Self>, app: &App) -> bool {
        app.get(self).position_should_be_animated_timer.is_some()
    }

    fn determine_magnifier_position_and_focal_point(self: Handle<Self>, app: &mut App) {
        let magnifier_info = self.widget(app).magnifier_info.clone();
        let selection_info = *magnifier_info.value(app);
        let context = self.context(app);
        let screen_rect = Offset::ZERO & MediaQuery::size_of(app, context);

        // Since by default we draw at the top left corner, this offset
        // shifts the magnifier so we draw at the center, and then also includes
        // the "above touch point" shift.
        let basic_magnifier_offset = Offset::new(
            Magnifier::K_DEFAULT_MAGNIFIER_SIZE.width() / 2.0,
            Magnifier::K_DEFAULT_MAGNIFIER_SIZE.height()
                + Magnifier::K_STANDARD_VERTICAL_FOCAL_POINT_SHIFT,
        );

        // Since the magnifier should not go past the edges of the line,
        // but must track the gesture otherwise, constrain the X of the magnifier
        // to always stay between line start and end.
        let magnifier_x = clamp_double(
            selection_info.global_gesture_position.dx(),
            selection_info.current_line_boundaries.left,
            selection_info.current_line_boundaries.right,
        );

        // Place the magnifier at the previously calculated X, and the Y should be
        // exactly at the center of the handle.
        let unadjusted_magnifier_rect =
            (Offset::new(magnifier_x, selection_info.caret_rect.center().dy())
                - basic_magnifier_offset)
                & Magnifier::K_DEFAULT_MAGNIFIER_SIZE;

        // Shift the magnifier so that, if we are ever out of the screen, we become in bounds.
        // This probably won't have much of an effect on the X, since it is already bound
        // to the currentLineBoundaries, but will shift vertically if the magnifier is out of bounds.
        let screen_bounds_adjusted_magnifier_rect =
            MagnifierController::shift_within_bounds(unadjusted_magnifier_rect, screen_rect);

        // Done with the magnifier position!
        let final_magnifier_position = screen_bounds_adjusted_magnifier_rect.top_left();

        // The insets, from either edge, that the focal point should not point
        // past lest the magnifier displays something out of bounds.
        let horizontal_max_focal_point_edge_insets =
            (Magnifier::K_DEFAULT_MAGNIFIER_SIZE.width() / 2.0) / Magnifier::MAGNIFICATION;

        // Adjust the focal point horizontally such that none of the magnifier
        // ever points to anything out of bounds.
        // If the text field is so narrow that we must show out of bounds,
        // then settle for pointing to the center all the time.
        let new_global_focal_point_x =
            if selection_info.field_bounds.width() < horizontal_max_focal_point_edge_insets * 2.0 {
                selection_info.field_bounds.center().dx()
            } else {
                // Otherwise, we can clamp the focal point to always point in bounds.
                clamp_double(
                    screen_bounds_adjusted_magnifier_rect.center().dx(),
                    selection_info.field_bounds.left + horizontal_max_focal_point_edge_insets,
                    selection_info.field_bounds.right - horizontal_max_focal_point_edge_insets,
                )
            };

        // Since the previous value is now a global offset (i.e. `new_global_focal_point`
        // is now a global offset), we must subtract the magnifier's global offset
        // to obtain the relative shift in the focal point.
        let new_relative_focal_point_x =
            new_global_focal_point_x - screen_bounds_adjusted_magnifier_rect.center().dx();

        // The Y component means that if we are pressed up against the top of the screen,
        // then we should adjust the focal point such that it now points to how far we moved
        // the magnifier. screen_bounds_adjusted_magnifier_rect.top == unadjusted_magnifier_rect.top for most cases,
        // but when pressed up against the top of the screen, we adjust the focal point by
        // the amount that we shifted from our "natural" position.
        let focal_point_adjustment_for_screen_bounds_adjustment = Offset::new(
            new_relative_focal_point_x,
            unadjusted_magnifier_rect.top - screen_bounds_adjusted_magnifier_rect.top,
        );

        let mut position_should_be_animated = app.get(self).position_should_be_animated_timer;

        if let Some(magnifier_position) = app.get(self).magnifier_position
            && final_magnifier_position.dy() != magnifier_position.dy()
        {
            if let Some(timer) = position_should_be_animated
                && timer.is_active(app)
            {
                timer.cancel(app);
            }

            // Create a timer that deletes itself when the timer is complete.
            // This is `mounted` safe, since the timer is canceled in `dispose`.
            position_should_be_animated = Some(Timer::new(
                app,
                TextMagnifier::JUMP_BETWEEN_LINES_ANIMATION_DURATION,
                Listener::new(move |app| {
                    self.set_state(app, |state| {
                        state.position_should_be_animated_timer = None;
                    });
                }),
            ));
        }

        self.set_state(app, |state| {
            state.magnifier_position = Some(final_magnifier_position);
            state.position_should_be_animated_timer = position_should_be_animated;
            state.extra_focal_point_offset = focal_point_adjustment_for_screen_bounds_adjustment;
        });
    }
}

impl State for TextMagnifierState {
    type Widget = TextMagnifier;
    waft_widgets::state_accessors!();

    fn init_state(self: Handle<Self>, app: &mut App) {
        let magnifier_info = self.widget(app).magnifier_info.clone();
        magnifier_info.add_listener(
            app,
            Listener::handle_method(self, Self::determine_magnifier_position_and_focal_point),
        );
    }

    fn dispose(self: Handle<Self>, app: &mut App) {
        let magnifier_info = self.widget(app).magnifier_info.clone();
        magnifier_info.remove_listener(
            app,
            &Listener::handle_method(self, Self::determine_magnifier_position_and_focal_point),
        );
        if let Some(timer) = app.get(self).position_should_be_animated_timer {
            timer.cancel(app);
        }
    }

    fn did_change_dependencies(self: Handle<Self>, app: &mut App) {
        self.determine_magnifier_position_and_focal_point(app);
    }

    fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &TextMagnifier) {
        let magnifier_info = self.widget(app).magnifier_info.clone();
        if old_widget.magnifier_info != magnifier_info {
            old_widget.magnifier_info.remove_listener(
                app,
                &Listener::handle_method(self, Self::determine_magnifier_position_and_focal_point),
            );
            magnifier_info.add_listener(
                app,
                Listener::handle_method(self, Self::determine_magnifier_position_and_focal_point),
            );
        }
    }

    fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
        let magnifier_position = app.get(self).magnifier_position;
        debug_assert!(
            magnifier_position.is_some(),
            "Magnifier position should only be null before the first build."
        );
        let magnifier_position = magnifier_position.expect("set in did_change_dependencies");
        let extra_focal_point_offset = app.get(self).extra_focal_point_offset;
        let duration = if self.position_should_be_animated(app) {
            TextMagnifier::JUMP_BETWEEN_LINES_ANIMATION_DURATION
        } else {
            Duration::ZERO
        };

        AnimatedPositioned::new(
            Magnifier::new().additional_focal_point_offset(extra_focal_point_offset),
            // Material magnifier typically does not animate, unless we jump between lines,
            // in which case we animate between lines.
            duration,
        )
        .top(magnifier_position.dy())
        .left(magnifier_position.dx())
        .into_widget()
    }
}

/// A Material-styled magnifying glass.
///
/// This widget focuses on mimicking the _style_ of the magnifier on material.
/// For a widget that is focused on mimicking the _behavior_ of a material
/// magnifier, see [`TextMagnifier`], which uses [`Magnifier`].
///
/// The styles implemented in this widget were based on the Android 12 source
/// code, where possible, and on eyeballing a Pixel 6 running Android 12
/// otherwise.
#[derive(Debug)]
pub struct Magnifier {
    pub key: Option<KeyRef>,
    /// Any additional offset the focal point requires to "point"
    /// to the correct place.
    ///
    /// This value is added to [`K_STANDARD_VERTICAL_FOCAL_POINT_SHIFT`](Self::K_STANDARD_VERTICAL_FOCAL_POINT_SHIFT)
    /// to obtain the actual offset.
    ///
    /// This is useful for instances where the magnifier is not pointing to
    /// something directly below it.
    pub additional_focal_point_offset: Offset,
    /// The border radius for this magnifier.
    ///
    /// The magnifier's shape is a [`RoundedRectangleBorder`] with this radius.
    pub border_radius: BorderRadius,
    /// The color to tint the image in this [`Magnifier`].
    ///
    /// On native Android, there is a almost transparent gray tint to the
    /// magnifier, in order to better distinguish the contents of the lens from
    /// the background.
    pub film_color: Color,
    /// A list of shadows cast by the [`Magnifier`].
    ///
    /// If the shadows use a [`BlurStyle`] that paints inside the shape, or if they
    /// are offset, then a [`clip_behavior`](Self::clip_behavior) that enables clipping (such as the
    /// default [`Clip::HardEdge`]) is recommended, otherwise the shadow will occlude
    /// the magnifier (the shadow is drawn above the magnifier so as to not be
    /// included in the magnified image).
    ///
    /// By default, the shadows are offset vertically by two logical pixels, so
    /// clipping is recommended.
    ///
    /// A shadow that uses [`BlurStyle::Outer`] and is not offset does not need
    /// clipping; in that case, consider setting [`clip_behavior`](Self::clip_behavior) to [`Clip::None`].
    pub shadows: Vec<BoxShadow>,
    /// Whether and how to clip the [`shadows`](Self::shadows) that render inside the loupe.
    ///
    /// Defaults to [`Clip::HardEdge`].
    ///
    /// A value of [`Clip::None`] can be used if the shadow will not paint where the
    /// magnified image appears, or if doing so is intentional (e.g. to blur the
    /// edges of the magnified image).
    ///
    /// See the discussion at [`shadows`](Self::shadows).
    pub clip_behavior: Clip,
    /// The [`Size`] of this [`Magnifier`].
    ///
    /// The [`shadows`](Self::shadows) are drawn outside of the [`size`](Self::size).
    pub size: Size,
}

impl Magnifier {
    /// Creates a [`RawMagnifier`] in the Material style.
    pub fn new() -> Magnifier {
        Magnifier::default()
    }

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

    /// Dart `Magnifier(additionalFocalPointOffset:)`.
    pub fn additional_focal_point_offset(
        mut self,
        additional_focal_point_offset: Offset,
    ) -> Magnifier {
        self.additional_focal_point_offset = additional_focal_point_offset;
        self
    }

    /// Dart `Magnifier(borderRadius:)`.
    pub fn border_radius(mut self, border_radius: BorderRadius) -> Magnifier {
        self.border_radius = border_radius;
        self
    }

    /// Dart `Magnifier(filmColor:)`.
    pub fn film_color(mut self, film_color: Color) -> Magnifier {
        self.film_color = film_color;
        self
    }

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

    /// Dart `Magnifier(clipBehavior:)`.
    pub fn clip_behavior(mut self, clip_behavior: Clip) -> Magnifier {
        self.clip_behavior = clip_behavior;
        self
    }

    /// Dart `Magnifier(size:)`.
    pub fn size(mut self, size: Size) -> Magnifier {
        self.size = size;
        self
    }

    /// The default size of this [`Magnifier`].
    ///
    /// The size of the magnifier may be modified through the constructor;
    /// [`K_DEFAULT_MAGNIFIER_SIZE`](Self::K_DEFAULT_MAGNIFIER_SIZE) is extracted from the default
    /// parameter of [`Magnifier`]'s constructor so that positioners may depend on it.
    pub const K_DEFAULT_MAGNIFIER_SIZE: Size = Size::new(77.37, 37.9);

    /// The vertical distance that the magnifier should be above the focal point.
    ///
    /// The [`K_STANDARD_VERTICAL_FOCAL_POINT_SHIFT`](Self::K_STANDARD_VERTICAL_FOCAL_POINT_SHIFT)
    /// value is a constant so that positioning of this [`Magnifier`] can be done with a guaranteed
    /// size, as opposed to an estimate.
    pub const K_STANDARD_VERTICAL_FOCAL_POINT_SHIFT: f64 = 22.0;

    const BORDER_RADIUS: f64 = 40.0;
    const MAGNIFICATION: f64 = 1.25;
}

impl Default for Magnifier {
    fn default() -> Magnifier {
        Magnifier {
            key: None,
            additional_focal_point_offset: Offset::ZERO,
            border_radius: BorderRadius::all(Radius::circular(Magnifier::BORDER_RADIUS)),
            film_color: Color::from_argb(8, 158, 158, 158),
            shadows: vec![BoxShadow::new(
                Color::from_argb(25, 0, 0, 0),
                Offset::new(0.0, 2.0),
                1.5,
                0.75,
                BlurStyle::Normal,
            )],
            clip_behavior: Clip::HardEdge,
            size: Magnifier::K_DEFAULT_MAGNIFIER_SIZE,
        }
    }
}

impl StatelessWidget for Magnifier {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
        RawMagnifier::new(self.size)
            .decoration(
                MagnifierDecoration::new()
                    .shape(RoundedRectangleBorder::new(
                        BorderSide::NONE,
                        self.border_radius.into(),
                    ))
                    .shadows(self.shadows.clone()),
            )
            .clip_behavior(self.clip_behavior)
            .magnification_scale(Magnifier::MAGNIFICATION)
            .focal_point_offset(
                self.additional_focal_point_offset
                    + Offset::new(
                        0.0,
                        Magnifier::K_STANDARD_VERTICAL_FOCAL_POINT_SHIFT
                            + Magnifier::K_DEFAULT_MAGNIFIER_SIZE.height() / 2.0,
                    ),
            )
            .child(
                // This couldn't be part of the decoration (even if the
                // MagnifierDecoration supported specifying a color) because the
                // decoration's shadows are offset and therefore we set a clipBehavior
                // that clips the inner part of the decoration to avoid occluding the
                // magnified image with the shadow.
                ColoredBox::new(self.film_color),
            )
            .into_widget()
    }
}

#[cfg(test)]
mod tests {
    use waft_embedder::{Rect, TargetPlatform};
    use waft_widgets::{
        GlobalKey, ImplicitlyAnimatedWidget, Positioned, SizedBox, downcast_widget,
    };

    use waft_widgets::testing::{WidgetTester, find, test_widgets};

    use super::*;
    use crate::app::MaterialApp;
    use crate::theme_data::ThemeData;

    const REASONABLE_TEXT_FIELD: Rect = Rect::from_ltrb(50.0, 100.0, 200.0, 100.0);
    const BASIC_OFFSET: Offset = Offset::new(
        Magnifier::K_DEFAULT_MAGNIFIER_SIZE.width() / 2.0,
        Magnifier::K_STANDARD_VERTICAL_FOCAL_POINT_SHIFT
            + Magnifier::K_DEFAULT_MAGNIFIER_SIZE.height(),
    );

    fn info(
        line: Rect,
        field: Rect,
        caret: Rect,
        global_gesture_position: Offset,
    ) -> MagnifierInfo {
        MagnifierInfo::new(global_gesture_position, caret, field, line)
    }

    /// An info whose line, field and caret are all `rect`, the gesture at `position`.
    fn info_over(rect: Rect, position: Offset) -> MagnifierInfo {
        info(rect, rect, rect, position)
    }

    /// Dart's `MaterialApp(home: Placeholder())`, whose home is a sized box under `key`.
    fn pump_app(tester: &mut WidgetTester, key: &GlobalKey) {
        let root = {
            let app = tester.cell.borrow();
            MaterialApp::new()
                .theme(ThemeData::new(&app))
                .home(SizedBox::new().key(key.clone().into()))
                .into_widget()
        };
        tester.pump_widget(root);
    }

    fn app_context(tester: &mut WidgetTester) -> BuildContext {
        let home = GlobalKey::new();
        pump_app(tester, &home);
        home.current_context(&tester.cell.borrow_mut())
            .expect("mounted")
    }

    /// Dart's `showMagnifier`: shows a [`TextMagnifier`] over `magnifier_info` and settles.
    fn show_magnifier(
        tester: &mut WidgetTester,
        context: BuildContext,
        magnifier_info: MagnifierInfo,
    ) -> (Ref<MagnifierController>, Ref<ValueNotifier<MagnifierInfo>>) {
        let (controller, notifier) = {
            let mut app = tester.cell.borrow_mut();
            let notifier = app.create(ValueNotifier::new(magnifier_info));
            let controller = MagnifierController::new(&mut app);
            let shown = notifier.clone();
            controller.show(
                &mut app,
                context,
                Rc::new(move |_app: &mut App, _context: BuildContext| {
                    TextMagnifier::new(shown.clone()).into_widget()
                }),
                None,
            );
            (controller, notifier)
        };
        tester.pump_and_settle();
        (controller, notifier)
    }

    fn magnifier_position(tester: &WidgetTester) -> Offset {
        let positioned = find::by_type::<Positioned>().evaluate(tester)[0];
        tester.widget::<Positioned, _>(positioned, |positioned| {
            Offset::new(
                positioned.left.unwrap_or(0.0),
                positioned.top.unwrap_or(0.0),
            )
        })
    }

    fn magnifier_additional_focal_point(tester: &WidgetTester) -> Offset {
        let magnifier = find::by_type::<Magnifier>().evaluate(tester)[0];
        tester.widget::<Magnifier, _>(magnifier, |magnifier| {
            magnifier.additional_focal_point_offset
        })
    }

    fn is_animated(tester: &WidgetTester) -> bool {
        let positioned = find::by_type::<AnimatedPositioned>().evaluate(tester)[0];
        tester.widget::<AnimatedPositioned, _>(positioned, |positioned| {
            !positioned
                .implicitly_animated_widget_data()
                .duration
                .is_zero()
        })
    }

    /// What the configuration's builder builds on `platform`.
    fn built_by_adaptive_configuration(platform: TargetPlatform) -> Option<WidgetRef> {
        let mut tester = WidgetTester::on(platform);
        let context = app_context(&mut tester);
        let mut app = tester.cell.borrow_mut();
        let notifier = app.create(ValueNotifier::new(MagnifierInfo::EMPTY));
        let controller = MagnifierController::new(&mut app);
        let builder = TextMagnifier::adaptive_magnifier_configuration(&app).magnifier_builder();
        builder(&mut app, context, controller, notifier)
    }

    mod adaptive_magnifier_controller_builder {
        use super::*;

        #[test]
        fn should_return_a_text_editing_magnifier_on_android() {
            test_widgets(|_tester| {
                let built = built_by_adaptive_configuration(TargetPlatform::Android)
                    .expect("a magnifier on Android");
                assert!(downcast_widget::<TextMagnifier>(&*built).is_some());
            });
        }

        #[test]
        fn should_return_a_cupertino_magnifier_on_ios() {
            test_widgets(|_tester| {
                let built = built_by_adaptive_configuration(TargetPlatform::IOS)
                    .expect("a magnifier on iOS");
                assert!(downcast_widget::<CupertinoTextMagnifier>(&*built).is_some());
            });
        }

        #[test]
        fn should_return_null_on_all_platforms_not_android_ios() {
            test_widgets(|_tester| {
                for platform in [
                    TargetPlatform::Fuchsia,
                    TargetPlatform::Linux,
                    TargetPlatform::MacOS,
                    TargetPlatform::Windows,
                ] {
                    assert!(
                        built_by_adaptive_configuration(platform).is_none(),
                        "{platform:?}"
                    );
                }
            });
        }

        #[test]
        fn shows_the_editing_handles_only_on_ios() {
            test_widgets(|_tester| {
                for platform in [
                    TargetPlatform::Android,
                    TargetPlatform::Fuchsia,
                    TargetPlatform::IOS,
                    TargetPlatform::Linux,
                    TargetPlatform::MacOS,
                    TargetPlatform::Windows,
                ] {
                    let tester = WidgetTester::on(platform);
                    let configuration =
                        TextMagnifier::adaptive_magnifier_configuration(&tester.cell.borrow());
                    assert_eq!(
                        configuration.should_display_handles_in_magnifier,
                        platform == TargetPlatform::IOS,
                        "{platform:?}"
                    );
                }
            });
        }

        #[test]
        fn keeps_one_builder_identity_across_calls() {
            test_widgets(|tester| {
                let app = tester.cell.borrow();
                let first =
                    TextMagnifier::adaptive_magnifier_configuration(&app).magnifier_builder();
                let second =
                    TextMagnifier::adaptive_magnifier_configuration(&app).magnifier_builder();
                assert!(Rc::ptr_eq(&first, &second));
            });
        }
    }

    mod position {
        use super::*;

        #[test]
        fn should_never_move_outside_the_right_bounds_of_the_editing_line() {
            test_widgets(|tester| {
                const GESTURE_OUTSIDE_LINE: f64 = 100.0;
                let context = app_context(tester);

                let (controller, _) = show_magnifier(
                    tester,
                    context,
                    info(
                        REASONABLE_TEXT_FIELD,
                        // Inflate these two to make sure we're bounding on the
                        // current line boundaries, not anything else.
                        REASONABLE_TEXT_FIELD.inflate(GESTURE_OUTSIDE_LINE),
                        REASONABLE_TEXT_FIELD.inflate(GESTURE_OUTSIDE_LINE),
                        // The tap position is far out of the right side of the app.
                        Offset::new(REASONABLE_TEXT_FIELD.right + GESTURE_OUTSIDE_LINE, 0.0),
                    ),
                );

                // Should be less than the right edge, since we have padding.
                assert!(magnifier_position(tester).dx() <= REASONABLE_TEXT_FIELD.right);
                controller.remove_from_overlay(&mut tester.cell.borrow_mut());
            });
        }

        #[test]
        fn should_never_move_outside_the_left_bounds_of_the_editing_line() {
            test_widgets(|tester| {
                const GESTURE_OUTSIDE_LINE: f64 = 100.0;
                let context = app_context(tester);

                let (controller, _) = show_magnifier(
                    tester,
                    context,
                    info(
                        REASONABLE_TEXT_FIELD,
                        REASONABLE_TEXT_FIELD.inflate(GESTURE_OUTSIDE_LINE),
                        REASONABLE_TEXT_FIELD.inflate(GESTURE_OUTSIDE_LINE),
                        // The tap position is far out of the left side of the app.
                        Offset::new(REASONABLE_TEXT_FIELD.left - GESTURE_OUTSIDE_LINE, 0.0),
                    ),
                );

                assert!(
                    magnifier_position(tester).dx() + BASIC_OFFSET.dx()
                        >= REASONABLE_TEXT_FIELD.left
                );
                controller.remove_from_overlay(&mut tester.cell.borrow_mut());
            });
        }

        #[test]
        fn should_position_vertically_at_the_center_of_the_line() {
            test_widgets(|tester| {
                let context = app_context(tester);

                let (controller, _) = show_magnifier(
                    tester,
                    context,
                    info_over(REASONABLE_TEXT_FIELD, REASONABLE_TEXT_FIELD.center()),
                );

                assert_eq!(
                    magnifier_position(tester).dy(),
                    REASONABLE_TEXT_FIELD.center().dy() - BASIC_OFFSET.dy()
                );
                controller.remove_from_overlay(&mut tester.cell.borrow_mut());
            });
        }

        #[test]
        fn should_reposition_vertically_if_mashed_against_the_ceiling() {
            test_widgets(|tester| {
                let top_of_screen_text_field =
                    Rect::from_points(Offset::ZERO, Offset::new(200.0, 0.0));
                let context = app_context(tester);

                let (controller, _) = show_magnifier(
                    tester,
                    context,
                    info_over(
                        top_of_screen_text_field,
                        top_of_screen_text_field.top_center(),
                    ),
                );

                assert!(magnifier_position(tester).dy() >= 0.0);
                controller.remove_from_overlay(&mut tester.cell.borrow_mut());
            });
        }
    }

    mod focal_point {
        use super::*;

        #[test]
        fn should_shift_focal_point_so_that_the_lens_sees_nothing_out_of_bounds() {
            test_widgets(|tester| {
                let context = app_context(tester);

                let (controller, _) = show_magnifier(
                    tester,
                    context,
                    // Gesture on the far right of the magnifier.
                    info_over(REASONABLE_TEXT_FIELD, REASONABLE_TEXT_FIELD.top_left()),
                );

                assert!(magnifier_additional_focal_point(tester).dx() < REASONABLE_TEXT_FIELD.left);
                controller.remove_from_overlay(&mut tester.cell.borrow_mut());
            });
        }

        #[test]
        fn focal_point_should_shift_if_mashed_against_the_top_to_always_point_to_text() {
            test_widgets(|tester| {
                let top_of_screen_text_field =
                    Rect::from_points(Offset::ZERO, Offset::new(200.0, 0.0));
                let context = app_context(tester);

                let (controller, _) = show_magnifier(
                    tester,
                    context,
                    info_over(
                        top_of_screen_text_field,
                        top_of_screen_text_field.top_center(),
                    ),
                );

                assert!(magnifier_additional_focal_point(tester).dy() < 0.0);
                controller.remove_from_overlay(&mut tester.cell.borrow_mut());
            });
        }
    }

    mod animation_state {
        use super::*;

        const VERTICAL_SHIFT: Offset = Offset::new(0.0, 200.0);

        /// The info of a gesture that went down a line from `REASONABLE_TEXT_FIELD`.
        fn info_one_line_down() -> MagnifierInfo {
            info(
                REASONABLE_TEXT_FIELD.shift(VERTICAL_SHIFT),
                Rect::from_points(
                    REASONABLE_TEXT_FIELD.top_left(),
                    REASONABLE_TEXT_FIELD.bottom_right() + VERTICAL_SHIFT,
                ),
                REASONABLE_TEXT_FIELD.shift(VERTICAL_SHIFT),
                REASONABLE_TEXT_FIELD.center() + VERTICAL_SHIFT,
            )
        }

        #[test]
        fn should_not_be_animated_on_the_initial_state() {
            test_widgets(|tester| {
                let context = app_context(tester);

                let (controller, _) = show_magnifier(
                    tester,
                    context,
                    info_over(REASONABLE_TEXT_FIELD, REASONABLE_TEXT_FIELD.center()),
                );

                assert!(!is_animated(tester));
                controller.remove_from_overlay(&mut tester.cell.borrow_mut());
            });
        }

        #[test]
        fn should_not_be_animated_on_horizontal_shifts() {
            test_widgets(|tester| {
                let context = app_context(tester);
                let (controller, notifier) = show_magnifier(
                    tester,
                    context,
                    info_over(REASONABLE_TEXT_FIELD, REASONABLE_TEXT_FIELD.center()),
                );

                // New position has a horizontal shift.
                notifier.set_value(
                    &mut tester.cell.borrow_mut(),
                    info_over(
                        REASONABLE_TEXT_FIELD,
                        REASONABLE_TEXT_FIELD.center() + Offset::new(200.0, 0.0),
                    ),
                );
                tester.pump_and_settle();

                assert!(!is_animated(tester));
                controller.remove_from_overlay(&mut tester.cell.borrow_mut());
            });
        }

        #[test]
        fn should_be_animated_on_vertical_shifts() {
            test_widgets(|tester| {
                let context = app_context(tester);
                let (controller, notifier) = show_magnifier(
                    tester,
                    context,
                    info_over(REASONABLE_TEXT_FIELD, REASONABLE_TEXT_FIELD.center()),
                );

                // New position has a vertical shift.
                notifier.set_value(&mut tester.cell.borrow_mut(), info_one_line_down());

                tester.pump();
                assert!(is_animated(tester));
                controller.remove_from_overlay(&mut tester.cell.borrow_mut());
            });
        }

        #[test]
        fn should_stop_being_animated_when_timer_is_up() {
            test_widgets(|tester| {
                let context = app_context(tester);
                let (controller, notifier) = show_magnifier(
                    tester,
                    context,
                    info_over(REASONABLE_TEXT_FIELD, REASONABLE_TEXT_FIELD.center()),
                );

                // New position has a vertical shift.
                notifier.set_value(&mut tester.cell.borrow_mut(), info_one_line_down());

                tester.pump();
                assert!(is_animated(tester));
                tester.pump_by(
                    TextMagnifier::JUMP_BETWEEN_LINES_ANIMATION_DURATION + Duration::from_secs(2),
                );
                assert!(!is_animated(tester));
                controller.remove_from_overlay(&mut tester.cell.borrow_mut());
            });
        }
    }

    mod material_magnifier {
        use waft_widgets::RawMagnifier;

        use super::*;

        #[test]
        fn builds_a_raw_magnifier_in_the_material_style_by_default() {
            test_widgets(|tester| {
                let _context = app_context(tester);
                let root = {
                    let app = tester.cell.borrow();
                    MaterialApp::new()
                        .theme(ThemeData::new(&app))
                        .home(Magnifier::new())
                        .into_widget()
                };
                tester.pump_widget(root);

                let raw = find::by_type::<RawMagnifier>().evaluate(tester)[0];
                tester.widget::<RawMagnifier, _>(raw, |raw| {
                    assert_eq!(raw.size, Magnifier::K_DEFAULT_MAGNIFIER_SIZE);
                    assert_eq!(raw.size, Size::new(77.37, 37.9));
                    assert_eq!(raw.clip_behavior, Clip::HardEdge);
                    assert_eq!(raw.magnification_scale, 1.25);
                    assert_eq!(raw.focal_point_offset, Offset::new(0.0, 22.0 + 37.9 / 2.0));
                    assert_eq!(
                        raw.decoration.shadows,
                        Some(vec![BoxShadow::new(
                            Color::from_argb(25, 0, 0, 0),
                            Offset::new(0.0, 2.0),
                            1.5,
                            0.75,
                            BlurStyle::Normal,
                        )])
                    );
                    let expected_shape = RoundedRectangleBorder::new(
                        BorderSide::NONE,
                        BorderRadius::all(Radius::circular(40.0)).into(),
                    );
                    assert!(raw.decoration.shape.eq_shape(&expected_shape));
                });
            });
        }

        #[test]
        fn adds_the_additional_focal_point_offset_to_the_standard_shift() {
            test_widgets(|tester| {
                let _context = app_context(tester);
                let root = {
                    let app = tester.cell.borrow();
                    MaterialApp::new()
                        .theme(ThemeData::new(&app))
                        .home(
                            Magnifier::new().additional_focal_point_offset(Offset::new(5.0, -3.0)),
                        )
                        .into_widget()
                };
                tester.pump_widget(root);

                let raw = find::by_type::<RawMagnifier>().evaluate(tester)[0];
                let focal_point_offset =
                    tester.widget::<RawMagnifier, _>(raw, |raw| raw.focal_point_offset);
                assert_eq!(
                    focal_point_offset,
                    Offset::new(5.0, 22.0 + 37.9 / 2.0 - 3.0)
                );
            });
        }

        #[test]
        fn sizes_the_lens_to_zero_area_when_given_a_zero_size() {
            test_widgets(|tester| {
                let _context = app_context(tester);
                let root = {
                    let app = tester.cell.borrow();
                    MaterialApp::new()
                        .theme(ThemeData::new(&app))
                        .home(Magnifier::new().size(Size::ZERO))
                        .into_widget()
                };
                tester.pump_widget(root);

                let raw = find::by_type::<RawMagnifier>().evaluate(tester)[0];
                assert_eq!(
                    tester.widget::<RawMagnifier, _>(raw, |raw| raw.size),
                    Size::ZERO
                );
            });
        }
    }
}