1use core::cell::Cell;
4use core::time::Duration;
5
6use crate::arena::StrRef;
7use crate::paint::{
8 BackdropFilter, BlendMode, ColorMatrix, ColorStop, CornerRadii, DropShadow, ExtendMode,
9 GradientSpace, LayerFilter, Paint, PaintEffects, Transform2D,
10};
11use crate::pod::{ActionScope, CursorShape, DragEvent, FrameScope};
12use crate::style::{AlignItems, Color, Easing, Edges, LoopMode, Style, StyleRefinement, Styled};
13
14use super::div_node::{
15 duration_to_ms, pack_visual_flags, DivExtPtr, DivExtended, DivInteractivity, DivNode,
16 DivRareHandlers, DivStateStyles, DivVisual, EnterFrom, TransitionSpec, FOCUS_BORDER_NONE,
17 NO_FOCUS_BORDER_DIV_VISUAL,
18};
19use super::{Element, ElementNode, IntoElement};
20
21impl<'a> IntoElement<'a> for Div<'a> {
22 #[inline(always)]
23 fn into_element(mut self, cx: &'a FrameScope) -> Element<'a> {
24 let has_state_styles = self.text_color.is_some()
25 || self.text_size.is_some()
26 || self.hover_style.is_some()
27 || self.active_style.is_some()
28 || self.focus_style.is_some();
29 let has_effects = !self.effects.is_empty() || self.external_texture.is_some();
30
31 let alloc_visual = |extended: DivExtPtr<'a>,
32 transition: Option<&'a TransitionSpec>|
33 -> &'a DivVisual<'a> {
34 let (pos_x, pos_y) = self.pos.unwrap_or((0, 0));
35 let (bg_grad_to, bg_grad_horiz) = self.bg_grad.unwrap_or((Color::TRANSPARENT, false));
36 let (focus_border_w, focus_border_color) = match self.focus_border {
37 Some((w, c)) => (w.min(254), c),
38 None => (FOCUS_BORDER_NONE, Color::TRANSPARENT),
39 };
40 let (shadow_blur, shadow_ox, shadow_oy, shadow_color) =
41 self.shadow.unwrap_or((0, 0, 0, Color::TRANSPARENT));
42 let extra_flags = pack_visual_flags(
43 self.pos.is_some(),
44 self.scrollable_max.is_some(),
45 self.bg_grad.is_some(),
46 bg_grad_horiz,
47 self.items_baseline,
48 self.align_self,
49 );
50 &*cx.arena().alloc_temp(DivVisual {
51 extended,
52 transition,
53 margin: self.margin,
54 extra_padding: self.extra_padding,
55 bg_grad_to,
56 focus_border_color,
57 shadow_color,
58 min_w: self.min_w,
59 max_w: self.max_w,
60 min_h: self.min_h,
61 max_h: self.max_h,
62 pos_x,
63 pos_y,
64 raw_scrollable_max: self.scrollable_max.unwrap_or(0),
65 extra_flags,
66 focus_border_w,
67 shadow_blur,
68 shadow_ox,
69 shadow_oy,
70 cursor: self.cursor,
71 })
72 };
73
74 let visual = if self.has_non_focus_visual_extra() {
75 let state_styles = if has_state_styles {
76 let hover_style = self.hover_style.map(|r| &*cx.arena().alloc_temp(r));
77 let active_style = self.active_style.map(|r| &*cx.arena().alloc_temp(r));
78 let focus_style = self.focus_style.map(|r| &*cx.arena().alloc_temp(r));
79 Some(&*cx.arena().alloc_temp(DivStateStyles {
80 hover_style,
81 active_style,
82 focus_style,
83 text_color: self.text_color.unwrap_or(Color::TRANSPARENT),
84 text_size: self.text_size.unwrap_or(0),
85 flags: if self.text_color.is_some() { 1 } else { 0 },
86 }))
87 } else {
88 None
89 };
90 let effects = if has_effects {
91 if let Some(ext_spec) = self.external_texture {
92 self.effects.external_texture = Some(&*cx.arena().alloc_temp(ext_spec));
93 }
94 Some(&*cx.arena().alloc_temp(self.effects))
95 } else {
96 None
97 };
98 let extended = match (state_styles, effects) {
99 (None, None) => DivExtPtr::NONE,
100 (Some(s), None) => DivExtPtr::from_state_styles(s),
101 (None, Some(e)) => DivExtPtr::from_effects(e),
102 (Some(s), Some(e)) => DivExtPtr::from_both(&*cx.arena().alloc_temp(DivExtended {
103 state_styles: Some(s),
104 effects: Some(e),
105 })),
106 };
107 let transition = self.transition.map(|t| &*cx.arena().alloc_temp(t));
108 Some(alloc_visual(extended, transition))
109 } else if let Some((fw, fc)) = self.focus_border {
110 if fw == 0 && fc == Color::TRANSPARENT {
111 Some(&NO_FOCUS_BORDER_DIV_VISUAL)
112 } else {
113 Some(alloc_visual(DivExtPtr::NONE, None))
114 }
115 } else {
116 None
117 };
118
119 let interact = if self.has_interactivity() {
120 let rare = if self.has_rare_handlers() {
121 Some(&*cx.arena().alloc(DivRareHandlers {
122 on_long_press: self.on_long_press,
123 on_drag: self.on_drag,
124 on_swipe: self.on_swipe,
125 on_key_input: self.on_key_input,
126 on_key_nav: self.on_key_nav,
127 on_navigate_back: self.on_navigate_back,
128 on_secondary_click: self.on_secondary_click,
129 }))
130 } else {
131 None
132 };
133 Some(&*cx.arena().alloc(DivInteractivity {
134 on_click: self.on_click,
135 rare,
136 }))
137 } else {
138 None
139 };
140
141 let mut flags = 0u8;
142 if self.focusable {
143 flags |= 1 << 0;
144 }
145 if self.is_overlay {
146 flags |= 1 << 1;
147 }
148 if self.occlude_hit {
149 flags |= 1 << 2;
150 }
151 if self.auto_repeat {
152 flags |= 1 << 3;
153 }
154 if self.clip_children {
155 flags |= 1 << 4;
156 }
157 if self.id.is_some() {
158 flags |= 1 << 5;
159 }
160 if self.active_bg.is_some() {
161 flags |= 1 << 6;
162 }
163 if self.hover_bg.is_some() {
164 flags |= 1 << 7;
165 }
166
167 Element::Div(DivNode {
168 style: self.style,
169 first_child: self.first_child,
170 visual,
171 interact,
172 raw_id: self.id.unwrap_or(0),
173 raw_active_bg: self.active_bg.unwrap_or(Color::TRANSPARENT),
174 raw_hover_bg: self.hover_bg.unwrap_or(Color::TRANSPARENT),
175 opacity: self.opacity,
176 z_index: self.z_index,
177 grid_cols: self.grid_cols,
178 flags,
179 })
180 }
181}
182
183
184pub struct Div<'a> {
186 pub id: Option<u32>,
188 pub style: Style,
190 pub margin: Edges<i16>,
192 pub extra_padding: Edges<i16>,
194 pub min_w: i16,
196 pub max_w: i16,
198 pub min_h: i16,
200 pub max_h: i16,
202 pub opacity: u8,
204 pub active_bg: Option<Color>,
206 pub hover_bg: Option<Color>,
208 pub bg_grad: Option<(Color, bool)>,
210 pub focus_border: Option<(u8, Color)>,
212 pub focusable: bool,
214 pub is_overlay: bool,
216 pub occlude_hit: bool,
218 pub auto_repeat: bool,
220 pub scrollable_max: Option<i16>,
222 pub pos: Option<(i16, i16)>,
224 pub first_child: Option<&'a ElementNode<'a>>,
226 pub last_child: Option<&'a ElementNode<'a>>,
228 pub on_click: Option<&'a dyn Fn(&mut ActionScope)>,
230 pub on_long_press: Option<&'a dyn Fn(&mut ActionScope)>,
232 pub on_drag: Option<&'a dyn Fn(&mut ActionScope)>,
234 pub on_swipe: Option<&'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope)>,
236 pub on_key_input: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
238 pub on_key_nav: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
240 pub on_navigate_back: Option<&'a dyn Fn(&mut ActionScope)>,
242 pub on_secondary_click: Option<&'a dyn Fn(&mut ActionScope)>,
244 pub z_index: i8,
246 pub shadow: Option<(u8, i8, i8, Color)>,
248 pub grid_cols: u8,
250 pub transition: Option<TransitionSpec>,
252 pub clip_children: bool,
254 pub align_self: Option<AlignItems>,
256 pub cursor: Option<CursorShape>,
258 pub text_color: Option<Color>,
260 pub text_size: Option<u8>,
262 pub hover_style: Option<StyleRefinement>,
264 pub active_style: Option<StyleRefinement>,
266 pub focus_style: Option<StyleRefinement>,
268 pub items_baseline: bool,
270 pub effects: PaintEffects<'a>,
272 pub external_texture: Option<crate::paint::ExternalTextureSpec>,
274}
275
276#[inline(always)]
278pub fn div<'a>() -> Div<'a> {
279 Div {
280 id: None,
281 style: Style::default(),
282 margin: Edges::all(0),
283 extra_padding: Edges::all(0),
284 min_w: 0,
285 max_w: 0,
286 min_h: 0,
287 max_h: 0,
288 opacity: 255,
289 active_bg: None,
290 hover_bg: None,
291 bg_grad: None,
292 focus_border: None,
293 focusable: false,
294 is_overlay: false,
295 occlude_hit: false,
296 auto_repeat: false,
297 scrollable_max: None,
298 pos: None,
299 first_child: None,
300 last_child: None,
301 on_click: None,
302 on_long_press: None,
303 on_drag: None,
304 on_swipe: None,
305 on_key_input: None,
306 on_key_nav: None,
307 on_navigate_back: None,
308 on_secondary_click: None,
309 z_index: 0,
310 shadow: None,
311 grid_cols: 0,
312 transition: None,
313 clip_children: false,
314 align_self: None,
315 cursor: None,
316 text_color: None,
317 text_size: None,
318 hover_style: None,
319 active_style: None,
320 focus_style: None,
321 items_baseline: false,
322 effects: PaintEffects::EMPTY,
323 external_texture: None,
324 }
325}
326
327impl<'a> Styled for Div<'a> {
328 #[inline(always)]
329 fn style_mut(&mut self) -> &mut Style {
330 &mut self.style
331 }
332}
333
334impl<'a> Div<'a> {
335 #[inline(always)]
336 fn has_non_focus_visual_extra(&self) -> bool {
337 self.margin != Edges::all(0)
338 || self.extra_padding != Edges::all(0)
339 || self.min_w != 0
340 || self.max_w != 0
341 || self.min_h != 0
342 || self.max_h != 0
343 || self.bg_grad.is_some()
344 || self.scrollable_max.is_some()
345 || self.pos.is_some()
346 || self.shadow.is_some()
347 || self.transition.is_some()
348 || self.align_self.is_some()
349 || self.cursor.is_some()
350 || self.text_color.is_some()
351 || self.text_size.is_some()
352 || self.hover_style.is_some()
353 || self.active_style.is_some()
354 || self.focus_style.is_some()
355 || self.items_baseline
356 || !self.effects.is_empty()
357 || self.external_texture.is_some()
358 }
359
360 #[inline(always)]
361 fn has_interactivity(&self) -> bool {
362 self.on_click.is_some() || self.has_rare_handlers()
363 }
364
365 #[inline(always)]
366 fn has_rare_handlers(&self) -> bool {
367 self.on_long_press.is_some()
368 || self.on_drag.is_some()
369 || self.on_swipe.is_some()
370 || self.on_key_input.is_some()
371 || self.on_key_nav.is_some()
372 || self.on_navigate_back.is_some()
373 || self.on_secondary_click.is_some()
374 }
375
376 #[inline(always)]
378 pub fn id(mut self, id: u32) -> Self {
379 self.id = Some(id);
380 self
381 }
382
383 #[inline(always)]
385 pub fn scoped_id(mut self, cx: &FrameScope, local_id: u32) -> Self {
386 self.id = Some(cx.scoped_id(local_id));
387 self
388 }
389
390 #[inline(always)]
392 pub fn pos(mut self, x: i16, y: i16) -> Self {
393 self.pos = Some((x, y));
394 self
395 }
396
397 #[inline(always)]
399 pub fn safe_area(mut self, cx: &FrameScope) -> Self {
400 let sa = cx.safe_area();
401 let kb = cx.keyboard_inset();
402 self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
403 self.extra_padding.right = self.extra_padding.right.saturating_add(sa.right.max(0));
404 self.extra_padding.bottom = self
405 .extra_padding
406 .bottom
407 .saturating_add(sa.bottom.saturating_add(kb).max(0));
408 self.extra_padding.left = self.extra_padding.left.saturating_add(sa.left.max(0));
409 self
410 }
411
412 #[inline(always)]
414 pub fn pt_safe(mut self, cx: &FrameScope) -> Self {
415 let sa = cx.safe_area();
416 self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
417 self
418 }
419
420 #[inline(always)]
422 pub fn pb_safe(mut self, cx: &FrameScope) -> Self {
423 let sa = cx.safe_area();
424 let kb = cx.keyboard_inset();
425 self.extra_padding.bottom = self
426 .extra_padding
427 .bottom
428 .saturating_add(sa.bottom.saturating_add(kb).max(0));
429 self
430 }
431
432 #[inline(always)]
434 pub fn mx(mut self, px: i16) -> Self {
435 self.margin.left = px;
436 self.margin.right = px;
437 self
438 }
439
440 #[inline(always)]
442 pub fn my(mut self, px: i16) -> Self {
443 self.margin.top = px;
444 self.margin.bottom = px;
445 self
446 }
447
448 #[inline(always)]
450 pub fn mt(mut self, px: i16) -> Self {
451 self.margin.top = px;
452 self
453 }
454
455 #[inline(always)]
457 pub fn mb(mut self, px: i16) -> Self {
458 self.margin.bottom = px;
459 self
460 }
461
462 #[inline(always)]
464 pub fn ml(mut self, px: i16) -> Self {
465 self.margin.left = px;
466 self
467 }
468
469 #[inline(always)]
471 pub fn mr(mut self, px: i16) -> Self {
472 self.margin.right = px;
473 self
474 }
475
476 #[inline(always)]
478 pub fn min_w(mut self, px: i16) -> Self {
479 self.min_w = px.max(0);
480 self
481 }
482
483 #[inline(always)]
485 pub fn max_w(mut self, px: i16) -> Self {
486 self.max_w = px.max(0);
487 self
488 }
489
490 #[inline(always)]
492 pub fn min_h(mut self, px: i16) -> Self {
493 self.min_h = px.max(0);
494 self
495 }
496
497 #[inline(always)]
499 pub fn max_h(mut self, px: i16) -> Self {
500 self.max_h = px.max(0);
501 self
502 }
503
504 #[inline(always)]
506 pub fn opacity(mut self, alpha: u8) -> Self {
507 self.opacity = alpha;
508 self
509 }
510
511 #[inline(always)]
513 pub fn active_bg(mut self, color: Color) -> Self {
514 self.active_bg = Some(color);
515 self
516 }
517
518 #[inline(always)]
520 pub fn hover_bg(mut self, color: Color) -> Self {
521 self.hover_bg = Some(color);
522 self
523 }
524
525 #[inline(always)]
529 pub fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
530 let base = self.hover_style.unwrap_or_else(|| {
531 let mut r = StyleRefinement::new();
532 r.bg = self.hover_bg;
533 r
534 });
535 let refined = f(base);
536 if refined.is_empty() {
537 return self;
538 }
539 if refined.is_bg_only() {
540 self.hover_bg = refined.bg;
541 self.hover_style = None;
542 } else {
543 if let Some(bg) = refined.bg {
544 self.hover_bg = Some(bg);
545 }
546 self.hover_style = Some(refined);
547 }
548 self
549 }
550
551 #[inline(always)]
553 pub fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
554 let base = self.active_style.unwrap_or_else(|| {
555 let mut r = StyleRefinement::new();
556 r.bg = self.active_bg;
557 r
558 });
559 let refined = f(base);
560 if refined.is_empty() {
561 return self;
562 }
563 if refined.is_bg_only() {
564 self.active_bg = refined.bg;
565 self.active_style = None;
566 } else {
567 if let Some(bg) = refined.bg {
568 self.active_bg = Some(bg);
569 }
570 self.active_style = Some(refined);
571 }
572 self
573 }
574
575 #[inline(always)]
577 pub fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
578 self.focusable = true;
579 let refined = f(self.focus_style.unwrap_or_default());
580 if !refined.is_empty() {
581 self.focus_style = Some(refined);
582 }
583 self
584 }
585
586 #[inline(always)]
588 pub fn text_color(mut self, color: Color) -> Self {
589 self.text_color = Some(color);
590 self
591 }
592
593 #[inline(always)]
595 pub fn text_size(mut self, px: u8) -> Self {
596 self.text_size = Some(px);
597 self
598 }
599
600 #[inline(always)] pub fn text_xs(self) -> Self { self.text_size(11) }
602 #[inline(always)] pub fn text_sm(self) -> Self { self.text_size(12) }
603 #[inline(always)] pub fn text_base(self) -> Self { self.text_size(14) }
604 #[inline(always)] pub fn text_lg(self) -> Self { self.text_size(16) }
605 #[inline(always)] pub fn text_xl(self) -> Self { self.text_size(18) }
606 #[inline(always)] pub fn text_2xl(self) -> Self { self.text_size(22) }
607 #[inline(always)] pub fn text_3xl(self) -> Self { self.text_size(28) }
608
609 #[inline(always)]
611 pub fn opacity_f(mut self, alpha: f32) -> Self {
612 let c = if alpha <= 0.0 {
613 0.0
614 } else if alpha >= 1.0 {
615 1.0
616 } else {
617 alpha
618 };
619 self.opacity = (c * 255.0 + 0.5) as u8;
620 self
621 }
622
623 #[inline(always)] pub fn mx_0(self) -> Self { self.mx(0) }
625 #[inline(always)] pub fn mx_1(self) -> Self { self.mx(4) }
626 #[inline(always)] pub fn mx_2(self) -> Self { self.mx(8) }
627 #[inline(always)] pub fn mx_3(self) -> Self { self.mx(12) }
628 #[inline(always)] pub fn mx_4(self) -> Self { self.mx(16) }
629 #[inline(always)] pub fn my_0(self) -> Self { self.my(0) }
630 #[inline(always)] pub fn my_1(self) -> Self { self.my(4) }
631 #[inline(always)] pub fn my_2(self) -> Self { self.my(8) }
632 #[inline(always)] pub fn my_3(self) -> Self { self.my(12) }
633 #[inline(always)] pub fn my_4(self) -> Self { self.my(16) }
634 #[inline(always)] pub fn mt_1(self) -> Self { self.mt(4) }
635 #[inline(always)] pub fn mt_2(self) -> Self { self.mt(8) }
636 #[inline(always)] pub fn mt_3(self) -> Self { self.mt(12) }
637 #[inline(always)] pub fn mt_4(self) -> Self { self.mt(16) }
638 #[inline(always)] pub fn mb_1(self) -> Self { self.mb(4) }
639 #[inline(always)] pub fn mb_2(self) -> Self { self.mb(8) }
640 #[inline(always)] pub fn mb_3(self) -> Self { self.mb(12) }
641 #[inline(always)] pub fn mb_4(self) -> Self { self.mb(16) }
642 #[inline(always)] pub fn ml_1(self) -> Self { self.ml(4) }
643 #[inline(always)] pub fn ml_2(self) -> Self { self.ml(8) }
644 #[inline(always)] pub fn ml_3(self) -> Self { self.ml(12) }
645 #[inline(always)] pub fn ml_4(self) -> Self { self.ml(16) }
646 #[inline(always)] pub fn mr_1(self) -> Self { self.mr(4) }
647 #[inline(always)] pub fn mr_2(self) -> Self { self.mr(8) }
648 #[inline(always)] pub fn mr_3(self) -> Self { self.mr(12) }
649 #[inline(always)] pub fn mr_4(self) -> Self { self.mr(16) }
650
651 #[inline(always)] pub fn relative(self) -> Self { self }
654 #[inline(always)] pub fn block(self) -> Self { self.flex_col() }
656 #[inline(always)] pub fn inline_flex(self) -> Self { self.flex_row() }
658
659 #[inline(always)] pub fn cursor_pointer(self) -> Self { self.cursor(CursorShape::Pointer) }
660 #[inline(always)] pub fn cursor_default(self) -> Self { self.cursor(CursorShape::Default) }
661 #[inline(always)] pub fn cursor_text(self) -> Self { self.cursor(CursorShape::Text) }
662 #[inline(always)] pub fn cursor_not_allowed(self) -> Self { self.cursor(CursorShape::NotAllowed) }
663 #[inline(always)] pub fn cursor_grab(self) -> Self { self.cursor(CursorShape::Grab) }
664 #[inline(always)] pub fn cursor_grabbing(self) -> Self { self.cursor(CursorShape::Grabbing) }
665
666 #[inline(always)] pub fn shadow_none(mut self) -> Self { self.shadow = None; self }
667 #[inline(always)] pub fn shadow_sm(self) -> Self { self.shadow(4, 0, 1, Color::rgba(0, 0, 0, 48)) }
668 #[inline(always)] pub fn shadow_md(self) -> Self { self.shadow(10, 0, 3, Color::rgba(0, 0, 0, 72)) }
669 #[inline(always)] pub fn shadow_lg(self) -> Self { self.shadow(18, 0, 6, Color::rgba(0, 0, 0, 96)) }
670 #[inline(always)] pub fn shadow_xl(self) -> Self { self.shadow(28, 0, 10, Color::rgba(0, 0, 0, 120)) }
671
672 #[inline(always)]
674 pub fn bg_gradient(mut self, to_color: Color) -> Self {
675 self.bg_grad = Some((to_color, false));
676 self
677 }
678
679 #[inline(always)]
681 pub fn bg_gradient_x(mut self, to_color: Color) -> Self {
682 self.bg_grad = Some((to_color, true));
683 self
684 }
685
686 #[inline(always)]
688 pub fn overlay(mut self) -> Self {
689 self.is_overlay = true;
690 self
691 }
692
693 #[inline(always)]
695 pub fn occlude_hit(mut self) -> Self {
696 self.occlude_hit = true;
697 self
698 }
699
700 #[inline(always)]
702 pub fn auto_repeat(mut self) -> Self {
703 self.auto_repeat = true;
704 self
705 }
706
707 #[inline(always)]
709 pub fn on_long_press_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
710 self.on_long_press = Some(handler);
711 self.focusable = true;
712 self
713 }
714
715 #[inline(always)]
717 pub fn on_long_press(
718 mut self,
719 cx: &'a FrameScope,
720 handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
721 ) -> Self {
722 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
723 let (x, y) = acx.click_position();
724 handler(crate::pod::ClickEvent { x, y }, acx);
725 });
726 self.on_long_press = Some(wrapped);
727 self.focusable = true;
728 self
729 }
730
731 #[inline(always)]
733 pub fn focus_border(mut self, width: u8, color: Color) -> Self {
734 self.focus_border = Some((width, color));
735 self.focusable = true;
736 self
737 }
738
739 #[inline(always)]
741 pub fn no_focus_border(mut self) -> Self {
742 self.focus_border = Some((0, Color::TRANSPARENT));
743 self
744 }
745
746 #[inline(always)]
748 pub fn cursor(mut self, cursor: CursorShape) -> Self {
749 self.cursor = Some(cursor);
750 self
751 }
752
753 #[inline(always)]
755 pub fn focusable(mut self) -> Self {
756 self.focusable = true;
757 self
758 }
759
760 #[inline(always)]
762 pub fn unfocusable(mut self) -> Self {
763 self.focusable = false;
764 self
765 }
766
767 #[inline(always)]
769 pub fn scrollable(mut self, max_scroll: i16) -> Self {
770 self.scrollable_max = Some(max_scroll.max(0));
771 self.clip_children = true;
772 self
773 }
774
775 #[inline(always)]
777 pub fn scrollable_auto(mut self) -> Self {
778 self.scrollable_max = Some(i16::MAX);
779 self.clip_children = true;
780 self
781 }
782
783 #[inline(always)]
785 pub fn align_self(mut self, align: AlignItems) -> Self {
786 self.align_self = Some(align);
787 self
788 }
789
790 #[inline(always)]
792 pub fn self_start(self) -> Self {
793 self.align_self(AlignItems::Start)
794 }
795
796 #[inline(always)]
798 pub fn self_center(self) -> Self {
799 self.align_self(AlignItems::Center)
800 }
801
802 #[inline(always)]
804 pub fn self_end(self) -> Self {
805 self.align_self(AlignItems::End)
806 }
807
808 #[inline(always)]
810 pub fn self_stretch(self) -> Self {
811 self.align_self(AlignItems::Stretch)
812 }
813
814 #[inline(always)]
816 pub fn items_baseline(mut self) -> Self {
817 self.items_baseline = true;
818 self
819 }
820
821 #[inline(always)]
823 pub fn overflow_hidden(mut self) -> Self {
824 self.clip_children = true;
825 self
826 }
827
828 #[inline(always)]
830 pub fn overflow_visible(mut self) -> Self {
831 self.clip_children = false;
832 self
833 }
834
835 #[inline(always)]
837 pub fn overflow_scroll(self) -> Self {
838 self.scrollable_auto()
839 }
840
841 #[inline(always)]
843 pub fn overflow_y_scroll(self) -> Self {
844 self.flex_col().scrollable_auto()
845 }
846
847 #[inline(always)]
849 pub fn overflow_x_scroll(self) -> Self {
850 self.flex_row().scrollable_auto()
851 }
852
853 #[inline(always)]
855 pub fn transition(self) -> Self {
856 self.animate_all(Duration::from_millis(150))
857 }
858
859 #[inline(always)]
861 pub fn transition_colors(self) -> Self {
862 self.animate_color(Duration::from_millis(150))
863 }
864
865 #[inline(always)]
867 pub fn transition_opacity(self) -> Self {
868 self.animate_opacity(Duration::from_millis(150))
869 }
870
871 #[inline(always)]
873 pub fn transition_all(self) -> Self {
874 self.animate_all(Duration::from_millis(150))
875 }
876
877 #[inline(always)]
879 pub fn transition_none(mut self) -> Self {
880 self.transition = None;
881 self
882 }
883
884 #[inline(always)] pub fn duration_75(self) -> Self { self.duration(Duration::from_millis(75)) }
885 #[inline(always)] pub fn duration_100(self) -> Self { self.duration(Duration::from_millis(100)) }
886 #[inline(always)] pub fn duration_150(self) -> Self { self.duration(Duration::from_millis(150)) }
887 #[inline(always)] pub fn duration_200(self) -> Self { self.duration(Duration::from_millis(200)) }
888 #[inline(always)] pub fn duration_300(self) -> Self { self.duration(Duration::from_millis(300)) }
889 #[inline(always)] pub fn duration_500(self) -> Self { self.duration(Duration::from_millis(500)) }
890
891 #[inline(always)] pub fn ease_linear(self) -> Self { self.ease(Easing::Linear) }
892 #[inline(always)] pub fn ease_in(self) -> Self { self.ease(Easing::EaseIn) }
893 #[inline(always)] pub fn ease_out(self) -> Self { self.ease(Easing::EaseOut) }
894 #[inline(always)] pub fn ease_in_out(self) -> Self { self.ease(Easing::EaseInOut) }
895 #[inline(always)] pub fn ease_spring(self) -> Self { self.ease(Easing::Spring) }
896
897 #[inline(always)]
899 pub fn animate_color(mut self, duration: Duration) -> Self {
900 let ms = duration.as_millis().min(u16::MAX as u128) as u16;
901 let mut spec = self.transition.unwrap_or(TransitionSpec {
902 duration_ms: ms,
903 ..TransitionSpec::default()
904 });
905 spec.duration_ms = ms;
906 spec.animate_color = true;
907 self.transition = Some(spec);
908 self
909 }
910
911 #[inline(always)]
913 pub fn animate_bg(self, duration: Duration) -> Self {
914 self.animate_color(duration)
915 }
916
917 #[inline(always)]
919 pub fn animate_bounds(mut self, duration: Duration) -> Self {
920 let ms = duration.as_millis().min(u16::MAX as u128) as u16;
921 let mut spec = self.transition.unwrap_or(TransitionSpec {
922 duration_ms: ms,
923 ..TransitionSpec::default()
924 });
925 spec.duration_ms = ms;
926 spec.animate_bounds = true;
927 self.transition = Some(spec);
928 self
929 }
930
931 #[inline(always)]
933 pub fn ease(mut self, easing: Easing) -> Self {
934 let mut spec = self.transition.unwrap_or_default();
935 spec.easing = easing;
936 self.transition = Some(spec);
937 self
938 }
939
940 #[inline(always)]
942 pub fn repeat_infinite(mut self) -> Self {
943 let mut spec = self.transition.unwrap_or_default();
944 spec.loop_mode = LoopMode::Infinite;
945 self.transition = Some(spec);
946 self
947 }
948
949 #[inline(always)]
951 pub fn repeat_ping_pong(mut self) -> Self {
952 let mut spec = self.transition.unwrap_or_default();
953 spec.loop_mode = LoopMode::PingPong;
954 self.transition = Some(spec);
955 self
956 }
957
958 #[inline(always)]
960 pub fn duration(mut self, duration: Duration) -> Self {
961 let mut spec = self.transition.unwrap_or_default();
962 spec.duration_ms = duration_to_ms(duration);
963 self.transition = Some(spec);
964 self
965 }
966
967 #[inline(always)]
969 pub fn delay(mut self, delay: Duration) -> Self {
970 let mut spec = self.transition.unwrap_or_default();
971 spec.delay_ms = duration_to_ms(delay);
972 self.transition = Some(spec);
973 self
974 }
975
976 #[inline(always)]
978 pub fn animate_opacity(mut self, duration: Duration) -> Self {
979 let mut spec = self.transition.unwrap_or_default();
980 spec.duration_ms = duration_to_ms(duration);
981 spec.animate_opacity = true;
982 self.transition = Some(spec);
983 self
984 }
985
986 #[inline(always)]
988 pub fn animate_all(mut self, duration: Duration) -> Self {
989 let mut spec = self.transition.unwrap_or_default();
990 spec.duration_ms = duration_to_ms(duration);
991 spec.animate_color = true;
992 spec.animate_bounds = true;
993 spec.animate_opacity = true;
994 self.transition = Some(spec);
995 self
996 }
997
998 #[inline(always)]
1000 pub fn enter_from(mut self, opacity: u8, dx: i16, dy: i16, duration: Duration) -> Self {
1001 let mut spec = self.transition.unwrap_or_default();
1002 spec.duration_ms = duration_to_ms(duration);
1003 spec.enter = Some(EnterFrom { opacity, dx, dy });
1004 self.transition = Some(spec);
1005 self
1006 }
1007
1008 #[inline(always)]
1010 pub fn fade_in(self, duration: Duration) -> Self {
1011 self.enter_from(0, 0, 0, duration)
1012 }
1013
1014 #[inline(always)]
1016 pub fn slide_in(self, dx: i16, dy: i16, duration: Duration) -> Self {
1017 self.enter_from(0, dx, dy, duration)
1018 }
1019
1020 #[inline(always)]
1022 pub fn ping_pong_bg(mut self, from_bg: Color, duration: Duration) -> Self {
1023 let ms = duration.as_millis().min(u16::MAX as u128) as u16;
1024 let mut spec = self.transition.unwrap_or_default();
1025 spec.duration_ms = ms;
1026 spec.animate_color = true;
1027 spec.loop_mode = LoopMode::PingPong;
1028 spec.easing = Easing::EaseInOut;
1029 spec.pulse_from_bg = Some(from_bg);
1030 self.transition = Some(spec);
1031 self
1032 }
1033
1034 #[inline(always)]
1036 pub fn on_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1037 self.on_click = Some(handler);
1038 self.focusable = true;
1039 self
1040 }
1041
1042 #[inline(always)]
1044 pub fn on_click(
1045 mut self,
1046 cx: &'a FrameScope,
1047 handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
1048 ) -> Self {
1049 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1050 let (x, y) = acx.click_position();
1051 handler(crate::pod::ClickEvent { x, y }, acx);
1052 });
1053 self.on_click = Some(wrapped);
1054 self.focusable = true;
1055 self
1056 }
1057
1058 #[inline(always)]
1060 pub fn on_secondary_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1061 self.on_secondary_click = Some(handler);
1062 self
1063 }
1064
1065 #[inline(always)]
1067 pub fn on_secondary_click(
1068 mut self,
1069 cx: &'a FrameScope,
1070 handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
1071 ) -> Self {
1072 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1073 let (x, y) = acx.click_position();
1074 handler(crate::pod::ClickEvent { x, y }, acx);
1075 });
1076 self.on_secondary_click = Some(wrapped);
1077 self
1078 }
1079
1080 #[inline(always)]
1082 pub fn on_drag_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1083 self.on_drag = Some(handler);
1084 self
1085 }
1086
1087 #[inline(always)]
1089 pub fn on_drag(
1090 mut self,
1091 cx: &'a FrameScope,
1092 handler: &'a dyn Fn(DragEvent, &mut ActionScope),
1093 ) -> Self {
1094 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1095 let ev = acx.drag_event();
1096 handler(ev, acx);
1097 });
1098 self.on_drag = Some(wrapped);
1099 self
1100 }
1101
1102 #[inline(always)]
1104 pub fn on_swipe(
1105 mut self,
1106 _cx: &'a FrameScope,
1107 handler: &'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope),
1108 ) -> Self {
1109 self.on_swipe = Some(handler);
1110 self
1111 }
1112
1113 #[inline(always)]
1115 pub fn key(mut self, key: impl AsRef<[u8]>) -> Self {
1116 let mut h: u32 = 0x811c_9dc5;
1117 for &b in key.as_ref() {
1118 h ^= b as u32;
1119 h = h.wrapping_mul(0x0100_0193);
1120 }
1121 self.id = Some(h | 1);
1122 self
1123 }
1124
1125 #[inline(always)]
1127 pub fn z_index(mut self, z: i8) -> Self {
1128 self.z_index = z;
1129 self
1130 }
1131
1132 #[inline(always)]
1134 pub fn on_key_input(
1135 mut self,
1136 handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
1137 ) -> Self {
1138 self.on_key_input = Some(handler);
1139 self.focusable = true;
1140 self
1141 }
1142
1143 #[inline(always)]
1145 pub fn on_key_nav(
1146 mut self,
1147 handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
1148 ) -> Self {
1149 self.on_key_nav = Some(handler);
1150 self
1151 }
1152
1153 #[inline(always)]
1155 pub fn on_navigate_back(
1156 mut self,
1157 handler: &'a dyn Fn(&mut ActionScope),
1158 ) -> Self {
1159 self.on_navigate_back = Some(handler);
1160 self
1161 }
1162
1163 #[inline(always)]
1165 pub fn shadow(mut self, blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
1166 if blur > 0 && color.a() > 0 {
1167 self.shadow = Some((blur.min(64), offset_x, offset_y, color));
1168 }
1169 self
1170 }
1171
1172 #[inline(always)]
1174 pub fn grid(mut self, cols: u8) -> Self {
1175 self.grid_cols = cols.max(1);
1176 self
1177 }
1178
1179 pub fn child(mut self, cx: &'a FrameScope, child: impl IntoElement<'a>) -> Self {
1181 let elem = child.into_element(cx);
1182 let node: &'a ElementNode<'a> = cx.arena().alloc_temp(ElementNode {
1183 element: elem,
1184 next_sibling: Cell::new(None),
1185 });
1186 if let Some(last) = self.last_child {
1187 last.next_sibling.set(Some(node));
1188 } else {
1189 self.first_child = Some(node);
1190 }
1191 self.last_child = Some(node);
1192 self
1193 }
1194
1195 #[inline(always)]
1197 pub fn child_opt(self, cx: &'a FrameScope, child: Option<impl IntoElement<'a>>) -> Self {
1198 match child {
1199 Some(c) => self.child(cx, c),
1200 None => self,
1201 }
1202 }
1203
1204 pub fn children<I, E>(mut self, cx: &'a FrameScope, iter: I) -> Self
1206 where
1207 I: IntoIterator<Item = E>,
1208 E: IntoElement<'a>,
1209 {
1210 for item in iter {
1211 self = self.child(cx, item);
1212 }
1213 self
1214 }
1215
1216 #[inline(always)]
1218 pub fn justify_start_localized(mut self, cx: &FrameScope) -> Self {
1219 if cx.is_rtl() {
1220 self.style.set_justify(crate::style::Justify::End);
1221 } else {
1222 self.style.set_justify(crate::style::Justify::Start);
1223 }
1224 self
1225 }
1226
1227 #[inline(always)]
1229 pub fn justify_end_localized(mut self, cx: &FrameScope) -> Self {
1230 if cx.is_rtl() {
1231 self.style.set_justify(crate::style::Justify::Start);
1232 } else {
1233 self.style.set_justify(crate::style::Justify::End);
1234 }
1235 self
1236 }
1237
1238 #[inline(always)]
1240 pub fn ps(self, cx: &FrameScope, px: i8) -> Self {
1241 if cx.is_rtl() {
1242 self.pr(px)
1243 } else {
1244 self.pl(px)
1245 }
1246 }
1247
1248 #[inline(always)]
1250 pub fn pe(self, cx: &FrameScope, px: i8) -> Self {
1251 if cx.is_rtl() {
1252 self.pl(px)
1253 } else {
1254 self.pr(px)
1255 }
1256 }
1257
1258 #[inline(always)]
1260 pub fn ms(self, cx: &FrameScope, px: i16) -> Self {
1261 if cx.is_rtl() {
1262 self.mr(px)
1263 } else {
1264 self.ml(px)
1265 }
1266 }
1267
1268 #[inline(always)]
1270 pub fn me(self, cx: &FrameScope, px: i16) -> Self {
1271 if cx.is_rtl() {
1272 self.ml(px)
1273 } else {
1274 self.mr(px)
1275 }
1276 }
1277
1278 #[inline(always)]
1284 pub fn rounded_corners(mut self, radii: CornerRadii) -> Self {
1285 self.effects.corner_radii = Some(radii);
1286 self
1287 }
1288
1289 #[inline(always)]
1291 pub fn rounded_t(self, r: u8) -> Self {
1292 self.rounded_corners(CornerRadii::top(r))
1293 }
1294
1295 #[inline(always)]
1297 pub fn rounded_b(self, r: u8) -> Self {
1298 self.rounded_corners(CornerRadii::bottom(r))
1299 }
1300
1301 #[inline(always)]
1303 pub fn rounded_l(self, r: u8) -> Self {
1304 self.rounded_corners(CornerRadii::left(r))
1305 }
1306
1307 #[inline(always)]
1309 pub fn rounded_r(self, r: u8) -> Self {
1310 self.rounded_corners(CornerRadii::right(r))
1311 }
1312
1313 #[inline(always)]
1315 pub fn bg_paint(mut self, paint: Paint<'a>) -> Self {
1316 self.effects.bg_paint = Some(paint);
1317 self
1318 }
1319
1320 #[inline]
1322 pub fn bg_linear_gradient(
1323 mut self,
1324 cx: &'a FrameScope,
1325 angle_deg: f32,
1326 stops: &[ColorStop],
1327 ) -> Self {
1328 let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
1329 self.effects.bg_paint = Some(Paint::linear_angle(angle_deg, arena_stops));
1330 self
1331 }
1332
1333 #[inline]
1335 pub fn bg_radial_gradient(mut self, cx: &'a FrameScope, stops: &[ColorStop]) -> Self {
1336 let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
1337 self.effects.bg_paint = Some(Paint::radial(arena_stops));
1338 self
1339 }
1340
1341 #[inline]
1343 pub fn bg_radial_focal_gradient(
1344 mut self,
1345 cx: &'a FrameScope,
1346 center: [f32; 2],
1347 radius: f32,
1348 focal: [f32; 2],
1349 focal_radius: f32,
1350 stops: &[ColorStop],
1351 ) -> Self {
1352 let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
1353 self.effects.bg_paint = Some(Paint::radial_focal(
1354 center,
1355 radius,
1356 focal,
1357 focal_radius,
1358 arena_stops,
1359 ));
1360 self
1361 }
1362
1363 #[inline]
1365 pub fn bg_conic_gradient(
1366 mut self,
1367 cx: &'a FrameScope,
1368 start_angle_deg: f32,
1369 stops: &[ColorStop],
1370 ) -> Self {
1371 let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
1372 self.effects.bg_paint = Some(Paint::conic(start_angle_deg, arena_stops));
1373 self
1374 }
1375
1376 #[inline]
1378 pub fn bg_gradient_extend(mut self, mode: ExtendMode) -> Self {
1379 if let Some(p) = self.effects.bg_paint {
1380 self.effects.bg_paint = Some(p.with_extend(mode));
1381 }
1382 self
1383 }
1384
1385 #[inline]
1387 pub fn bg_gradient_space(mut self, space: GradientSpace) -> Self {
1388 if let Some(p) = self.effects.bg_paint {
1389 self.effects.bg_paint = Some(p.with_space(space));
1390 }
1391 self
1392 }
1393
1394 #[inline(always)]
1396 pub fn blend_mode(mut self, mode: BlendMode) -> Self {
1397 self.effects.blend_mode = mode;
1398 self
1399 }
1400
1401 #[inline(always)]
1403 pub fn isolate(mut self) -> Self {
1404 self.effects.isolate = true;
1405 self
1406 }
1407
1408 #[inline(always)]
1410 pub fn filter(mut self, filter: LayerFilter) -> Self {
1411 self.effects.filter = filter;
1412 self
1413 }
1414
1415 #[inline(always)]
1417 pub fn blur(mut self, radius: u8) -> Self {
1418 self.effects.filter.blur_radius = radius;
1419 self
1420 }
1421
1422 #[inline]
1424 pub fn color_matrix(mut self, matrix: ColorMatrix) -> Self {
1425 self.effects.filter.color_matrix = Some(match self.effects.filter.color_matrix {
1426 Some(prev) => prev.then(matrix),
1427 None => matrix,
1428 });
1429 self
1430 }
1431
1432 #[inline]
1434 pub fn grayscale(self, amount: f32) -> Self {
1435 self.color_matrix(ColorMatrix::grayscale(amount))
1436 }
1437
1438 #[inline]
1440 pub fn sepia(self, amount: f32) -> Self {
1441 self.color_matrix(ColorMatrix::sepia(amount))
1442 }
1443
1444 #[inline]
1446 pub fn saturate(self, amount: f32) -> Self {
1447 self.color_matrix(ColorMatrix::saturate(amount))
1448 }
1449
1450 #[inline]
1452 pub fn brightness(self, amount: f32) -> Self {
1453 self.color_matrix(ColorMatrix::brightness(amount))
1454 }
1455
1456 #[inline]
1458 pub fn contrast(self, amount: f32) -> Self {
1459 self.color_matrix(ColorMatrix::contrast(amount))
1460 }
1461
1462 #[inline]
1464 pub fn hue_rotate(self, deg: f32) -> Self {
1465 self.color_matrix(ColorMatrix::hue_rotate(deg))
1466 }
1467
1468 #[inline]
1470 pub fn invert(self, amount: f32) -> Self {
1471 self.color_matrix(ColorMatrix::invert(amount))
1472 }
1473
1474 #[inline(always)]
1476 pub fn drop_shadow(mut self, blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
1477 self.effects.filter.drop_shadow = Some(DropShadow::new(blur, offset_x, offset_y, color));
1478 self
1479 }
1480
1481 #[inline(always)]
1483 pub fn backdrop_filter(mut self, filter: BackdropFilter) -> Self {
1484 self.effects.backdrop_filter = filter;
1485 self
1486 }
1487
1488 #[inline(always)]
1490 pub fn backdrop_blur(mut self, radius: u8) -> Self {
1491 self.effects.backdrop_filter.blur_radius = radius;
1492 self
1493 }
1494
1495 #[inline]
1497 pub fn backdrop_acrylic(mut self, blur_radius: u8, tint: Color) -> Self {
1498 self.effects.backdrop_filter = BackdropFilter::acrylic(blur_radius, tint);
1499 self
1500 }
1501
1502 #[inline]
1504 pub fn transform(mut self, t: Transform2D) -> Self {
1505 self.effects.transform = Some(match self.effects.transform {
1506 Some(prev) => prev.concat(t),
1507 None => t,
1508 });
1509 self
1510 }
1511
1512 #[inline]
1514 pub fn rotate_deg(self, deg: f32) -> Self {
1515 self.transform(Transform2D::rotate(deg))
1516 }
1517
1518 #[inline]
1520 pub fn scale(self, sx: f32, sy: f32) -> Self {
1521 self.transform(Transform2D::scale(sx, sy))
1522 }
1523
1524 #[inline]
1526 pub fn translate(self, tx: f32, ty: f32) -> Self {
1527 self.transform(Transform2D::translate(tx, ty))
1528 }
1529
1530 #[inline]
1532 pub fn skew(self, ax_deg: f32, ay_deg: f32) -> Self {
1533 let mut s = self;
1534 if ax_deg != 0.0 {
1535 s = s.transform(Transform2D::skew_x(ax_deg));
1536 }
1537 if ay_deg != 0.0 {
1538 s = s.transform(Transform2D::skew_y(ay_deg));
1539 }
1540 s
1541 }
1542
1543 #[inline(always)]
1545 pub fn clip_path(mut self, path_d: &'static str) -> Self {
1546 self.effects.clip_path = Some(StrRef::Static(path_d));
1547 self
1548 }
1549
1550 #[inline]
1552 pub fn clip_path_str(mut self, cx: &'a FrameScope, path_d: &str) -> Self {
1553 let buf = cx.arena().alloc_bytes(path_d.len());
1554 buf.copy_from_slice(path_d.as_bytes());
1555 let s = unsafe { core::str::from_utf8_unchecked(buf) };
1557 self.effects.clip_path = Some(StrRef::Scratch(s));
1558 self
1559 }
1560
1561 #[inline(always)]
1566 pub fn external_texture(mut self, id: crate::paint::ExternalTextureId) -> Self {
1567 self.external_texture = Some(crate::paint::ExternalTextureSpec::new(id));
1568 self
1569 }
1570
1571 #[inline(always)]
1573 pub fn external_texture_spec(mut self, spec: crate::paint::ExternalTextureSpec) -> Self {
1574 self.external_texture = Some(spec);
1575 self
1576 }
1577
1578 #[inline(always)]
1580 pub fn external_texture_uv(mut self, uv_rect: [f32; 4]) -> Self {
1581 if let Some(ext) = &mut self.external_texture {
1582 ext.uv_rect = uv_rect;
1583 }
1584 self
1585 }
1586
1587 #[inline(always)]
1589 pub fn external_texture_revision(mut self, revision: u64) -> Self {
1590 if let Some(ext) = &mut self.external_texture {
1591 ext.content_revision = revision;
1592 }
1593 self
1594 }
1595
1596 #[inline(always)]
1598 pub fn external_texture_format(mut self, premultiplied: bool, fallback_bg: Color) -> Self {
1599 if let Some(ext) = &mut self.external_texture {
1600 ext.premultiplied = premultiplied;
1601 ext.fallback_bg = fallback_bg;
1602 }
1603 self
1604 }
1605}
1606