1use std::fmt;
16
17use waft_embedder::{Radius, Size};
18use waft_foundation::{App, Listener, ValueChanged};
19use waft_painting::{
20 BorderRadiusDirectional, BorderRadiusGeometry, BorderSide, EdgeInsetsDirectional,
21 EdgeInsetsGeometry, OutlinedBorderRef, RoundedRectangleBorder,
22};
23use waft_widgets::{
24 BuildContext, CrossAxisAlignment, Flexible, IntoWidget, IntrinsicHeight, KeyRef, MainAxisSize,
25 Row, SizedBox, StatelessWidget, WidgetRef, WidgetState, WidgetStateProperty,
26 WidgetStatePropertyRef,
27};
28
29use crate::button_style::ButtonStyle;
30use crate::elevated_button::ElevatedButton;
31use crate::filled_button::FilledButton;
32use crate::outlined_button::OutlinedButton;
33
34const K_SPLIT_BUTTON_MIN_WIDTH: f64 = 48.0;
36
37#[derive(Clone, Debug, PartialEq)]
40pub struct SplitButtonShapes {
41 pub shape: OutlinedBorderRef,
43 pub pressed_shape: Option<OutlinedBorderRef>,
45 pub checked_shape: Option<OutlinedBorderRef>,
47}
48
49impl SplitButtonShapes {
50 pub fn new(
52 shape: OutlinedBorderRef,
53 pressed_shape: Option<OutlinedBorderRef>,
54 checked_shape: Option<OutlinedBorderRef>,
55 ) -> SplitButtonShapes {
56 SplitButtonShapes {
57 shape,
58 pressed_shape,
59 checked_shape,
60 }
61 }
62}
63
64#[derive(Clone, Copy, Debug, PartialEq, Eq)]
66pub enum SplitButtonVariant {
67 Filled,
69 Tonal,
71 Elevated,
73 Outlined,
75}
76
77pub struct SplitButtonDefaults;
80
81impl SplitButtonDefaults {
82 pub const SPACING: f64 = 2.0;
84
85 pub const EXTRA_SMALL_CONTAINER_HEIGHT: f64 = 32.0;
87 pub const SMALL_CONTAINER_HEIGHT: f64 = 40.0;
89 pub const MEDIUM_CONTAINER_HEIGHT: f64 = 56.0;
91 pub const LARGE_CONTAINER_HEIGHT: f64 = 96.0;
93 pub const EXTRA_LARGE_CONTAINER_HEIGHT: f64 = 136.0;
95
96 pub const LEADING_ICON_SIZE: f64 = 20.0;
98 pub const TRAILING_ICON_SIZE: f64 = 22.0;
100
101 pub fn leading_button_shapes_for(button_height: f64) -> SplitButtonShapes {
104 let (inner, pressed) = inner_corners_for(button_height);
105 SplitButtonShapes::new(
106 rounded(full_corner(button_height), inner),
107 Some(rounded(full_corner(button_height), pressed)),
108 None,
109 )
110 }
111
112 pub fn trailing_button_shapes_for(button_height: f64) -> SplitButtonShapes {
114 let (inner, pressed) = inner_corners_for(button_height);
115 SplitButtonShapes::new(
116 rounded(inner, full_corner(button_height)),
117 Some(rounded(pressed, full_corner(button_height))),
118 Some(rounded(
119 full_corner(button_height),
120 full_corner(button_height),
121 )),
122 )
123 }
124
125 pub fn leading_button_content_padding_for(button_height: f64) -> EdgeInsetsGeometry {
127 let (start, end) = if button_height < Self::SMALL_CONTAINER_HEIGHT {
128 (12.0, 10.0)
129 } else if button_height < Self::MEDIUM_CONTAINER_HEIGHT {
130 (16.0, 12.0)
131 } else if button_height < Self::LARGE_CONTAINER_HEIGHT {
132 (24.0, 24.0)
133 } else if button_height < Self::EXTRA_LARGE_CONTAINER_HEIGHT {
134 (48.0, 48.0)
135 } else {
136 (64.0, 64.0)
137 };
138 EdgeInsetsDirectional::only(start, 0.0, end, 0.0).into()
139 }
140
141 pub fn trailing_button_content_padding_for(button_height: f64) -> EdgeInsetsGeometry {
143 let horizontal = if button_height < Self::MEDIUM_CONTAINER_HEIGHT {
144 13.0
145 } else if button_height < Self::LARGE_CONTAINER_HEIGHT {
146 15.0
147 } else if button_height < Self::EXTRA_LARGE_CONTAINER_HEIGHT {
148 29.0
149 } else {
150 43.0
151 };
152 EdgeInsetsDirectional::only(horizontal, 0.0, horizontal, 0.0).into()
153 }
154
155 pub fn leading_button_icon_size_for(button_height: f64) -> f64 {
157 if button_height < Self::MEDIUM_CONTAINER_HEIGHT {
158 20.0
159 } else if button_height < Self::LARGE_CONTAINER_HEIGHT {
160 24.0
161 } else if button_height < Self::EXTRA_LARGE_CONTAINER_HEIGHT {
162 32.0
163 } else {
164 40.0
165 }
166 }
167
168 pub fn trailing_button_icon_size_for(button_height: f64) -> f64 {
170 if button_height < Self::MEDIUM_CONTAINER_HEIGHT {
171 22.0
172 } else if button_height < Self::LARGE_CONTAINER_HEIGHT {
173 26.0
174 } else if button_height < Self::EXTRA_LARGE_CONTAINER_HEIGHT {
175 38.0
176 } else {
177 50.0
178 }
179 }
180}
181
182fn rounded(start: Radius, end: Radius) -> OutlinedBorderRef {
185 OutlinedBorderRef::new(RoundedRectangleBorder::new(
186 BorderSide::NONE,
187 BorderRadiusGeometry::Directional(BorderRadiusDirectional::only(start, end, start, end)),
188 ))
189}
190
191fn full_corner(button_height: f64) -> Radius {
193 Radius::circular(button_height / 2.0)
194}
195
196fn inner_corners_for(button_height: f64) -> (Radius, Radius) {
198 use SplitButtonDefaults as tokens;
199 let x_small = tokens::EXTRA_SMALL_CONTAINER_HEIGHT;
200 let small = tokens::SMALL_CONTAINER_HEIGHT;
201 let medium = tokens::MEDIUM_CONTAINER_HEIGHT;
202 let large = tokens::LARGE_CONTAINER_HEIGHT;
203 let x_large = tokens::EXTRA_LARGE_CONTAINER_HEIGHT;
204 if button_height <= (x_small + small) / 2.0 {
205 (Radius::circular(4.0), Radius::circular(8.0))
206 } else if button_height <= (medium + large) / 2.0 {
207 (Radius::circular(4.0), Radius::circular(12.0))
208 } else if button_height <= (large + x_large) / 2.0 {
209 (Radius::circular(8.0), Radius::circular(20.0))
210 } else {
211 (Radius::circular(12.0), Radius::circular(20.0))
212 }
213}
214
215fn part_style(
218 shapes: &SplitButtonShapes,
219 content_padding: EdgeInsetsGeometry,
220 checked: bool,
221) -> ButtonStyle {
222 let resting = shapes.shape.clone();
223 let pressed = shapes.pressed_shape.clone();
224 let checked_shape = shapes.checked_shape.clone();
225 let shape: WidgetStatePropertyRef<Option<OutlinedBorderRef>> =
226 <dyn WidgetStateProperty<Option<OutlinedBorderRef>>>::resolve_with(move |states| {
227 let shape = if states.contains(&WidgetState::Pressed) {
228 pressed.as_ref()
229 } else if checked {
230 checked_shape.as_ref()
231 } else {
232 None
233 };
234 Some(shape.unwrap_or(&resting).clone())
235 });
236 let padding: WidgetStatePropertyRef<Option<EdgeInsetsGeometry>> =
237 <dyn WidgetStateProperty<Option<EdgeInsetsGeometry>>>::all(Some(content_padding));
238 let minimum_size: WidgetStatePropertyRef<Option<Size>> =
239 <dyn WidgetStateProperty<Option<Size>>>::all(Some(Size::new(
240 K_SPLIT_BUTTON_MIN_WIDTH,
241 SplitButtonDefaults::SMALL_CONTAINER_HEIGHT,
242 )));
243 ButtonStyle::new()
244 .shape(shape)
245 .padding(padding)
246 .minimum_size(minimum_size)
247 .animation_duration(crate::constants::K_THEME_CHANGE_DURATION)
248}
249
250fn part_button(
252 variant: SplitButtonVariant,
253 on_pressed: Option<Listener>,
254 style: ButtonStyle,
255 child: WidgetRef,
256) -> WidgetRef {
257 match variant {
258 SplitButtonVariant::Filled => FilledButton::new(on_pressed, Some(child))
259 .style(style)
260 .into_widget(),
261 SplitButtonVariant::Tonal => FilledButton::tonal(on_pressed, Some(child))
262 .style(style)
263 .into_widget(),
264 SplitButtonVariant::Elevated => ElevatedButton::new(on_pressed, Some(child))
265 .style(style)
266 .into_widget(),
267 SplitButtonVariant::Outlined => OutlinedButton::new(on_pressed, Some(child))
268 .style(style)
269 .into_widget(),
270 }
271}
272
273pub struct SplitButtonLeadingButton {
276 pub key: Option<KeyRef>,
277 pub on_pressed: Option<Listener>,
279 pub variant: SplitButtonVariant,
281 pub shapes: SplitButtonShapes,
283 pub content_padding: EdgeInsetsGeometry,
285 pub child: WidgetRef,
287}
288
289impl SplitButtonLeadingButton {
290 #[cfg_attr(debug_assertions, track_caller)]
292 pub fn new<K>(
293 on_pressed: Option<Listener>,
294 child: impl IntoWidget<K>,
295 ) -> SplitButtonLeadingButton {
296 let shapes = SplitButtonDefaults::leading_button_shapes_for(
297 SplitButtonDefaults::SMALL_CONTAINER_HEIGHT,
298 );
299 SplitButtonLeadingButton {
300 key: None,
301 on_pressed,
302 variant: SplitButtonVariant::Filled,
303 shapes,
304 content_padding: SplitButtonDefaults::leading_button_content_padding_for(
305 SplitButtonDefaults::SMALL_CONTAINER_HEIGHT,
306 ),
307 child: child.into_widget(),
308 }
309 }
310
311 pub fn key(mut self, key: KeyRef) -> SplitButtonLeadingButton {
313 self.key = Some(key);
314 self
315 }
316
317 pub fn variant(mut self, variant: SplitButtonVariant) -> SplitButtonLeadingButton {
319 self.variant = variant;
320 self
321 }
322
323 pub fn shapes(mut self, shapes: SplitButtonShapes) -> SplitButtonLeadingButton {
325 self.shapes = shapes;
326 self
327 }
328
329 pub fn content_padding(
331 mut self,
332 content_padding: impl Into<EdgeInsetsGeometry>,
333 ) -> SplitButtonLeadingButton {
334 self.content_padding = content_padding.into();
335 self
336 }
337}
338
339impl fmt::Debug for SplitButtonLeadingButton {
340 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
341 f.debug_struct("SplitButtonLeadingButton")
342 .field("variant", &self.variant)
343 .finish_non_exhaustive()
344 }
345}
346
347impl StatelessWidget for SplitButtonLeadingButton {
348 fn key(&self) -> Option<&KeyRef> {
349 self.key.as_ref()
350 }
351
352 fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
353 part_button(
354 self.variant,
355 self.on_pressed.clone(),
356 part_style(&self.shapes, self.content_padding, false),
357 self.child.clone(),
358 )
359 }
360}
361
362pub struct SplitButtonTrailingButton {
365 pub key: Option<KeyRef>,
366 pub on_pressed: Option<Listener>,
368 pub checked: Option<bool>,
370 pub variant: SplitButtonVariant,
372 pub shapes: SplitButtonShapes,
374 pub content_padding: EdgeInsetsGeometry,
376 pub child: WidgetRef,
378}
379
380impl SplitButtonTrailingButton {
381 #[cfg_attr(debug_assertions, track_caller)]
384 pub fn new<K>(
385 on_pressed: Option<Listener>,
386 child: impl IntoWidget<K>,
387 ) -> SplitButtonTrailingButton {
388 let shapes = SplitButtonDefaults::trailing_button_shapes_for(
389 SplitButtonDefaults::SMALL_CONTAINER_HEIGHT,
390 );
391 SplitButtonTrailingButton {
392 key: None,
393 on_pressed,
394 checked: None,
395 variant: SplitButtonVariant::Filled,
396 shapes,
397 content_padding: SplitButtonDefaults::trailing_button_content_padding_for(
398 SplitButtonDefaults::SMALL_CONTAINER_HEIGHT,
399 ),
400 child: child.into_widget(),
401 }
402 }
403
404 #[cfg_attr(debug_assertions, track_caller)]
408 pub fn toggle<K>(
409 checked: bool,
410 on_checked_change: Option<ValueChanged<bool>>,
411 child: impl IntoWidget<K>,
412 ) -> SplitButtonTrailingButton {
413 let on_pressed = on_checked_change.map(|on_checked_change| {
414 Listener::new(move |app: &mut App| on_checked_change(app, !checked))
415 });
416 let mut button = SplitButtonTrailingButton::new(on_pressed, child);
417 button.checked = Some(checked);
418 button
419 }
420
421 pub fn key(mut self, key: KeyRef) -> SplitButtonTrailingButton {
423 self.key = Some(key);
424 self
425 }
426
427 pub fn variant(mut self, variant: SplitButtonVariant) -> SplitButtonTrailingButton {
429 self.variant = variant;
430 self
431 }
432
433 pub fn shapes(mut self, shapes: SplitButtonShapes) -> SplitButtonTrailingButton {
435 self.shapes = shapes;
436 self
437 }
438
439 pub fn content_padding(
441 mut self,
442 content_padding: impl Into<EdgeInsetsGeometry>,
443 ) -> SplitButtonTrailingButton {
444 self.content_padding = content_padding.into();
445 self
446 }
447}
448
449impl fmt::Debug for SplitButtonTrailingButton {
450 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
451 f.debug_struct("SplitButtonTrailingButton")
452 .field("variant", &self.variant)
453 .field("checked", &self.checked)
454 .finish_non_exhaustive()
455 }
456}
457
458impl StatelessWidget for SplitButtonTrailingButton {
459 fn key(&self) -> Option<&KeyRef> {
460 self.key.as_ref()
461 }
462
463 fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
464 part_button(
465 self.variant,
466 self.on_pressed.clone(),
467 part_style(
468 &self.shapes,
469 self.content_padding,
470 self.checked.unwrap_or(false),
471 ),
472 self.child.clone(),
473 )
474 }
475}
476
477pub struct SplitButtonLayout {
486 pub key: Option<KeyRef>,
487 pub leading_button: WidgetRef,
489 pub trailing_button: WidgetRef,
491 pub spacing: f64,
493}
494
495impl SplitButtonLayout {
496 #[cfg_attr(debug_assertions, track_caller)]
498 pub fn new<L, T>(
499 leading_button: impl IntoWidget<L>,
500 trailing_button: impl IntoWidget<T>,
501 ) -> SplitButtonLayout {
502 SplitButtonLayout {
503 key: None,
504 leading_button: leading_button.into_widget(),
505 trailing_button: trailing_button.into_widget(),
506 spacing: SplitButtonDefaults::SPACING,
507 }
508 }
509
510 pub fn key(mut self, key: KeyRef) -> SplitButtonLayout {
512 self.key = Some(key);
513 self
514 }
515
516 pub fn spacing(mut self, spacing: f64) -> SplitButtonLayout {
518 self.spacing = spacing;
519 self
520 }
521}
522
523impl fmt::Debug for SplitButtonLayout {
524 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
525 f.debug_struct("SplitButtonLayout")
526 .field("spacing", &self.spacing)
527 .finish_non_exhaustive()
528 }
529}
530
531impl StatelessWidget for SplitButtonLayout {
532 fn key(&self) -> Option<&KeyRef> {
533 self.key.as_ref()
534 }
535
536 fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
537 IntrinsicHeight::new()
538 .child(
539 Row::new()
540 .main_axis_size(MainAxisSize::Min)
541 .cross_axis_alignment(CrossAxisAlignment::Stretch)
542 .children(vec![
543 Flexible::new(self.leading_button.clone()).into_widget(),
544 SizedBox::new().width(self.spacing).into_widget(),
545 self.trailing_button.clone(),
546 ]),
547 )
548 .into_widget()
549 }
550}
551
552#[cfg(test)]
553mod tests {
554 use std::cell::Cell;
555 use std::rc::Rc;
556
557 use waft_widgets::testing::{WidgetTester, find, test_widgets};
558 use waft_widgets::{
559 Axis, Center, Icon, IntoWidget, Row, SingleChildScrollView, SizedBox, StatefulBuilder, Text,
560 };
561
562 use super::*;
563 use crate::icons::Icons;
564 use crate::test_support::material_app;
565 use crate::theme_data::ThemeData;
566
567 fn pump<K>(tester: &mut WidgetTester, layout: impl IntoWidget<K>) {
568 let theme = ThemeData::builder().build(&tester.cell.borrow());
569 tester.pump_widget(material_app(
570 theme,
571 Center::new().child(layout).into_widget(),
572 ));
573 }
574
575 fn recorded(flag: Rc<Cell<bool>>) -> Listener {
576 Listener::new(move |_app| flag.set(true))
577 }
578
579 fn ignored() -> Option<Listener> {
580 Some(Listener::new(|_app| {}))
581 }
582
583 fn icon_spacing_for(height: f64) -> f64 {
586 if height < SplitButtonDefaults::SMALL_CONTAINER_HEIGHT {
587 4.0
588 } else if height < SplitButtonDefaults::LARGE_CONTAINER_HEIGHT {
589 8.0
590 } else if height < SplitButtonDefaults::EXTRA_LARGE_CONTAINER_HEIGHT {
591 12.0
592 } else {
593 16.0
594 }
595 }
596
597 fn leading_with_icon_and_text(
598 on_pressed: Option<Listener>,
599 icon_size: f64,
600 icon_spacing: f64,
601 ) -> SplitButtonLeadingButton {
602 SplitButtonLeadingButton::new(
603 on_pressed,
604 Row::new()
605 .main_axis_size(MainAxisSize::Min)
606 .children(vec![
607 Icon::new(Some(Icons::edit())).size(icon_size).into_widget(),
608 SizedBox::new().width(icon_spacing).into_widget(),
609 Text::new("My Button").into_widget(),
610 ])
611 .into_widget(),
612 )
613 }
614
615 fn trailing_toggle(
616 checked: bool,
617 on_checked_change: Option<ValueChanged<bool>>,
618 ) -> SplitButtonTrailingButton {
619 SplitButtonTrailingButton::toggle(
620 checked,
621 on_checked_change,
622 Icon::new(Some(Icons::keyboard_arrow_down())),
623 )
624 }
625
626 fn label_leading() -> SplitButtonLeadingButton {
627 SplitButtonLeadingButton::new(ignored(), Text::new("Label"))
628 }
629
630 fn icon_trailing() -> SplitButtonTrailingButton {
631 SplitButtonTrailingButton::new(ignored(), Icon::new(Some(Icons::keyboard_arrow_down())))
632 }
633
634 fn part_heights(tester: &WidgetTester) -> (f64, f64) {
635 (
636 tester
637 .get_size(find::by_type::<SplitButtonLeadingButton>())
638 .height(),
639 tester
640 .get_size(find::by_type::<SplitButtonTrailingButton>())
641 .height(),
642 )
643 }
644
645 fn assert_content_displayed(tester: &WidgetTester) {
646 assert_eq!(find::text("My Button").evaluate(tester).len(), 1);
647 assert_eq!(find::by_icon(&Icons::edit()).evaluate(tester).len(), 1);
648 assert_eq!(
649 find::by_icon(&Icons::keyboard_arrow_down())
650 .evaluate(tester)
651 .len(),
652 1
653 );
654 }
655
656 #[test]
659 fn filled_split_button_content_display() {
660 test_widgets(|tester| {
661 let leading_pressed = Rc::new(Cell::new(false));
662 let trailing_pressed = Rc::new(Cell::new(false));
663 let on_checked_change: ValueChanged<bool> = {
664 let trailing_pressed = Rc::clone(&trailing_pressed);
665 Rc::new(move |_app, _checked| trailing_pressed.set(true))
666 };
667 pump(
668 tester,
669 SplitButtonLayout::new(
670 leading_with_icon_and_text(
671 Some(recorded(Rc::clone(&leading_pressed))),
672 24.0,
673 8.0,
674 ),
675 SizedBox::new()
676 .width(34.0)
677 .height(34.0)
678 .child(trailing_toggle(false, Some(on_checked_change))),
679 ),
680 );
681
682 assert_content_displayed(tester);
683
684 tester.tap(find::text("My Button"));
685 assert!(leading_pressed.get());
686 tester.tap(find::by_icon(&Icons::keyboard_arrow_down()));
687 assert!(trailing_pressed.get());
688 });
689 }
690
691 #[test]
694 fn filled_split_button_trailing_button_checked() {
695 test_widgets(|tester| {
696 let checked = Rc::new(Cell::new(false));
697 let layout = {
698 let checked = Rc::clone(&checked);
699 StatefulBuilder::new(move |_app, _context, set_state| {
700 let on_checked_change: ValueChanged<bool> = {
701 let checked = Rc::clone(&checked);
702 Rc::new(move |app, value| {
703 let checked = Rc::clone(&checked);
704 set_state(app, &mut |_app| checked.set(value));
705 })
706 };
707 SplitButtonLayout::new(
708 leading_with_icon_and_text(ignored(), 24.0, 8.0),
709 SizedBox::new()
710 .width(34.0)
711 .height(34.0)
712 .child(trailing_toggle(checked.get(), Some(on_checked_change))),
713 )
714 .into_widget()
715 })
716 };
717 pump(tester, layout);
718
719 assert_eq!(tester_checked(tester), Some(false));
720 tester.tap(find::by_icon(&Icons::keyboard_arrow_down()));
721 tester.pump();
722 assert!(checked.get());
723 assert_eq!(tester_checked(tester), Some(true));
724 });
725 }
726
727 fn tester_checked(tester: &WidgetTester) -> Option<bool> {
728 tester.widget(
729 find::by_type::<SplitButtonTrailingButton>(),
730 |button: &SplitButtonTrailingButton| button.checked,
731 )
732 }
733
734 #[test]
737 fn tonal_elevated_and_outlined_split_buttons_display_their_content() {
738 test_widgets(|tester| {
739 for variant in [
740 SplitButtonVariant::Tonal,
741 SplitButtonVariant::Elevated,
742 SplitButtonVariant::Outlined,
743 ] {
744 pump(
745 tester,
746 SplitButtonLayout::new(
747 leading_with_icon_and_text(ignored(), 24.0, 8.0).variant(variant),
748 SizedBox::new().width(34.0).height(34.0).child(
749 trailing_toggle(false, Some(Rc::new(|_app, _checked| {})))
750 .variant(variant),
751 ),
752 ),
753 );
754 assert_content_displayed(tester);
755 }
756 });
757 }
758
759 fn check_leading_positioning(
762 tester: &mut WidgetTester,
763 height: f64,
764 start: f64,
765 between: f64,
766 end: f64,
767 ) {
768 pump(
769 tester,
770 SplitButtonLayout::new(
771 SizedBox::new().height(height).child(
772 leading_with_icon_and_text(ignored(), 24.0, icon_spacing_for(height))
773 .shapes(SplitButtonDefaults::leading_button_shapes_for(height))
774 .content_padding(SplitButtonDefaults::leading_button_content_padding_for(
775 height,
776 )),
777 ),
778 SizedBox::new().height(height).child(
779 trailing_toggle(false, Some(Rc::new(|_app, _checked| {})))
780 .shapes(SplitButtonDefaults::trailing_button_shapes_for(height))
781 .content_padding(SplitButtonDefaults::trailing_button_content_padding_for(
782 height,
783 )),
784 ),
785 ),
786 );
787
788 let button = tester.get_rect(find::by_type::<SplitButtonLeadingButton>());
789 let icon = tester.get_rect(find::by_icon(&Icons::edit()));
790 let text = tester.get_rect(find::text("My Button"));
791 assert_is_equal_to(
792 icon.left - button.left,
793 start,
794 "start padding for leading button",
795 );
796 assert_is_equal_to(
797 text.left - icon.right,
798 between,
799 "icon and text between spacing for leading button",
800 );
801 assert_is_equal_to(
802 button.right - text.right,
803 end,
804 "end padding for leading button",
805 );
806 }
807
808 fn assert_is_equal_to(actual: f64, expected: f64, subject: &str) {
810 assert!(
811 (actual - expected).abs() <= 0.5,
812 "{subject}: expected {expected}, got {actual}"
813 );
814 }
815
816 #[test]
817 fn filled_split_button_extra_small_positioning() {
818 test_widgets(|tester| {
819 check_leading_positioning(
820 tester,
821 SplitButtonDefaults::EXTRA_SMALL_CONTAINER_HEIGHT,
822 12.0,
823 4.0,
824 10.0,
825 );
826 });
827 }
828
829 #[test]
830 fn filled_split_button_small_positioning() {
831 test_widgets(|tester| {
832 check_leading_positioning(
833 tester,
834 SplitButtonDefaults::SMALL_CONTAINER_HEIGHT,
835 16.0,
836 8.0,
837 12.0,
838 );
839 });
840 }
841
842 #[test]
843 fn filled_split_button_medium_positioning() {
844 test_widgets(|tester| {
845 check_leading_positioning(
846 tester,
847 SplitButtonDefaults::MEDIUM_CONTAINER_HEIGHT,
848 24.0,
849 8.0,
850 24.0,
851 );
852 });
853 }
854
855 #[test]
856 fn filled_split_button_large_positioning() {
857 test_widgets(|tester| {
858 check_leading_positioning(
859 tester,
860 SplitButtonDefaults::LARGE_CONTAINER_HEIGHT,
861 48.0,
862 12.0,
863 48.0,
864 );
865 });
866 }
867
868 #[test]
869 fn filled_split_button_extra_large_positioning() {
870 test_widgets(|tester| {
871 check_leading_positioning(
872 tester,
873 SplitButtonDefaults::EXTRA_LARGE_CONTAINER_HEIGHT,
874 64.0,
875 16.0,
876 64.0,
877 );
878 });
879 }
880
881 #[test]
885 fn split_button_in_a_horizontal_scroll_shows_its_label() {
886 test_widgets(|tester| {
887 pump(
888 tester,
889 SingleChildScrollView::new()
890 .scroll_direction(Axis::Horizontal)
891 .child(SplitButtonLayout::new(label_leading(), icon_trailing())),
892 );
893 assert_eq!(find::text("Label").evaluate(tester).len(), 1);
894 assert!(tester.get_size(find::text("Label")).width() > 0.0);
895 });
896 }
897
898 #[test]
900 fn split_button_stretches_buttons_to_max_height() {
901 test_widgets(|tester| {
902 pump(
903 tester,
904 SizedBox::new()
905 .height(100.0)
906 .child(SplitButtonLayout::new(label_leading(), icon_trailing())),
907 );
908 assert_eq!(part_heights(tester), (100.0, 100.0));
909 });
910 }
911
912 #[test]
914 fn split_button_trailing_matches_leading_height() {
915 test_widgets(|tester| {
916 pump(
917 tester,
918 SplitButtonLayout::new(
919 SizedBox::new().height(100.0).child(label_leading()),
920 icon_trailing(),
921 ),
922 );
923 assert_eq!(part_heights(tester), (100.0, 100.0));
924 });
925 }
926
927 #[test]
929 fn split_button_leading_matches_trailing_height() {
930 test_widgets(|tester| {
931 pump(
932 tester,
933 SplitButtonLayout::new(
934 label_leading(),
935 SizedBox::new().height(100.0).child(icon_trailing()),
936 ),
937 );
938 assert_eq!(part_heights(tester), (100.0, 100.0));
939 });
940 }
941
942 #[test]
944 fn a_split_button_spaces_its_parts_by_the_given_spacing() {
945 test_widgets(|tester| {
946 pump(
947 tester,
948 SplitButtonLayout::new(label_leading(), icon_trailing()).spacing(12.0),
949 );
950 let leading = tester.get_rect(find::by_type::<SplitButtonLeadingButton>());
951 let trailing = tester.get_rect(find::by_type::<SplitButtonTrailingButton>());
952 assert_eq!(trailing.left - leading.right, 12.0);
953 });
954 }
955
956 #[test]
959 fn a_narrow_split_button_shrinks_the_leading_part() {
960 test_widgets(|tester| {
961 pump(
962 tester,
963 SplitButtonLayout::new(label_leading(), icon_trailing()),
964 );
965 let trailing_width = tester
966 .get_size(find::by_type::<SplitButtonTrailingButton>())
967 .width();
968
969 pump(
970 tester,
971 SizedBox::new()
972 .width(100.0)
973 .child(SplitButtonLayout::new(label_leading(), icon_trailing())),
974 );
975 let leading = tester.get_size(find::by_type::<SplitButtonLeadingButton>());
976 let trailing = tester.get_size(find::by_type::<SplitButtonTrailingButton>());
977 assert_eq!(trailing.width(), trailing_width);
978 assert_eq!(
979 leading.width(),
980 100.0 - SplitButtonDefaults::SPACING - trailing_width
981 );
982 });
983 }
984}