1use core::cell::Cell;
4use core::time::Duration;
5
6use crate::pod::{ActionScope, CursorShape, DragEvent, FrameScope};
7use crate::style::{AlignItems, Color, Easing, Edges, LoopMode, Style, StyleRefinement, Styled};
8
9use super::div_node::{
10 duration_to_ms, pack_visual_flags, DivInteractivity, DivNode, DivRareHandlers, DivStateStyles,
11 DivVisual, EnterFrom, TransitionSpec, FOCUS_BORDER_NONE, NO_FOCUS_BORDER_DIV_VISUAL,
12};
13use super::{Element, ElementNode, IntoElement};
14
15impl<'a> IntoElement<'a> for Div<'a> {
16 #[inline(always)]
17 fn into_element(self, cx: &'a FrameScope) -> Element<'a> {
18 let has_state_styles = self.text_color.is_some()
19 || self.text_size.is_some()
20 || self.hover_style.is_some()
21 || self.active_style.is_some()
22 || self.focus_style.is_some();
23
24 let alloc_visual = |state_styles: Option<&'a DivStateStyles<'a>>,
25 transition: Option<&'a TransitionSpec>|
26 -> &'a DivVisual<'a> {
27 let (pos_x, pos_y) = self.pos.unwrap_or((0, 0));
28 let (bg_grad_to, bg_grad_horiz) = self.bg_grad.unwrap_or((Color::TRANSPARENT, false));
29 let (focus_border_w, focus_border_color) = match self.focus_border {
30 Some((w, c)) => (w.min(254), c),
31 None => (FOCUS_BORDER_NONE, Color::TRANSPARENT),
32 };
33 let (shadow_blur, shadow_ox, shadow_oy, shadow_color) =
34 self.shadow.unwrap_or((0, 0, 0, Color::TRANSPARENT));
35 let extra_flags = pack_visual_flags(
36 self.pos.is_some(),
37 self.scrollable_max.is_some(),
38 self.bg_grad.is_some(),
39 bg_grad_horiz,
40 self.items_baseline,
41 self.align_self,
42 );
43 &*cx.arena().alloc_temp(DivVisual {
44 state_styles,
45 transition,
46 margin: self.margin,
47 extra_padding: self.extra_padding,
48 bg_grad_to,
49 focus_border_color,
50 shadow_color,
51 min_w: self.min_w,
52 max_w: self.max_w,
53 min_h: self.min_h,
54 max_h: self.max_h,
55 pos_x,
56 pos_y,
57 raw_scrollable_max: self.scrollable_max.unwrap_or(0),
58 extra_flags,
59 focus_border_w,
60 shadow_blur,
61 shadow_ox,
62 shadow_oy,
63 cursor: self.cursor,
64 })
65 };
66
67 let visual = if self.has_non_focus_visual_extra() {
68 let state_styles = if has_state_styles {
69 let hover_style = self.hover_style.map(|r| &*cx.arena().alloc_temp(r));
70 let active_style = self.active_style.map(|r| &*cx.arena().alloc_temp(r));
71 let focus_style = self.focus_style.map(|r| &*cx.arena().alloc_temp(r));
72 Some(&*cx.arena().alloc_temp(DivStateStyles {
73 hover_style,
74 active_style,
75 focus_style,
76 text_color: self.text_color.unwrap_or(Color::TRANSPARENT),
77 text_size: self.text_size.unwrap_or(0),
78 flags: if self.text_color.is_some() { 1 } else { 0 },
79 }))
80 } else {
81 None
82 };
83 let transition = self.transition.map(|t| &*cx.arena().alloc_temp(t));
84 Some(alloc_visual(state_styles, transition))
85 } else if let Some((fw, fc)) = self.focus_border {
86 if fw == 0 && fc == Color::TRANSPARENT {
87 Some(&NO_FOCUS_BORDER_DIV_VISUAL)
88 } else {
89 Some(alloc_visual(None, None))
90 }
91 } else {
92 None
93 };
94
95 let interact = if self.has_interactivity() {
96 let rare = if self.has_rare_handlers() {
97 Some(&*cx.arena().alloc(DivRareHandlers {
98 on_long_press: self.on_long_press,
99 on_drag: self.on_drag,
100 on_swipe: self.on_swipe,
101 on_key_input: self.on_key_input,
102 on_key_nav: self.on_key_nav,
103 on_navigate_back: self.on_navigate_back,
104 on_secondary_click: self.on_secondary_click,
105 }))
106 } else {
107 None
108 };
109 Some(&*cx.arena().alloc(DivInteractivity {
110 on_click: self.on_click,
111 rare,
112 }))
113 } else {
114 None
115 };
116
117 let mut flags = 0u8;
118 if self.focusable {
119 flags |= 1 << 0;
120 }
121 if self.is_overlay {
122 flags |= 1 << 1;
123 }
124 if self.occlude_hit {
125 flags |= 1 << 2;
126 }
127 if self.auto_repeat {
128 flags |= 1 << 3;
129 }
130 if self.clip_children {
131 flags |= 1 << 4;
132 }
133 if self.id.is_some() {
134 flags |= 1 << 5;
135 }
136 if self.active_bg.is_some() {
137 flags |= 1 << 6;
138 }
139 if self.hover_bg.is_some() {
140 flags |= 1 << 7;
141 }
142
143 Element::Div(DivNode {
144 style: self.style,
145 first_child: self.first_child,
146 visual,
147 interact,
148 raw_id: self.id.unwrap_or(0),
149 raw_active_bg: self.active_bg.unwrap_or(Color::TRANSPARENT),
150 raw_hover_bg: self.hover_bg.unwrap_or(Color::TRANSPARENT),
151 opacity: self.opacity,
152 z_index: self.z_index,
153 grid_cols: self.grid_cols,
154 flags,
155 })
156 }
157}
158
159
160pub struct Div<'a> {
162 pub id: Option<u32>,
164 pub style: Style,
166 pub margin: Edges<i16>,
168 pub extra_padding: Edges<i16>,
170 pub min_w: i16,
172 pub max_w: i16,
174 pub min_h: i16,
176 pub max_h: i16,
178 pub opacity: u8,
180 pub active_bg: Option<Color>,
182 pub hover_bg: Option<Color>,
184 pub bg_grad: Option<(Color, bool)>,
186 pub focus_border: Option<(u8, Color)>,
188 pub focusable: bool,
190 pub is_overlay: bool,
192 pub occlude_hit: bool,
194 pub auto_repeat: bool,
196 pub scrollable_max: Option<i16>,
198 pub pos: Option<(i16, i16)>,
200 pub first_child: Option<&'a ElementNode<'a>>,
202 pub last_child: Option<&'a ElementNode<'a>>,
204 pub on_click: Option<&'a dyn Fn(&mut ActionScope)>,
206 pub on_long_press: Option<&'a dyn Fn(&mut ActionScope)>,
208 pub on_drag: Option<&'a dyn Fn(&mut ActionScope)>,
210 pub on_swipe: Option<&'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope)>,
212 pub on_key_input: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
214 pub on_key_nav: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
216 pub on_navigate_back: Option<&'a dyn Fn(&mut ActionScope)>,
218 pub on_secondary_click: Option<&'a dyn Fn(&mut ActionScope)>,
220 pub z_index: i8,
222 pub shadow: Option<(u8, i8, i8, Color)>,
224 pub grid_cols: u8,
226 pub transition: Option<TransitionSpec>,
228 pub clip_children: bool,
230 pub align_self: Option<AlignItems>,
232 pub cursor: Option<CursorShape>,
234 pub text_color: Option<Color>,
236 pub text_size: Option<u8>,
238 pub hover_style: Option<StyleRefinement>,
240 pub active_style: Option<StyleRefinement>,
242 pub focus_style: Option<StyleRefinement>,
244 pub items_baseline: bool,
246}
247
248#[inline(always)]
250pub fn div<'a>() -> Div<'a> {
251 Div {
252 id: None,
253 style: Style::default(),
254 margin: Edges::all(0),
255 extra_padding: Edges::all(0),
256 min_w: 0,
257 max_w: 0,
258 min_h: 0,
259 max_h: 0,
260 opacity: 255,
261 active_bg: None,
262 hover_bg: None,
263 bg_grad: None,
264 focus_border: None,
265 focusable: false,
266 is_overlay: false,
267 occlude_hit: false,
268 auto_repeat: false,
269 scrollable_max: None,
270 pos: None,
271 first_child: None,
272 last_child: None,
273 on_click: None,
274 on_long_press: None,
275 on_drag: None,
276 on_swipe: None,
277 on_key_input: None,
278 on_key_nav: None,
279 on_navigate_back: None,
280 on_secondary_click: None,
281 z_index: 0,
282 shadow: None,
283 grid_cols: 0,
284 transition: None,
285 clip_children: false,
286 align_self: None,
287 cursor: None,
288 text_color: None,
289 text_size: None,
290 hover_style: None,
291 active_style: None,
292 focus_style: None,
293 items_baseline: false,
294 }
295}
296
297impl<'a> Styled for Div<'a> {
298 #[inline(always)]
299 fn style_mut(&mut self) -> &mut Style {
300 &mut self.style
301 }
302}
303
304impl<'a> Div<'a> {
305 #[inline(always)]
306 fn has_non_focus_visual_extra(&self) -> bool {
307 self.margin != Edges::all(0)
308 || self.extra_padding != Edges::all(0)
309 || self.min_w != 0
310 || self.max_w != 0
311 || self.min_h != 0
312 || self.max_h != 0
313 || self.bg_grad.is_some()
314 || self.scrollable_max.is_some()
315 || self.pos.is_some()
316 || self.shadow.is_some()
317 || self.transition.is_some()
318 || self.align_self.is_some()
319 || self.cursor.is_some()
320 || self.text_color.is_some()
321 || self.text_size.is_some()
322 || self.hover_style.is_some()
323 || self.active_style.is_some()
324 || self.focus_style.is_some()
325 || self.items_baseline
326 }
327
328 #[inline(always)]
329 fn has_interactivity(&self) -> bool {
330 self.on_click.is_some() || self.has_rare_handlers()
331 }
332
333 #[inline(always)]
334 fn has_rare_handlers(&self) -> bool {
335 self.on_long_press.is_some()
336 || self.on_drag.is_some()
337 || self.on_swipe.is_some()
338 || self.on_key_input.is_some()
339 || self.on_key_nav.is_some()
340 || self.on_navigate_back.is_some()
341 || self.on_secondary_click.is_some()
342 }
343
344 #[inline(always)]
346 pub fn id(mut self, id: u32) -> Self {
347 self.id = Some(id);
348 self
349 }
350
351 #[inline(always)]
353 pub fn scoped_id(mut self, cx: &FrameScope, local_id: u32) -> Self {
354 self.id = Some(cx.scoped_id(local_id));
355 self
356 }
357
358 #[inline(always)]
360 pub fn pos(mut self, x: i16, y: i16) -> Self {
361 self.pos = Some((x, y));
362 self
363 }
364
365 #[inline(always)]
367 pub fn safe_area(mut self, cx: &FrameScope) -> Self {
368 let sa = cx.safe_area();
369 let kb = cx.keyboard_inset();
370 self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
371 self.extra_padding.right = self.extra_padding.right.saturating_add(sa.right.max(0));
372 self.extra_padding.bottom = self
373 .extra_padding
374 .bottom
375 .saturating_add(sa.bottom.saturating_add(kb).max(0));
376 self.extra_padding.left = self.extra_padding.left.saturating_add(sa.left.max(0));
377 self
378 }
379
380 #[inline(always)]
382 pub fn pt_safe(mut self, cx: &FrameScope) -> Self {
383 let sa = cx.safe_area();
384 self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
385 self
386 }
387
388 #[inline(always)]
390 pub fn pb_safe(mut self, cx: &FrameScope) -> Self {
391 let sa = cx.safe_area();
392 let kb = cx.keyboard_inset();
393 self.extra_padding.bottom = self
394 .extra_padding
395 .bottom
396 .saturating_add(sa.bottom.saturating_add(kb).max(0));
397 self
398 }
399
400 #[inline(always)]
402 pub fn mx(mut self, px: i16) -> Self {
403 self.margin.left = px;
404 self.margin.right = px;
405 self
406 }
407
408 #[inline(always)]
410 pub fn my(mut self, px: i16) -> Self {
411 self.margin.top = px;
412 self.margin.bottom = px;
413 self
414 }
415
416 #[inline(always)]
418 pub fn mt(mut self, px: i16) -> Self {
419 self.margin.top = px;
420 self
421 }
422
423 #[inline(always)]
425 pub fn mb(mut self, px: i16) -> Self {
426 self.margin.bottom = px;
427 self
428 }
429
430 #[inline(always)]
432 pub fn ml(mut self, px: i16) -> Self {
433 self.margin.left = px;
434 self
435 }
436
437 #[inline(always)]
439 pub fn mr(mut self, px: i16) -> Self {
440 self.margin.right = px;
441 self
442 }
443
444 #[inline(always)]
446 pub fn min_w(mut self, px: i16) -> Self {
447 self.min_w = px.max(0);
448 self
449 }
450
451 #[inline(always)]
453 pub fn max_w(mut self, px: i16) -> Self {
454 self.max_w = px.max(0);
455 self
456 }
457
458 #[inline(always)]
460 pub fn min_h(mut self, px: i16) -> Self {
461 self.min_h = px.max(0);
462 self
463 }
464
465 #[inline(always)]
467 pub fn max_h(mut self, px: i16) -> Self {
468 self.max_h = px.max(0);
469 self
470 }
471
472 #[inline(always)]
474 pub fn opacity(mut self, alpha: u8) -> Self {
475 self.opacity = alpha;
476 self
477 }
478
479 #[inline(always)]
481 pub fn active_bg(mut self, color: Color) -> Self {
482 self.active_bg = Some(color);
483 self
484 }
485
486 #[inline(always)]
488 pub fn hover_bg(mut self, color: Color) -> Self {
489 self.hover_bg = Some(color);
490 self
491 }
492
493 #[inline(always)]
497 pub fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
498 let base = self.hover_style.unwrap_or_else(|| {
499 let mut r = StyleRefinement::new();
500 r.bg = self.hover_bg;
501 r
502 });
503 let refined = f(base);
504 if refined.is_empty() {
505 return self;
506 }
507 if refined.is_bg_only() {
508 self.hover_bg = refined.bg;
509 self.hover_style = None;
510 } else {
511 if let Some(bg) = refined.bg {
512 self.hover_bg = Some(bg);
513 }
514 self.hover_style = Some(refined);
515 }
516 self
517 }
518
519 #[inline(always)]
521 pub fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
522 let base = self.active_style.unwrap_or_else(|| {
523 let mut r = StyleRefinement::new();
524 r.bg = self.active_bg;
525 r
526 });
527 let refined = f(base);
528 if refined.is_empty() {
529 return self;
530 }
531 if refined.is_bg_only() {
532 self.active_bg = refined.bg;
533 self.active_style = None;
534 } else {
535 if let Some(bg) = refined.bg {
536 self.active_bg = Some(bg);
537 }
538 self.active_style = Some(refined);
539 }
540 self
541 }
542
543 #[inline(always)]
545 pub fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
546 self.focusable = true;
547 let refined = f(self.focus_style.unwrap_or_default());
548 if !refined.is_empty() {
549 self.focus_style = Some(refined);
550 }
551 self
552 }
553
554 #[inline(always)]
556 pub fn text_color(mut self, color: Color) -> Self {
557 self.text_color = Some(color);
558 self
559 }
560
561 #[inline(always)]
563 pub fn text_size(mut self, px: u8) -> Self {
564 self.text_size = Some(px);
565 self
566 }
567
568 #[inline(always)] pub fn text_xs(self) -> Self { self.text_size(11) }
570 #[inline(always)] pub fn text_sm(self) -> Self { self.text_size(12) }
571 #[inline(always)] pub fn text_base(self) -> Self { self.text_size(14) }
572 #[inline(always)] pub fn text_lg(self) -> Self { self.text_size(16) }
573 #[inline(always)] pub fn text_xl(self) -> Self { self.text_size(18) }
574 #[inline(always)] pub fn text_2xl(self) -> Self { self.text_size(22) }
575 #[inline(always)] pub fn text_3xl(self) -> Self { self.text_size(28) }
576
577 #[inline(always)]
579 pub fn opacity_f(mut self, alpha: f32) -> Self {
580 let c = if alpha <= 0.0 {
581 0.0
582 } else if alpha >= 1.0 {
583 1.0
584 } else {
585 alpha
586 };
587 self.opacity = (c * 255.0 + 0.5) as u8;
588 self
589 }
590
591 #[inline(always)] pub fn mx_0(self) -> Self { self.mx(0) }
593 #[inline(always)] pub fn mx_1(self) -> Self { self.mx(4) }
594 #[inline(always)] pub fn mx_2(self) -> Self { self.mx(8) }
595 #[inline(always)] pub fn mx_3(self) -> Self { self.mx(12) }
596 #[inline(always)] pub fn mx_4(self) -> Self { self.mx(16) }
597 #[inline(always)] pub fn my_0(self) -> Self { self.my(0) }
598 #[inline(always)] pub fn my_1(self) -> Self { self.my(4) }
599 #[inline(always)] pub fn my_2(self) -> Self { self.my(8) }
600 #[inline(always)] pub fn my_3(self) -> Self { self.my(12) }
601 #[inline(always)] pub fn my_4(self) -> Self { self.my(16) }
602 #[inline(always)] pub fn mt_1(self) -> Self { self.mt(4) }
603 #[inline(always)] pub fn mt_2(self) -> Self { self.mt(8) }
604 #[inline(always)] pub fn mt_3(self) -> Self { self.mt(12) }
605 #[inline(always)] pub fn mt_4(self) -> Self { self.mt(16) }
606 #[inline(always)] pub fn mb_1(self) -> Self { self.mb(4) }
607 #[inline(always)] pub fn mb_2(self) -> Self { self.mb(8) }
608 #[inline(always)] pub fn mb_3(self) -> Self { self.mb(12) }
609 #[inline(always)] pub fn mb_4(self) -> Self { self.mb(16) }
610 #[inline(always)] pub fn ml_1(self) -> Self { self.ml(4) }
611 #[inline(always)] pub fn ml_2(self) -> Self { self.ml(8) }
612 #[inline(always)] pub fn ml_3(self) -> Self { self.ml(12) }
613 #[inline(always)] pub fn ml_4(self) -> Self { self.ml(16) }
614 #[inline(always)] pub fn mr_1(self) -> Self { self.mr(4) }
615 #[inline(always)] pub fn mr_2(self) -> Self { self.mr(8) }
616 #[inline(always)] pub fn mr_3(self) -> Self { self.mr(12) }
617 #[inline(always)] pub fn mr_4(self) -> Self { self.mr(16) }
618
619 #[inline(always)] pub fn relative(self) -> Self { self }
622 #[inline(always)] pub fn block(self) -> Self { self.flex_col() }
624 #[inline(always)] pub fn inline_flex(self) -> Self { self.flex_row() }
626
627 #[inline(always)] pub fn cursor_pointer(self) -> Self { self.cursor(CursorShape::Pointer) }
628 #[inline(always)] pub fn cursor_default(self) -> Self { self.cursor(CursorShape::Default) }
629 #[inline(always)] pub fn cursor_text(self) -> Self { self.cursor(CursorShape::Text) }
630 #[inline(always)] pub fn cursor_not_allowed(self) -> Self { self.cursor(CursorShape::NotAllowed) }
631 #[inline(always)] pub fn cursor_grab(self) -> Self { self.cursor(CursorShape::Grab) }
632 #[inline(always)] pub fn cursor_grabbing(self) -> Self { self.cursor(CursorShape::Grabbing) }
633
634 #[inline(always)] pub fn shadow_none(mut self) -> Self { self.shadow = None; self }
635 #[inline(always)] pub fn shadow_sm(self) -> Self { self.shadow(4, 0, 1, Color::rgba(0, 0, 0, 48)) }
636 #[inline(always)] pub fn shadow_md(self) -> Self { self.shadow(10, 0, 3, Color::rgba(0, 0, 0, 72)) }
637 #[inline(always)] pub fn shadow_lg(self) -> Self { self.shadow(18, 0, 6, Color::rgba(0, 0, 0, 96)) }
638 #[inline(always)] pub fn shadow_xl(self) -> Self { self.shadow(28, 0, 10, Color::rgba(0, 0, 0, 120)) }
639
640 #[inline(always)]
642 pub fn bg_gradient(mut self, to_color: Color) -> Self {
643 self.bg_grad = Some((to_color, false));
644 self
645 }
646
647 #[inline(always)]
649 pub fn bg_gradient_x(mut self, to_color: Color) -> Self {
650 self.bg_grad = Some((to_color, true));
651 self
652 }
653
654 #[inline(always)]
656 pub fn overlay(mut self) -> Self {
657 self.is_overlay = true;
658 self
659 }
660
661 #[inline(always)]
663 pub fn occlude_hit(mut self) -> Self {
664 self.occlude_hit = true;
665 self
666 }
667
668 #[inline(always)]
670 pub fn auto_repeat(mut self) -> Self {
671 self.auto_repeat = true;
672 self
673 }
674
675 #[inline(always)]
677 pub fn on_long_press_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
678 self.on_long_press = Some(handler);
679 self.focusable = true;
680 self
681 }
682
683 #[inline(always)]
685 pub fn on_long_press(
686 mut self,
687 cx: &'a FrameScope,
688 handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
689 ) -> Self {
690 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
691 let (x, y) = acx.click_position();
692 handler(crate::pod::ClickEvent { x, y }, acx);
693 });
694 self.on_long_press = Some(wrapped);
695 self.focusable = true;
696 self
697 }
698
699 #[inline(always)]
701 pub fn focus_border(mut self, width: u8, color: Color) -> Self {
702 self.focus_border = Some((width, color));
703 self.focusable = true;
704 self
705 }
706
707 #[inline(always)]
709 pub fn no_focus_border(mut self) -> Self {
710 self.focus_border = Some((0, Color::TRANSPARENT));
711 self
712 }
713
714 #[inline(always)]
716 pub fn cursor(mut self, cursor: CursorShape) -> Self {
717 self.cursor = Some(cursor);
718 self
719 }
720
721 #[inline(always)]
723 pub fn focusable(mut self) -> Self {
724 self.focusable = true;
725 self
726 }
727
728 #[inline(always)]
730 pub fn unfocusable(mut self) -> Self {
731 self.focusable = false;
732 self
733 }
734
735 #[inline(always)]
737 pub fn scrollable(mut self, max_scroll: i16) -> Self {
738 self.scrollable_max = Some(max_scroll.max(0));
739 self.clip_children = true;
740 self
741 }
742
743 #[inline(always)]
745 pub fn scrollable_auto(mut self) -> Self {
746 self.scrollable_max = Some(i16::MAX);
747 self.clip_children = true;
748 self
749 }
750
751 #[inline(always)]
753 pub fn align_self(mut self, align: AlignItems) -> Self {
754 self.align_self = Some(align);
755 self
756 }
757
758 #[inline(always)]
760 pub fn self_start(self) -> Self {
761 self.align_self(AlignItems::Start)
762 }
763
764 #[inline(always)]
766 pub fn self_center(self) -> Self {
767 self.align_self(AlignItems::Center)
768 }
769
770 #[inline(always)]
772 pub fn self_end(self) -> Self {
773 self.align_self(AlignItems::End)
774 }
775
776 #[inline(always)]
778 pub fn self_stretch(self) -> Self {
779 self.align_self(AlignItems::Stretch)
780 }
781
782 #[inline(always)]
784 pub fn items_baseline(mut self) -> Self {
785 self.items_baseline = true;
786 self
787 }
788
789 #[inline(always)]
791 pub fn overflow_hidden(mut self) -> Self {
792 self.clip_children = true;
793 self
794 }
795
796 #[inline(always)]
798 pub fn overflow_visible(mut self) -> Self {
799 self.clip_children = false;
800 self
801 }
802
803 #[inline(always)]
805 pub fn overflow_scroll(self) -> Self {
806 self.scrollable_auto()
807 }
808
809 #[inline(always)]
811 pub fn overflow_y_scroll(self) -> Self {
812 self.flex_col().scrollable_auto()
813 }
814
815 #[inline(always)]
817 pub fn overflow_x_scroll(self) -> Self {
818 self.flex_row().scrollable_auto()
819 }
820
821 #[inline(always)]
823 pub fn transition(self) -> Self {
824 self.animate_all(Duration::from_millis(150))
825 }
826
827 #[inline(always)]
829 pub fn transition_colors(self) -> Self {
830 self.animate_color(Duration::from_millis(150))
831 }
832
833 #[inline(always)]
835 pub fn transition_opacity(self) -> Self {
836 self.animate_opacity(Duration::from_millis(150))
837 }
838
839 #[inline(always)]
841 pub fn transition_all(self) -> Self {
842 self.animate_all(Duration::from_millis(150))
843 }
844
845 #[inline(always)]
847 pub fn transition_none(mut self) -> Self {
848 self.transition = None;
849 self
850 }
851
852 #[inline(always)] pub fn duration_75(self) -> Self { self.duration(Duration::from_millis(75)) }
853 #[inline(always)] pub fn duration_100(self) -> Self { self.duration(Duration::from_millis(100)) }
854 #[inline(always)] pub fn duration_150(self) -> Self { self.duration(Duration::from_millis(150)) }
855 #[inline(always)] pub fn duration_200(self) -> Self { self.duration(Duration::from_millis(200)) }
856 #[inline(always)] pub fn duration_300(self) -> Self { self.duration(Duration::from_millis(300)) }
857 #[inline(always)] pub fn duration_500(self) -> Self { self.duration(Duration::from_millis(500)) }
858
859 #[inline(always)] pub fn ease_linear(self) -> Self { self.ease(Easing::Linear) }
860 #[inline(always)] pub fn ease_in(self) -> Self { self.ease(Easing::EaseIn) }
861 #[inline(always)] pub fn ease_out(self) -> Self { self.ease(Easing::EaseOut) }
862 #[inline(always)] pub fn ease_in_out(self) -> Self { self.ease(Easing::EaseInOut) }
863 #[inline(always)] pub fn ease_spring(self) -> Self { self.ease(Easing::Spring) }
864
865 #[inline(always)]
867 pub fn animate_color(mut self, duration: Duration) -> Self {
868 let ms = duration.as_millis().min(u16::MAX as u128) as u16;
869 let mut spec = self.transition.unwrap_or(TransitionSpec {
870 duration_ms: ms,
871 ..TransitionSpec::default()
872 });
873 spec.duration_ms = ms;
874 spec.animate_color = true;
875 self.transition = Some(spec);
876 self
877 }
878
879 #[inline(always)]
881 pub fn animate_bg(self, duration: Duration) -> Self {
882 self.animate_color(duration)
883 }
884
885 #[inline(always)]
887 pub fn animate_bounds(mut self, duration: Duration) -> Self {
888 let ms = duration.as_millis().min(u16::MAX as u128) as u16;
889 let mut spec = self.transition.unwrap_or(TransitionSpec {
890 duration_ms: ms,
891 ..TransitionSpec::default()
892 });
893 spec.duration_ms = ms;
894 spec.animate_bounds = true;
895 self.transition = Some(spec);
896 self
897 }
898
899 #[inline(always)]
901 pub fn ease(mut self, easing: Easing) -> Self {
902 let mut spec = self.transition.unwrap_or_default();
903 spec.easing = easing;
904 self.transition = Some(spec);
905 self
906 }
907
908 #[inline(always)]
910 pub fn repeat_infinite(mut self) -> Self {
911 let mut spec = self.transition.unwrap_or_default();
912 spec.loop_mode = LoopMode::Infinite;
913 self.transition = Some(spec);
914 self
915 }
916
917 #[inline(always)]
919 pub fn repeat_ping_pong(mut self) -> Self {
920 let mut spec = self.transition.unwrap_or_default();
921 spec.loop_mode = LoopMode::PingPong;
922 self.transition = Some(spec);
923 self
924 }
925
926 #[inline(always)]
928 pub fn duration(mut self, duration: Duration) -> Self {
929 let mut spec = self.transition.unwrap_or_default();
930 spec.duration_ms = duration_to_ms(duration);
931 self.transition = Some(spec);
932 self
933 }
934
935 #[inline(always)]
937 pub fn delay(mut self, delay: Duration) -> Self {
938 let mut spec = self.transition.unwrap_or_default();
939 spec.delay_ms = duration_to_ms(delay);
940 self.transition = Some(spec);
941 self
942 }
943
944 #[inline(always)]
946 pub fn animate_opacity(mut self, duration: Duration) -> Self {
947 let mut spec = self.transition.unwrap_or_default();
948 spec.duration_ms = duration_to_ms(duration);
949 spec.animate_opacity = true;
950 self.transition = Some(spec);
951 self
952 }
953
954 #[inline(always)]
956 pub fn animate_all(mut self, duration: Duration) -> Self {
957 let mut spec = self.transition.unwrap_or_default();
958 spec.duration_ms = duration_to_ms(duration);
959 spec.animate_color = true;
960 spec.animate_bounds = true;
961 spec.animate_opacity = true;
962 self.transition = Some(spec);
963 self
964 }
965
966 #[inline(always)]
968 pub fn enter_from(mut self, opacity: u8, dx: i16, dy: i16, duration: Duration) -> Self {
969 let mut spec = self.transition.unwrap_or_default();
970 spec.duration_ms = duration_to_ms(duration);
971 spec.enter = Some(EnterFrom { opacity, dx, dy });
972 self.transition = Some(spec);
973 self
974 }
975
976 #[inline(always)]
978 pub fn fade_in(self, duration: Duration) -> Self {
979 self.enter_from(0, 0, 0, duration)
980 }
981
982 #[inline(always)]
984 pub fn slide_in(self, dx: i16, dy: i16, duration: Duration) -> Self {
985 self.enter_from(0, dx, dy, duration)
986 }
987
988 #[inline(always)]
990 pub fn ping_pong_bg(mut self, from_bg: Color, duration: Duration) -> Self {
991 let ms = duration.as_millis().min(u16::MAX as u128) as u16;
992 let mut spec = self.transition.unwrap_or_default();
993 spec.duration_ms = ms;
994 spec.animate_color = true;
995 spec.loop_mode = LoopMode::PingPong;
996 spec.easing = Easing::EaseInOut;
997 spec.pulse_from_bg = Some(from_bg);
998 self.transition = Some(spec);
999 self
1000 }
1001
1002 #[inline(always)]
1004 pub fn on_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1005 self.on_click = Some(handler);
1006 self.focusable = true;
1007 self
1008 }
1009
1010 #[inline(always)]
1012 pub fn on_click(
1013 mut self,
1014 cx: &'a FrameScope,
1015 handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
1016 ) -> Self {
1017 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1018 let (x, y) = acx.click_position();
1019 handler(crate::pod::ClickEvent { x, y }, acx);
1020 });
1021 self.on_click = Some(wrapped);
1022 self.focusable = true;
1023 self
1024 }
1025
1026 #[inline(always)]
1028 pub fn on_secondary_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1029 self.on_secondary_click = Some(handler);
1030 self
1031 }
1032
1033 #[inline(always)]
1035 pub fn on_secondary_click(
1036 mut self,
1037 cx: &'a FrameScope,
1038 handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
1039 ) -> Self {
1040 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1041 let (x, y) = acx.click_position();
1042 handler(crate::pod::ClickEvent { x, y }, acx);
1043 });
1044 self.on_secondary_click = Some(wrapped);
1045 self
1046 }
1047
1048 #[inline(always)]
1050 pub fn on_drag_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1051 self.on_drag = Some(handler);
1052 self
1053 }
1054
1055 #[inline(always)]
1057 pub fn on_drag(
1058 mut self,
1059 cx: &'a FrameScope,
1060 handler: &'a dyn Fn(DragEvent, &mut ActionScope),
1061 ) -> Self {
1062 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1063 let ev = acx.drag_event();
1064 handler(ev, acx);
1065 });
1066 self.on_drag = Some(wrapped);
1067 self
1068 }
1069
1070 #[inline(always)]
1072 pub fn on_swipe(
1073 mut self,
1074 _cx: &'a FrameScope,
1075 handler: &'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope),
1076 ) -> Self {
1077 self.on_swipe = Some(handler);
1078 self
1079 }
1080
1081 #[inline(always)]
1083 pub fn key(mut self, key: impl AsRef<[u8]>) -> Self {
1084 let mut h: u32 = 0x811c_9dc5;
1085 for &b in key.as_ref() {
1086 h ^= b as u32;
1087 h = h.wrapping_mul(0x0100_0193);
1088 }
1089 self.id = Some(h | 1);
1090 self
1091 }
1092
1093 #[inline(always)]
1095 pub fn z_index(mut self, z: i8) -> Self {
1096 self.z_index = z;
1097 self
1098 }
1099
1100 #[inline(always)]
1102 pub fn on_key_input(
1103 mut self,
1104 handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
1105 ) -> Self {
1106 self.on_key_input = Some(handler);
1107 self.focusable = true;
1108 self
1109 }
1110
1111 #[inline(always)]
1113 pub fn on_key_nav(
1114 mut self,
1115 handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
1116 ) -> Self {
1117 self.on_key_nav = Some(handler);
1118 self
1119 }
1120
1121 #[inline(always)]
1123 pub fn on_navigate_back(
1124 mut self,
1125 handler: &'a dyn Fn(&mut ActionScope),
1126 ) -> Self {
1127 self.on_navigate_back = Some(handler);
1128 self
1129 }
1130
1131 #[inline(always)]
1133 pub fn shadow(mut self, blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
1134 if blur > 0 && color.a() > 0 {
1135 self.shadow = Some((blur.min(64), offset_x, offset_y, color));
1136 }
1137 self
1138 }
1139
1140 #[inline(always)]
1142 pub fn grid(mut self, cols: u8) -> Self {
1143 self.grid_cols = cols.max(1);
1144 self
1145 }
1146
1147 pub fn child(mut self, cx: &'a FrameScope, child: impl IntoElement<'a>) -> Self {
1149 let elem = child.into_element(cx);
1150 let node: &'a ElementNode<'a> = cx.arena().alloc_temp(ElementNode {
1151 element: elem,
1152 next_sibling: Cell::new(None),
1153 });
1154 if let Some(last) = self.last_child {
1155 last.next_sibling.set(Some(node));
1156 } else {
1157 self.first_child = Some(node);
1158 }
1159 self.last_child = Some(node);
1160 self
1161 }
1162
1163 #[inline(always)]
1165 pub fn child_opt(self, cx: &'a FrameScope, child: Option<impl IntoElement<'a>>) -> Self {
1166 match child {
1167 Some(c) => self.child(cx, c),
1168 None => self,
1169 }
1170 }
1171
1172 pub fn children<I, E>(mut self, cx: &'a FrameScope, iter: I) -> Self
1174 where
1175 I: IntoIterator<Item = E>,
1176 E: IntoElement<'a>,
1177 {
1178 for item in iter {
1179 self = self.child(cx, item);
1180 }
1181 self
1182 }
1183
1184 #[inline(always)]
1186 pub fn justify_start_localized(mut self, cx: &FrameScope) -> Self {
1187 if cx.is_rtl() {
1188 self.style.set_justify(crate::style::Justify::End);
1189 } else {
1190 self.style.set_justify(crate::style::Justify::Start);
1191 }
1192 self
1193 }
1194
1195 #[inline(always)]
1197 pub fn justify_end_localized(mut self, cx: &FrameScope) -> Self {
1198 if cx.is_rtl() {
1199 self.style.set_justify(crate::style::Justify::Start);
1200 } else {
1201 self.style.set_justify(crate::style::Justify::End);
1202 }
1203 self
1204 }
1205
1206 #[inline(always)]
1208 pub fn ps(self, cx: &FrameScope, px: i8) -> Self {
1209 if cx.is_rtl() {
1210 self.pr(px)
1211 } else {
1212 self.pl(px)
1213 }
1214 }
1215
1216 #[inline(always)]
1218 pub fn pe(self, cx: &FrameScope, px: i8) -> Self {
1219 if cx.is_rtl() {
1220 self.pl(px)
1221 } else {
1222 self.pr(px)
1223 }
1224 }
1225
1226 #[inline(always)]
1228 pub fn ms(self, cx: &FrameScope, px: i16) -> Self {
1229 if cx.is_rtl() {
1230 self.mr(px)
1231 } else {
1232 self.ml(px)
1233 }
1234 }
1235
1236 #[inline(always)]
1238 pub fn me(self, cx: &FrameScope, px: i16) -> Self {
1239 if cx.is_rtl() {
1240 self.ml(px)
1241 } else {
1242 self.mr(px)
1243 }
1244 }
1245}
1246