1use waft_embedder::{Color, Radius, lerp_double};
4use waft_foundation::{App, DiagnosticsProperty, DoubleProperty};
5use waft_widgets::{
6 BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef,
7 WidgetStateProperty, WidgetStatePropertyRef,
8};
9
10use crate::theme::Theme;
11
12#[derive(Clone, Debug, Default)]
30pub struct ScrollbarThemeData {
31 pub thumb_visibility: Option<WidgetStatePropertyRef<Option<bool>>>,
34
35 pub thickness: Option<WidgetStatePropertyRef<Option<f64>>>,
41
42 pub track_visibility: Option<WidgetStatePropertyRef<Option<bool>>>,
45
46 pub interactive: Option<bool>,
49
50 pub radius: Option<Radius>,
53
54 pub thumb_color: Option<WidgetStatePropertyRef<Option<Color>>>,
61
62 pub track_color: Option<WidgetStatePropertyRef<Option<Color>>>,
69
70 pub track_border_color: Option<WidgetStatePropertyRef<Option<Color>>>,
77
78 pub cross_axis_margin: Option<f64>,
86
87 pub main_axis_margin: Option<f64>,
95
96 pub min_thumb_length: Option<f64>,
106}
107
108impl ScrollbarThemeData {
109 pub fn new() -> ScrollbarThemeData {
112 ScrollbarThemeData::default()
113 }
114
115 pub fn thumb_visibility(
117 mut self,
118 thumb_visibility: WidgetStatePropertyRef<Option<bool>>,
119 ) -> ScrollbarThemeData {
120 self.thumb_visibility = Some(thumb_visibility);
121 self
122 }
123
124 pub fn thickness(
126 mut self,
127 thickness: WidgetStatePropertyRef<Option<f64>>,
128 ) -> ScrollbarThemeData {
129 self.thickness = Some(thickness);
130 self
131 }
132
133 pub fn track_visibility(
135 mut self,
136 track_visibility: WidgetStatePropertyRef<Option<bool>>,
137 ) -> ScrollbarThemeData {
138 self.track_visibility = Some(track_visibility);
139 self
140 }
141
142 pub fn radius(mut self, radius: Radius) -> ScrollbarThemeData {
144 self.radius = Some(radius);
145 self
146 }
147
148 pub fn thumb_color(
150 mut self,
151 thumb_color: WidgetStatePropertyRef<Option<Color>>,
152 ) -> ScrollbarThemeData {
153 self.thumb_color = Some(thumb_color);
154 self
155 }
156
157 pub fn track_color(
159 mut self,
160 track_color: WidgetStatePropertyRef<Option<Color>>,
161 ) -> ScrollbarThemeData {
162 self.track_color = Some(track_color);
163 self
164 }
165
166 pub fn track_border_color(
168 mut self,
169 track_border_color: WidgetStatePropertyRef<Option<Color>>,
170 ) -> ScrollbarThemeData {
171 self.track_border_color = Some(track_border_color);
172 self
173 }
174
175 pub fn cross_axis_margin(mut self, cross_axis_margin: f64) -> ScrollbarThemeData {
177 self.cross_axis_margin = Some(cross_axis_margin);
178 self
179 }
180
181 pub fn main_axis_margin(mut self, main_axis_margin: f64) -> ScrollbarThemeData {
183 self.main_axis_margin = Some(main_axis_margin);
184 self
185 }
186
187 pub fn min_thumb_length(mut self, min_thumb_length: f64) -> ScrollbarThemeData {
189 self.min_thumb_length = Some(min_thumb_length);
190 self
191 }
192
193 pub fn interactive(mut self, interactive: bool) -> ScrollbarThemeData {
195 self.interactive = Some(interactive);
196 self
197 }
198
199 pub fn copy_with(&self) -> ScrollbarThemeData {
205 self.clone()
206 }
207
208 pub fn lerp(
212 a: Option<&ScrollbarThemeData>,
213 b: Option<&ScrollbarThemeData>,
214 t: f64,
215 ) -> ScrollbarThemeData {
216 if let (Some(a), Some(b)) = (a, b)
217 && std::ptr::eq(a, b)
218 {
219 return a.clone();
220 }
221 ScrollbarThemeData {
222 thumb_visibility: <dyn WidgetStateProperty<Option<bool>>>::lerp_optional(
223 a.and_then(|a| a.thumb_visibility.clone()),
224 b.and_then(|b| b.thumb_visibility.clone()),
225 t,
226 lerp_bool,
227 ),
228 thickness: <dyn WidgetStateProperty<Option<f64>>>::lerp_optional(
229 a.and_then(|a| a.thickness.clone()),
230 b.and_then(|b| b.thickness.clone()),
231 t,
232 lerp_double,
233 ),
234 track_visibility: <dyn WidgetStateProperty<Option<bool>>>::lerp_optional(
235 a.and_then(|a| a.track_visibility.clone()),
236 b.and_then(|b| b.track_visibility.clone()),
237 t,
238 lerp_bool,
239 ),
240 interactive: lerp_bool(
241 a.and_then(|a| a.interactive),
242 b.and_then(|b| b.interactive),
243 t,
244 ),
245 radius: Radius::lerp(a.and_then(|a| a.radius), b.and_then(|b| b.radius), t),
246 thumb_color: <dyn WidgetStateProperty<Option<Color>>>::lerp_optional(
247 a.and_then(|a| a.thumb_color.clone()),
248 b.and_then(|b| b.thumb_color.clone()),
249 t,
250 Color::lerp,
251 ),
252 track_color: <dyn WidgetStateProperty<Option<Color>>>::lerp_optional(
253 a.and_then(|a| a.track_color.clone()),
254 b.and_then(|b| b.track_color.clone()),
255 t,
256 Color::lerp,
257 ),
258 track_border_color: <dyn WidgetStateProperty<Option<Color>>>::lerp_optional(
259 a.and_then(|a| a.track_border_color.clone()),
260 b.and_then(|b| b.track_border_color.clone()),
261 t,
262 Color::lerp,
263 ),
264 cross_axis_margin: lerp_double(
265 a.and_then(|a| a.cross_axis_margin),
266 b.and_then(|b| b.cross_axis_margin),
267 t,
268 ),
269 main_axis_margin: lerp_double(
270 a.and_then(|a| a.main_axis_margin),
271 b.and_then(|b| b.main_axis_margin),
272 t,
273 ),
274 min_thumb_length: lerp_double(
275 a.and_then(|a| a.min_thumb_length),
276 b.and_then(|b| b.min_thumb_length),
277 t,
278 ),
279 }
280 }
281}
282
283impl PartialEq for ScrollbarThemeData {
284 fn eq(&self, other: &ScrollbarThemeData) -> bool {
285 if std::ptr::eq(self, other) {
286 return true;
287 }
288 other.thumb_visibility == self.thumb_visibility
289 && other.thickness == self.thickness
290 && other.track_visibility == self.track_visibility
291 && other.interactive == self.interactive
292 && other.radius == self.radius
293 && other.thumb_color == self.thumb_color
294 && other.track_color == self.track_color
295 && other.track_border_color == self.track_border_color
296 && other.cross_axis_margin == self.cross_axis_margin
297 && other.main_axis_margin == self.main_axis_margin
298 && other.min_thumb_length == self.min_thumb_length
299 }
300}
301
302fn lerp_bool(a: Option<bool>, b: Option<bool>, t: f64) -> Option<bool> {
303 if t < 0.5 { a } else { b }
304}
305
306#[derive(Debug)]
320pub struct ScrollbarTheme {
321 pub key: Option<KeyRef>,
322 pub data: ScrollbarThemeData,
324 pub child: WidgetRef,
325}
326
327impl ScrollbarTheme {
328 #[cfg_attr(debug_assertions, track_caller)]
331 pub fn new<K>(data: ScrollbarThemeData, child: impl IntoWidget<K>) -> ScrollbarTheme {
332 ScrollbarTheme {
333 key: None,
334 data,
335 child: child.into_widget(),
336 }
337 }
338
339 pub fn key(mut self, key: KeyRef) -> ScrollbarTheme {
341 self.key = Some(key);
342 self
343 }
344
345 pub fn of(app: &mut App, context: BuildContext) -> ScrollbarThemeData {
354 match context.depend_on_inherited_widget_of_exact_type::<ScrollbarTheme>(app) {
355 Some(scrollbar_theme) => scrollbar_theme.data.clone(),
356 None => Theme::of(app, context).scrollbar_theme.clone(),
357 }
358 }
359}
360
361impl InheritedTheme for ScrollbarTheme {
362 fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
363 ScrollbarTheme::new(self.data.clone(), child).into_widget()
364 }
365}
366
367impl InheritedWidget for ScrollbarTheme {
368 fn key(&self) -> Option<&KeyRef> {
369 self.key.as_ref()
370 }
371
372 fn child(&self) -> &WidgetRef {
373 &self.child
374 }
375
376 fn update_should_notify(&self, old_widget: &ScrollbarTheme) -> bool {
377 self.data != old_widget.data
378 }
379
380 fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
381 Some(self)
382 }
383}
384
385impl waft_foundation::Diagnosticable for ScrollbarThemeData {
386 fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
387 properties.add(
388 DiagnosticsProperty::<String>::new(
389 "thumbVisibility",
390 self.thumb_visibility
391 .as_deref()
392 .map(|property| property.to_string()),
393 )
394 .default_value(None),
395 );
396 properties.add(
397 DiagnosticsProperty::<String>::new(
398 "thickness",
399 self.thickness
400 .as_deref()
401 .map(|property| property.to_string()),
402 )
403 .default_value(None),
404 );
405 properties.add(
406 DiagnosticsProperty::<String>::new(
407 "trackVisibility",
408 self.track_visibility
409 .as_deref()
410 .map(|property| property.to_string()),
411 )
412 .default_value(None),
413 );
414 properties.add(
415 DiagnosticsProperty::<bool>::new("interactive", self.interactive).default_value(None),
416 );
417 properties.add(
418 DiagnosticsProperty::<waft_embedder::Radius>::new("radius", self.radius)
419 .default_value(None),
420 );
421 properties.add(
422 DiagnosticsProperty::<String>::new(
423 "thumbColor",
424 self.thumb_color
425 .as_deref()
426 .map(|property| property.to_string()),
427 )
428 .default_value(None),
429 );
430 properties.add(
431 DiagnosticsProperty::<String>::new(
432 "trackColor",
433 self.track_color
434 .as_deref()
435 .map(|property| property.to_string()),
436 )
437 .default_value(None),
438 );
439 properties.add(
440 DiagnosticsProperty::<String>::new(
441 "trackBorderColor",
442 self.track_border_color
443 .as_deref()
444 .map(|property| property.to_string()),
445 )
446 .default_value(None),
447 );
448 properties.add(
449 DoubleProperty::new("crossAxisMargin", self.cross_axis_margin).default_value(None),
450 );
451 properties
452 .add(DoubleProperty::new("mainAxisMargin", self.main_axis_margin).default_value(None));
453 properties
454 .add(DoubleProperty::new("minThumbLength", self.min_thumb_length).default_value(None));
455 }
456}
457
458impl std::fmt::Display for ScrollbarThemeData {
459 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
460 f.write_str(&waft_foundation::Diagnosticable::to_string(self))
461 }
462}
463
464#[cfg(test)]
465mod tests {
466 use std::rc::Rc;
467
468 use waft_embedder::{Offset, Rect, TargetPlatform};
469 use waft_widgets::{ScrollController, WidgetState, WidgetStates};
470
471 use waft_widgets::testing::{
472 TargetPlatformVariant, WidgetTester, test_widgets, test_widgets_variant,
473 };
474
475 use super::*;
476 use crate::color_scheme::ColorScheme;
477 use crate::scrollbar::Scrollbar;
478 use crate::scrollbar::tests::{
479 DESKTOP, K_DEFAULT_IDLE_THUMB_COLOR, K_DEFAULT_THUMB_RADIUS, assert_thumb_rrect,
480 assert_track, hover_at, move_by, no_scrollbar, painted, scroll_view, start_gesture,
481 };
482 use crate::test_support::material_app;
483 use crate::theme_data::ThemeData;
484 use waft_rendering::testing::{PaintCall, draws, paints_exactly};
485
486 const K_MATERIAL_DESIGN_INITIAL_THUMB_RECT: Rect = Rect::from_ltrb(790.0, 0.0, 798.0, 90.0);
490 const K_DEFAULT_DRAG_THUMB_COLOR: u32 = 0x99000000;
491
492 const RED: Color = Color::new(0xfff44336);
495 const BLUE: Color = Color::new(0xff2196f3);
496 const GREEN: Color = Color::new(0xff4caf50);
497 const YELLOW: Color = Color::new(0xffffeb3b);
498 const BLACK: Color = Color::new(0xff000000);
499
500 const DESKTOP_AND_FUCHSIA: [TargetPlatform; 4] =
501 [DESKTOP[0], DESKTOP[1], DESKTOP[2], TargetPlatform::Fuchsia];
502
503 #[test]
504 fn scrollbar_theme_data_copy_with_equality_basics() {
505 assert_eq!(
506 ScrollbarThemeData::new(),
507 ScrollbarThemeData::new().copy_with()
508 );
509 }
510
511 #[test]
512 fn scrollbar_theme_data_lerp_special_cases() {
513 assert_eq!(
514 ScrollbarThemeData::lerp(None, None, 0.0),
515 ScrollbarThemeData::new()
516 );
517 let data = ScrollbarThemeData::new()
518 .thickness(<dyn WidgetStateProperty<Option<f64>>>::all(Some(3.0)));
519 let lerped = ScrollbarThemeData::lerp(Some(&data), Some(&data), 0.5);
521 assert!(Rc::ptr_eq(
522 lerped.thickness.as_ref().expect("lerped"),
523 data.thickness.as_ref().expect("given")
524 ));
525 }
526
527 fn pump_frame(
531 tester: &mut WidgetTester,
532 theme: ThemeData,
533 scrollbar: impl FnOnce(Scrollbar) -> Scrollbar,
534 wrap: impl FnOnce(WidgetRef) -> WidgetRef,
535 ) {
536 let controller = ScrollController::default(&mut tester.cell.borrow_mut())
537 .into_erased::<waft_widgets::AnyScrollController>();
538 let scrollbar = scrollbar(
539 Scrollbar::new(scroll_view(Some(controller.clone()), 4000.0, 4000.0))
540 .thumb_visibility(true)
541 .controller(controller.clone()),
542 );
543 tester.pump_widget(material_app(
544 theme,
545 no_scrollbar(wrap(scrollbar.into_widget())),
546 ));
547 }
548
549 fn hovered_track_visibility() -> WidgetStatePropertyRef<Option<bool>> {
550 <dyn WidgetStateProperty<Option<bool>>>::resolve_with(|states: &WidgetStates| {
551 Some(states.contains(&WidgetState::Hovered))
552 })
553 }
554
555 fn drag_thumb(tester: &mut WidgetTester, rect: Rect, radius: Radius, drag_color: u32) {
558 const SCROLL_AMOUNT: f64 = 10.0;
559 let mut drag_scrollbar_gesture = start_gesture(tester, Offset::new(797.0, 45.0));
560 tester.pump_and_settle();
561
562 assert_thumb_rrect(tester, rect, radius, drag_color);
564
565 move_by(
566 tester,
567 &mut drag_scrollbar_gesture,
568 Offset::new(0.0, SCROLL_AMOUNT),
569 );
570 tester.pump_and_settle();
571 tester.up(&drag_scrollbar_gesture);
572 tester.pump_and_settle();
573 }
574
575 #[test]
576 fn passing_no_scrollbar_theme_returns_defaults() {
577 for platform in DESKTOP_AND_FUCHSIA {
578 test_widgets_variant(TargetPlatformVariant::only(platform), |tester| {
579 let theme = ThemeData::builder()
580 .use_material3(false)
581 .scrollbar_theme(
582 ScrollbarThemeData::new().track_visibility(hovered_track_visibility()),
583 )
584 .build(&tester.cell.borrow());
585 pump_frame(tester, theme, |scrollbar| scrollbar, |child| child);
586 tester.pump_and_settle();
587 assert_thumb_rrect(
589 tester,
590 K_MATERIAL_DESIGN_INITIAL_THUMB_RECT,
591 K_DEFAULT_THUMB_RADIUS,
592 K_DEFAULT_IDLE_THUMB_COLOR,
593 );
594
595 drag_thumb(
597 tester,
598 K_MATERIAL_DESIGN_INITIAL_THUMB_RECT,
599 K_DEFAULT_THUMB_RADIUS,
600 K_DEFAULT_DRAG_THUMB_COLOR,
601 );
602
603 hover_at(tester, Offset::new(794.0, 5.0));
605 tester.pump_and_settle();
606
607 let drawn = painted(tester);
608 assert_track(
609 tester,
610 &drawn,
611 Rect::from_ltrb(784.0, 0.0, 800.0, 600.0),
612 0x08000000,
613 (Offset::new(784.0, 0.0), Offset::new(784.0, 600.0)),
614 0x1a000000,
615 );
616 assert_thumb_rrect(
618 tester,
619 Rect::from_ltrb(786.0, 10.0, 798.0, 100.0),
620 K_DEFAULT_THUMB_RADIUS,
621 0x80000000,
622 );
623 });
624 }
625 }
626
627 #[test]
628 fn scrollbar_uses_values_from_scrollbar_theme() {
629 for platform in DESKTOP_AND_FUCHSIA {
630 test_widgets_variant(TargetPlatformVariant::only(platform), |tester| {
631 let theme = ThemeData::builder()
632 .scrollbar_theme(scrollbar_theme())
633 .build(&tester.cell.borrow());
634 pump_frame(tester, theme, |scrollbar| scrollbar, |child| child);
635 tester.pump_and_settle();
636 assert_thumb_rrect(
638 tester,
639 Rect::from_ltrb(785.0, 10.0, 795.0, 97.0),
640 Radius::circular(6.0),
641 GREEN.to_argb32(),
642 );
643
644 drag_thumb(
646 tester,
647 Rect::from_ltrb(785.0, 10.0, 795.0, 97.0),
648 Radius::circular(6.0),
649 RED.to_argb32(),
650 );
651
652 hover_at(tester, Offset::new(794.0, 15.0));
654 tester.pump_and_settle();
655
656 let drawn = painted(tester);
657 assert_track(
658 tester,
659 &drawn,
660 Rect::from_ltrb(770.0, 0.0, 800.0, 600.0),
661 BLACK.to_argb32(),
662 (Offset::new(770.0, 0.0), Offset::new(770.0, 600.0)),
663 YELLOW.to_argb32(),
664 );
665 assert_thumb_rrect(
667 tester,
668 Rect::from_ltrb(775.0, 20.0, 795.0, 107.0),
669 Radius::circular(6.0),
670 BLUE.to_argb32(),
671 );
672 });
673 }
674 }
675
676 #[test]
677 fn scrollbar_uses_values_from_scrollbar_theme_if_exists_instead_of_values_from_theme() {
678 test_widgets(|tester| {
679 let theme = ThemeData::builder()
680 .scrollbar_theme(scrollbar_theme())
681 .build(&tester.cell.borrow());
682 pump_frame(
683 tester,
684 theme,
685 |scrollbar| scrollbar,
686 |child| {
687 ScrollbarTheme::new(
688 scrollbar_theme()
689 .copy_with()
690 .thumb_color(<dyn WidgetStateProperty<Option<Color>>>::all(Some(
691 Color::new(0xFF000000),
692 ))),
693 child,
694 )
695 .into_widget()
696 },
697 );
698 tester.pump_and_settle();
699 assert_thumb_rrect(
701 tester,
702 Rect::from_ltrb(785.0, 10.0, 795.0, 97.0),
703 Radius::circular(6.0),
704 0xFF000000,
705 );
706 });
707 }
708
709 #[test]
710 fn scrollbar_theme_can_disable_gestures() {
711 test_widgets_variant(
712 TargetPlatformVariant::only(TargetPlatform::Fuchsia),
713 |tester| {
714 let theme = ThemeData::builder()
715 .use_material3(false)
716 .scrollbar_theme(ScrollbarThemeData::new().interactive(false))
717 .build(&tester.cell.borrow());
718 let controller = ScrollController::default(&mut tester.cell.borrow_mut())
719 .into_erased::<waft_widgets::AnyScrollController>();
720 tester.pump_widget(material_app(
721 theme,
722 Scrollbar::new(scroll_view(Some(controller.clone()), 4000.0, 4000.0))
723 .thumb_visibility(true)
724 .controller(controller.clone())
725 .into_widget(),
726 ));
727 tester.pump_and_settle();
728 assert_thumb_rrect(
730 tester,
731 K_MATERIAL_DESIGN_INITIAL_THUMB_RECT,
732 K_DEFAULT_THUMB_RADIUS,
733 K_DEFAULT_IDLE_THUMB_COLOR,
734 );
735
736 drag_thumb(
738 tester,
739 K_MATERIAL_DESIGN_INITIAL_THUMB_RECT,
740 K_DEFAULT_THUMB_RADIUS,
741 K_DEFAULT_IDLE_THUMB_COLOR,
742 );
743 assert_thumb_rrect(
745 tester,
746 K_MATERIAL_DESIGN_INITIAL_THUMB_RECT,
747 K_DEFAULT_THUMB_RADIUS,
748 K_DEFAULT_IDLE_THUMB_COLOR,
749 );
750 },
751 );
752 }
753
754 #[test]
755 fn scrollbar_interactive_takes_priority_over_scrollbar_theme() {
756 test_widgets_variant(
757 TargetPlatformVariant::only(TargetPlatform::Fuchsia),
758 |tester| {
759 let theme = ThemeData::builder()
760 .use_material3(false)
761 .scrollbar_theme(ScrollbarThemeData::new().interactive(false))
762 .build(&tester.cell.borrow());
763 let controller = ScrollController::default(&mut tester.cell.borrow_mut())
764 .into_erased::<waft_widgets::AnyScrollController>();
765 tester.pump_widget(material_app(
766 theme,
767 Scrollbar::new(scroll_view(Some(controller.clone()), 4000.0, 4000.0))
768 .interactive(true)
769 .thumb_visibility(true)
770 .controller(controller.clone())
771 .into_widget(),
772 ));
773 tester.pump_and_settle();
774 assert_thumb_rrect(
776 tester,
777 K_MATERIAL_DESIGN_INITIAL_THUMB_RECT,
778 K_DEFAULT_THUMB_RADIUS,
779 K_DEFAULT_IDLE_THUMB_COLOR,
780 );
781
782 drag_thumb(
784 tester,
785 K_MATERIAL_DESIGN_INITIAL_THUMB_RECT,
786 K_DEFAULT_THUMB_RADIUS,
787 K_DEFAULT_DRAG_THUMB_COLOR,
788 );
789 assert_thumb_rrect(
791 tester,
792 Rect::from_ltrb(790.0, 10.0, 798.0, 100.0),
793 K_DEFAULT_THUMB_RADIUS,
794 K_DEFAULT_IDLE_THUMB_COLOR,
795 );
796 },
797 );
798 }
799
800 #[test]
801 fn scrollbar_widget_properties_take_priority_over_theme() {
802 for platform in DESKTOP_AND_FUCHSIA {
803 test_widgets(|_| {
804 const THICKNESS: f64 = 4.0;
805 const EDGE_MARGIN: f64 = 2.0;
806 const RADIUS: Radius = Radius::circular(3.0);
807 let mut tester = WidgetTester::on(platform);
808 let tester = &mut tester;
809 let theme = ThemeData::builder()
810 .color_scheme(ColorScheme::light())
811 .scrollbar_theme(
812 ScrollbarThemeData::new().track_visibility(hovered_track_visibility()),
813 )
814 .build(&tester.cell.borrow());
815 pump_frame(
816 tester,
817 theme,
818 |scrollbar| scrollbar.thickness(THICKNESS).radius(RADIUS),
819 |child| child,
820 );
821
822 tester.pump_and_settle();
823 let thumb = Rect::from_ltrb(800.0 - THICKNESS - EDGE_MARGIN, 0.0, 798.0, 90.0);
825 assert_thumb_rrect(
826 tester,
827 thumb,
828 Radius::circular(3.0),
829 K_DEFAULT_IDLE_THUMB_COLOR,
830 );
831
832 drag_thumb(
834 tester,
835 thumb,
836 Radius::circular(3.0),
837 K_DEFAULT_DRAG_THUMB_COLOR,
838 );
839
840 hover_at(tester, Offset::new(794.0, 5.0));
842 tester.pump_and_settle();
843
844 let drawn = painted(tester);
845 assert_track(
846 tester,
847 &drawn,
848 Rect::from_ltrb(792.0, 0.0, 800.0, 600.0),
849 0x08000000,
850 (Offset::new(792.0, 0.0), Offset::new(792.0, 600.0)),
851 0x1a000000,
852 );
853 assert_thumb_rrect(
855 tester,
856 Rect::from_ltrb(800.0 - THICKNESS - EDGE_MARGIN, 10.0, 798.0, 100.0),
857 Radius::circular(3.0),
858 0x80000000,
859 );
860 });
861 }
862 }
863
864 #[test]
865 fn theme_data_color_scheme_is_used_when_no_scrollbar_theme_is_set() {
866 for platform in DESKTOP_AND_FUCHSIA {
867 test_widgets_variant(TargetPlatformVariant::only(platform), |tester| {
868 let build_frame = |tester: &mut WidgetTester, color_scheme: ColorScheme| {
869 let theme = ThemeData::builder()
870 .color_scheme(color_scheme)
871 .build(&tester.cell.borrow())
872 .copy_with()
873 .scrollbar_theme(
874 ScrollbarThemeData::new().track_visibility(hovered_track_visibility()),
875 )
876 .build();
877 pump_frame(tester, theme, |scrollbar| scrollbar, |child| child);
878 };
879
880 build_frame(tester, ColorScheme::light());
883 tester.pump_and_settle();
884 assert_thumb_rrect(
886 tester,
887 K_MATERIAL_DESIGN_INITIAL_THUMB_RECT,
888 K_DEFAULT_THUMB_RADIUS,
889 K_DEFAULT_IDLE_THUMB_COLOR,
890 );
891
892 drag_thumb(
894 tester,
895 K_MATERIAL_DESIGN_INITIAL_THUMB_RECT,
896 K_DEFAULT_THUMB_RADIUS,
897 K_DEFAULT_DRAG_THUMB_COLOR,
898 );
899
900 let mut hover_gesture = hover_at(tester, Offset::new(794.0, 5.0));
902 tester.pump_and_settle();
903
904 let drawn = painted(tester);
905 assert_track(
906 tester,
907 &drawn,
908 Rect::from_ltrb(784.0, 0.0, 800.0, 600.0),
909 0x08000000,
910 (Offset::new(784.0, 0.0), Offset::new(784.0, 600.0)),
911 0x1a000000,
912 );
913 assert_thumb_rrect(
915 tester,
916 Rect::from_ltrb(786.0, 10.0, 798.0, 100.0),
917 K_DEFAULT_THUMB_RADIUS,
918 0x80000000,
919 );
920
921 tester.move_to(&mut hover_gesture, Offset::ZERO, false);
922
923 build_frame(tester, ColorScheme::dark());
926 tester.pump_and_settle(); assert_thumb_rrect(
930 tester,
931 Rect::from_ltrb(790.0, 10.0, 798.0, 100.0),
932 K_DEFAULT_THUMB_RADIUS,
933 0x4dffffff,
934 );
935
936 drag_thumb(
938 tester,
939 Rect::from_ltrb(790.0, 10.0, 798.0, 100.0),
940 K_DEFAULT_THUMB_RADIUS,
941 0xbfffffff,
942 );
943
944 tester.move_to(&mut hover_gesture, Offset::new(794.0, 5.0), false);
946 tester.pump_and_settle();
947
948 let drawn = painted(tester);
949 assert_track(
950 tester,
951 &drawn,
952 Rect::from_ltrb(784.0, 0.0, 800.0, 600.0),
953 0x0dffffff,
954 (Offset::new(784.0, 0.0), Offset::new(784.0, 600.0)),
955 0x40ffffff,
956 );
957 assert_thumb_rrect(
959 tester,
960 Rect::from_ltrb(786.0, 20.0, 798.0, 110.0),
961 K_DEFAULT_THUMB_RADIUS,
962 0xa6ffffff,
963 );
964 });
965 }
966 }
967
968 #[test]
969 fn scrollbar_theme_data_track_visibility_test() {
970 for platform in DESKTOP_AND_FUCHSIA {
971 test_widgets_variant(TargetPlatformVariant::only(platform), |tester| {
972 let theme = ThemeData::builder()
973 .use_material3(false)
974 .build(&tester.cell.borrow())
975 .copy_with()
976 .scrollbar_theme(scrollbar_theme().track_visibility(<dyn WidgetStateProperty<
977 Option<bool>,
978 >>::resolve_with(
979 |_states: &WidgetStates| Some(true),
980 )))
981 .build();
982 pump_frame(tester, theme, |scrollbar| scrollbar, |child| child);
983 tester.pump_and_settle();
984
985 let drawn = painted(tester);
986 paints_exactly(
987 &draws(&drawn),
988 &[
989 PaintCall::rect(Some(Color::new(0x08000000))),
990 PaintCall::Line {
991 p1: None,
992 p2: None,
993 color: Some(Color::new(0x1a000000)),
994 stroke_width: None,
995 },
996 PaintCall::rrect(Some(GREEN)),
997 ],
998 )
999 .unwrap();
1000 });
1001 }
1002 }
1003
1004 fn scrollbar_theme() -> ScrollbarThemeData {
1006 ScrollbarThemeData::new()
1007 .thickness(<dyn WidgetStateProperty<Option<f64>>>::resolve_with(
1008 get_thickness,
1009 ))
1010 .track_visibility(hovered_track_visibility())
1011 .radius(Radius::circular(6.0))
1012 .thumb_color(<dyn WidgetStateProperty<Option<Color>>>::resolve_with(
1013 get_thumb_color,
1014 ))
1015 .track_color(<dyn WidgetStateProperty<Option<Color>>>::resolve_with(
1016 get_track_color,
1017 ))
1018 .track_border_color(<dyn WidgetStateProperty<Option<Color>>>::resolve_with(
1019 get_track_border_color,
1020 ))
1021 .cross_axis_margin(5.0)
1022 .main_axis_margin(10.0)
1023 .min_thumb_length(50.0)
1024 }
1025
1026 fn get_thickness(states: &WidgetStates) -> Option<f64> {
1027 if states.contains(&WidgetState::Hovered) {
1028 return Some(20.0);
1029 }
1030 Some(10.0)
1031 }
1032
1033 fn get_thumb_color(states: &WidgetStates) -> Option<Color> {
1034 if states.contains(&WidgetState::Dragged) {
1035 return Some(RED);
1036 }
1037 if states.contains(&WidgetState::Hovered) {
1038 return Some(BLUE);
1039 }
1040 Some(GREEN)
1041 }
1042
1043 fn get_track_color(states: &WidgetStates) -> Option<Color> {
1044 if states.contains(&WidgetState::Hovered) {
1045 return Some(BLACK);
1046 }
1047 None
1048 }
1049
1050 fn get_track_border_color(states: &WidgetStates) -> Option<Color> {
1051 if states.contains(&WidgetState::Hovered) {
1052 return Some(YELLOW);
1053 }
1054 None
1055 }
1056 #[test]
1058 fn a_scrollbar_theme_data_describes_nothing_by_default() {
1059 assert_eq!(
1060 waft_widgets::testing::describe_properties(|properties| {
1061 waft_foundation::Diagnosticable::debug_fill_properties(
1062 &ScrollbarThemeData::new(),
1063 properties,
1064 )
1065 }),
1066 Vec::<String>::new()
1067 );
1068 }
1069}