waft-material 0.9.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
//! Flutter counterpart: material_ui's `segmented_button_theme.dart`.

use std::rc::Rc;

use waft_foundation::App;
use waft_widgets::{BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef};

use crate::button_style::ButtonStyle;
use crate::theme::Theme;

/// Overrides the default values of visual properties for descendant
/// `SegmentedButton` widgets.
///
/// Descendant widgets obtain the current [`SegmentedButtonThemeData`] object with
/// [`SegmentedButtonTheme::of`]. Instances of [`SegmentedButtonThemeData`] can
/// be customized with [`SegmentedButtonThemeData::copy_with`].
///
/// Typically a [`SegmentedButtonThemeData`] is specified as part of the overall
/// `Theme` with `ThemeData::segmented_button_theme`.
///
/// All [`SegmentedButtonThemeData`] properties are null by default. When null,
/// the `SegmentedButton` computes its own default values, typically based on
/// the overall theme's `ThemeData::color_scheme`, `ThemeData::text_theme`, and
/// `ThemeData::icon_theme`.
#[derive(Clone, Debug, Default)]
pub struct SegmentedButtonThemeData {
    /// Overrides the `SegmentedButton`'s default style.
    ///
    /// Non-null properties or non-null resolved `WidgetStateProperty`
    /// values override the default values used by `SegmentedButton`.
    ///
    /// If [`style`](Self::style) is null, then this theme doesn't override anything.
    ///
    /// If [`ButtonStyle::side`] is provided, `WidgetStateProperty::resolve` is used
    /// for the following `WidgetState`s:
    ///
    ///  * `WidgetState::Focused`.
    ///  * `WidgetState::Hovered`.
    ///  * `WidgetState::Disabled`.
    ///  * `WidgetState::Selected`.
    pub style: Option<ButtonStyle>,

    /// Override for `SegmentedButton::selected_icon` property.
    ///
    /// If non-null, then [`selected_icon`](Self::selected_icon) will be used instead of default
    /// value for `SegmentedButton::selected_icon`.
    pub selected_icon: Option<WidgetRef>,
}

crate::theme_data_getters::theme_data_getters! {
    /// Dart's getters of `SegmentedButtonThemeData`: the theme data answers with its fields,
    /// and a defaults subclass (Dart's `_SegmentedButtonDefaultsM3(context)`) computes the ones
    /// it overrides when they are read.
    pub trait SegmentedButtonThemeDataGetters for SegmentedButtonThemeData,
    fields: segmented_button_theme_data {
        style: Option<ButtonStyle>,
        selected_icon: Option<WidgetRef>,
    }
}

impl SegmentedButtonThemeData {
    /// Creates a [`SegmentedButtonThemeData`] that can be used to override default properties
    /// in a [`SegmentedButtonTheme`] widget.
    pub fn new() -> SegmentedButtonThemeData {
        SegmentedButtonThemeData::default()
    }

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

    /// Dart `SegmentedButtonThemeData(selectedIcon:)`.
    pub fn selected_icon<K>(
        mut self,
        selected_icon: impl IntoWidget<K>,
    ) -> SegmentedButtonThemeData {
        self.selected_icon = Some(selected_icon.into_widget());
        self
    }

    /// Creates a copy of this object with the given fields replaced with the
    /// new values.
    ///
    /// Dart's named arguments are the fluent setters on the copy
    /// (`data.copy_with().style(style)`).
    pub fn copy_with(&self) -> SegmentedButtonThemeData {
        self.clone()
    }

    /// Linearly interpolates between two segmented button themes.
    pub fn lerp(
        a: Option<&SegmentedButtonThemeData>,
        b: Option<&SegmentedButtonThemeData>,
        t: f64,
    ) -> SegmentedButtonThemeData {
        if let (Some(a), Some(b)) = (a, b)
            && std::ptr::eq(a, b)
        {
            return a.clone();
        }
        SegmentedButtonThemeData {
            style: ButtonStyle::lerp(
                a.and_then(|a| a.style.as_ref()),
                b.and_then(|b| b.style.as_ref()),
                t,
            ),
            selected_icon: if t < 0.5 {
                a.and_then(|a| a.selected_icon.clone())
            } else {
                b.and_then(|b| b.selected_icon.clone())
            },
        }
    }
}

/// A widget is equal to the widget it is: `selectedIcon` compares by identity.
impl PartialEq for SegmentedButtonThemeData {
    fn eq(&self, other: &SegmentedButtonThemeData) -> bool {
        self.style == other.style
            && match (&self.selected_icon, &other.selected_icon) {
                (Some(a), Some(b)) => Rc::ptr_eq(a, b),
                (None, None) => true,
                _ => false,
            }
    }
}

/// An inherited widget that defines the visual properties for
/// `SegmentedButton`s in this widget's subtree.
///
/// Values specified here are used for `SegmentedButton` properties that are not
/// given an explicit non-null value.
#[derive(Debug)]
pub struct SegmentedButtonTheme {
    pub key: Option<KeyRef>,
    /// Specifies the visual properties used by descendant `SegmentedButton`
    /// widgets.
    pub data: SegmentedButtonThemeData,
    pub child: WidgetRef,
}

impl SegmentedButtonTheme {
    /// Creates a [`SegmentedButtonTheme`] that controls visual parameters for
    /// descendant `SegmentedButton`s.
    pub fn new<K>(
        data: SegmentedButtonThemeData,
        child: impl IntoWidget<K>,
    ) -> SegmentedButtonTheme {
        SegmentedButtonTheme {
            key: None,
            data,
            child: child.into_widget(),
        }
    }

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

    /// The [`data`](Self::data) from the closest instance of this class that encloses the given
    /// context.
    ///
    /// If there is no [`SegmentedButtonTheme`] in scope, this will return
    /// `ThemeData::segmented_button_theme` from the ambient `Theme`.
    ///
    /// Typical usage is as follows:
    ///
    /// ```text
    /// let theme = SegmentedButtonTheme::of(app, context);
    /// ```
    ///
    /// See also:
    ///
    ///  * [`maybe_of`](Self::maybe_of), which returns null if it doesn't find a
    ///    [`SegmentedButtonTheme`] ancestor.
    pub fn of(app: &mut App, context: BuildContext) -> SegmentedButtonThemeData {
        match SegmentedButtonTheme::maybe_of(app, context) {
            Some(data) => data,
            None => Theme::of(app, context).segmented_button_theme.clone(),
        }
    }

    /// The data from the closest instance of this class that encloses the given
    /// context, if any.
    ///
    /// Use this function if you want to allow situations where no
    /// [`SegmentedButtonTheme`] is in scope. Prefer using [`SegmentedButtonTheme::of`]
    /// in situations where a [`SegmentedButtonThemeData`] is expected to be
    /// non-null.
    ///
    /// If there is no [`SegmentedButtonTheme`] in scope, then this function will
    /// return null.
    ///
    /// Typical usage is as follows:
    ///
    /// ```text
    /// if let Some(theme) = SegmentedButtonTheme::maybe_of(app, context) {
    ///     // ...
    /// }
    /// ```
    ///
    /// See also:
    ///
    ///  * [`of`](Self::of), which will return `ThemeData::segmented_button_theme` if it doesn't
    ///    find a [`SegmentedButtonTheme`] ancestor, instead of returning null.
    pub fn maybe_of(app: &mut App, context: BuildContext) -> Option<SegmentedButtonThemeData> {
        context
            .depend_on_inherited_widget_of_exact_type::<SegmentedButtonTheme>(app)
            .map(|theme| theme.data.clone())
    }
}

impl InheritedTheme for SegmentedButtonTheme {
    fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
        SegmentedButtonTheme::new(self.data.clone(), child).into_widget()
    }
}

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

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

    fn update_should_notify(&self, old_widget: &SegmentedButtonTheme) -> bool {
        self.data != old_widget.data
    }

    fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
        Some(self)
    }
}

#[cfg(test)]
#[allow(deprecated, reason = "Dart's tests call `withOpacity`")]
mod tests {
    //! Ported from `test/segmented_button_theme_test.dart`; the `debugFillProperties` case waits
    //! for diagnostics, and `hashCode` for `Hash`.

    use std::time::Duration;

    use waft_embedder::{Color, FontWeight};
    use waft_painting::{RoundedRectangleBorder, TextStyle};
    use waft_services::{LogicalKeyboardKey, PhysicalKeyboardKey};
    use waft_widgets::{
        Center, Icon, IconData, WidgetState, WidgetStateProperty, WidgetStatePropertyRef,
    };

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

    use super::*;
    use crate::colors::Colors;
    use crate::icons::Icons;
    use crate::material::Material;
    use crate::scaffold::Scaffold;
    use crate::segmented_button::tests::{
        argb, ink_rect_colors, label, mouse_at, numbered, text_center,
    };
    use crate::segmented_button::{ButtonSegment, SegmentedButton};
    use crate::test_support::material_app;
    use crate::theme_data::ThemeData;

    #[test]
    fn segmented_button_theme_data_copy_with_and_equality_basics() {
        assert_eq!(
            SegmentedButtonThemeData::new(),
            SegmentedButtonThemeData::new().copy_with()
        );

        let custom = SegmentedButtonThemeData::new()
            .style(ButtonStyle::new().background_color(Rc::new(
                waft_widgets::WidgetStatePropertyAll::new(Some(Colors::GREEN.primary())),
            )))
            .selected_icon(Icon::new(Some(Icons::error())));
        let copy = SegmentedButtonThemeData::new().copy_with();
        let mut copy = copy;
        copy.style = custom.style.clone();
        copy.selected_icon = custom.selected_icon.clone();
        assert_eq!(copy, custom);
    }

    #[test]
    fn segmented_button_theme_data_lerp_special_cases() {
        assert_eq!(
            SegmentedButtonThemeData::lerp(None, None, 0.0),
            SegmentedButtonThemeData::new()
        );
        let theme = SegmentedButtonThemeData::new();
        assert_eq!(
            SegmentedButtonThemeData::lerp(Some(&theme), Some(&theme), 0.5),
            theme
        );
    }

    /// What Dart's `ButtonStyle(backgroundColor: ..resolveWith, foregroundColor: ..resolveWith)`
    /// of these tests gives: the colors for the disabled, the selected and (the foreground only)
    /// every other state.
    fn colored_style(
        disabled: (Color, Color),
        selected: (Color, Color),
        other_foreground: Color,
    ) -> ButtonStyle {
        let background: WidgetStatePropertyRef<Option<Color>> =
            <dyn WidgetStateProperty<_>>::resolve_with(move |states| {
                if states.contains(&WidgetState::Disabled) {
                    return Some(disabled.0);
                }
                if states.contains(&WidgetState::Selected) {
                    return Some(selected.0);
                }
                None
            });
        let foreground: WidgetStatePropertyRef<Option<Color>> =
            <dyn WidgetStateProperty<_>>::resolve_with(move |states| {
                if states.contains(&WidgetState::Disabled) {
                    return Some(disabled.1);
                }
                if states.contains(&WidgetState::Selected) {
                    return Some(selected.1);
                }
                Some(other_foreground)
            });
        ButtonStyle::new()
            .background_color(background)
            .foreground_color(foreground)
    }

    fn three_segments() -> Vec<ButtonSegment<i32>> {
        let mut segments = numbered(&[1, 2, 3]);
        segments[2].enabled = false;
        segments
    }

    /// What a segment's `Material` and selected icon were expected to be.
    struct Expected {
        animation_duration: bool,
        color: Color,
        text_color: Color,
        selected_icon: Option<IconData>,
    }

    fn check_segment(tester: &WidgetTester, text: &str, expected: Expected) {
        let parent =
            tester.element(find::ancestor(find::text(text), find::by_type::<Material>()).first());
        let icons = find::descendant(
            find::ancestor(find::text(text), find::by_type::<Material>()).first(),
            find::by_type::<Icon>(),
        )
        .evaluate(tester)
        .into_iter()
        .filter(|icon| {
            tester.widget(*icon, |icon: &Icon| {
                expected.selected_icon.is_some() && icon.icon == expected.selected_icon
            })
        })
        .count();
        tester.widget(parent, |material: &Material| {
            if expected.animation_duration {
                assert_eq!(material.animation_duration, Duration::from_millis(200));
                assert_eq!(material.border_radius, None);
            }
            assert_eq!(material.color, Some(expected.color));
            assert_eq!(
                material.shape,
                Some(waft_painting::ShapeBorderRef::new(
                    RoundedRectangleBorder::default()
                ))
            );
            let text_style: &TextStyle = material.text_style.as_ref().expect("a text style");
            assert_eq!(
                text_style.color.as_deref().copied(),
                Some(expected.text_color)
            );
            assert_eq!(text_style.font_family.as_deref(), Some("Roboto"));
            assert_eq!(text_style.font_size, Some(14.0));
            assert_eq!(text_style.font_weight, Some(FontWeight::W500));
        });
        assert_eq!(icons, usize::from(expected.selected_icon.is_some()));
    }

    fn pump_three(
        tester: &mut WidgetTester,
        theme: ThemeData,
        home: impl FnOnce(WidgetRef) -> WidgetRef,
        button: SegmentedButton<i32>,
    ) {
        let home = home(
            Scaffold::new()
                .body(Center::new().child(button))
                .into_widget(),
        );
        tester.pump_widget(material_app(theme, home));
    }

    // 'With no other configuration, defaults are used'
    #[test]
    fn with_no_other_configuration_the_defaults_are_used() {
        test_widgets(|tester| {
            let theme = ThemeData::new(&tester.cell.borrow());
            let colors = theme.color_scheme.clone();
            pump_three(
                tester,
                theme,
                |home| home,
                SegmentedButton::new(three_segments(), vec![2])
                    .on_selection_changed(|_app, _selected| {}),
            );

            // Test first segment, should be enabled
            check_segment(
                tester,
                "1",
                Expected {
                    animation_duration: false,
                    color: Colors::TRANSPARENT,
                    text_color: colors.on_surface(),
                    selected_icon: None,
                },
            );
            // Test second segment, should be enabled and selected
            check_segment(
                tester,
                "2",
                Expected {
                    animation_duration: false,
                    color: colors.secondary_container(),
                    text_color: colors.on_secondary_container(),
                    selected_icon: Some(Icons::check()),
                },
            );
            // Test last segment, should be disabled
            check_segment(
                tester,
                "3",
                Expected {
                    animation_duration: false,
                    color: Colors::TRANSPARENT,
                    text_color: colors.on_surface().with_opacity(0.38),
                    selected_icon: None,
                },
            );
        });
    }

    fn global_theme_data() -> SegmentedButtonThemeData {
        SegmentedButtonThemeData::new()
            .style(colored_style(
                (Colors::BLUE.primary(), Colors::YELLOW.primary()),
                (Colors::PURPLE.primary(), Colors::BROWN.primary()),
                Colors::CYAN.primary(),
            ))
            .selected_icon(Icon::new(Some(Icons::error())))
    }

    fn segmented_theme_data() -> SegmentedButtonThemeData {
        SegmentedButtonThemeData::new()
            .style(colored_style(
                (Colors::LIGHT_BLUE.primary(), Colors::LIME.primary()),
                (Colors::LIGHT_GREEN.primary(), Colors::AMBER.primary()),
                Colors::DEEP_PURPLE.primary(),
            ))
            .selected_icon(Icon::new(Some(Icons::plus_one())))
    }

    // 'ThemeData.segmentedButtonTheme overrides defaults'
    #[test]
    fn theme_data_segmented_button_theme_overrides_the_defaults() {
        test_widgets(|tester| {
            let theme = ThemeData::builder()
                .segmented_button_theme(global_theme_data())
                .build(&tester.cell.borrow());
            pump_three(
                tester,
                theme,
                |home| home,
                SegmentedButton::new(three_segments(), vec![2])
                    .on_selection_changed(|_app, _selected| {}),
            );

            check_segment(
                tester,
                "1",
                Expected {
                    animation_duration: false,
                    color: Colors::TRANSPARENT,
                    text_color: Colors::CYAN.primary(),
                    selected_icon: None,
                },
            );
            check_segment(
                tester,
                "2",
                Expected {
                    animation_duration: false,
                    color: Colors::PURPLE.primary(),
                    text_color: Colors::BROWN.primary(),
                    selected_icon: Some(Icons::error()),
                },
            );
            check_segment(
                tester,
                "3",
                Expected {
                    animation_duration: false,
                    color: Colors::BLUE.primary(),
                    text_color: Colors::YELLOW.primary(),
                    selected_icon: None,
                },
            );
        });
    }

    // 'SegmentedButtonTheme overrides ThemeData and defaults'
    #[test]
    fn segmented_button_theme_overrides_theme_data_and_the_defaults() {
        test_widgets(|tester| {
            let theme = ThemeData::builder()
                .segmented_button_theme(global_theme_data())
                .build(&tester.cell.borrow());
            pump_three(
                tester,
                theme,
                |home| SegmentedButtonTheme::new(segmented_theme_data(), home).into_widget(),
                SegmentedButton::new(three_segments(), vec![2])
                    .on_selection_changed(|_app, _selected| {}),
            );

            check_segment(
                tester,
                "1",
                Expected {
                    animation_duration: true,
                    color: Colors::TRANSPARENT,
                    text_color: Colors::DEEP_PURPLE.primary(),
                    selected_icon: None,
                },
            );
            check_segment(
                tester,
                "2",
                Expected {
                    animation_duration: true,
                    color: Colors::LIGHT_GREEN.primary(),
                    text_color: Colors::AMBER.primary(),
                    selected_icon: Some(Icons::plus_one()),
                },
            );
            check_segment(
                tester,
                "3",
                Expected {
                    animation_duration: true,
                    color: Colors::LIGHT_BLUE.primary(),
                    text_color: Colors::LIME.primary(),
                    selected_icon: None,
                },
            );
        });
    }

    // 'Widget parameters overrides SegmentedTheme, ThemeData and defaults'
    #[test]
    fn widget_parameters_override_the_segmented_theme_theme_data_and_the_defaults() {
        test_widgets(|tester| {
            let theme = ThemeData::builder()
                .segmented_button_theme(global_theme_data())
                .build(&tester.cell.borrow());
            pump_three(
                tester,
                theme,
                |home| SegmentedButtonTheme::new(segmented_theme_data(), home).into_widget(),
                SegmentedButton::new(three_segments(), vec![2])
                    .on_selection_changed(|_app, _selected| {})
                    .style(colored_style(
                        (Colors::BLACK12, Colors::AMBER_ACCENT.primary()),
                        (Colors::GREY.primary(), Colors::DEEP_ORANGE.primary()),
                        Colors::DEEP_PURPLE_ACCENT.primary(),
                    ))
                    .selected_icon(Icon::new(Some(Icons::alarm()))),
            );

            check_segment(
                tester,
                "1",
                Expected {
                    animation_duration: true,
                    color: Colors::TRANSPARENT,
                    text_color: Colors::DEEP_PURPLE_ACCENT.primary(),
                    selected_icon: None,
                },
            );
            check_segment(
                tester,
                "2",
                Expected {
                    animation_duration: true,
                    color: Colors::GREY.primary(),
                    text_color: Colors::DEEP_ORANGE.primary(),
                    selected_icon: Some(Icons::alarm()),
                },
            );
            check_segment(
                tester,
                "3",
                Expected {
                    animation_duration: true,
                    color: Colors::BLACK12,
                    text_color: Colors::AMBER_ACCENT.primary(),
                    selected_icon: None,
                },
            );
        });
    }

    // 'SegmentedButtonTheme SegmentedButton.styleFrom overlayColor overrides default overlay
    // color'
    #[test]
    fn the_theme_style_from_overlay_color_overrides_the_default_overlay_color() {
        test_widgets(|tester| {
            let overlay_color = Color::new(0xffff0000);
            let theme = ThemeData::builder()
                .segmented_button_theme(
                    SegmentedButtonThemeData::new().style(
                        SegmentedButton::<i32>::style_from()
                            .overlay_color(overlay_color)
                            .build(),
                    ),
                )
                .build(&tester.cell.borrow());
            let segments = vec![
                ButtonSegment::new(0).label(label("Option 1")),
                ButtonSegment::new(1).label(label("Option 2")),
            ];
            pump_three(
                tester,
                theme,
                |home| home,
                SegmentedButton::new(segments, vec![1]).on_selection_changed(|_app, _selected| {}),
            );
            let hover = argb(overlay_color.with_opacity(0.08));
            let press = argb(overlay_color.with_opacity(0.1));

            // Hovered selected segment,
            let center = text_center(tester, "Option 1");
            let mut gesture = mouse_at(tester, center);
            tester.pump_and_settle();
            assert!(ink_rect_colors(tester).contains(&hover));

            // Hovered unselected segment,
            let center = text_center(tester, "Option 2");
            tester.move_to(&mut gesture, center, false);
            tester.pump_and_settle();
            assert!(ink_rect_colors(tester).contains(&hover));

            // Highlighted unselected segment (pressed).
            let center = text_center(tester, "Option 1");
            tester.down(&mut gesture, center);
            tester.pump_and_settle();
            let colors = ink_rect_colors(tester);
            assert!(colors.contains(&hover) && colors.contains(&press));
            // Remove pressed and hovered states,
            tester.up(&gesture);
            tester.pump_and_settle();
            tester.move_to(&mut gesture, waft_embedder::Offset::new(0.0, 50.0), false);
            tester.pump_and_settle();

            // Highlighted selected segment (pressed)
            let center = text_center(tester, "Option 2");
            tester.down(&mut gesture, center);
            tester.pump_and_settle();
            let colors = ink_rect_colors(tester);
            assert!(colors.contains(&hover) && colors.contains(&press));
            // Remove pressed and hovered states,
            tester.up(&gesture);
            tester.pump_and_settle();
            tester.move_to(&mut gesture, waft_embedder::Offset::new(0.0, 50.0), false);
            tester.pump_and_settle();

            // Focused unselected segment.
            tester.send_key_event(PhysicalKeyboardKey::TAB, LogicalKeyboardKey::TAB);
            tester.pump_and_settle();
            assert!(ink_rect_colors(tester).contains(&press));

            // Focused selected segment.
            tester.send_key_event(PhysicalKeyboardKey::TAB, LogicalKeyboardKey::TAB);
            tester.pump_and_settle();
            assert!(ink_rect_colors(tester).contains(&press));
        });
    }
}