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()
30 || self.external_texture.is_some()
31 || !self.layout_ext.is_empty();
32
33 let alloc_visual = |extended: DivExtPtr<'a>,
34 transition: Option<&'a TransitionSpec>|
35 -> &'a DivVisual<'a> {
36 let (pos_x, pos_y) = self.pos.unwrap_or((0, 0));
37 let (bg_grad_to, bg_grad_horiz) = self.bg_grad.unwrap_or((Color::TRANSPARENT, false));
38 let (focus_border_w, focus_border_color) = match self.focus_border {
39 Some((w, c)) => (w.min(254), c),
40 None => (FOCUS_BORDER_NONE, Color::TRANSPARENT),
41 };
42 let (shadow_blur, shadow_ox, shadow_oy, shadow_color) =
43 self.shadow.unwrap_or((0, 0, 0, Color::TRANSPARENT));
44 let extra_flags = pack_visual_flags(
45 self.pos.is_some(),
46 self.scrollable_max.is_some(),
47 self.bg_grad.is_some(),
48 bg_grad_horiz,
49 self.items_baseline,
50 self.align_self,
51 self.is_ime_caret,
52 );
53 &*cx.arena().alloc_temp(DivVisual {
54 extended,
55 transition,
56 margin: self.margin,
57 extra_padding: self.extra_padding,
58 bg_grad_to,
59 focus_border_color,
60 shadow_color,
61 min_w: self.min_w,
62 max_w: self.max_w,
63 min_h: self.min_h,
64 max_h: self.max_h,
65 pos_x,
66 pos_y,
67 raw_scrollable_max: self.scrollable_max.unwrap_or(0),
68 extra_flags,
69 focus_border_w,
70 shadow_blur,
71 shadow_ox,
72 shadow_oy,
73 cursor: self.cursor,
74 })
75 };
76
77 let visual = if self.has_non_focus_visual_extra() {
78 let state_styles = if has_state_styles {
79 let hover_style = self.hover_style.map(|r| &*cx.arena().alloc_temp(r));
80 let active_style = self.active_style.map(|r| &*cx.arena().alloc_temp(r));
81 let focus_style = self.focus_style.map(|r| &*cx.arena().alloc_temp(r));
82 Some(&*cx.arena().alloc_temp(DivStateStyles {
83 hover_style,
84 active_style,
85 focus_style,
86 text_color: self.text_color.unwrap_or(Color::TRANSPARENT),
87 text_size: self.text_size.unwrap_or(0),
88 flags: if self.text_color.is_some() { 1 } else { 0 },
89 }))
90 } else {
91 None
92 };
93 let effects = if has_effects {
94 if let Some(ext_spec) = self.external_texture {
95 self.effects.external_texture = Some(&*cx.arena().alloc_temp(ext_spec));
96 }
97 if !self.layout_ext.is_empty() {
98 self.effects.layout_ext = Some(&*cx.arena().alloc_temp(self.layout_ext));
99 }
100 Some(&*cx.arena().alloc_temp(self.effects))
101 } else {
102 None
103 };
104 let extended = match (state_styles, effects) {
105 (None, None) => DivExtPtr::NONE,
106 (Some(s), None) => DivExtPtr::from_state_styles(s),
107 (None, Some(e)) => DivExtPtr::from_effects(e),
108 (Some(s), Some(e)) => DivExtPtr::from_both(&*cx.arena().alloc_temp(DivExtended {
109 state_styles: Some(s),
110 effects: Some(e),
111 })),
112 };
113 let transition = self.transition.map(|t| &*cx.arena().alloc_temp(t));
114 Some(alloc_visual(extended, transition))
115 } else if let Some((fw, fc)) = self.focus_border {
116 if fw == 0 && fc == Color::TRANSPARENT {
117 Some(&NO_FOCUS_BORDER_DIV_VISUAL)
118 } else {
119 Some(alloc_visual(DivExtPtr::NONE, None))
120 }
121 } else {
122 None
123 };
124
125 let interact = if self.has_interactivity() {
126 let rare = if self.has_rare_handlers() {
127 Some(&*cx.arena().alloc(DivRareHandlers {
128 on_long_press: self.on_long_press,
129 on_drag: self.on_drag,
130 on_hover: self.on_hover,
131 on_pointer_move: self.on_pointer_move,
132 on_wheel: self.on_wheel,
133 on_pinch: self.on_pinch,
134 on_swipe: self.on_swipe,
135 on_key_input: self.on_key_input,
136 on_key_nav: self.on_key_nav,
137 on_navigate_back: self.on_navigate_back,
138 on_secondary_click: self.on_secondary_click,
139 }))
140 } else {
141 None
142 };
143 Some(&*cx.arena().alloc(DivInteractivity {
144 on_click: self.on_click,
145 rare,
146 }))
147 } else {
148 None
149 };
150
151 let mut flags = 0u8;
152 if self.focusable {
153 flags |= 1 << 0;
154 }
155 if self.is_overlay {
156 flags |= 1 << 1;
157 }
158 if self.occlude_hit {
159 flags |= 1 << 2;
160 }
161 if self.auto_repeat {
162 flags |= 1 << 3;
163 }
164 if self.clip_children {
165 flags |= 1 << 4;
166 }
167 if self.id.is_some() {
168 flags |= 1 << 5;
169 }
170 if self.active_bg.is_some() {
171 flags |= 1 << 6;
172 }
173 if self.hover_bg.is_some() {
174 flags |= 1 << 7;
175 }
176
177 Element::Div(DivNode {
178 style: self.style,
179 first_child: self.first_child,
180 visual,
181 interact,
182 raw_id: self.id.unwrap_or(0),
183 raw_active_bg: self.active_bg.unwrap_or(Color::TRANSPARENT),
184 raw_hover_bg: self.hover_bg.unwrap_or(Color::TRANSPARENT),
185 opacity: self.opacity,
186 z_index: self.z_index,
187 grid_cols: self.grid_cols,
188 flags,
189 })
190 }
191}
192
193
194pub struct Div<'a> {
196 pub id: Option<u32>,
198 pub style: Style,
200 pub margin: Edges<i16>,
202 pub extra_padding: Edges<i16>,
204 pub min_w: i16,
206 pub max_w: i16,
208 pub min_h: i16,
210 pub max_h: i16,
212 pub opacity: u8,
214 pub active_bg: Option<Color>,
216 pub hover_bg: Option<Color>,
218 pub bg_grad: Option<(Color, bool)>,
220 pub focus_border: Option<(u8, Color)>,
222 pub focusable: bool,
224 pub is_overlay: bool,
226 pub occlude_hit: bool,
228 pub auto_repeat: bool,
230 pub scrollable_max: Option<i16>,
232 pub pos: Option<(i16, i16)>,
234 pub first_child: Option<&'a ElementNode<'a>>,
236 pub last_child: Option<&'a ElementNode<'a>>,
238 pub on_click: Option<&'a dyn Fn(&mut ActionScope)>,
240 pub on_long_press: Option<&'a dyn Fn(&mut ActionScope)>,
242 pub on_drag: Option<&'a dyn Fn(&mut ActionScope)>,
244 pub on_hover: Option<&'a dyn Fn(bool, &mut ActionScope)>,
246 pub on_pointer_move: Option<&'a dyn Fn(&mut ActionScope)>,
248 pub on_wheel: Option<&'a dyn Fn(crate::pod::WheelEvent, &mut ActionScope)>,
250 pub on_pinch: Option<&'a dyn Fn(crate::pod::PinchEvent, &mut ActionScope)>,
252 pub on_swipe: Option<&'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope)>,
254 pub on_key_input: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
256 pub on_key_nav: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
258 pub on_navigate_back: Option<&'a dyn Fn(&mut ActionScope)>,
260 pub on_secondary_click: Option<&'a dyn Fn(&mut ActionScope)>,
262 pub z_index: i8,
264 pub shadow: Option<(u8, i8, i8, Color)>,
266 pub grid_cols: u8,
268 pub transition: Option<TransitionSpec>,
270 pub clip_children: bool,
272 pub align_self: Option<AlignItems>,
274 pub cursor: Option<CursorShape>,
276 pub text_color: Option<Color>,
278 pub text_size: Option<u8>,
280 pub hover_style: Option<StyleRefinement>,
282 pub active_style: Option<StyleRefinement>,
284 pub focus_style: Option<StyleRefinement>,
286 pub items_baseline: bool,
288 pub is_ime_caret: bool,
290 pub effects: PaintEffects<'a>,
292 pub external_texture: Option<crate::paint::ExternalTextureSpec>,
294 pub layout_ext: crate::paint::LayoutExtras<'a>,
296}
297
298#[inline(always)]
300pub fn div<'a>() -> Div<'a> {
301 Div {
302 id: None,
303 style: Style::default(),
304 margin: Edges::all(0),
305 extra_padding: Edges::all(0),
306 min_w: 0,
307 max_w: 0,
308 min_h: 0,
309 max_h: 0,
310 opacity: 255,
311 active_bg: None,
312 hover_bg: None,
313 bg_grad: None,
314 focus_border: None,
315 focusable: false,
316 is_overlay: false,
317 occlude_hit: false,
318 auto_repeat: false,
319 scrollable_max: None,
320 pos: None,
321 first_child: None,
322 last_child: None,
323 on_click: None,
324 on_long_press: None,
325 on_drag: None,
326 on_hover: None,
327 on_pointer_move: None,
328 on_wheel: None,
329 on_pinch: None,
330 on_swipe: None,
331 on_key_input: None,
332 on_key_nav: None,
333 on_navigate_back: None,
334 on_secondary_click: None,
335 z_index: 0,
336 shadow: None,
337 grid_cols: 0,
338 transition: None,
339 clip_children: false,
340 align_self: None,
341 cursor: None,
342 text_color: None,
343 text_size: None,
344 hover_style: None,
345 active_style: None,
346 focus_style: None,
347 items_baseline: false,
348 is_ime_caret: false,
349 effects: PaintEffects::EMPTY,
350 external_texture: None,
351 layout_ext: crate::paint::LayoutExtras::EMPTY,
352 }
353}
354
355impl<'a> Styled for Div<'a> {
356 #[inline(always)]
357 fn style_mut(&mut self) -> &mut Style {
358 &mut self.style
359 }
360}
361
362impl<'a> Div<'a> {
363 #[inline(always)]
366 pub const fn ime_caret(mut self) -> Self {
367 self.is_ime_caret = true;
368 self
369 }
370
371 #[inline(always)]
372 fn has_non_focus_visual_extra(&self) -> bool {
373 self.margin != Edges::all(0)
374 || self.extra_padding != Edges::all(0)
375 || self.min_w != 0
376 || self.max_w != 0
377 || self.min_h != 0
378 || self.max_h != 0
379 || self.bg_grad.is_some()
380 || self.scrollable_max.is_some()
381 || self.pos.is_some()
382 || self.shadow.is_some()
383 || self.transition.is_some()
384 || self.align_self.is_some()
385 || self.is_ime_caret
386 || self.cursor.is_some()
387 || self.text_color.is_some()
388 || self.text_size.is_some()
389 || self.hover_style.is_some()
390 || self.active_style.is_some()
391 || self.focus_style.is_some()
392 || self.items_baseline
393 || !self.effects.is_empty()
394 || self.external_texture.is_some()
395 || !self.layout_ext.is_empty()
396 }
397
398 #[inline(always)]
399 fn has_interactivity(&self) -> bool {
400 self.on_click.is_some() || self.has_rare_handlers()
401 }
402
403 #[inline(always)]
404 fn has_rare_handlers(&self) -> bool {
405 self.on_long_press.is_some()
406 || self.on_drag.is_some()
407 || self.on_hover.is_some()
408 || self.on_pointer_move.is_some()
409 || self.on_wheel.is_some()
410 || self.on_pinch.is_some()
411 || self.on_swipe.is_some()
412 || self.on_key_input.is_some()
413 || self.on_key_nav.is_some()
414 || self.on_navigate_back.is_some()
415 || self.on_secondary_click.is_some()
416 }
417
418 #[inline(always)]
420 pub fn id(mut self, id: u32) -> Self {
421 self.id = Some(id);
422 self
423 }
424
425 #[inline(always)]
427 pub fn scoped_id(mut self, cx: &FrameScope, local_id: u32) -> Self {
428 self.id = Some(cx.scoped_id(local_id));
429 self
430 }
431
432 #[inline(always)]
434 pub fn pos(mut self, x: i16, y: i16) -> Self {
435 self.pos = Some((x, y));
436 self
437 }
438
439 #[inline(always)]
441 pub fn safe_area(mut self, cx: &FrameScope) -> Self {
442 let sa = cx.safe_area();
443 let kb = cx.keyboard_inset();
444 self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
445 self.extra_padding.right = self.extra_padding.right.saturating_add(sa.right.max(0));
446 self.extra_padding.bottom = self
447 .extra_padding
448 .bottom
449 .saturating_add(sa.bottom.saturating_add(kb).max(0));
450 self.extra_padding.left = self.extra_padding.left.saturating_add(sa.left.max(0));
451 self
452 }
453
454 #[inline(always)]
456 pub fn pt_safe(mut self, cx: &FrameScope) -> Self {
457 let sa = cx.safe_area();
458 self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
459 self
460 }
461
462 #[inline(always)]
464 pub fn pb_safe(mut self, cx: &FrameScope) -> Self {
465 let sa = cx.safe_area();
466 let kb = cx.keyboard_inset();
467 self.extra_padding.bottom = self
468 .extra_padding
469 .bottom
470 .saturating_add(sa.bottom.saturating_add(kb).max(0));
471 self
472 }
473
474 #[inline(always)]
476 pub fn mx(mut self, px: i16) -> Self {
477 self.margin.left = px;
478 self.margin.right = px;
479 self
480 }
481
482 #[inline(always)]
484 pub fn my(mut self, px: i16) -> Self {
485 self.margin.top = px;
486 self.margin.bottom = px;
487 self
488 }
489
490 #[inline(always)]
492 pub fn mt(mut self, px: i16) -> Self {
493 self.margin.top = px;
494 self
495 }
496
497 #[inline(always)]
499 pub fn mb(mut self, px: i16) -> Self {
500 self.margin.bottom = px;
501 self
502 }
503
504 #[inline(always)]
506 pub fn ml(mut self, px: i16) -> Self {
507 self.margin.left = px;
508 self
509 }
510
511 #[inline(always)]
513 pub fn mr(mut self, px: i16) -> Self {
514 self.margin.right = px;
515 self
516 }
517
518 #[inline(always)]
520 pub fn min_w(mut self, px: i16) -> Self {
521 self.min_w = px.max(0);
522 self
523 }
524
525 #[inline(always)]
527 pub fn max_w(mut self, px: i16) -> Self {
528 self.max_w = px.max(0);
529 self
530 }
531
532 #[inline(always)]
534 pub fn min_h(mut self, px: i16) -> Self {
535 self.min_h = px.max(0);
536 self
537 }
538
539 #[inline(always)]
541 pub fn max_h(mut self, px: i16) -> Self {
542 self.max_h = px.max(0);
543 self
544 }
545
546 #[inline(always)]
548 pub fn opacity(mut self, alpha: u8) -> Self {
549 self.opacity = alpha;
550 self
551 }
552
553 #[inline(always)]
555 pub fn active_bg(mut self, color: Color) -> Self {
556 self.active_bg = Some(color);
557 self
558 }
559
560 #[inline(always)]
562 pub fn hover_bg(mut self, color: Color) -> Self {
563 self.hover_bg = Some(color);
564 self
565 }
566
567 #[inline(always)]
571 pub fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
572 let base = self.hover_style.unwrap_or_else(|| {
573 let mut r = StyleRefinement::new();
574 r.bg = self.hover_bg;
575 r
576 });
577 let refined = f(base);
578 if refined.is_empty() {
579 return self;
580 }
581 if refined.is_bg_only() {
582 self.hover_bg = refined.bg;
583 self.hover_style = None;
584 } else {
585 if let Some(bg) = refined.bg {
586 self.hover_bg = Some(bg);
587 }
588 self.hover_style = Some(refined);
589 }
590 self
591 }
592
593 #[inline(always)]
595 pub fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
596 let base = self.active_style.unwrap_or_else(|| {
597 let mut r = StyleRefinement::new();
598 r.bg = self.active_bg;
599 r
600 });
601 let refined = f(base);
602 if refined.is_empty() {
603 return self;
604 }
605 if refined.is_bg_only() {
606 self.active_bg = refined.bg;
607 self.active_style = None;
608 } else {
609 if let Some(bg) = refined.bg {
610 self.active_bg = Some(bg);
611 }
612 self.active_style = Some(refined);
613 }
614 self
615 }
616
617 #[inline(always)]
619 pub fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
620 self.focusable = true;
621 let refined = f(self.focus_style.unwrap_or_default());
622 if !refined.is_empty() {
623 self.focus_style = Some(refined);
624 }
625 self
626 }
627
628 #[inline(always)]
630 pub fn text_color(mut self, color: Color) -> Self {
631 self.text_color = Some(color);
632 self
633 }
634
635 #[inline(always)]
637 pub fn text_size(mut self, px: u8) -> Self {
638 self.text_size = Some(px);
639 self
640 }
641
642 #[inline(always)] pub fn text_xs(self) -> Self { self.text_size(11) }
644 #[inline(always)] pub fn text_sm(self) -> Self { self.text_size(12) }
645 #[inline(always)] pub fn text_base(self) -> Self { self.text_size(14) }
646 #[inline(always)] pub fn text_lg(self) -> Self { self.text_size(16) }
647 #[inline(always)] pub fn text_xl(self) -> Self { self.text_size(18) }
648 #[inline(always)] pub fn text_2xl(self) -> Self { self.text_size(22) }
649 #[inline(always)] pub fn text_3xl(self) -> Self { self.text_size(28) }
650
651 #[inline(always)]
653 pub fn opacity_f(mut self, alpha: f32) -> Self {
654 let c = if alpha <= 0.0 {
655 0.0
656 } else if alpha >= 1.0 {
657 1.0
658 } else {
659 alpha
660 };
661 self.opacity = (c * 255.0 + 0.5) as u8;
662 self
663 }
664
665 #[inline(always)] pub fn mx_0(self) -> Self { self.mx(0) }
667 #[inline(always)] pub fn mx_1(self) -> Self { self.mx(4) }
668 #[inline(always)] pub fn mx_2(self) -> Self { self.mx(8) }
669 #[inline(always)] pub fn mx_3(self) -> Self { self.mx(12) }
670 #[inline(always)] pub fn mx_4(self) -> Self { self.mx(16) }
671 #[inline(always)] pub fn my_0(self) -> Self { self.my(0) }
672 #[inline(always)] pub fn my_1(self) -> Self { self.my(4) }
673 #[inline(always)] pub fn my_2(self) -> Self { self.my(8) }
674 #[inline(always)] pub fn my_3(self) -> Self { self.my(12) }
675 #[inline(always)] pub fn my_4(self) -> Self { self.my(16) }
676 #[inline(always)] pub fn mt_1(self) -> Self { self.mt(4) }
677 #[inline(always)] pub fn mt_2(self) -> Self { self.mt(8) }
678 #[inline(always)] pub fn mt_3(self) -> Self { self.mt(12) }
679 #[inline(always)] pub fn mt_4(self) -> Self { self.mt(16) }
680 #[inline(always)] pub fn mb_1(self) -> Self { self.mb(4) }
681 #[inline(always)] pub fn mb_2(self) -> Self { self.mb(8) }
682 #[inline(always)] pub fn mb_3(self) -> Self { self.mb(12) }
683 #[inline(always)] pub fn mb_4(self) -> Self { self.mb(16) }
684 #[inline(always)] pub fn ml_1(self) -> Self { self.ml(4) }
685 #[inline(always)] pub fn ml_2(self) -> Self { self.ml(8) }
686 #[inline(always)] pub fn ml_3(self) -> Self { self.ml(12) }
687 #[inline(always)] pub fn ml_4(self) -> Self { self.ml(16) }
688 #[inline(always)] pub fn mr_1(self) -> Self { self.mr(4) }
689 #[inline(always)] pub fn mr_2(self) -> Self { self.mr(8) }
690 #[inline(always)] pub fn mr_3(self) -> Self { self.mr(12) }
691 #[inline(always)] pub fn mr_4(self) -> Self { self.mr(16) }
692
693 #[inline(always)] pub fn relative(self) -> Self { self }
696 #[inline(always)] pub fn block(self) -> Self { self.flex_col() }
698 #[inline(always)] pub fn inline_flex(self) -> Self { self.flex_row() }
700
701 #[inline(always)] pub fn cursor_pointer(self) -> Self { self.cursor(CursorShape::Pointer) }
702 #[inline(always)] pub fn cursor_default(self) -> Self { self.cursor(CursorShape::Default) }
703 #[inline(always)] pub fn cursor_text(self) -> Self { self.cursor(CursorShape::Text) }
704 #[inline(always)] pub fn cursor_not_allowed(self) -> Self { self.cursor(CursorShape::NotAllowed) }
705 #[inline(always)] pub fn cursor_grab(self) -> Self { self.cursor(CursorShape::Grab) }
706 #[inline(always)] pub fn cursor_grabbing(self) -> Self { self.cursor(CursorShape::Grabbing) }
707
708 #[inline(always)] pub fn shadow_none(mut self) -> Self { self.shadow = None; self }
709 #[inline(always)] pub fn shadow_sm(self) -> Self { self.shadow(4, 0, 1, Color::rgba(0, 0, 0, 48)) }
710 #[inline(always)] pub fn shadow_md(self) -> Self { self.shadow(10, 0, 3, Color::rgba(0, 0, 0, 72)) }
711 #[inline(always)] pub fn shadow_lg(self) -> Self { self.shadow(18, 0, 6, Color::rgba(0, 0, 0, 96)) }
712 #[inline(always)] pub fn shadow_xl(self) -> Self { self.shadow(28, 0, 10, Color::rgba(0, 0, 0, 120)) }
713
714 #[inline(always)]
716 pub fn bg_gradient(mut self, to_color: Color) -> Self {
717 self.bg_grad = Some((to_color, false));
718 self
719 }
720
721 #[inline(always)]
723 pub fn bg_gradient_x(mut self, to_color: Color) -> Self {
724 self.bg_grad = Some((to_color, true));
725 self
726 }
727
728 #[inline(always)]
730 pub fn overlay(mut self) -> Self {
731 self.is_overlay = true;
732 self
733 }
734
735 #[inline(always)]
737 pub fn occlude_hit(mut self) -> Self {
738 self.occlude_hit = true;
739 self
740 }
741
742 #[inline(always)]
744 pub fn auto_repeat(mut self) -> Self {
745 self.auto_repeat = true;
746 self
747 }
748
749 #[inline(always)]
751 pub fn on_long_press_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
752 self.on_long_press = Some(handler);
753 self.focusable = true;
754 self
755 }
756
757 #[inline(always)]
759 pub fn on_long_press(
760 mut self,
761 cx: &'a FrameScope,
762 handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
763 ) -> Self {
764 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
765 let (x, y) = acx.click_position();
766 handler(crate::pod::ClickEvent { x, y }, acx);
767 });
768 self.on_long_press = Some(wrapped);
769 self.focusable = true;
770 self
771 }
772
773 #[inline(always)]
775 pub fn focus_border(mut self, width: u8, color: Color) -> Self {
776 self.focus_border = Some((width, color));
777 self.focusable = true;
778 self
779 }
780
781 #[inline(always)]
783 pub fn no_focus_border(mut self) -> Self {
784 self.focus_border = Some((0, Color::TRANSPARENT));
785 self
786 }
787
788 #[inline(always)]
790 pub fn cursor(mut self, cursor: CursorShape) -> Self {
791 self.cursor = Some(cursor);
792 self
793 }
794
795 #[inline(always)]
797 pub fn focusable(mut self) -> Self {
798 self.focusable = true;
799 self
800 }
801
802 #[inline(always)]
804 pub fn unfocusable(mut self) -> Self {
805 self.focusable = false;
806 self
807 }
808
809 #[inline(always)]
811 pub fn scrollable(mut self, max_scroll: i16) -> Self {
812 self.scrollable_max = Some(max_scroll.max(0));
813 self.clip_children = true;
814 self
815 }
816
817 #[inline(always)]
819 pub fn scrollable_auto(mut self) -> Self {
820 self.scrollable_max = Some(i16::MAX);
821 self.clip_children = true;
822 self
823 }
824
825 #[inline(always)]
827 pub fn align_self(mut self, align: AlignItems) -> Self {
828 self.align_self = Some(align);
829 self
830 }
831
832 #[inline(always)]
834 pub fn self_start(self) -> Self {
835 self.align_self(AlignItems::Start)
836 }
837
838 #[inline(always)]
840 pub fn self_center(self) -> Self {
841 self.align_self(AlignItems::Center)
842 }
843
844 #[inline(always)]
846 pub fn self_end(self) -> Self {
847 self.align_self(AlignItems::End)
848 }
849
850 #[inline(always)]
852 pub fn self_stretch(self) -> Self {
853 self.align_self(AlignItems::Stretch)
854 }
855
856 #[inline(always)]
858 pub fn items_baseline(mut self) -> Self {
859 self.items_baseline = true;
860 self
861 }
862
863 #[inline(always)]
865 pub fn overflow_hidden(mut self) -> Self {
866 self.clip_children = true;
867 self
868 }
869
870 #[inline(always)]
872 pub fn overflow_visible(mut self) -> Self {
873 self.clip_children = false;
874 self
875 }
876
877 #[inline(always)]
879 pub fn overflow_scroll(self) -> Self {
880 self.scrollable_auto()
881 }
882
883 #[inline(always)]
885 pub fn overflow_y_scroll(self) -> Self {
886 self.flex_col().scrollable_auto()
887 }
888
889 #[inline(always)]
891 pub fn overflow_x_scroll(self) -> Self {
892 self.flex_row().scrollable_auto()
893 }
894
895 #[inline(always)]
897 pub fn transition(self) -> Self {
898 self.animate_all(Duration::from_millis(150))
899 }
900
901 #[inline(always)]
903 pub fn transition_colors(self) -> Self {
904 self.animate_color(Duration::from_millis(150))
905 }
906
907 #[inline(always)]
909 pub fn transition_opacity(self) -> Self {
910 self.animate_opacity(Duration::from_millis(150))
911 }
912
913 #[inline(always)]
915 pub fn transition_all(self) -> Self {
916 self.animate_all(Duration::from_millis(150))
917 }
918
919 #[inline(always)]
921 pub fn transition_none(mut self) -> Self {
922 self.transition = None;
923 self
924 }
925
926 #[inline(always)] pub fn duration_75(self) -> Self { self.duration(Duration::from_millis(75)) }
927 #[inline(always)] pub fn duration_100(self) -> Self { self.duration(Duration::from_millis(100)) }
928 #[inline(always)] pub fn duration_150(self) -> Self { self.duration(Duration::from_millis(150)) }
929 #[inline(always)] pub fn duration_200(self) -> Self { self.duration(Duration::from_millis(200)) }
930 #[inline(always)] pub fn duration_300(self) -> Self { self.duration(Duration::from_millis(300)) }
931 #[inline(always)] pub fn duration_500(self) -> Self { self.duration(Duration::from_millis(500)) }
932
933 #[inline(always)] pub fn ease_linear(self) -> Self { self.ease(Easing::Linear) }
934 #[inline(always)] pub fn ease_in(self) -> Self { self.ease(Easing::EaseIn) }
935 #[inline(always)] pub fn ease_out(self) -> Self { self.ease(Easing::EaseOut) }
936 #[inline(always)] pub fn ease_in_out(self) -> Self { self.ease(Easing::EaseInOut) }
937 #[inline(always)] pub fn ease_spring(self) -> Self { self.ease(Easing::Spring) }
938
939 #[inline(always)]
941 pub fn animate_color(mut self, duration: Duration) -> Self {
942 let ms = duration.as_millis().min(u16::MAX as u128) as u16;
943 let mut spec = self.transition.unwrap_or(TransitionSpec {
944 duration_ms: ms,
945 ..TransitionSpec::default()
946 });
947 spec.duration_ms = ms;
948 spec.animate_color = true;
949 self.transition = Some(spec);
950 self
951 }
952
953 #[inline(always)]
955 pub fn animate_bg(self, duration: Duration) -> Self {
956 self.animate_color(duration)
957 }
958
959 #[inline(always)]
961 pub fn animate_bounds(mut self, duration: Duration) -> Self {
962 let ms = duration.as_millis().min(u16::MAX as u128) as u16;
963 let mut spec = self.transition.unwrap_or(TransitionSpec {
964 duration_ms: ms,
965 ..TransitionSpec::default()
966 });
967 spec.duration_ms = ms;
968 spec.animate_bounds = true;
969 self.transition = Some(spec);
970 self
971 }
972
973 #[inline(always)]
975 pub fn ease(mut self, easing: Easing) -> Self {
976 let mut spec = self.transition.unwrap_or_default();
977 spec.easing = easing;
978 self.transition = Some(spec);
979 self
980 }
981
982 #[inline(always)]
984 pub fn repeat_infinite(mut self) -> Self {
985 let mut spec = self.transition.unwrap_or_default();
986 spec.loop_mode = LoopMode::Infinite;
987 self.transition = Some(spec);
988 self
989 }
990
991 #[inline(always)]
993 pub fn repeat_ping_pong(mut self) -> Self {
994 let mut spec = self.transition.unwrap_or_default();
995 spec.loop_mode = LoopMode::PingPong;
996 self.transition = Some(spec);
997 self
998 }
999
1000 #[inline(always)]
1002 pub fn duration(mut self, duration: Duration) -> Self {
1003 let mut spec = self.transition.unwrap_or_default();
1004 spec.duration_ms = duration_to_ms(duration);
1005 self.transition = Some(spec);
1006 self
1007 }
1008
1009 #[inline(always)]
1011 pub fn delay(mut self, delay: Duration) -> Self {
1012 let mut spec = self.transition.unwrap_or_default();
1013 spec.delay_ms = duration_to_ms(delay);
1014 self.transition = Some(spec);
1015 self
1016 }
1017
1018 #[inline(always)]
1020 pub fn animate_opacity(mut self, duration: Duration) -> Self {
1021 let mut spec = self.transition.unwrap_or_default();
1022 spec.duration_ms = duration_to_ms(duration);
1023 spec.animate_opacity = true;
1024 self.transition = Some(spec);
1025 self
1026 }
1027
1028 #[inline(always)]
1030 pub fn animate_all(mut self, duration: Duration) -> Self {
1031 let mut spec = self.transition.unwrap_or_default();
1032 spec.duration_ms = duration_to_ms(duration);
1033 spec.animate_color = true;
1034 spec.animate_bounds = true;
1035 spec.animate_opacity = true;
1036 self.transition = Some(spec);
1037 self
1038 }
1039
1040 #[inline(always)]
1042 pub fn enter_from(mut self, opacity: u8, dx: i16, dy: i16, duration: Duration) -> Self {
1043 let mut spec = self.transition.unwrap_or_default();
1044 spec.duration_ms = duration_to_ms(duration);
1045 spec.enter = Some(EnterFrom { opacity, dx, dy });
1046 self.transition = Some(spec);
1047 self
1048 }
1049
1050 #[inline(always)]
1052 pub fn fade_in(self, duration: Duration) -> Self {
1053 self.enter_from(0, 0, 0, duration)
1054 }
1055
1056 #[inline(always)]
1058 pub fn slide_in(self, dx: i16, dy: i16, duration: Duration) -> Self {
1059 self.enter_from(0, dx, dy, duration)
1060 }
1061
1062 #[inline(always)]
1065 pub fn exit_to(mut self, opacity: u8, dx: i16, dy: i16, duration: Duration) -> Self {
1066 let mut spec = self.transition.unwrap_or_default();
1067 let exit_ms = duration_to_ms(duration);
1068 spec.exit_duration_ms = exit_ms;
1069 if self.transition.is_none() {
1070 spec.duration_ms = exit_ms;
1071 }
1072 spec.exit = Some(EnterFrom { opacity, dx, dy });
1073 self.transition = Some(spec);
1074 self
1075 }
1076
1077 #[inline(always)]
1079 pub fn fade_out(self, duration: Duration) -> Self {
1080 self.exit_to(0, 0, 0, duration)
1081 }
1082
1083 #[inline(always)]
1085 pub fn slide_out(self, dx: i16, dy: i16, duration: Duration) -> Self {
1086 self.exit_to(0, dx, dy, duration)
1087 }
1088
1089 #[inline(always)]
1092 pub fn auto_animate(self) -> Self {
1093 self.auto_animate_with(Duration::from_millis(220))
1094 }
1095
1096 #[inline(always)]
1098 pub fn auto_animate_with(mut self, duration: Duration) -> Self {
1099 let mut spec = self.transition.unwrap_or_default();
1100 spec.duration_ms = duration_to_ms(duration);
1101 spec.auto_animate = true;
1102 spec.easing = Easing::Spring;
1103 self.transition = Some(spec);
1104 self
1105 }
1106
1107 #[inline(always)]
1109 pub fn stagger(mut self, step: Duration) -> Self {
1110 let mut spec = self.transition.unwrap_or_default();
1111 spec.stagger_ms = duration_to_ms(step);
1112 self.transition = Some(spec);
1113 self
1114 }
1115
1116 #[inline(always)]
1118 pub fn spring_bounds(mut self, duration: Duration) -> Self {
1119 let mut spec = self.transition.unwrap_or_default();
1120 spec.duration_ms = duration_to_ms(duration);
1121 spec.animate_bounds = true;
1122 spec.easing = Easing::Spring;
1123 self.transition = Some(spec);
1124 self
1125 }
1126
1127 #[inline(always)]
1129 pub fn spring_all(mut self, duration: Duration) -> Self {
1130 let mut spec = self.transition.unwrap_or_default();
1131 spec.duration_ms = duration_to_ms(duration);
1132 spec.animate_color = true;
1133 spec.animate_bounds = true;
1134 spec.animate_opacity = true;
1135 spec.easing = Easing::Spring;
1136 self.transition = Some(spec);
1137 self
1138 }
1139
1140 #[inline(always)]
1143 pub fn flip_layout(mut self, duration: Duration) -> Self {
1144 let mut spec = self.transition.unwrap_or_default();
1145 spec.duration_ms = duration_to_ms(duration);
1146 spec.animate_bounds = true;
1147 spec.flip_transform = true;
1148 spec.easing = Easing::Spring;
1149 self.transition = Some(spec);
1150 self
1151 }
1152
1153 #[inline(always)]
1155 pub fn ping_pong_bg(mut self, from_bg: Color, duration: Duration) -> Self {
1156 let ms = duration.as_millis().min(u16::MAX as u128) as u16;
1157 let mut spec = self.transition.unwrap_or_default();
1158 spec.duration_ms = ms;
1159 spec.animate_color = true;
1160 spec.loop_mode = LoopMode::PingPong;
1161 spec.easing = Easing::EaseInOut;
1162 spec.pulse_from_bg = Some(from_bg);
1163 self.transition = Some(spec);
1164 self
1165 }
1166
1167 #[inline(always)]
1169 pub fn on_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1170 self.on_click = Some(handler);
1171 self.focusable = true;
1172 self
1173 }
1174
1175 #[inline(always)]
1177 pub fn on_click(
1178 mut self,
1179 cx: &'a FrameScope,
1180 handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
1181 ) -> Self {
1182 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1183 let (x, y) = acx.click_position();
1184 handler(crate::pod::ClickEvent { x, y }, acx);
1185 });
1186 self.on_click = Some(wrapped);
1187 self.focusable = true;
1188 self
1189 }
1190
1191 #[inline(always)]
1193 pub fn on_secondary_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1194 self.on_secondary_click = Some(handler);
1195 self
1196 }
1197
1198 #[inline(always)]
1200 pub fn on_secondary_click(
1201 mut self,
1202 cx: &'a FrameScope,
1203 handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
1204 ) -> Self {
1205 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1206 let (x, y) = acx.click_position();
1207 handler(crate::pod::ClickEvent { x, y }, acx);
1208 });
1209 self.on_secondary_click = Some(wrapped);
1210 self
1211 }
1212
1213 #[inline(always)]
1215 pub fn on_drag_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1216 self.on_drag = Some(handler);
1217 self
1218 }
1219
1220 #[inline(always)]
1222 pub fn on_drag(
1223 mut self,
1224 cx: &'a FrameScope,
1225 handler: &'a dyn Fn(DragEvent, &mut ActionScope),
1226 ) -> Self {
1227 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1228 let ev = acx.drag_event();
1229 handler(ev, acx);
1230 });
1231 self.on_drag = Some(wrapped);
1232 self
1233 }
1234
1235 #[inline(always)]
1237 pub fn on_hover_raw(mut self, handler: &'a dyn Fn(bool, &mut ActionScope)) -> Self {
1238 self.on_hover = Some(handler);
1239 self
1240 }
1241
1242 #[inline(always)]
1244 pub fn on_hover(
1245 mut self,
1246 _cx: &'a FrameScope,
1247 handler: &'a dyn Fn(bool, &mut ActionScope),
1248 ) -> Self {
1249 self.on_hover = Some(handler);
1250 self
1251 }
1252
1253 #[inline(always)]
1255 pub fn on_pointer_move_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1256 self.on_pointer_move = Some(handler);
1257 self
1258 }
1259
1260 #[inline(always)]
1262 pub fn on_pointer_move(
1263 mut self,
1264 cx: &'a FrameScope,
1265 handler: &'a dyn Fn(DragEvent, &mut ActionScope),
1266 ) -> Self {
1267 let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1268 let ev = acx.drag_event();
1269 handler(ev, acx);
1270 });
1271 self.on_pointer_move = Some(wrapped);
1272 self
1273 }
1274
1275 #[inline(always)]
1277 pub fn on_wheel_raw(
1278 mut self,
1279 handler: &'a dyn Fn(crate::pod::WheelEvent, &mut ActionScope),
1280 ) -> Self {
1281 self.on_wheel = Some(handler);
1282 self
1283 }
1284
1285 #[inline(always)]
1287 pub fn on_wheel(
1288 mut self,
1289 _cx: &'a FrameScope,
1290 handler: &'a dyn Fn(crate::pod::WheelEvent, &mut ActionScope),
1291 ) -> Self {
1292 self.on_wheel = Some(handler);
1293 self
1294 }
1295
1296 #[inline(always)]
1298 pub fn on_pinch_raw(
1299 mut self,
1300 handler: &'a dyn Fn(crate::pod::PinchEvent, &mut ActionScope),
1301 ) -> Self {
1302 self.on_pinch = Some(handler);
1303 self
1304 }
1305
1306 #[inline(always)]
1308 pub fn on_pinch(
1309 mut self,
1310 _cx: &'a FrameScope,
1311 handler: &'a dyn Fn(crate::pod::PinchEvent, &mut ActionScope),
1312 ) -> Self {
1313 self.on_pinch = Some(handler);
1314 self
1315 }
1316
1317 #[inline(always)]
1319 pub fn on_swipe(
1320 mut self,
1321 _cx: &'a FrameScope,
1322 handler: &'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope),
1323 ) -> Self {
1324 self.on_swipe = Some(handler);
1325 self
1326 }
1327
1328 #[inline(always)]
1330 pub fn key(mut self, key: impl AsRef<[u8]>) -> Self {
1331 let mut h: u32 = 0x811c_9dc5;
1332 for &b in key.as_ref() {
1333 h ^= b as u32;
1334 h = h.wrapping_mul(0x0100_0193);
1335 }
1336 self.id = Some(h | 1);
1337 self
1338 }
1339
1340 #[inline(always)]
1342 pub fn z_index(mut self, z: i8) -> Self {
1343 self.z_index = z;
1344 self
1345 }
1346
1347 #[inline(always)]
1349 pub fn on_key_input(
1350 mut self,
1351 handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
1352 ) -> Self {
1353 self.on_key_input = Some(handler);
1354 self.focusable = true;
1355 self
1356 }
1357
1358 #[inline(always)]
1360 pub fn on_key_nav(
1361 mut self,
1362 handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
1363 ) -> Self {
1364 self.on_key_nav = Some(handler);
1365 self
1366 }
1367
1368 #[inline(always)]
1370 pub fn on_navigate_back(
1371 mut self,
1372 handler: &'a dyn Fn(&mut ActionScope),
1373 ) -> Self {
1374 self.on_navigate_back = Some(handler);
1375 self
1376 }
1377
1378 #[inline(always)]
1380 pub fn shadow(mut self, blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
1381 if blur > 0 && color.a() > 0 {
1382 self.shadow = Some((blur.min(64), offset_x, offset_y, color));
1383 }
1384 self
1385 }
1386
1387 #[inline(always)]
1389 pub fn grid(mut self, cols: u8) -> Self {
1390 self.grid_cols = cols.max(1);
1391 self
1392 }
1393
1394 pub fn child(mut self, cx: &'a FrameScope, child: impl IntoElement<'a>) -> Self {
1396 let elem = child.into_element(cx);
1397 let node: &'a ElementNode<'a> = cx.arena().alloc_temp(ElementNode {
1398 element: elem,
1399 next_sibling: Cell::new(None),
1400 });
1401 if let Some(last) = self.last_child {
1402 last.next_sibling.set(Some(node));
1403 } else {
1404 self.first_child = Some(node);
1405 }
1406 self.last_child = Some(node);
1407 self
1408 }
1409
1410 #[inline(always)]
1412 pub fn child_opt(self, cx: &'a FrameScope, child: Option<impl IntoElement<'a>>) -> Self {
1413 match child {
1414 Some(c) => self.child(cx, c),
1415 None => self,
1416 }
1417 }
1418
1419 pub fn children<I, E>(mut self, cx: &'a FrameScope, iter: I) -> Self
1421 where
1422 I: IntoIterator<Item = E>,
1423 E: IntoElement<'a>,
1424 {
1425 for item in iter {
1426 self = self.child(cx, item);
1427 }
1428 self
1429 }
1430
1431 #[inline(always)]
1433 pub fn justify_start_localized(mut self, cx: &FrameScope) -> Self {
1434 if cx.is_rtl() {
1435 self.style.set_justify(crate::style::Justify::End);
1436 } else {
1437 self.style.set_justify(crate::style::Justify::Start);
1438 }
1439 self
1440 }
1441
1442 #[inline(always)]
1444 pub fn justify_end_localized(mut self, cx: &FrameScope) -> Self {
1445 if cx.is_rtl() {
1446 self.style.set_justify(crate::style::Justify::Start);
1447 } else {
1448 self.style.set_justify(crate::style::Justify::End);
1449 }
1450 self
1451 }
1452
1453 #[inline(always)]
1455 pub fn ps(self, cx: &FrameScope, px: i8) -> Self {
1456 if cx.is_rtl() {
1457 self.pr(px)
1458 } else {
1459 self.pl(px)
1460 }
1461 }
1462
1463 #[inline(always)]
1465 pub fn pe(self, cx: &FrameScope, px: i8) -> Self {
1466 if cx.is_rtl() {
1467 self.pl(px)
1468 } else {
1469 self.pr(px)
1470 }
1471 }
1472
1473 #[inline(always)]
1475 pub fn ms(self, cx: &FrameScope, px: i16) -> Self {
1476 if cx.is_rtl() {
1477 self.mr(px)
1478 } else {
1479 self.ml(px)
1480 }
1481 }
1482
1483 #[inline(always)]
1485 pub fn me(self, cx: &FrameScope, px: i16) -> Self {
1486 if cx.is_rtl() {
1487 self.ml(px)
1488 } else {
1489 self.mr(px)
1490 }
1491 }
1492
1493 #[inline(always)]
1499 pub fn rounded_corners(mut self, radii: CornerRadii) -> Self {
1500 self.effects.corner_radii = Some(radii);
1501 self
1502 }
1503
1504 #[inline(always)]
1506 pub fn rounded_t(self, r: u8) -> Self {
1507 self.rounded_corners(CornerRadii::top(r))
1508 }
1509
1510 #[inline(always)]
1512 pub fn rounded_b(self, r: u8) -> Self {
1513 self.rounded_corners(CornerRadii::bottom(r))
1514 }
1515
1516 #[inline(always)]
1518 pub fn rounded_l(self, r: u8) -> Self {
1519 self.rounded_corners(CornerRadii::left(r))
1520 }
1521
1522 #[inline(always)]
1524 pub fn rounded_r(self, r: u8) -> Self {
1525 self.rounded_corners(CornerRadii::right(r))
1526 }
1527
1528 #[inline(always)]
1530 pub fn bg_paint(mut self, paint: Paint<'a>) -> Self {
1531 self.effects.bg_paint = Some(paint);
1532 self
1533 }
1534
1535 #[inline]
1537 pub fn bg_linear_gradient(
1538 mut self,
1539 cx: &'a FrameScope,
1540 angle_deg: f32,
1541 stops: &[ColorStop],
1542 ) -> Self {
1543 let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
1544 self.effects.bg_paint = Some(Paint::linear_angle(angle_deg, arena_stops));
1545 self
1546 }
1547
1548 #[inline]
1550 pub fn bg_radial_gradient(mut self, cx: &'a FrameScope, stops: &[ColorStop]) -> Self {
1551 let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
1552 self.effects.bg_paint = Some(Paint::radial(arena_stops));
1553 self
1554 }
1555
1556 #[inline]
1558 pub fn bg_radial_focal_gradient(
1559 mut self,
1560 cx: &'a FrameScope,
1561 center: [f32; 2],
1562 radius: f32,
1563 focal: [f32; 2],
1564 focal_radius: f32,
1565 stops: &[ColorStop],
1566 ) -> Self {
1567 let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
1568 self.effects.bg_paint = Some(Paint::radial_focal(
1569 center,
1570 radius,
1571 focal,
1572 focal_radius,
1573 arena_stops,
1574 ));
1575 self
1576 }
1577
1578 #[inline]
1580 pub fn bg_conic_gradient(
1581 mut self,
1582 cx: &'a FrameScope,
1583 start_angle_deg: f32,
1584 stops: &[ColorStop],
1585 ) -> Self {
1586 let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
1587 self.effects.bg_paint = Some(Paint::conic(start_angle_deg, arena_stops));
1588 self
1589 }
1590
1591 #[inline]
1593 pub fn bg_gradient_extend(mut self, mode: ExtendMode) -> Self {
1594 if let Some(p) = self.effects.bg_paint {
1595 self.effects.bg_paint = Some(p.with_extend(mode));
1596 }
1597 self
1598 }
1599
1600 #[inline]
1602 pub fn bg_gradient_space(mut self, space: GradientSpace) -> Self {
1603 if let Some(p) = self.effects.bg_paint {
1604 self.effects.bg_paint = Some(p.with_space(space));
1605 }
1606 self
1607 }
1608
1609 #[inline(always)]
1611 pub fn blend_mode(mut self, mode: BlendMode) -> Self {
1612 self.effects.blend_mode = mode;
1613 self
1614 }
1615
1616 #[inline(always)]
1618 pub fn isolate(mut self) -> Self {
1619 self.effects.isolate = true;
1620 self
1621 }
1622
1623 #[inline(always)]
1625 pub fn filter(mut self, filter: LayerFilter) -> Self {
1626 self.effects.filter = filter;
1627 self
1628 }
1629
1630 #[inline(always)]
1632 pub fn blur(mut self, radius: u8) -> Self {
1633 self.effects.filter.blur_radius = radius;
1634 self
1635 }
1636
1637 #[inline]
1639 pub fn color_matrix(mut self, matrix: ColorMatrix) -> Self {
1640 self.effects.filter.color_matrix = Some(match self.effects.filter.color_matrix {
1641 Some(prev) => prev.then(matrix),
1642 None => matrix,
1643 });
1644 self
1645 }
1646
1647 #[inline]
1649 pub fn grayscale(self, amount: f32) -> Self {
1650 self.color_matrix(ColorMatrix::grayscale(amount))
1651 }
1652
1653 #[inline]
1655 pub fn sepia(self, amount: f32) -> Self {
1656 self.color_matrix(ColorMatrix::sepia(amount))
1657 }
1658
1659 #[inline]
1661 pub fn saturate(self, amount: f32) -> Self {
1662 self.color_matrix(ColorMatrix::saturate(amount))
1663 }
1664
1665 #[inline]
1667 pub fn brightness(self, amount: f32) -> Self {
1668 self.color_matrix(ColorMatrix::brightness(amount))
1669 }
1670
1671 #[inline]
1673 pub fn contrast(self, amount: f32) -> Self {
1674 self.color_matrix(ColorMatrix::contrast(amount))
1675 }
1676
1677 #[inline]
1679 pub fn hue_rotate(self, deg: f32) -> Self {
1680 self.color_matrix(ColorMatrix::hue_rotate(deg))
1681 }
1682
1683 #[inline]
1685 pub fn invert(self, amount: f32) -> Self {
1686 self.color_matrix(ColorMatrix::invert(amount))
1687 }
1688
1689 #[inline(always)]
1691 pub fn drop_shadow(mut self, blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
1692 self.effects.filter.drop_shadow = Some(DropShadow::new(blur, offset_x, offset_y, color));
1693 self
1694 }
1695
1696 #[inline(always)]
1698 pub fn backdrop_filter(mut self, filter: BackdropFilter) -> Self {
1699 self.effects.backdrop_filter = filter;
1700 self
1701 }
1702
1703 #[inline(always)]
1705 pub fn backdrop_blur(mut self, radius: u8) -> Self {
1706 self.effects.backdrop_filter.blur_radius = radius;
1707 self
1708 }
1709
1710 #[inline]
1712 pub fn backdrop_acrylic(mut self, blur_radius: u8, tint: Color) -> Self {
1713 self.effects.backdrop_filter = BackdropFilter::acrylic(blur_radius, tint);
1714 self
1715 }
1716
1717 #[inline]
1719 pub fn transform(mut self, t: Transform2D) -> Self {
1720 self.effects.transform = Some(match self.effects.transform {
1721 Some(prev) => prev.concat(t),
1722 None => t,
1723 });
1724 self
1725 }
1726
1727 #[inline]
1729 pub fn rotate_deg(self, deg: f32) -> Self {
1730 self.transform(Transform2D::rotate(deg))
1731 }
1732
1733 #[inline]
1735 pub fn scale(self, sx: f32, sy: f32) -> Self {
1736 self.transform(Transform2D::scale(sx, sy))
1737 }
1738
1739 #[inline]
1741 pub fn translate(self, tx: f32, ty: f32) -> Self {
1742 self.transform(Transform2D::translate(tx, ty))
1743 }
1744
1745 #[inline]
1747 pub fn skew(self, ax_deg: f32, ay_deg: f32) -> Self {
1748 let mut s = self;
1749 if ax_deg != 0.0 {
1750 s = s.transform(Transform2D::skew_x(ax_deg));
1751 }
1752 if ay_deg != 0.0 {
1753 s = s.transform(Transform2D::skew_y(ay_deg));
1754 }
1755 s
1756 }
1757
1758 #[inline(always)]
1760 pub fn clip_path(mut self, path_d: &'static str) -> Self {
1761 self.effects.clip_path = Some(StrRef::Static(path_d));
1762 self
1763 }
1764
1765 #[inline]
1767 pub fn clip_path_str(mut self, cx: &'a FrameScope, path_d: &str) -> Self {
1768 let buf = cx.arena().alloc_bytes(path_d.len());
1769 buf.copy_from_slice(path_d.as_bytes());
1770 let s = unsafe { core::str::from_utf8_unchecked(buf) };
1772 self.effects.clip_path = Some(StrRef::Scratch(s));
1773 self
1774 }
1775
1776 #[inline(always)]
1781 pub fn external_texture(mut self, id: crate::paint::ExternalTextureId) -> Self {
1782 self.external_texture = Some(crate::paint::ExternalTextureSpec::new(id));
1783 self
1784 }
1785
1786 #[inline(always)]
1788 pub fn external_texture_spec(mut self, spec: crate::paint::ExternalTextureSpec) -> Self {
1789 self.external_texture = Some(spec);
1790 self
1791 }
1792
1793 #[inline(always)]
1795 pub fn external_texture_uv(mut self, uv_rect: [f32; 4]) -> Self {
1796 if let Some(ext) = &mut self.external_texture {
1797 ext.uv_rect = uv_rect;
1798 }
1799 self
1800 }
1801
1802 #[inline(always)]
1804 pub fn external_texture_revision(mut self, revision: u64) -> Self {
1805 if let Some(ext) = &mut self.external_texture {
1806 ext.content_revision = revision;
1807 }
1808 self
1809 }
1810
1811 #[inline(always)]
1813 pub fn external_texture_format(mut self, premultiplied: bool, fallback_bg: Color) -> Self {
1814 if let Some(ext) = &mut self.external_texture {
1815 ext.premultiplied = premultiplied;
1816 ext.fallback_bg = fallback_bg;
1817 }
1818 self
1819 }
1820
1821 #[inline(always)]
1823 pub fn aspect_ratio(mut self, ratio: f32) -> Self {
1824 self.layout_ext.aspect_ratio = ratio.max(0.0);
1825 self
1826 }
1827
1828 #[inline(always)]
1830 pub fn grid_col_span(mut self, span: u8) -> Self {
1831 self.layout_ext.grid_col_span = span.max(1);
1832 self
1833 }
1834
1835 #[inline]
1837 pub fn grid_template_fr(mut self, cx: &'a FrameScope, weights: &[u16]) -> Self {
1838 if !weights.is_empty() {
1839 self.grid_cols = weights.len().min(u8::MAX as usize) as u8;
1840 self.layout_ext.grid_template_fr = cx.arena().alloc_iter(weights.iter().copied());
1841 }
1842 self
1843 }
1844
1845 #[inline(always)]
1847 pub fn sticky_top(mut self, offset: i16) -> Self {
1848 self.layout_ext.sticky_top = Some(offset);
1849 self
1850 }
1851
1852 #[inline(always)]
1854 pub fn rtl(mut self, rtl: bool) -> Self {
1855 self.layout_ext.rtl = rtl;
1856 self
1857 }
1858
1859 #[inline(always)]
1861 pub fn rtl_auto(mut self, cx: &FrameScope) -> Self {
1862 self.layout_ext.rtl = cx.is_rtl();
1863 self
1864 }
1865
1866 #[inline(always)]
1868 pub fn overflow_auto(mut self) -> Self {
1869 self.scrollable_max = Some(i16::MAX);
1870 self.clip_children = true;
1871 self
1872 }
1873
1874 #[inline(always)]
1876 pub fn scroll_chaining(mut self, enable: bool) -> Self {
1877 self.layout_ext.scroll_chaining = enable;
1878 self
1879 }
1880}
1881