1use std::{
2 borrow::Cow,
3 hash::{
4 Hash,
5 Hasher,
6 },
7};
8
9use paste::paste;
10use rustc_hash::{
11 FxHashMap,
12 FxHasher,
13};
14use torin::{
15 content::Content,
16 gaps::Gaps,
17 prelude::{
18 Alignment,
19 Direction,
20 Length,
21 Position,
22 VisibleSize,
23 },
24 size::Size,
25};
26
27use crate::{
28 data::{
29 AccessibilityData,
30 EffectData,
31 LayoutData,
32 Overflow,
33 TextStyleData,
34 },
35 diff_key::DiffKey,
36 element::{
37 Element,
38 EventHandlerType,
39 },
40 elements::image::{
41 AspectRatio,
42 ImageCover,
43 ImageData,
44 SamplingMode,
45 },
46 event_handler::EventHandler,
47 events::{
48 data::{
49 Event,
50 KeyboardEventData,
51 MouseEventData,
52 SizedEventData,
53 WheelEventData,
54 },
55 name::EventName,
56 },
57 layers::Layer,
58 prelude::*,
59 style::{
60 font_size::FontSize,
61 font_slant::FontSlant,
62 font_weight::FontWeight,
63 font_width::FontWidth,
64 scale::Scale,
65 text_height::TextHeightBehavior,
66 text_overflow::TextOverflow,
67 text_shadow::TextShadow,
68 },
69};
70
71pub trait ChildrenExt: Sized {
73 fn get_children(&mut self) -> &mut Vec<Element>;
84
85 fn children(mut self, children: impl IntoIterator<Item = Element>) -> Self {
92 self.get_children().extend(children);
93 self
94 }
95
96 fn maybe_child<C: IntoElement>(mut self, child: Option<C>) -> Self {
103 if let Some(child) = child {
104 self.get_children().push(child.into_element());
105 }
106 self
107 }
108
109 fn child<C: IntoElement>(mut self, child: C) -> Self {
116 self.get_children().push(child.into_element());
117 self
118 }
119}
120
121pub trait KeyExt: Sized {
122 fn write_key(&mut self) -> &mut DiffKey;
123
124 fn key(mut self, key: impl Hash) -> Self {
125 let mut hasher = FxHasher::default();
126 key.hash(&mut hasher);
127 *self.write_key() = DiffKey::U64(hasher.finish());
128 self
129 }
130}
131
132pub trait ListExt {
133 fn with(self, other: Self) -> Self;
134}
135
136impl<T> ListExt for Vec<T> {
137 fn with(mut self, other: Self) -> Self {
138 self.extend(other);
139 self
140 }
141}
142
143macro_rules! event_handlers {
144 (
145 $handler_variant:ident, $event_data:ty;
146 $(
147 $name:ident => $event_variant:expr ;
148 )*
149 ) => {
150 paste! {
151 $(
152 fn [<on_$name>](mut self, [<on_$name>]: impl Into<EventHandler<Event<$event_data>>>) -> Self {
153 self.get_event_handlers()
154 .insert($event_variant, EventHandlerType::$handler_variant([<on_$name>].into()));
155 self
156 }
157 )*
158 }
159 };
160}
161
162pub trait EventHandlersExt: Sized {
163 fn get_event_handlers(&mut self) -> &mut FxHashMap<EventName, EventHandlerType>;
164
165 fn with_event_handlers(
166 mut self,
167 event_handlers: FxHashMap<EventName, EventHandlerType>,
168 ) -> Self {
169 *self.get_event_handlers() = event_handlers;
170 self
171 }
172
173 event_handlers! {
174 Mouse,
175 MouseEventData;
176
177 mouse_down => EventName::MouseDown;
178 mouse_up => EventName::MouseUp;
179 mouse_move => EventName::MouseMove;
180
181 }
182
183 event_handlers! {
184 Pointer,
185 PointerEventData;
186
187 global_pointer_press => EventName::GlobalPointerPress;
188 global_pointer_down => EventName::GlobalPointerDown;
189 global_pointer_move => EventName::GlobalPointerMove;
190
191 capture_global_pointer_move => EventName::CaptureGlobalPointerMove;
192 capture_global_pointer_press => EventName::CaptureGlobalPointerPress;
193 }
194
195 event_handlers! {
196 Keyboard,
197 KeyboardEventData;
198
199 key_down => EventName::KeyDown;
200 key_up => EventName::KeyUp;
201
202 global_key_down => EventName::GlobalKeyDown;
203 global_key_up => EventName::GlobalKeyUp;
204 }
205
206 event_handlers! {
207 Wheel,
208 WheelEventData;
209
210 wheel => EventName::Wheel;
211 }
212
213 event_handlers! {
214 Touch,
215 TouchEventData;
216
217 touch_cancel => EventName::TouchCancel;
218 touch_start => EventName::TouchStart;
219 touch_move => EventName::TouchMove;
220 touch_end => EventName::TouchEnd;
221 }
222
223 event_handlers! {
224 Pointer,
225 PointerEventData;
226
227 pointer_press => EventName::PointerPress;
228 pointer_down => EventName::PointerDown;
229 pointer_enter => EventName::PointerEnter;
230 pointer_leave => EventName::PointerLeave;
231 }
232
233 event_handlers! {
234 File,
235 FileEventData;
236
237 file_drop => EventName::FileDrop;
238 global_file_hover => EventName::GlobalFileHover;
239 global_file_hover_cancelled => EventName::GlobalFileHoverCancelled;
240 }
241
242 event_handlers! {
243 ImePreedit,
244 ImePreeditEventData;
245
246 ime_preedit => EventName::ImePreedit;
247 }
248
249 fn on_sized(mut self, on_sized: impl Into<EventHandler<Event<SizedEventData>>>) -> Self
250 where
251 Self: LayoutExt,
252 {
253 self.get_event_handlers()
254 .insert(EventName::Sized, EventHandlerType::Sized(on_sized.into()));
255 self.get_layout().layout.has_layout_references = true;
256 self
257 }
258
259 fn on_press(self, on_press: impl Into<EventHandler<Event<PressEventData>>>) -> Self {
266 let on_press = on_press.into();
267 self.on_pointer_press({
268 let on_press = on_press.clone();
269 move |e: Event<PointerEventData>| {
270 let event = e.try_map(|d| match d {
271 PointerEventData::Mouse(m) if m.button == Some(MouseButton::Left) => {
272 Some(PressEventData::Mouse(m))
273 }
274 PointerEventData::Touch(t) => Some(PressEventData::Touch(t)),
275 _ => None,
276 });
277 if let Some(event) = event {
278 on_press.call(event);
279 }
280 }
281 })
282 .on_key_down({
283 let on_press = on_press.clone();
284 move |e: Event<KeyboardEventData>| {
285 if Focus::is_pressed(&e) {
286 on_press.call(e.map(PressEventData::Keyboard))
287 }
288 }
289 })
290 }
291
292 fn on_secondary_press(
296 self,
297 on_pointer_press: impl Into<EventHandler<Event<PressEventData>>>,
298 ) -> Self {
299 let on_pointer_press = on_pointer_press.into();
300 self.on_pointer_press({
301 let on_pointer_press = on_pointer_press.clone();
302 move |e: Event<PointerEventData>| {
303 let event = e.try_map(|d| match d {
304 PointerEventData::Mouse(m) if m.button == Some(MouseButton::Right) => {
305 Some(PressEventData::Mouse(m))
306 }
307 _ => None,
308 });
309 if let Some(event) = event {
310 on_pointer_press.call(event);
311 }
312 }
313 })
314 }
315
316 fn on_all_press(self, on_press: impl Into<EventHandler<Event<PressEventData>>>) -> Self {
321 let on_press = on_press.into();
322 self.on_pointer_press({
323 let on_press = on_press.clone();
324 move |e: Event<PointerEventData>| {
325 let event = e.try_map(|d| match d {
326 PointerEventData::Mouse(m) => Some(PressEventData::Mouse(m)),
327 PointerEventData::Touch(t) => Some(PressEventData::Touch(t)),
328 });
329 if let Some(event) = event {
330 on_press.call(event);
331 }
332 }
333 })
334 .on_key_down({
335 let on_press = on_press.clone();
336 move |e: Event<KeyboardEventData>| {
337 if Focus::is_pressed(&e) {
338 on_press.call(e.map(PressEventData::Keyboard))
339 }
340 }
341 })
342 }
343}
344
345#[derive(Debug, Clone, PartialEq)]
346pub enum PressEventData {
347 Mouse(MouseEventData),
348 Keyboard(KeyboardEventData),
349 Touch(TouchEventData),
350}
351
352pub trait ContainerWithContentExt
353where
354 Self: LayoutExt,
355{
356 fn direction(mut self, direction: Direction) -> Self {
357 self.get_layout().layout.direction = direction;
358 self
359 }
360 fn main_align(mut self, main_align: Alignment) -> Self {
361 self.get_layout().layout.main_alignment = main_align;
362 self
363 }
364
365 fn cross_align(mut self, cross_align: Alignment) -> Self {
366 self.get_layout().layout.cross_alignment = cross_align;
367 self
368 }
369
370 fn spacing(mut self, spacing: impl Into<f32>) -> Self {
371 self.get_layout().layout.spacing = Length::new(spacing.into());
372 self
373 }
374
375 fn content(mut self, content: Content) -> Self {
376 self.get_layout().layout.content = content;
377 self
378 }
379 fn center(mut self) -> Self {
380 self.get_layout().layout.main_alignment = Alignment::Center;
381 self.get_layout().layout.cross_alignment = Alignment::Center;
382
383 self
384 }
385
386 fn offset_x(mut self, offset_x: impl Into<f32>) -> Self {
387 self.get_layout().layout.offset_x = Length::new(offset_x.into());
388 self
389 }
390
391 fn offset_y(mut self, offset_y: impl Into<f32>) -> Self {
392 self.get_layout().layout.offset_y = Length::new(offset_y.into());
393 self
394 }
395
396 fn vertical(mut self) -> Self {
397 self.get_layout().layout.direction = Direction::vertical();
398 self
399 }
400
401 fn horizontal(mut self) -> Self {
402 self.get_layout().layout.direction = Direction::horizontal();
403 self
404 }
405}
406
407pub trait ContainerSizeExt
408where
409 Self: LayoutExt,
410{
411 fn width(mut self, width: impl Into<Size>) -> Self {
412 self.get_layout().layout.width = width.into();
413 self
414 }
415
416 fn height(mut self, height: impl Into<Size>) -> Self {
417 self.get_layout().layout.height = height.into();
418 self
419 }
420
421 fn expanded(mut self) -> Self {
423 self.get_layout().layout.width = Size::fill();
424 self.get_layout().layout.height = Size::fill();
425 self
426 }
427}
428
429impl<T: ContainerExt> ContainerSizeExt for T {}
430
431pub trait ContainerExt
432where
433 Self: LayoutExt,
434{
435 fn position(mut self, position: impl Into<Position>) -> Self {
436 self.get_layout().layout.position = position.into();
437 self
438 }
439
440 fn padding(mut self, padding: impl Into<Gaps>) -> Self {
441 self.get_layout().layout.padding = padding.into();
442 self
443 }
444
445 fn margin(mut self, margin: impl Into<Gaps>) -> Self {
446 self.get_layout().layout.margin = margin.into();
447 self
448 }
449
450 fn min_width(mut self, minimum_width: impl Into<Size>) -> Self {
451 self.get_layout().layout.minimum_width = minimum_width.into();
452 self
453 }
454
455 fn min_height(mut self, minimum_height: impl Into<Size>) -> Self {
456 self.get_layout().layout.minimum_height = minimum_height.into();
457 self
458 }
459
460 fn max_width(mut self, maximum_width: impl Into<Size>) -> Self {
461 self.get_layout().layout.maximum_width = maximum_width.into();
462 self
463 }
464
465 fn max_height(mut self, maximum_height: impl Into<Size>) -> Self {
466 self.get_layout().layout.maximum_height = maximum_height.into();
467 self
468 }
469
470 fn visible_width(mut self, visible_width: impl Into<VisibleSize>) -> Self {
471 self.get_layout().layout.visible_width = visible_width.into();
472 self
473 }
474
475 fn visible_height(mut self, visible_height: impl Into<VisibleSize>) -> Self {
476 self.get_layout().layout.visible_height = visible_height.into();
477 self
478 }
479}
480
481pub trait LayoutExt
482where
483 Self: Sized,
484{
485 fn get_layout(&mut self) -> &mut LayoutData;
486
487 fn layout(mut self, layout: LayoutData) -> Self {
488 *self.get_layout() = layout;
489 self
490 }
491}
492
493pub trait ImageExt
494where
495 Self: LayoutExt,
496{
497 fn get_image_data(&mut self) -> &mut ImageData;
498
499 fn image_data(mut self, image_data: ImageData) -> Self {
500 *self.get_image_data() = image_data;
501 self
502 }
503
504 fn sampling_mode(mut self, sampling_mode: SamplingMode) -> Self {
505 self.get_image_data().sampling_mode = sampling_mode;
506 self
507 }
508
509 fn aspect_ratio(mut self, aspect_ratio: AspectRatio) -> Self {
510 self.get_image_data().aspect_ratio = aspect_ratio;
511 self
512 }
513
514 fn image_cover(mut self, image_cover: ImageCover) -> Self {
515 self.get_image_data().image_cover = image_cover;
516 self
517 }
518}
519
520pub trait AccessibilityExt: Sized {
521 fn get_accessibility_data(&mut self) -> &mut AccessibilityData;
522
523 fn accessibility(mut self, accessibility: AccessibilityData) -> Self {
524 *self.get_accessibility_data() = accessibility;
525 self
526 }
527
528 fn a11y_id(mut self, a11y_id: impl Into<Option<AccessibilityId>>) -> Self {
529 self.get_accessibility_data().a11y_id = a11y_id.into();
530 self
531 }
532
533 fn a11y_focusable(mut self, a11y_focusable: impl Into<Focusable>) -> Self {
534 self.get_accessibility_data().a11y_focusable = a11y_focusable.into();
535 self
536 }
537
538 fn a11y_auto_focus(mut self, a11y_auto_focus: impl Into<bool>) -> Self {
539 self.get_accessibility_data().a11y_auto_focus = a11y_auto_focus.into();
540 self
541 }
542
543 fn a11y_member_of(mut self, a11y_member_of: impl Into<AccessibilityId>) -> Self {
544 self.get_accessibility_data()
545 .builder
546 .set_member_of(a11y_member_of.into());
547 self
548 }
549
550 fn a11y_role(mut self, a11y_role: impl Into<AccessibilityRole>) -> Self {
551 self.get_accessibility_data()
552 .builder
553 .set_role(a11y_role.into());
554 self
555 }
556
557 fn a11y_alt(mut self, value: impl Into<Box<str>>) -> Self {
558 self.get_accessibility_data().builder.set_label(value);
559 self
560 }
561
562 fn a11y_builder(mut self, with: impl FnOnce(&mut accesskit::Node)) -> Self {
563 with(&mut self.get_accessibility_data().builder);
564 self
565 }
566}
567
568pub trait TextStyleExt
569where
570 Self: Sized,
571{
572 fn get_text_style_data(&mut self) -> &mut TextStyleData;
573
574 fn color(mut self, color: impl Into<Color>) -> Self {
575 self.get_text_style_data().color = Some(color.into());
576 self
577 }
578
579 fn text_align(mut self, text_align: impl Into<TextAlign>) -> Self {
580 self.get_text_style_data().text_align = Some(text_align.into());
581 self
582 }
583
584 fn font_size(mut self, font_size: impl Into<FontSize>) -> Self {
585 self.get_text_style_data().font_size = Some(font_size.into());
586 self
587 }
588
589 fn font_family(mut self, font_family: impl Into<Cow<'static, str>>) -> Self {
590 self.get_text_style_data()
591 .font_families
592 .push(font_family.into());
593 self
594 }
595
596 fn font_slant(mut self, font_slant: impl Into<FontSlant>) -> Self {
597 self.get_text_style_data().font_slant = Some(font_slant.into());
598 self
599 }
600
601 fn font_weight(mut self, font_weight: impl Into<FontWeight>) -> Self {
602 self.get_text_style_data().font_weight = Some(font_weight.into());
603 self
604 }
605
606 fn font_width(mut self, font_width: impl Into<FontWidth>) -> Self {
607 self.get_text_style_data().font_width = Some(font_width.into());
608 self
609 }
610
611 fn text_height(mut self, text_height: impl Into<TextHeightBehavior>) -> Self {
612 self.get_text_style_data().text_height = Some(text_height.into());
613 self
614 }
615
616 fn text_overflow(mut self, text_overflow: impl Into<TextOverflow>) -> Self {
617 self.get_text_style_data().text_overflow = Some(text_overflow.into());
618 self
619 }
620
621 fn text_shadow(mut self, text_shadow: impl Into<TextShadow>) -> Self {
622 self.get_text_style_data()
623 .text_shadows
624 .push(text_shadow.into());
625 self
626 }
627}
628
629pub trait StyleExt
630where
631 Self: Sized,
632{
633 fn get_style(&mut self) -> &mut StyleState;
634
635 fn background<S: Into<Color>>(mut self, background: S) -> Self {
636 self.get_style().background = Fill::Color(background.into());
637 self
638 }
639
640 fn background_conic_gradient<S: Into<ConicGradient>>(mut self, background: S) -> Self {
641 self.get_style().background = Fill::ConicGradient(Box::new(background.into()));
642 self
643 }
644
645 fn background_linear_gradient<S: Into<LinearGradient>>(mut self, background: S) -> Self {
646 self.get_style().background = Fill::LinearGradient(Box::new(background.into()));
647 self
648 }
649
650 fn background_radial_gradient<S: Into<RadialGradient>>(mut self, background: S) -> Self {
651 self.get_style().background = Fill::RadialGradient(Box::new(background.into()));
652 self
653 }
654
655 fn border(mut self, border: impl Into<Option<Border>>) -> Self {
656 if let Some(border) = border.into() {
657 self.get_style().borders.push(border);
658 }
659 self
660 }
661
662 fn shadow(mut self, shadow: impl Into<Shadow>) -> Self {
663 self.get_style().shadows.push(shadow.into());
664 self
665 }
666
667 fn corner_radius(mut self, corner_radius: impl Into<CornerRadius>) -> Self {
668 self.get_style().corner_radius = corner_radius.into();
669 self
670 }
671}
672
673impl<T: StyleExt> CornerRadiusExt for T {
674 fn with_corner_radius(mut self, corner_radius: f32) -> Self {
675 self.get_style().corner_radius = CornerRadius::new_all(corner_radius);
676 self
677 }
678}
679
680pub trait CornerRadiusExt: Sized {
681 fn with_corner_radius(self, corner_radius: f32) -> Self;
682
683 fn rounded_none(self) -> Self {
685 self.with_corner_radius(0.)
686 }
687
688 fn rounded(self) -> Self {
690 self.with_corner_radius(6.)
691 }
692
693 fn rounded_sm(self) -> Self {
695 self.with_corner_radius(4.)
696 }
697
698 fn rounded_md(self) -> Self {
700 self.with_corner_radius(6.)
701 }
702
703 fn rounded_lg(self) -> Self {
705 self.with_corner_radius(8.)
706 }
707
708 fn rounded_xl(self) -> Self {
710 self.with_corner_radius(12.)
711 }
712
713 fn rounded_2xl(self) -> Self {
715 self.with_corner_radius(16.)
716 }
717
718 fn rounded_3xl(self) -> Self {
720 self.with_corner_radius(24.)
721 }
722
723 fn rounded_4xl(self) -> Self {
725 self.with_corner_radius(32.)
726 }
727
728 fn rounded_full(self) -> Self {
730 self.with_corner_radius(99.)
731 }
732}
733
734pub trait MaybeExt
735where
736 Self: Sized,
737{
738 fn maybe(self, bool: impl Into<bool>, then: impl FnOnce(Self) -> Self) -> Self {
739 if bool.into() { then(self) } else { self }
740 }
741
742 fn map<T>(self, data: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self {
743 if let Some(data) = data {
744 then(self, data)
745 } else {
746 self
747 }
748 }
749}
750
751pub trait LayerExt
752where
753 Self: Sized,
754{
755 fn get_layer(&mut self) -> &mut Layer;
756
757 fn layer(mut self, layer: impl Into<Layer>) -> Self {
758 *self.get_layer() = layer.into();
759 self
760 }
761}
762
763pub trait ScrollableExt
764where
765 Self: Sized,
766{
767 fn get_effect(&mut self) -> &mut EffectData;
768
769 fn scrollable(mut self, scrollable: impl Into<bool>) -> Self {
770 self.get_effect().scrollable = scrollable.into();
771 self
772 }
773}
774
775pub trait InteractiveExt
776where
777 Self: Sized,
778{
779 fn get_effect(&mut self) -> &mut EffectData;
780
781 fn interactive(mut self, interactive: impl Into<Interactive>) -> Self {
782 self.get_effect().interactive = interactive.into();
783 self
784 }
785}
786
787pub trait EffectExt: Sized {
788 fn get_effect(&mut self) -> &mut EffectData;
789
790 fn effect(mut self, effect: EffectData) -> Self {
791 *self.get_effect() = effect;
792 self
793 }
794
795 fn overflow(mut self, overflow: impl Into<Overflow>) -> Self {
796 self.get_effect().overflow = overflow.into();
797 self
798 }
799
800 fn blur(mut self, blur: impl Into<f32>) -> Self {
801 self.get_effect().blur = Some(blur.into());
802 self
803 }
804
805 fn rotation(mut self, rotation: impl Into<f32>) -> Self {
806 self.get_effect().rotation = Some(rotation.into());
807 self
808 }
809
810 fn opacity(mut self, opacity: impl Into<f32>) -> Self {
811 self.get_effect().opacity = Some(opacity.into());
812 self
813 }
814
815 fn scale(mut self, scale: impl Into<Scale>) -> Self {
816 self.get_effect().scale = Some(scale.into());
817 self
818 }
819}