1use std::{
2 borrow::Cow,
3 hash::{
4 Hash,
5 Hasher,
6 },
7 rc::Rc,
8};
9
10use paste::paste;
11use ragnarok::CursorPoint;
12use rustc_hash::FxHasher;
13use torin::{
14 content::Content,
15 gaps::Gaps,
16 prelude::{
17 Alignment,
18 Direction,
19 Length,
20 Position,
21 VisibleSize,
22 },
23 size::Size,
24};
25
26use crate::{
27 data::{
28 AccessibilityData,
29 EffectData,
30 LayoutData,
31 Overflow,
32 TextStyleData,
33 },
34 diff_key::DiffKey,
35 element::{
36 Element,
37 EventHandlerType,
38 EventHandlers,
39 },
40 elements::{
41 image::{
42 AspectRatio,
43 Image,
44 ImageCover,
45 ImageData,
46 SamplingMode,
47 },
48 label::Label,
49 paragraph::Paragraph,
50 rect::Rect,
51 },
52 event_handler::EventHandler,
53 events::{
54 data::{
55 Event,
56 KeyboardEventData,
57 MouseEventData,
58 SizedEventData,
59 StyledEventData,
60 VisibleEventData,
61 WheelEventData,
62 },
63 name::EventName,
64 },
65 layers::Layer,
66 prelude::*,
67 style::{
68 font_feature::FontFeature,
69 font_size::FontSize,
70 font_slant::FontSlant,
71 font_weight::FontWeight,
72 font_width::FontWidth,
73 letter_spacing::LetterSpacing,
74 scale::Scale,
75 text_height::TextHeightBehavior,
76 text_overflow::TextOverflow,
77 text_shadow::TextShadow,
78 transform_origin::TransformOrigin,
79 },
80};
81
82pub trait ChildrenExt: Sized {
84 fn children_mut(&mut self) -> &mut Vec<Element>;
95
96 fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self {
103 self.children_mut()
104 .extend(children.into_iter().map(IntoElement::into_element));
105 self
106 }
107
108 fn maybe_child<C: IntoElement>(mut self, child: Option<C>) -> Self {
115 if let Some(child) = child {
116 self.children_mut().push(child.into_element());
117 }
118 self
119 }
120
121 fn child<C: IntoElement>(mut self, child: C) -> Self {
128 self.children_mut().push(child.into_element());
129 self
130 }
131}
132
133pub trait RetainedChildrenExt: ChildrenExt {
136 fn retained_children(self, children: Rc<Vec<Element>>) -> Self;
138}
139
140pub trait KeyExt: Sized {
142 fn key_mut(&mut self) -> &mut DiffKey;
144
145 fn key(mut self, key: impl Hash) -> Self
149 where
150 Self: 'static,
151 {
152 let mut hasher = FxHasher::default();
153 std::any::TypeId::of::<Self>().hash(&mut hasher);
154 key.hash(&mut hasher);
155 *self.key_mut() = DiffKey::U64(hasher.finish());
156 self
157 }
158}
159
160pub trait WithExt: Sized {
162 fn with(self, with: impl FnOnce(Self) -> Self) -> Self {
164 with(self)
165 }
166}
167
168impl WithExt for Rect {}
169impl WithExt for Label {}
170impl WithExt for Paragraph {}
171impl WithExt for Image {}
172
173macro_rules! event_handlers {
174 (
175 $handler_variant:ident, $event_data:ty;
176 $(
177 $(#[$attr:meta])*
178 $name:ident => $event_variant:expr ;
179 )*
180 ) => {
181 paste! {
182 $(
183 $(#[$attr])*
184 fn [<on_$name>](mut self, [<on_$name>]: impl Into<EventHandler<Event<$event_data>>>) -> Self {
185 self.event_handlers_mut()
186 .insert($event_variant, EventHandlerType::$handler_variant([<on_$name>].into()));
187 self
188 }
189 )*
190 }
191 };
192}
193
194pub trait EventHandlersExt: Sized {
202 fn event_handlers_mut(&mut self) -> &mut EventHandlers;
204
205 fn event_handlers(mut self, event_handlers: EventHandlers) -> Self {
207 *self.event_handlers_mut() = event_handlers;
208 self
209 }
210
211 event_handlers! {
212 AccessibilityAction,
213 AccessibilityActionEventData;
214
215 accessibility_action => EventName::AccessibilityAction;
217 }
218
219 event_handlers! {
220 Mouse,
221 MouseEventData;
222
223 mouse_down => EventName::MouseDown;
225 mouse_up => EventName::MouseUp;
227 mouse_move => EventName::MouseMove;
229
230 }
231
232 event_handlers! {
233 Pointer,
234 PointerEventData;
235
236 global_pointer_up => EventName::GlobalPointerUp;
238 global_pointer_down => EventName::GlobalPointerDown;
240 global_pointer_move => EventName::GlobalPointerMove;
242
243 capture_global_pointer_move => EventName::CaptureGlobalPointerMove;
245 capture_global_pointer_up => EventName::CaptureGlobalPointerUp;
247 }
248
249 event_handlers! {
250 Keyboard,
251 KeyboardEventData;
252
253 key_down => EventName::KeyDown;
255 key_up => EventName::KeyUp;
257
258 global_key_down => EventName::GlobalKeyDown;
260 global_key_up => EventName::GlobalKeyUp;
262 }
263
264 event_handlers! {
265 Wheel,
266 WheelEventData;
267
268 wheel => EventName::Wheel;
270 }
271
272 event_handlers! {
273 Touch,
274 TouchEventData;
275
276 touch_cancel => EventName::TouchCancel;
278 touch_start => EventName::TouchStart;
280 touch_move => EventName::TouchMove;
282 touch_end => EventName::TouchEnd;
284 }
285
286 event_handlers! {
287 Pointer,
288 PointerEventData;
289
290 pointer_press => EventName::PointerPress;
292 pointer_down => EventName::PointerDown;
294 pointer_move => EventName::PointerMove;
296 pointer_enter => EventName::PointerEnter;
298 pointer_leave => EventName::PointerLeave;
300 pointer_over => EventName::PointerOver;
302 pointer_out => EventName::PointerOut;
304 }
305
306 event_handlers! {
307 File,
308 FileEventData;
309
310 file_drop => EventName::FileDrop;
312 global_file_hover => EventName::GlobalFileHover;
314 global_file_hover_cancelled => EventName::GlobalFileHoverCancelled;
316 }
317
318 event_handlers! {
319 ImePreedit,
320 ImePreeditEventData;
321
322 ime_preedit => EventName::ImePreedit;
324 }
325
326 fn on_sized(mut self, on_sized: impl Into<EventHandler<Event<SizedEventData>>>) -> Self
328 where
329 Self: LayoutExt,
330 {
331 self.event_handlers_mut()
332 .insert(EventName::Sized, EventHandlerType::Sized(on_sized.into()));
333 self.layout_mut().layout.has_layout_references = true;
334 self
335 }
336
337 fn on_visible(mut self, on_visible: impl Into<EventHandler<Event<VisibleEventData>>>) -> Self {
339 self.event_handlers_mut().insert(
340 EventName::Visible,
341 EventHandlerType::Visible(on_visible.into()),
342 );
343 self
344 }
345
346 fn on_hidden(mut self, on_hidden: impl Into<EventHandler<Event<VisibleEventData>>>) -> Self {
348 self.event_handlers_mut().insert(
349 EventName::Hidden,
350 EventHandlerType::Visible(on_hidden.into()),
351 );
352 self
353 }
354
355 fn on_styled(mut self, on_styled: impl Into<EventHandler<Event<StyledEventData>>>) -> Self {
357 self.event_handlers_mut().insert(
358 EventName::Styled,
359 EventHandlerType::Styled(on_styled.into()),
360 );
361 self
362 }
363
364 fn on_press(self, on_press: impl Into<EventHandler<Event<PressEventData>>>) -> Self
372 where
373 Self: AccessibilityExt,
374 {
375 let on_press = on_press.into();
376 self.a11y_builder(|builder| builder.add_action(AccessibilityAction::Click))
377 .on_accessibility_action({
378 let on_press = on_press.clone();
379 move |event: Event<AccessibilityActionEventData>| {
380 if event.action == AccessibilityAction::Click {
381 on_press.call(event.map(PressEventData::Accessibility));
382 }
383 }
384 })
385 .on_pointer_press({
386 let on_press = on_press.clone();
387 move |e: Event<PointerEventData>| {
388 let event = e.try_map(|d| match d {
389 PointerEventData::Mouse(m) if m.button == Some(MouseButton::Left) => {
390 Some(PressEventData::Mouse(m))
391 }
392 PointerEventData::Touch(t) => Some(PressEventData::Touch(t)),
393 _ => None,
394 });
395 if let Some(event) = event {
396 on_press.call(event);
397 }
398 }
399 })
400 .on_key_down(move |e: Event<KeyboardEventData>| {
401 if e.is_press_event() {
402 on_press.call(e.map(PressEventData::Keyboard))
403 }
404 })
405 }
406
407 fn on_secondary_down(
411 self,
412 on_secondary_down: impl Into<EventHandler<Event<PressEventData>>>,
413 ) -> Self {
414 let on_secondary_down = on_secondary_down.into();
415 self.on_pointer_down(move |e: Event<PointerEventData>| {
416 let event = e.try_map(|d| match d {
417 PointerEventData::Mouse(m) if m.button == Some(MouseButton::Right) => {
418 Some(PressEventData::Mouse(m))
419 }
420 _ => None,
421 });
422 if let Some(event) = event {
423 on_secondary_down.call(event);
424 }
425 })
426 }
427
428 fn on_all_press(self, on_press: impl Into<EventHandler<Event<PressEventData>>>) -> Self
434 where
435 Self: AccessibilityExt,
436 {
437 let on_press = on_press.into();
438 self.a11y_builder(|builder| builder.add_action(AccessibilityAction::Click))
439 .on_accessibility_action({
440 let on_press = on_press.clone();
441 move |event: Event<AccessibilityActionEventData>| {
442 if event.action == AccessibilityAction::Click {
443 on_press.call(event.map(PressEventData::Accessibility));
444 }
445 }
446 })
447 .on_pointer_press({
448 let on_press = on_press.clone();
449 move |e: Event<PointerEventData>| {
450 let event = e.map(|d| match d {
451 PointerEventData::Mouse(m) => PressEventData::Mouse(m),
452 PointerEventData::Touch(t) => PressEventData::Touch(t),
453 });
454 on_press.call(event);
455 }
456 })
457 .on_key_down(move |e: Event<KeyboardEventData>| {
458 if e.is_press_event() {
459 on_press.call(e.map(PressEventData::Keyboard))
460 }
461 })
462 }
463 fn on_focus_press(
469 self,
470 on_focus_press: impl Into<EventHandler<Event<FocusPressEventData>>>,
471 ) -> Self {
472 let on_focus_press = on_focus_press.into();
473 if cfg!(target_os = "android") {
474 self.on_pointer_press(move |e: Event<PointerEventData>| {
475 let event = e.try_map(|d| match d {
476 PointerEventData::Mouse(m) if m.button == Some(MouseButton::Left) => {
477 Some(FocusPressEventData::Mouse(m))
478 }
479 PointerEventData::Touch(t) => Some(FocusPressEventData::Touch(t)),
480 _ => None,
481 });
482 if let Some(event) = event {
483 on_focus_press.call(event);
484 }
485 })
486 } else {
487 self.on_pointer_down(move |e: Event<PointerEventData>| {
488 let event = e.try_map(|d| match d {
489 PointerEventData::Mouse(m) if m.button == Some(MouseButton::Left) => {
490 Some(FocusPressEventData::Mouse(m))
491 }
492 PointerEventData::Touch(t) => Some(FocusPressEventData::Touch(t)),
493 _ => None,
494 });
495 if let Some(event) = event {
496 on_focus_press.call(event);
497 }
498 })
499 }
500 }
501}
502
503#[derive(Debug, Clone, PartialEq)]
505pub enum FocusPressEventData {
506 Mouse(MouseEventData),
507 Touch(TouchEventData),
508}
509
510impl FocusPressEventData {
511 pub fn global_location(&self) -> CursorPoint {
512 match self {
513 Self::Mouse(m) => m.global_location,
514 Self::Touch(t) => t.global_location,
515 }
516 }
517
518 pub fn element_location(&self) -> CursorPoint {
519 match self {
520 Self::Mouse(m) => m.element_location,
521 Self::Touch(t) => t.element_location,
522 }
523 }
524
525 pub fn button(&self) -> Option<MouseButton> {
526 match self {
527 Self::Mouse(m) => m.button,
528 Self::Touch(_) => None,
529 }
530 }
531}
532
533#[derive(Debug, Clone, PartialEq)]
535pub enum PressEventData {
536 Accessibility(AccessibilityActionEventData),
537 Mouse(MouseEventData),
538 Keyboard(KeyboardEventData),
539 Touch(TouchEventData),
540}
541
542pub trait ContainerWithContentExt
544where
545 Self: LayoutExt,
546{
547 fn direction(mut self, direction: Direction) -> Self {
549 self.layout_mut().layout.direction = direction;
550 self
551 }
552 fn main_align(mut self, main_align: impl Into<Alignment>) -> Self {
554 self.layout_mut().layout.main_alignment = main_align.into();
555 self
556 }
557
558 fn cross_align(mut self, cross_align: impl Into<Alignment>) -> Self {
560 self.layout_mut().layout.cross_alignment = cross_align.into();
561 self
562 }
563
564 fn spacing(mut self, spacing: f32) -> Self {
566 self.layout_mut().layout.spacing = Length::new(spacing);
567 self
568 }
569
570 fn content(mut self, content: Content) -> Self {
572 self.layout_mut().layout.content = content;
573 self
574 }
575 fn center(mut self) -> Self {
577 self.layout_mut().layout.main_alignment = Alignment::Center;
578 self.layout_mut().layout.cross_alignment = Alignment::Center;
579
580 self
581 }
582
583 fn offset_x(mut self, offset_x: f32) -> Self {
585 self.layout_mut().layout.offset_x = Length::new(offset_x);
586 self
587 }
588
589 fn offset_y(mut self, offset_y: f32) -> Self {
591 self.layout_mut().layout.offset_y = Length::new(offset_y);
592 self
593 }
594
595 fn vertical(mut self) -> Self {
597 self.layout_mut().layout.direction = Direction::vertical();
598 self
599 }
600
601 fn horizontal(mut self) -> Self {
603 self.layout_mut().layout.direction = Direction::horizontal();
604 self
605 }
606}
607
608pub trait ContainerSizeExt
610where
611 Self: LayoutExt,
612{
613 fn width(mut self, width: impl Into<Size>) -> Self {
615 self.layout_mut().layout.width = width.into();
616 self
617 }
618
619 fn height(mut self, height: impl Into<Size>) -> Self {
621 self.layout_mut().layout.height = height.into();
622 self
623 }
624
625 fn expanded(mut self) -> Self {
627 self.layout_mut().layout.width = Size::fill();
628 self.layout_mut().layout.height = Size::fill();
629 self
630 }
631}
632
633impl<T: ContainerExt> ContainerSizeExt for T {}
634
635pub trait ContainerPositionExt
637where
638 Self: LayoutExt,
639{
640 fn position(mut self, position: impl Into<Position>) -> Self {
642 self.layout_mut().layout.position = position.into();
643 self
644 }
645
646 fn margin(mut self, margin: impl Into<Gaps>) -> Self {
648 self.layout_mut().layout.margin = margin.into();
649 self
650 }
651}
652
653impl<T: ContainerExt> ContainerPositionExt for T {}
654
655pub trait ContainerExt
657where
658 Self: LayoutExt,
659{
660 fn padding(mut self, padding: impl Into<Gaps>) -> Self {
662 self.layout_mut().layout.padding = padding.into();
663 self
664 }
665}
666
667pub trait ContainerConstraintsExt
669where
670 Self: LayoutExt,
671{
672 fn min_width(mut self, minimum_width: impl Into<Size>) -> Self {
674 self.layout_mut().layout.minimum_width = minimum_width.into();
675 self
676 }
677
678 fn min_height(mut self, minimum_height: impl Into<Size>) -> Self {
680 self.layout_mut().layout.minimum_height = minimum_height.into();
681 self
682 }
683
684 fn max_width(mut self, maximum_width: impl Into<Size>) -> Self {
686 self.layout_mut().layout.maximum_width = maximum_width.into();
687 self
688 }
689
690 fn max_height(mut self, maximum_height: impl Into<Size>) -> Self {
692 self.layout_mut().layout.maximum_height = maximum_height.into();
693 self
694 }
695
696 fn visible_width(mut self, visible_width: impl Into<VisibleSize>) -> Self {
698 self.layout_mut().layout.visible_width = visible_width.into();
699 self
700 }
701
702 fn visible_height(mut self, visible_height: impl Into<VisibleSize>) -> Self {
704 self.layout_mut().layout.visible_height = visible_height.into();
705 self
706 }
707}
708
709impl<T: ContainerExt> ContainerConstraintsExt for T {}
710
711pub trait LayoutExt
713where
714 Self: Sized,
715{
716 fn layout_mut(&mut self) -> &mut LayoutData;
718
719 fn layout(mut self, layout: LayoutData) -> Self {
721 *self.layout_mut() = layout;
722 self
723 }
724}
725
726pub trait ImageExt
728where
729 Self: LayoutExt,
730{
731 fn image_data_mut(&mut self) -> &mut ImageData;
733
734 fn image_data(mut self, image_data: ImageData) -> Self {
736 *self.image_data_mut() = image_data;
737 self
738 }
739
740 fn sampling_mode(mut self, sampling_mode: SamplingMode) -> Self {
742 self.image_data_mut().sampling_mode = sampling_mode;
743 self
744 }
745
746 fn aspect_ratio(mut self, aspect_ratio: AspectRatio) -> Self {
748 self.image_data_mut().aspect_ratio = aspect_ratio;
749 self
750 }
751
752 fn image_cover(mut self, image_cover: ImageCover) -> Self {
754 self.image_data_mut().image_cover = image_cover;
755 self
756 }
757
758 fn snap_to_grid(mut self, snap_to_grid: bool) -> Self {
760 self.image_data_mut().snap_to_grid = snap_to_grid;
761 self
762 }
763
764 fn blur(mut self, blur: f32) -> Self {
766 self.image_data_mut().blur = blur;
767 self
768 }
769}
770
771pub trait AccessibilityExt: Sized {
773 fn accessibility_data_mut(&mut self) -> &mut AccessibilityData;
775
776 fn accessibility(mut self, accessibility: AccessibilityData) -> Self {
778 *self.accessibility_data_mut() = accessibility;
779 self
780 }
781
782 fn a11y_id(mut self, a11y_id: impl Into<Option<AccessibilityId>>) -> Self {
784 self.accessibility_data_mut().a11y_id = a11y_id.into();
785 self
786 }
787
788 fn a11y_enabled(mut self, enabled: impl Into<bool>) -> Self {
790 let builder = &mut self.accessibility_data_mut().builder;
791 if enabled.into() {
792 builder.clear_disabled();
793 } else {
794 builder.set_disabled();
795 }
796 self
797 }
798
799 fn a11y_focusable(mut self, a11y_focusable: impl Into<Focusable>) -> Self {
801 self.accessibility_data_mut().a11y_focusable = a11y_focusable.into();
802 self
803 }
804
805 fn a11y_auto_focus(mut self, a11y_auto_focus: impl Into<bool>) -> Self {
807 self.accessibility_data_mut().a11y_auto_focus = a11y_auto_focus.into();
808 self
809 }
810
811 fn a11y_member_of(mut self, a11y_member_of: impl Into<AccessibilityId>) -> Self {
813 self.accessibility_data_mut()
814 .builder
815 .set_member_of(a11y_member_of.into());
816 self
817 }
818
819 fn a11y_role(mut self, a11y_role: impl Into<AccessibilityRole>) -> Self {
821 self.accessibility_data_mut()
822 .builder
823 .set_role(a11y_role.into());
824 self
825 }
826
827 fn a11y_alt(mut self, value: impl Into<String>) -> Self {
829 self.accessibility_data_mut().builder.set_label(value);
830 self
831 }
832
833 fn a11y_builder(mut self, with: impl FnOnce(&mut accesskit::Node)) -> Self {
835 with(&mut self.accessibility_data_mut().builder);
836 self
837 }
838}
839
840pub trait TextStyleExt
842where
843 Self: Sized,
844{
845 fn text_style_data_mut(&mut self) -> &mut TextStyleData;
847
848 fn text_style(mut self, data: TextStyleData) -> Self {
850 *self.text_style_data_mut() = data;
851 self
852 }
853
854 fn color(mut self, color: impl Into<Fill>) -> Self {
856 self.text_style_data_mut().color = Some(color.into());
857 self
858 }
859
860 fn text_align(mut self, text_align: impl Into<TextAlign>) -> Self {
862 self.text_style_data_mut().text_align = Some(text_align.into());
863 self
864 }
865
866 fn font_size(mut self, font_size: impl Into<FontSize>) -> Self {
868 self.text_style_data_mut().font_size = Some(font_size.into());
869 self
870 }
871
872 fn letter_spacing(mut self, letter_spacing: impl Into<LetterSpacing>) -> Self {
874 self.text_style_data_mut().letter_spacing = Some(letter_spacing.into());
875 self
876 }
877
878 fn font_feature(mut self, font_feature: impl Into<FontFeature>) -> Self {
880 self.text_style_data_mut()
881 .font_features
882 .push(font_feature.into());
883 self
884 }
885
886 fn font_tabular(self) -> Self {
888 self.font_feature(FontFeature::TABULAR_NUMBERS)
889 }
890
891 fn font_small_caps(self) -> Self {
893 self.font_feature(FontFeature::SMALL_CAPS)
894 }
895
896 fn font_slashed_zero(self) -> Self {
898 self.font_feature(FontFeature::SLASHED_ZERO)
899 }
900
901 fn font_fractions(self) -> Self {
903 self.font_feature(FontFeature::FRACTIONS)
904 }
905
906 fn font_no_ligatures(self) -> Self {
908 self.font_feature(FontFeature::NO_LIGATURES)
909 }
910
911 fn font_family(mut self, font_family: impl Into<Cow<'static, str>>) -> Self {
913 self.text_style_data_mut()
914 .font_families
915 .push(font_family.into());
916 self
917 }
918
919 fn font_slant(mut self, font_slant: impl Into<FontSlant>) -> Self {
921 self.text_style_data_mut().font_slant = Some(font_slant.into());
922 self
923 }
924
925 fn font_weight(mut self, font_weight: impl Into<FontWeight>) -> Self {
927 self.text_style_data_mut().font_weight = Some(font_weight.into());
928 self
929 }
930
931 fn font_width(mut self, font_width: impl Into<FontWidth>) -> Self {
933 self.text_style_data_mut().font_width = Some(font_width.into());
934 self
935 }
936
937 fn text_height(mut self, text_height: impl Into<TextHeightBehavior>) -> Self {
939 self.text_style_data_mut().text_height = Some(text_height.into());
940 self
941 }
942
943 fn text_shadow(mut self, text_shadow: impl Into<TextShadow>) -> Self {
945 self.text_style_data_mut()
946 .text_shadows
947 .get_or_insert_default()
948 .push(text_shadow.into());
949 self
950 }
951
952 fn text_shadows(mut self, text_shadows: impl IntoIterator<Item = TextShadow>) -> Self {
954 self.text_style_data_mut().text_shadows = Some(text_shadows.into_iter().collect());
955 self
956 }
957
958 fn text_decoration(mut self, text_decoration: impl Into<TextDecoration>) -> Self {
960 self.text_style_data_mut().text_decoration = Some(text_decoration.into());
961 self
962 }
963}
964
965pub trait TextOverflowExt: TextStyleExt {
967 fn text_overflow(mut self, text_overflow: impl Into<TextOverflow>) -> Self {
969 self.text_style_data_mut().text_overflow = Some(text_overflow.into());
970 self
971 }
972}
973
974pub trait BackgroundExt: StyleExt {
976 fn background(mut self, background: impl Into<Fill>) -> Self {
978 self.style_mut().background = background.into();
979 self
980 }
981}
982
983pub trait StyleExt
985where
986 Self: Sized,
987{
988 fn style_mut(&mut self) -> &mut StyleState;
990
991 fn style(mut self, style: StyleState) -> Self {
993 *self.style_mut() = style;
994 self
995 }
996}
997
998pub trait DecorationExt: StyleExt {
1000 fn border(mut self, border: impl Into<Option<Border>>) -> Self {
1002 if let Some(border) = border.into() {
1003 self.style_mut().borders.push(border);
1004 }
1005 self
1006 }
1007
1008 fn shadow(mut self, shadow: impl Into<Shadow>) -> Self {
1010 self.style_mut().shadows.push(shadow.into());
1011 self
1012 }
1013
1014 fn corner_radius(mut self, corner_radius: impl Into<CornerRadius>) -> Self {
1016 self.style_mut().corner_radius = corner_radius.into();
1017 self
1018 }
1019}
1020
1021impl<T: DecorationExt> CornerRadiusExt for T {
1022 fn with_corner_radius(mut self, corner_radius: f32) -> Self {
1023 self.style_mut().corner_radius = CornerRadius::new_all(corner_radius);
1024 self
1025 }
1026}
1027
1028pub trait CornerRadiusExt: Sized {
1030 fn with_corner_radius(self, corner_radius: f32) -> Self;
1032
1033 fn rounded_none(self) -> Self {
1035 self.with_corner_radius(0.)
1036 }
1037
1038 fn rounded(self) -> Self {
1040 self.with_corner_radius(6.)
1041 }
1042
1043 fn rounded_sm(self) -> Self {
1045 self.with_corner_radius(4.)
1046 }
1047
1048 fn rounded_md(self) -> Self {
1050 self.with_corner_radius(6.)
1051 }
1052
1053 fn rounded_lg(self) -> Self {
1055 self.with_corner_radius(8.)
1056 }
1057
1058 fn rounded_xl(self) -> Self {
1060 self.with_corner_radius(12.)
1061 }
1062
1063 fn rounded_2xl(self) -> Self {
1065 self.with_corner_radius(16.)
1066 }
1067
1068 fn rounded_3xl(self) -> Self {
1070 self.with_corner_radius(24.)
1071 }
1072
1073 fn rounded_4xl(self) -> Self {
1075 self.with_corner_radius(32.)
1076 }
1077
1078 fn rounded_full(self) -> Self {
1080 self.with_corner_radius(99.)
1081 }
1082}
1083
1084pub trait MaybeExt
1086where
1087 Self: Sized,
1088{
1089 fn maybe(self, bool: impl Into<bool>, then: impl FnOnce(Self) -> Self) -> Self {
1091 if bool.into() { then(self) } else { self }
1092 }
1093
1094 fn map<T>(self, data: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self {
1096 if let Some(data) = data {
1097 then(self, data)
1098 } else {
1099 self
1100 }
1101 }
1102}
1103
1104pub trait LayerExt
1106where
1107 Self: Sized,
1108{
1109 fn layer_mut(&mut self) -> &mut Layer;
1111
1112 fn layer(mut self, layer: impl Into<Layer>) -> Self {
1132 *self.layer_mut() = layer.into();
1133 self
1134 }
1135}
1136
1137pub trait ScrollableExt
1138where
1139 Self: Sized,
1140{
1141 fn effect_mut(&mut self) -> &mut EffectData;
1143
1144 fn scrollable(mut self, scrollable: impl Into<bool>) -> Self {
1147 self.effect_mut().scrollable = scrollable.into();
1148 self
1149 }
1150}
1151
1152pub trait InteractiveExt
1154where
1155 Self: Sized,
1156{
1157 fn effect_mut(&mut self) -> &mut EffectData;
1159
1160 fn interactive(mut self, interactive: impl Into<Interactive>) -> Self {
1162 self.effect_mut().interactive = interactive.into();
1163 self
1164 }
1165}
1166
1167pub trait EffectExt: Sized {
1169 fn effect_mut(&mut self) -> &mut EffectData;
1171
1172 fn cursor(mut self, cursor: impl Into<Option<CursorIcon>>) -> Self {
1177 self.effect_mut().cursor = cursor.into();
1178 self
1179 }
1180
1181 fn cursor_visible(mut self, visible: impl Into<Option<bool>>) -> Self {
1185 self.effect_mut().cursor_visible = visible.into();
1186 self
1187 }
1188
1189 fn effect(mut self, effect: EffectData) -> Self {
1191 *self.effect_mut() = effect;
1192 self
1193 }
1194
1195 fn overflow(mut self, overflow: impl Into<Overflow>) -> Self {
1197 self.effect_mut().overflow = overflow.into();
1198 self
1199 }
1200
1201 fn backdrop_blur(mut self, blur: f32) -> Self {
1203 self.effect_mut().blur = Some(blur);
1204 self
1205 }
1206
1207 fn rotation(mut self, rotation: f32) -> Self {
1209 self.effect_mut().rotation = Some(rotation);
1210 self
1211 }
1212
1213 fn opacity(mut self, opacity: f32) -> Self {
1215 self.effect_mut().opacity = Some(opacity);
1216 self
1217 }
1218
1219 fn scale(mut self, scale: impl Into<Scale>) -> Self {
1221 self.effect_mut().scale = Some(scale.into());
1222 self
1223 }
1224
1225 fn transform_origin(mut self, transform_origin: impl Into<TransformOrigin>) -> Self {
1229 self.effect_mut().transform_origin = transform_origin.into();
1230 self
1231 }
1232}