1use waft_embedder::{Color, lerp_double};
4use waft_foundation::{App, DiagnosticsProperty};
5use waft_painting::{AnyBorderSide, BorderSide, EdgeInsetsGeometry, OutlinedBorderRef, TextStyle};
6use waft_rendering::BoxConstraints;
7use waft_widgets::{
8 BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef,
9 WidgetStateBorderSide, WidgetStateProperty, WidgetStates,
10};
11
12use crate::theme::Theme;
13
14#[derive(Clone, Debug, Default, PartialEq)]
34pub struct SearchViewThemeData {
35 pub background_color: Option<Color>,
37
38 pub elevation: Option<f64>,
40
41 pub surface_tint_color: Option<Color>,
43
44 pub side: Option<AnyBorderSide>,
46
47 pub shape: Option<OutlinedBorderRef>,
49
50 pub header_height: Option<f64>,
52
53 pub header_text_style: Option<TextStyle>,
55
56 pub header_hint_style: Option<TextStyle>,
58
59 pub constraints: Option<BoxConstraints>,
61
62 pub padding: Option<EdgeInsetsGeometry>,
64
65 pub bar_padding: Option<EdgeInsetsGeometry>,
67
68 pub shrink_wrap: Option<bool>,
71
72 pub divider_color: Option<Color>,
74}
75
76crate::theme_data_getters::theme_data_getters! {
77 pub trait SearchViewThemeDataGetters for SearchViewThemeData, fields: search_view_theme_data {
81 background_color: Option<Color>,
82 elevation: Option<f64>,
83 surface_tint_color: Option<Color>,
84 side: Option<AnyBorderSide>,
85 shape: Option<OutlinedBorderRef>,
86 header_height: Option<f64>,
87 header_text_style: Option<TextStyle>,
88 header_hint_style: Option<TextStyle>,
89 constraints: Option<BoxConstraints>,
90 padding: Option<EdgeInsetsGeometry>,
91 bar_padding: Option<EdgeInsetsGeometry>,
92 shrink_wrap: Option<bool>,
93 divider_color: Option<Color>,
94 }
95}
96
97impl SearchViewThemeData {
98 pub fn new() -> SearchViewThemeData {
100 SearchViewThemeData::default()
101 }
102
103 pub fn background_color(mut self, background_color: Color) -> SearchViewThemeData {
105 self.background_color = Some(background_color);
106 self
107 }
108
109 pub fn elevation(mut self, elevation: f64) -> SearchViewThemeData {
111 self.elevation = Some(elevation);
112 self
113 }
114
115 pub fn surface_tint_color(mut self, surface_tint_color: Color) -> SearchViewThemeData {
117 self.surface_tint_color = Some(surface_tint_color);
118 self
119 }
120
121 pub fn side(mut self, side: impl Into<AnyBorderSide>) -> SearchViewThemeData {
123 self.side = Some(side.into());
124 self
125 }
126
127 pub fn shape(mut self, shape: impl Into<OutlinedBorderRef>) -> SearchViewThemeData {
129 self.shape = Some(shape.into());
130 self
131 }
132
133 pub fn header_height(mut self, header_height: f64) -> SearchViewThemeData {
135 self.header_height = Some(header_height);
136 self
137 }
138
139 pub fn header_text_style(mut self, header_text_style: TextStyle) -> SearchViewThemeData {
141 self.header_text_style = Some(header_text_style);
142 self
143 }
144
145 pub fn header_hint_style(mut self, header_hint_style: TextStyle) -> SearchViewThemeData {
147 self.header_hint_style = Some(header_hint_style);
148 self
149 }
150
151 pub fn constraints(mut self, constraints: BoxConstraints) -> SearchViewThemeData {
153 self.constraints = Some(constraints);
154 self
155 }
156
157 pub fn padding(mut self, padding: impl Into<EdgeInsetsGeometry>) -> SearchViewThemeData {
159 self.padding = Some(padding.into());
160 self
161 }
162
163 pub fn bar_padding(
165 mut self,
166 bar_padding: impl Into<EdgeInsetsGeometry>,
167 ) -> SearchViewThemeData {
168 self.bar_padding = Some(bar_padding.into());
169 self
170 }
171
172 pub fn shrink_wrap(mut self, shrink_wrap: bool) -> SearchViewThemeData {
174 self.shrink_wrap = Some(shrink_wrap);
175 self
176 }
177
178 pub fn divider_color(mut self, divider_color: Color) -> SearchViewThemeData {
180 self.divider_color = Some(divider_color);
181 self
182 }
183
184 pub fn copy_with(&self) -> SearchViewThemeData {
187 self.clone()
188 }
189
190 pub fn lerp(
192 a: Option<&SearchViewThemeData>,
193 b: Option<&SearchViewThemeData>,
194 t: f64,
195 ) -> Option<SearchViewThemeData> {
196 match (a, b) {
197 (None, None) => return None,
198 (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
199 _ => {}
200 }
201 Some(SearchViewThemeData {
202 background_color: Color::lerp(
203 a.and_then(|a| a.background_color),
204 b.and_then(|b| b.background_color),
205 t,
206 ),
207 elevation: lerp_double(a.and_then(|a| a.elevation), b.and_then(|b| b.elevation), t),
208 surface_tint_color: Color::lerp(
209 a.and_then(|a| a.surface_tint_color),
210 b.and_then(|b| b.surface_tint_color),
211 t,
212 ),
213 side: Self::lerp_sides(
214 a.and_then(|a| a.side.as_ref()),
215 b.and_then(|b| b.side.as_ref()),
216 t,
217 )
218 .map(AnyBorderSide::new),
219 shape: OutlinedBorderRef::lerp(
220 a.and_then(|a| a.shape.as_ref()),
221 b.and_then(|b| b.shape.as_ref()),
222 t,
223 ),
224 header_height: lerp_double(
225 a.and_then(|a| a.header_height),
226 b.and_then(|b| b.header_height),
227 t,
228 ),
229 header_text_style: TextStyle::lerp(
230 a.and_then(|a| a.header_text_style.as_ref()),
231 b.and_then(|b| b.header_text_style.as_ref()),
232 t,
233 ),
234 header_hint_style: TextStyle::lerp(
236 a.and_then(|a| a.header_text_style.as_ref()),
237 b.and_then(|b| b.header_text_style.as_ref()),
238 t,
239 ),
240 constraints: BoxConstraints::lerp(
241 a.and_then(|a| a.constraints),
242 b.and_then(|b| b.constraints),
243 t,
244 ),
245 padding: EdgeInsetsGeometry::lerp(
246 a.and_then(|a| a.padding),
247 b.and_then(|b| b.padding),
248 t,
249 ),
250 bar_padding: EdgeInsetsGeometry::lerp(
251 a.and_then(|a| a.bar_padding),
252 b.and_then(|b| b.bar_padding),
253 t,
254 ),
255 shrink_wrap: if t < 0.5 {
256 a.and_then(|a| a.shrink_wrap)
257 } else {
258 b.and_then(|b| b.shrink_wrap)
259 },
260 divider_color: Color::lerp(
261 a.and_then(|a| a.divider_color),
262 b.and_then(|b| b.divider_color),
263 t,
264 ),
265 })
266 }
267
268 fn lerp_sides(
270 a: Option<&AnyBorderSide>,
271 b: Option<&AnyBorderSide>,
272 t: f64,
273 ) -> Option<BorderSide> {
274 if a.is_none() && b.is_none() {
275 return None;
276 }
277 let a = a.and_then(Self::resolve_unconditionally);
278 let b = b.and_then(Self::resolve_unconditionally);
279 let a = a.unwrap_or_else(|| BorderSide {
280 width: 0.0,
281 color: b.expect("b! in Dart").color.with_alpha(0),
282 ..BorderSide::default()
283 });
284 let b = b.unwrap_or_else(|| BorderSide {
285 width: 0.0,
286 color: a.color.with_alpha(0),
287 ..BorderSide::default()
288 });
289 Some(BorderSide::lerp(a, b, t))
290 }
291
292 fn resolve_unconditionally(side: &AnyBorderSide) -> Option<BorderSide> {
294 match side.extension::<WidgetStateBorderSide>() {
295 Some(side) => side.resolve(&WidgetStates::new()),
296 None => Some(**side),
297 }
298 }
299}
300
301#[derive(Debug)]
312pub struct SearchViewTheme {
313 pub key: Option<KeyRef>,
314 pub data: SearchViewThemeData,
316 pub child: WidgetRef,
317}
318
319impl SearchViewTheme {
320 #[cfg_attr(debug_assertions, track_caller)]
323 pub fn new<K>(data: SearchViewThemeData, child: impl IntoWidget<K>) -> SearchViewTheme {
324 SearchViewTheme {
325 key: None,
326 data,
327 child: child.into_widget(),
328 }
329 }
330
331 pub fn key(mut self, key: KeyRef) -> SearchViewTheme {
333 self.key = Some(key);
334 self
335 }
336
337 pub fn of(app: &mut App, context: BuildContext) -> SearchViewThemeData {
340 match context.depend_on_inherited_widget_of_exact_type::<SearchViewTheme>(app) {
341 Some(search_view_theme) => search_view_theme.data.clone(),
342 None => Theme::of(app, context).search_view_theme.clone(),
343 }
344 }
345}
346
347impl InheritedTheme for SearchViewTheme {
348 fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
349 SearchViewTheme::new(self.data.clone(), child).into_widget()
350 }
351}
352
353impl InheritedWidget for SearchViewTheme {
354 fn key(&self) -> Option<&KeyRef> {
355 self.key.as_ref()
356 }
357
358 fn child(&self) -> &WidgetRef {
359 &self.child
360 }
361
362 fn update_should_notify(&self, old_widget: &SearchViewTheme) -> bool {
363 self.data != old_widget.data
364 }
365
366 fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
367 Some(self)
368 }
369}
370
371impl waft_foundation::Diagnosticable for SearchViewThemeData {
372 fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
373 properties.add(
374 DiagnosticsProperty::<Color>::new("backgroundColor", self.background_color)
375 .default_value(None),
376 );
377 properties
378 .add(DiagnosticsProperty::<f64>::new("elevation", self.elevation).default_value(None));
379 properties.add(
380 DiagnosticsProperty::<Color>::new("surfaceTintColor", self.surface_tint_color)
381 .default_value(None),
382 );
383 properties.add(
384 DiagnosticsProperty::<AnyBorderSide>::new("side", self.side.clone())
385 .default_value(None),
386 );
387 properties.add(
388 DiagnosticsProperty::<OutlinedBorderRef>::new("shape", self.shape.clone())
389 .default_value(None),
390 );
391 properties.add(
392 DiagnosticsProperty::<f64>::new("headerHeight", self.header_height).default_value(None),
393 );
394 properties.add(
395 DiagnosticsProperty::<TextStyle>::new(
396 "headerTextStyle",
397 self.header_text_style.clone(),
398 )
399 .default_value(None),
400 );
401 properties.add(
402 DiagnosticsProperty::<TextStyle>::new(
403 "headerHintStyle",
404 self.header_hint_style.clone(),
405 )
406 .default_value(None),
407 );
408 properties.add(
409 DiagnosticsProperty::<BoxConstraints>::new("constraints", self.constraints)
410 .default_value(None),
411 );
412 properties.add(
413 DiagnosticsProperty::<EdgeInsetsGeometry>::new("padding", self.padding)
414 .default_value(None),
415 );
416 properties.add(
417 DiagnosticsProperty::<EdgeInsetsGeometry>::new("barPadding", self.bar_padding)
418 .default_value(None),
419 );
420 properties.add(
421 DiagnosticsProperty::<bool>::new("shrinkWrap", self.shrink_wrap).default_value(None),
422 );
423 properties.add(
424 DiagnosticsProperty::<Color>::new("dividerColor", self.divider_color)
425 .default_value(None),
426 );
427 }
428}
429
430impl std::fmt::Display for SearchViewThemeData {
431 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
432 f.write_str(&waft_foundation::Diagnosticable::to_string(self))
433 }
434}
435
436#[cfg(test)]
437mod tests {
438 use waft_foundation::Task;
439 use waft_painting::{BorderRadiusGeometry, RoundedRectangleBorder};
440 use waft_widgets::testing::{Finder, WidgetTester, find, test_widgets};
441 use waft_widgets::{Builder, Center, ConstrainedBox, EditableText, Icon, SizedBox, Text};
442
443 use super::*;
444 use crate::color_scheme::ColorScheme;
445 use crate::icons::Icons;
446 use crate::material::Material;
447 use crate::scaffold::Scaffold;
448 use crate::search_anchor::{SearchAnchor, SearchBar, ViewContent};
449 use crate::test_support::material_app;
450 use crate::text_field::TextField;
451 use crate::theme_data::ThemeData;
452
453 #[test]
455 fn search_view_theme_data_copy_with_equals_basics() {
456 assert_eq!(
457 SearchViewThemeData::new(),
458 SearchViewThemeData::new().copy_with()
459 );
460 }
461
462 #[test]
464 fn search_view_theme_data_lerp_special_cases() {
465 assert_eq!(SearchViewThemeData::lerp(None, None, 0.0), None);
466 let data = SearchViewThemeData::new();
467 assert_eq!(
468 SearchViewThemeData::lerp(Some(&data), Some(&data), 0.5),
469 Some(data)
470 );
471 }
472
473 #[test]
475 fn search_view_theme_data_defaults_are_all_null() {
476 let theme_data = SearchViewThemeData::new();
477 assert_eq!(theme_data.background_color, None);
478 assert_eq!(theme_data.elevation, None);
479 assert_eq!(theme_data.surface_tint_color, None);
480 assert_eq!(theme_data.constraints, None);
481 assert_eq!(theme_data.side, None);
482 assert_eq!(theme_data.shape, None);
483 assert_eq!(theme_data.header_height, None);
484 assert_eq!(theme_data.header_text_style, None);
485 assert_eq!(theme_data.header_hint_style, None);
486 assert_eq!(theme_data.padding, None);
487 assert_eq!(theme_data.bar_padding, None);
488 assert_eq!(theme_data.shrink_wrap, None);
489 assert_eq!(theme_data.divider_color, None);
490
491 let theme = SearchViewTheme::new(SearchViewThemeData::new(), SizedBox::new());
492 assert_eq!(theme.data, SearchViewThemeData::new());
493 }
494
495 const BACKGROUND_COLOR: Color = Color::new(0xff000001);
496 const ELEVATION: f64 = 5.0;
497 const SURFACE_TINT_COLOR: Color = Color::new(0xff000002);
498 const HEADER_HEIGHT: f64 = 45.0;
499
500 fn side() -> BorderSide {
501 BorderSide {
502 color: Color::new(0xff000003),
503 width: 2.0,
504 ..BorderSide::default()
505 }
506 }
507
508 fn shape() -> OutlinedBorderRef {
509 OutlinedBorderRef::new(RoundedRectangleBorder::new(
510 side(),
511 BorderRadiusGeometry::circular(20.0),
512 ))
513 }
514
515 fn header_text_style() -> TextStyle {
516 TextStyle::new()
517 .color(Color::new(0xff000004))
518 .font_size(20.0)
519 }
520
521 fn header_hint_style() -> TextStyle {
522 TextStyle::new()
523 .color(Color::new(0xff000005))
524 .font_size(18.0)
525 }
526
527 fn constraints() -> BoxConstraints {
528 BoxConstraints {
529 min_width: 250.0,
530 max_width: 300.0,
531 min_height: 450.0,
532 ..BoxConstraints::new()
533 }
534 }
535
536 fn search_view_theme() -> SearchViewThemeData {
537 SearchViewThemeData::new()
538 .background_color(BACKGROUND_COLOR)
539 .elevation(ELEVATION)
540 .surface_tint_color(SURFACE_TINT_COLOR)
541 .side(side())
542 .shape(shape())
543 .header_height(HEADER_HEIGHT)
544 .header_text_style(header_text_style())
545 .header_hint_style(header_hint_style())
546 .constraints(constraints())
547 }
548
549 #[derive(Clone, Copy)]
551 enum ThemeArgument {
552 Null,
553 Empty,
554 Full,
555 }
556
557 impl ThemeArgument {
558 fn data(self) -> Option<SearchViewThemeData> {
559 match self {
560 ThemeArgument::Null => None,
561 ThemeArgument::Empty => Some(SearchViewThemeData::new()),
562 ThemeArgument::Full => Some(search_view_theme()),
563 }
564 }
565 }
566
567 fn build_frame(
569 tester: &WidgetTester,
570 use_search_view_properties: bool,
571 search_view_theme_data: Option<SearchViewThemeData>,
572 overall_theme: Option<SearchViewThemeData>,
573 ) -> WidgetRef {
574 let child = Builder::new(move |_app, _context| {
575 let mut anchor = SearchAnchor::new(
576 |_app, _context, _controller| Icon::new(Some(Icons::search())).into_widget(),
577 |_app, _context, _controller| Task::ready(Vec::new()),
578 )
579 .view_hint_text("hint text")
580 .is_full_screen(false);
581 if use_search_view_properties {
582 anchor = anchor
583 .view_elevation(ELEVATION)
584 .view_background_color(BACKGROUND_COLOR)
585 .view_surface_tint_color(SURFACE_TINT_COLOR)
586 .view_side(side())
587 .view_shape(shape())
588 .header_height(HEADER_HEIGHT)
589 .header_text_style(header_text_style())
590 .header_hint_style(header_hint_style())
591 .view_constraints(constraints());
592 }
593 anchor.into_widget()
594 });
595 let theme = {
596 let app = tester.cell.borrow();
597 let theme = ThemeData::from(ColorScheme::light()).build(&app);
598 match overall_theme {
599 Some(overall_theme) => theme.copy_with().search_view_theme(overall_theme).build(),
600 None => theme,
601 }
602 };
603 material_app(
604 theme,
605 Scaffold::new()
606 .body(Center::new().child(
607 match search_view_theme_data {
610 None => child.into_widget(),
611 Some(data) => SearchViewTheme::new(data, child).into_widget(),
612 },
613 ))
614 .into_widget(),
615 )
616 }
617
618 fn find_view_content() -> Finder {
619 find::by_type::<ViewContent>()
620 }
621
622 fn check_search_view(tester: &mut WidgetTester) {
623 let (elevation, color, surface_tint_color, material_shape) = tester.widget(
624 find::descendant(find_view_content(), find::by_type::<Material>()).first(),
625 |material: &Material| {
626 (
627 material.elevation,
628 material.color,
629 material.surface_tint_color,
630 material.shape.clone(),
631 )
632 },
633 );
634 assert_eq!(elevation, ELEVATION);
635 assert_eq!(color, Some(BACKGROUND_COLOR));
636 assert_eq!(surface_tint_color, Some(SURFACE_TINT_COLOR));
637 assert_eq!(material_shape, Some(shape().into()));
638
639 let size = tester.get_size(
640 find::descendant(find_view_content(), find::by_type::<ConstrainedBox>()).first(),
641 );
642 assert_eq!(size.width(), 250.0);
643 assert_eq!(size.height(), 450.0);
644
645 let hint_style = tester
646 .widget(find::text("hint text"), |text: &Text| text.style.clone())
647 .expect("a hint style");
648 assert_eq!(
649 hint_style.color.as_ref().map(|color| color.color()),
650 header_hint_style()
651 .color
652 .as_ref()
653 .map(|color| color.color())
654 );
655 assert_eq!(hint_style.font_size, header_hint_style().font_size);
656
657 let bar_height = tester
658 .get_size(find::descendant(
659 find_view_content(),
660 find::by_type::<SearchBar>(),
661 ))
662 .height();
663 assert_eq!(bar_height, HEADER_HEIGHT);
664 tester.enter_text(find::by_type::<TextField>(), "input");
665 let input_style = tester.widget(find::text("input"), |text: &EditableText| {
666 text.style.clone()
667 });
668 assert_eq!(
669 input_style.color.as_ref().map(|color| color.color()),
670 header_text_style()
671 .color
672 .as_ref()
673 .map(|color| color.color())
674 );
675 assert_eq!(input_style.font_size, header_text_style().font_size);
676 }
677
678 fn pump_and_check(
679 use_search_view_properties: bool,
680 search_view_theme_data: ThemeArgument,
681 overall_theme: ThemeArgument,
682 ) {
683 test_widgets(move |tester| {
684 let root = build_frame(
685 tester,
686 use_search_view_properties,
687 search_view_theme_data.data(),
688 overall_theme.data(),
689 );
690 tester.pump_widget(root);
691 tester.tap(find::by_icon(&Icons::search()));
692 tester.pump_and_settle(); check_search_view(tester);
694 });
695 }
696
697 #[test]
699 fn search_view_properties_override_defaults() {
700 pump_and_check(true, ThemeArgument::Null, ThemeArgument::Null);
701 }
702
703 #[test]
705 fn search_view_theme_data_overrides_defaults() {
706 pump_and_check(false, ThemeArgument::Full, ThemeArgument::Null);
707 }
708
709 #[test]
711 fn overall_theme_search_view_theme_overrides_defaults() {
712 pump_and_check(false, ThemeArgument::Null, ThemeArgument::Full);
713 }
714
715 #[test]
718 fn search_view_properties_override_defaults_with_empty_theme_and_overall_theme() {
719 pump_and_check(true, ThemeArgument::Empty, ThemeArgument::Empty);
720 }
721
722 #[test]
724 fn search_view_theme_overrides_defaults_and_overall_theme() {
725 pump_and_check(false, ThemeArgument::Full, ThemeArgument::Empty);
726 }
727
728 #[test]
731 fn overall_theme_search_view_theme_overrides_defaults_and_null_theme() {
732 pump_and_check(false, ThemeArgument::Null, ThemeArgument::Full);
733 }
734 #[test]
736 fn a_search_view_theme_data_describes_nothing_by_default() {
737 assert_eq!(
738 waft_widgets::testing::describe_properties(|properties| {
739 waft_foundation::Diagnosticable::debug_fill_properties(
740 &SearchViewThemeData::new(),
741 properties,
742 )
743 }),
744 Vec::<String>::new()
745 );
746 }
747}