use core::cell::Cell;
use core::time::Duration;
use crate::arena::StrRef;
use crate::paint::{
BackdropFilter, BlendMode, ColorMatrix, ColorStop, CornerRadii, DropShadow, ExtendMode,
GradientSpace, LayerFilter, Paint, PaintEffects, Transform2D,
};
use crate::pod::{ActionScope, CursorShape, DragEvent, FrameScope};
use crate::style::{AlignItems, Color, Easing, Edges, LoopMode, Style, StyleRefinement, Styled};
use super::div_node::{
duration_to_ms, pack_visual_flags, DivExtPtr, DivExtended, DivInteractivity, DivNode,
DivRareHandlers, DivStateStyles, DivVisual, EnterFrom, TransitionSpec, FOCUS_BORDER_NONE,
NO_FOCUS_BORDER_DIV_VISUAL,
};
use super::{Element, ElementNode, IntoElement};
impl<'a> IntoElement<'a> for Div<'a> {
#[inline(always)]
fn into_element(mut self, cx: &'a FrameScope) -> Element<'a> {
let has_state_styles = self.text_color.is_some()
|| self.text_size.is_some()
|| self.hover_style.is_some()
|| self.active_style.is_some()
|| self.focus_style.is_some();
let has_effects = !self.effects.is_empty()
|| self.external_texture.is_some()
|| !self.layout_ext.is_empty();
let alloc_visual = |extended: DivExtPtr<'a>,
transition: Option<&'a TransitionSpec>|
-> &'a DivVisual<'a> {
let (pos_x, pos_y) = self.pos.unwrap_or((0, 0));
let (bg_grad_to, bg_grad_horiz) = self.bg_grad.unwrap_or((Color::TRANSPARENT, false));
let (focus_border_w, focus_border_color) = match self.focus_border {
Some((w, c)) => (w.min(254), c),
None => (FOCUS_BORDER_NONE, Color::TRANSPARENT),
};
let (shadow_blur, shadow_ox, shadow_oy, shadow_color) =
self.shadow.unwrap_or((0, 0, 0, Color::TRANSPARENT));
let extra_flags = pack_visual_flags(
self.pos.is_some(),
self.scrollable_max.is_some(),
self.bg_grad.is_some(),
bg_grad_horiz,
self.items_baseline,
self.align_self,
);
&*cx.arena().alloc_temp(DivVisual {
extended,
transition,
margin: self.margin,
extra_padding: self.extra_padding,
bg_grad_to,
focus_border_color,
shadow_color,
min_w: self.min_w,
max_w: self.max_w,
min_h: self.min_h,
max_h: self.max_h,
pos_x,
pos_y,
raw_scrollable_max: self.scrollable_max.unwrap_or(0),
extra_flags,
focus_border_w,
shadow_blur,
shadow_ox,
shadow_oy,
cursor: self.cursor,
})
};
let visual = if self.has_non_focus_visual_extra() {
let state_styles = if has_state_styles {
let hover_style = self.hover_style.map(|r| &*cx.arena().alloc_temp(r));
let active_style = self.active_style.map(|r| &*cx.arena().alloc_temp(r));
let focus_style = self.focus_style.map(|r| &*cx.arena().alloc_temp(r));
Some(&*cx.arena().alloc_temp(DivStateStyles {
hover_style,
active_style,
focus_style,
text_color: self.text_color.unwrap_or(Color::TRANSPARENT),
text_size: self.text_size.unwrap_or(0),
flags: if self.text_color.is_some() { 1 } else { 0 },
}))
} else {
None
};
let effects = if has_effects {
if let Some(ext_spec) = self.external_texture {
self.effects.external_texture = Some(&*cx.arena().alloc_temp(ext_spec));
}
if !self.layout_ext.is_empty() {
self.effects.layout_ext = Some(&*cx.arena().alloc_temp(self.layout_ext));
}
Some(&*cx.arena().alloc_temp(self.effects))
} else {
None
};
let extended = match (state_styles, effects) {
(None, None) => DivExtPtr::NONE,
(Some(s), None) => DivExtPtr::from_state_styles(s),
(None, Some(e)) => DivExtPtr::from_effects(e),
(Some(s), Some(e)) => DivExtPtr::from_both(&*cx.arena().alloc_temp(DivExtended {
state_styles: Some(s),
effects: Some(e),
})),
};
let transition = self.transition.map(|t| &*cx.arena().alloc_temp(t));
Some(alloc_visual(extended, transition))
} else if let Some((fw, fc)) = self.focus_border {
if fw == 0 && fc == Color::TRANSPARENT {
Some(&NO_FOCUS_BORDER_DIV_VISUAL)
} else {
Some(alloc_visual(DivExtPtr::NONE, None))
}
} else {
None
};
let interact = if self.has_interactivity() {
let rare = if self.has_rare_handlers() {
Some(&*cx.arena().alloc(DivRareHandlers {
on_long_press: self.on_long_press,
on_drag: self.on_drag,
on_hover: self.on_hover,
on_pointer_move: self.on_pointer_move,
on_wheel: self.on_wheel,
on_pinch: self.on_pinch,
on_swipe: self.on_swipe,
on_key_input: self.on_key_input,
on_key_nav: self.on_key_nav,
on_navigate_back: self.on_navigate_back,
on_secondary_click: self.on_secondary_click,
}))
} else {
None
};
Some(&*cx.arena().alloc(DivInteractivity {
on_click: self.on_click,
rare,
}))
} else {
None
};
let mut flags = 0u8;
if self.focusable {
flags |= 1 << 0;
}
if self.is_overlay {
flags |= 1 << 1;
}
if self.occlude_hit {
flags |= 1 << 2;
}
if self.auto_repeat {
flags |= 1 << 3;
}
if self.clip_children {
flags |= 1 << 4;
}
if self.id.is_some() {
flags |= 1 << 5;
}
if self.active_bg.is_some() {
flags |= 1 << 6;
}
if self.hover_bg.is_some() {
flags |= 1 << 7;
}
Element::Div(DivNode {
style: self.style,
first_child: self.first_child,
visual,
interact,
raw_id: self.id.unwrap_or(0),
raw_active_bg: self.active_bg.unwrap_or(Color::TRANSPARENT),
raw_hover_bg: self.hover_bg.unwrap_or(Color::TRANSPARENT),
opacity: self.opacity,
z_index: self.z_index,
grid_cols: self.grid_cols,
flags,
})
}
}
pub struct Div<'a> {
pub id: Option<u32>,
pub style: Style,
pub margin: Edges<i16>,
pub extra_padding: Edges<i16>,
pub min_w: i16,
pub max_w: i16,
pub min_h: i16,
pub max_h: i16,
pub opacity: u8,
pub active_bg: Option<Color>,
pub hover_bg: Option<Color>,
pub bg_grad: Option<(Color, bool)>,
pub focus_border: Option<(u8, Color)>,
pub focusable: bool,
pub is_overlay: bool,
pub occlude_hit: bool,
pub auto_repeat: bool,
pub scrollable_max: Option<i16>,
pub pos: Option<(i16, i16)>,
pub first_child: Option<&'a ElementNode<'a>>,
pub last_child: Option<&'a ElementNode<'a>>,
pub on_click: Option<&'a dyn Fn(&mut ActionScope)>,
pub on_long_press: Option<&'a dyn Fn(&mut ActionScope)>,
pub on_drag: Option<&'a dyn Fn(&mut ActionScope)>,
pub on_hover: Option<&'a dyn Fn(bool, &mut ActionScope)>,
pub on_pointer_move: Option<&'a dyn Fn(&mut ActionScope)>,
pub on_wheel: Option<&'a dyn Fn(crate::pod::WheelEvent, &mut ActionScope)>,
pub on_pinch: Option<&'a dyn Fn(crate::pod::PinchEvent, &mut ActionScope)>,
pub on_swipe: Option<&'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope)>,
pub on_key_input: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
pub on_key_nav: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
pub on_navigate_back: Option<&'a dyn Fn(&mut ActionScope)>,
pub on_secondary_click: Option<&'a dyn Fn(&mut ActionScope)>,
pub z_index: i8,
pub shadow: Option<(u8, i8, i8, Color)>,
pub grid_cols: u8,
pub transition: Option<TransitionSpec>,
pub clip_children: bool,
pub align_self: Option<AlignItems>,
pub cursor: Option<CursorShape>,
pub text_color: Option<Color>,
pub text_size: Option<u8>,
pub hover_style: Option<StyleRefinement>,
pub active_style: Option<StyleRefinement>,
pub focus_style: Option<StyleRefinement>,
pub items_baseline: bool,
pub effects: PaintEffects<'a>,
pub external_texture: Option<crate::paint::ExternalTextureSpec>,
pub layout_ext: crate::paint::LayoutExtras<'a>,
}
#[inline(always)]
pub fn div<'a>() -> Div<'a> {
Div {
id: None,
style: Style::default(),
margin: Edges::all(0),
extra_padding: Edges::all(0),
min_w: 0,
max_w: 0,
min_h: 0,
max_h: 0,
opacity: 255,
active_bg: None,
hover_bg: None,
bg_grad: None,
focus_border: None,
focusable: false,
is_overlay: false,
occlude_hit: false,
auto_repeat: false,
scrollable_max: None,
pos: None,
first_child: None,
last_child: None,
on_click: None,
on_long_press: None,
on_drag: None,
on_hover: None,
on_pointer_move: None,
on_wheel: None,
on_pinch: None,
on_swipe: None,
on_key_input: None,
on_key_nav: None,
on_navigate_back: None,
on_secondary_click: None,
z_index: 0,
shadow: None,
grid_cols: 0,
transition: None,
clip_children: false,
align_self: None,
cursor: None,
text_color: None,
text_size: None,
hover_style: None,
active_style: None,
focus_style: None,
items_baseline: false,
effects: PaintEffects::EMPTY,
external_texture: None,
layout_ext: crate::paint::LayoutExtras::EMPTY,
}
}
impl<'a> Styled for Div<'a> {
#[inline(always)]
fn style_mut(&mut self) -> &mut Style {
&mut self.style
}
}
impl<'a> Div<'a> {
#[inline(always)]
fn has_non_focus_visual_extra(&self) -> bool {
self.margin != Edges::all(0)
|| self.extra_padding != Edges::all(0)
|| self.min_w != 0
|| self.max_w != 0
|| self.min_h != 0
|| self.max_h != 0
|| self.bg_grad.is_some()
|| self.scrollable_max.is_some()
|| self.pos.is_some()
|| self.shadow.is_some()
|| self.transition.is_some()
|| self.align_self.is_some()
|| self.cursor.is_some()
|| self.text_color.is_some()
|| self.text_size.is_some()
|| self.hover_style.is_some()
|| self.active_style.is_some()
|| self.focus_style.is_some()
|| self.items_baseline
|| !self.effects.is_empty()
|| self.external_texture.is_some()
|| !self.layout_ext.is_empty()
}
#[inline(always)]
fn has_interactivity(&self) -> bool {
self.on_click.is_some() || self.has_rare_handlers()
}
#[inline(always)]
fn has_rare_handlers(&self) -> bool {
self.on_long_press.is_some()
|| self.on_drag.is_some()
|| self.on_hover.is_some()
|| self.on_pointer_move.is_some()
|| self.on_wheel.is_some()
|| self.on_pinch.is_some()
|| self.on_swipe.is_some()
|| self.on_key_input.is_some()
|| self.on_key_nav.is_some()
|| self.on_navigate_back.is_some()
|| self.on_secondary_click.is_some()
}
#[inline(always)]
pub fn id(mut self, id: u32) -> Self {
self.id = Some(id);
self
}
#[inline(always)]
pub fn scoped_id(mut self, cx: &FrameScope, local_id: u32) -> Self {
self.id = Some(cx.scoped_id(local_id));
self
}
#[inline(always)]
pub fn pos(mut self, x: i16, y: i16) -> Self {
self.pos = Some((x, y));
self
}
#[inline(always)]
pub fn safe_area(mut self, cx: &FrameScope) -> Self {
let sa = cx.safe_area();
let kb = cx.keyboard_inset();
self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
self.extra_padding.right = self.extra_padding.right.saturating_add(sa.right.max(0));
self.extra_padding.bottom = self
.extra_padding
.bottom
.saturating_add(sa.bottom.saturating_add(kb).max(0));
self.extra_padding.left = self.extra_padding.left.saturating_add(sa.left.max(0));
self
}
#[inline(always)]
pub fn pt_safe(mut self, cx: &FrameScope) -> Self {
let sa = cx.safe_area();
self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
self
}
#[inline(always)]
pub fn pb_safe(mut self, cx: &FrameScope) -> Self {
let sa = cx.safe_area();
let kb = cx.keyboard_inset();
self.extra_padding.bottom = self
.extra_padding
.bottom
.saturating_add(sa.bottom.saturating_add(kb).max(0));
self
}
#[inline(always)]
pub fn mx(mut self, px: i16) -> Self {
self.margin.left = px;
self.margin.right = px;
self
}
#[inline(always)]
pub fn my(mut self, px: i16) -> Self {
self.margin.top = px;
self.margin.bottom = px;
self
}
#[inline(always)]
pub fn mt(mut self, px: i16) -> Self {
self.margin.top = px;
self
}
#[inline(always)]
pub fn mb(mut self, px: i16) -> Self {
self.margin.bottom = px;
self
}
#[inline(always)]
pub fn ml(mut self, px: i16) -> Self {
self.margin.left = px;
self
}
#[inline(always)]
pub fn mr(mut self, px: i16) -> Self {
self.margin.right = px;
self
}
#[inline(always)]
pub fn min_w(mut self, px: i16) -> Self {
self.min_w = px.max(0);
self
}
#[inline(always)]
pub fn max_w(mut self, px: i16) -> Self {
self.max_w = px.max(0);
self
}
#[inline(always)]
pub fn min_h(mut self, px: i16) -> Self {
self.min_h = px.max(0);
self
}
#[inline(always)]
pub fn max_h(mut self, px: i16) -> Self {
self.max_h = px.max(0);
self
}
#[inline(always)]
pub fn opacity(mut self, alpha: u8) -> Self {
self.opacity = alpha;
self
}
#[inline(always)]
pub fn active_bg(mut self, color: Color) -> Self {
self.active_bg = Some(color);
self
}
#[inline(always)]
pub fn hover_bg(mut self, color: Color) -> Self {
self.hover_bg = Some(color);
self
}
#[inline(always)]
pub fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
let base = self.hover_style.unwrap_or_else(|| {
let mut r = StyleRefinement::new();
r.bg = self.hover_bg;
r
});
let refined = f(base);
if refined.is_empty() {
return self;
}
if refined.is_bg_only() {
self.hover_bg = refined.bg;
self.hover_style = None;
} else {
if let Some(bg) = refined.bg {
self.hover_bg = Some(bg);
}
self.hover_style = Some(refined);
}
self
}
#[inline(always)]
pub fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
let base = self.active_style.unwrap_or_else(|| {
let mut r = StyleRefinement::new();
r.bg = self.active_bg;
r
});
let refined = f(base);
if refined.is_empty() {
return self;
}
if refined.is_bg_only() {
self.active_bg = refined.bg;
self.active_style = None;
} else {
if let Some(bg) = refined.bg {
self.active_bg = Some(bg);
}
self.active_style = Some(refined);
}
self
}
#[inline(always)]
pub fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
self.focusable = true;
let refined = f(self.focus_style.unwrap_or_default());
if !refined.is_empty() {
self.focus_style = Some(refined);
}
self
}
#[inline(always)]
pub fn text_color(mut self, color: Color) -> Self {
self.text_color = Some(color);
self
}
#[inline(always)]
pub fn text_size(mut self, px: u8) -> Self {
self.text_size = Some(px);
self
}
#[inline(always)] pub fn text_xs(self) -> Self { self.text_size(11) }
#[inline(always)] pub fn text_sm(self) -> Self { self.text_size(12) }
#[inline(always)] pub fn text_base(self) -> Self { self.text_size(14) }
#[inline(always)] pub fn text_lg(self) -> Self { self.text_size(16) }
#[inline(always)] pub fn text_xl(self) -> Self { self.text_size(18) }
#[inline(always)] pub fn text_2xl(self) -> Self { self.text_size(22) }
#[inline(always)] pub fn text_3xl(self) -> Self { self.text_size(28) }
#[inline(always)]
pub fn opacity_f(mut self, alpha: f32) -> Self {
let c = if alpha <= 0.0 {
0.0
} else if alpha >= 1.0 {
1.0
} else {
alpha
};
self.opacity = (c * 255.0 + 0.5) as u8;
self
}
#[inline(always)] pub fn mx_0(self) -> Self { self.mx(0) }
#[inline(always)] pub fn mx_1(self) -> Self { self.mx(4) }
#[inline(always)] pub fn mx_2(self) -> Self { self.mx(8) }
#[inline(always)] pub fn mx_3(self) -> Self { self.mx(12) }
#[inline(always)] pub fn mx_4(self) -> Self { self.mx(16) }
#[inline(always)] pub fn my_0(self) -> Self { self.my(0) }
#[inline(always)] pub fn my_1(self) -> Self { self.my(4) }
#[inline(always)] pub fn my_2(self) -> Self { self.my(8) }
#[inline(always)] pub fn my_3(self) -> Self { self.my(12) }
#[inline(always)] pub fn my_4(self) -> Self { self.my(16) }
#[inline(always)] pub fn mt_1(self) -> Self { self.mt(4) }
#[inline(always)] pub fn mt_2(self) -> Self { self.mt(8) }
#[inline(always)] pub fn mt_3(self) -> Self { self.mt(12) }
#[inline(always)] pub fn mt_4(self) -> Self { self.mt(16) }
#[inline(always)] pub fn mb_1(self) -> Self { self.mb(4) }
#[inline(always)] pub fn mb_2(self) -> Self { self.mb(8) }
#[inline(always)] pub fn mb_3(self) -> Self { self.mb(12) }
#[inline(always)] pub fn mb_4(self) -> Self { self.mb(16) }
#[inline(always)] pub fn ml_1(self) -> Self { self.ml(4) }
#[inline(always)] pub fn ml_2(self) -> Self { self.ml(8) }
#[inline(always)] pub fn ml_3(self) -> Self { self.ml(12) }
#[inline(always)] pub fn ml_4(self) -> Self { self.ml(16) }
#[inline(always)] pub fn mr_1(self) -> Self { self.mr(4) }
#[inline(always)] pub fn mr_2(self) -> Self { self.mr(8) }
#[inline(always)] pub fn mr_3(self) -> Self { self.mr(12) }
#[inline(always)] pub fn mr_4(self) -> Self { self.mr(16) }
#[inline(always)] pub fn relative(self) -> Self { self }
#[inline(always)] pub fn block(self) -> Self { self.flex_col() }
#[inline(always)] pub fn inline_flex(self) -> Self { self.flex_row() }
#[inline(always)] pub fn cursor_pointer(self) -> Self { self.cursor(CursorShape::Pointer) }
#[inline(always)] pub fn cursor_default(self) -> Self { self.cursor(CursorShape::Default) }
#[inline(always)] pub fn cursor_text(self) -> Self { self.cursor(CursorShape::Text) }
#[inline(always)] pub fn cursor_not_allowed(self) -> Self { self.cursor(CursorShape::NotAllowed) }
#[inline(always)] pub fn cursor_grab(self) -> Self { self.cursor(CursorShape::Grab) }
#[inline(always)] pub fn cursor_grabbing(self) -> Self { self.cursor(CursorShape::Grabbing) }
#[inline(always)] pub fn shadow_none(mut self) -> Self { self.shadow = None; self }
#[inline(always)] pub fn shadow_sm(self) -> Self { self.shadow(4, 0, 1, Color::rgba(0, 0, 0, 48)) }
#[inline(always)] pub fn shadow_md(self) -> Self { self.shadow(10, 0, 3, Color::rgba(0, 0, 0, 72)) }
#[inline(always)] pub fn shadow_lg(self) -> Self { self.shadow(18, 0, 6, Color::rgba(0, 0, 0, 96)) }
#[inline(always)] pub fn shadow_xl(self) -> Self { self.shadow(28, 0, 10, Color::rgba(0, 0, 0, 120)) }
#[inline(always)]
pub fn bg_gradient(mut self, to_color: Color) -> Self {
self.bg_grad = Some((to_color, false));
self
}
#[inline(always)]
pub fn bg_gradient_x(mut self, to_color: Color) -> Self {
self.bg_grad = Some((to_color, true));
self
}
#[inline(always)]
pub fn overlay(mut self) -> Self {
self.is_overlay = true;
self
}
#[inline(always)]
pub fn occlude_hit(mut self) -> Self {
self.occlude_hit = true;
self
}
#[inline(always)]
pub fn auto_repeat(mut self) -> Self {
self.auto_repeat = true;
self
}
#[inline(always)]
pub fn on_long_press_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
self.on_long_press = Some(handler);
self.focusable = true;
self
}
#[inline(always)]
pub fn on_long_press(
mut self,
cx: &'a FrameScope,
handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
) -> Self {
let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
let (x, y) = acx.click_position();
handler(crate::pod::ClickEvent { x, y }, acx);
});
self.on_long_press = Some(wrapped);
self.focusable = true;
self
}
#[inline(always)]
pub fn focus_border(mut self, width: u8, color: Color) -> Self {
self.focus_border = Some((width, color));
self.focusable = true;
self
}
#[inline(always)]
pub fn no_focus_border(mut self) -> Self {
self.focus_border = Some((0, Color::TRANSPARENT));
self
}
#[inline(always)]
pub fn cursor(mut self, cursor: CursorShape) -> Self {
self.cursor = Some(cursor);
self
}
#[inline(always)]
pub fn focusable(mut self) -> Self {
self.focusable = true;
self
}
#[inline(always)]
pub fn unfocusable(mut self) -> Self {
self.focusable = false;
self
}
#[inline(always)]
pub fn scrollable(mut self, max_scroll: i16) -> Self {
self.scrollable_max = Some(max_scroll.max(0));
self.clip_children = true;
self
}
#[inline(always)]
pub fn scrollable_auto(mut self) -> Self {
self.scrollable_max = Some(i16::MAX);
self.clip_children = true;
self
}
#[inline(always)]
pub fn align_self(mut self, align: AlignItems) -> Self {
self.align_self = Some(align);
self
}
#[inline(always)]
pub fn self_start(self) -> Self {
self.align_self(AlignItems::Start)
}
#[inline(always)]
pub fn self_center(self) -> Self {
self.align_self(AlignItems::Center)
}
#[inline(always)]
pub fn self_end(self) -> Self {
self.align_self(AlignItems::End)
}
#[inline(always)]
pub fn self_stretch(self) -> Self {
self.align_self(AlignItems::Stretch)
}
#[inline(always)]
pub fn items_baseline(mut self) -> Self {
self.items_baseline = true;
self
}
#[inline(always)]
pub fn overflow_hidden(mut self) -> Self {
self.clip_children = true;
self
}
#[inline(always)]
pub fn overflow_visible(mut self) -> Self {
self.clip_children = false;
self
}
#[inline(always)]
pub fn overflow_scroll(self) -> Self {
self.scrollable_auto()
}
#[inline(always)]
pub fn overflow_y_scroll(self) -> Self {
self.flex_col().scrollable_auto()
}
#[inline(always)]
pub fn overflow_x_scroll(self) -> Self {
self.flex_row().scrollable_auto()
}
#[inline(always)]
pub fn transition(self) -> Self {
self.animate_all(Duration::from_millis(150))
}
#[inline(always)]
pub fn transition_colors(self) -> Self {
self.animate_color(Duration::from_millis(150))
}
#[inline(always)]
pub fn transition_opacity(self) -> Self {
self.animate_opacity(Duration::from_millis(150))
}
#[inline(always)]
pub fn transition_all(self) -> Self {
self.animate_all(Duration::from_millis(150))
}
#[inline(always)]
pub fn transition_none(mut self) -> Self {
self.transition = None;
self
}
#[inline(always)] pub fn duration_75(self) -> Self { self.duration(Duration::from_millis(75)) }
#[inline(always)] pub fn duration_100(self) -> Self { self.duration(Duration::from_millis(100)) }
#[inline(always)] pub fn duration_150(self) -> Self { self.duration(Duration::from_millis(150)) }
#[inline(always)] pub fn duration_200(self) -> Self { self.duration(Duration::from_millis(200)) }
#[inline(always)] pub fn duration_300(self) -> Self { self.duration(Duration::from_millis(300)) }
#[inline(always)] pub fn duration_500(self) -> Self { self.duration(Duration::from_millis(500)) }
#[inline(always)] pub fn ease_linear(self) -> Self { self.ease(Easing::Linear) }
#[inline(always)] pub fn ease_in(self) -> Self { self.ease(Easing::EaseIn) }
#[inline(always)] pub fn ease_out(self) -> Self { self.ease(Easing::EaseOut) }
#[inline(always)] pub fn ease_in_out(self) -> Self { self.ease(Easing::EaseInOut) }
#[inline(always)] pub fn ease_spring(self) -> Self { self.ease(Easing::Spring) }
#[inline(always)]
pub fn animate_color(mut self, duration: Duration) -> Self {
let ms = duration.as_millis().min(u16::MAX as u128) as u16;
let mut spec = self.transition.unwrap_or(TransitionSpec {
duration_ms: ms,
..TransitionSpec::default()
});
spec.duration_ms = ms;
spec.animate_color = true;
self.transition = Some(spec);
self
}
#[inline(always)]
pub fn animate_bg(self, duration: Duration) -> Self {
self.animate_color(duration)
}
#[inline(always)]
pub fn animate_bounds(mut self, duration: Duration) -> Self {
let ms = duration.as_millis().min(u16::MAX as u128) as u16;
let mut spec = self.transition.unwrap_or(TransitionSpec {
duration_ms: ms,
..TransitionSpec::default()
});
spec.duration_ms = ms;
spec.animate_bounds = true;
self.transition = Some(spec);
self
}
#[inline(always)]
pub fn ease(mut self, easing: Easing) -> Self {
let mut spec = self.transition.unwrap_or_default();
spec.easing = easing;
self.transition = Some(spec);
self
}
#[inline(always)]
pub fn repeat_infinite(mut self) -> Self {
let mut spec = self.transition.unwrap_or_default();
spec.loop_mode = LoopMode::Infinite;
self.transition = Some(spec);
self
}
#[inline(always)]
pub fn repeat_ping_pong(mut self) -> Self {
let mut spec = self.transition.unwrap_or_default();
spec.loop_mode = LoopMode::PingPong;
self.transition = Some(spec);
self
}
#[inline(always)]
pub fn duration(mut self, duration: Duration) -> Self {
let mut spec = self.transition.unwrap_or_default();
spec.duration_ms = duration_to_ms(duration);
self.transition = Some(spec);
self
}
#[inline(always)]
pub fn delay(mut self, delay: Duration) -> Self {
let mut spec = self.transition.unwrap_or_default();
spec.delay_ms = duration_to_ms(delay);
self.transition = Some(spec);
self
}
#[inline(always)]
pub fn animate_opacity(mut self, duration: Duration) -> Self {
let mut spec = self.transition.unwrap_or_default();
spec.duration_ms = duration_to_ms(duration);
spec.animate_opacity = true;
self.transition = Some(spec);
self
}
#[inline(always)]
pub fn animate_all(mut self, duration: Duration) -> Self {
let mut spec = self.transition.unwrap_or_default();
spec.duration_ms = duration_to_ms(duration);
spec.animate_color = true;
spec.animate_bounds = true;
spec.animate_opacity = true;
self.transition = Some(spec);
self
}
#[inline(always)]
pub fn enter_from(mut self, opacity: u8, dx: i16, dy: i16, duration: Duration) -> Self {
let mut spec = self.transition.unwrap_or_default();
spec.duration_ms = duration_to_ms(duration);
spec.enter = Some(EnterFrom { opacity, dx, dy });
self.transition = Some(spec);
self
}
#[inline(always)]
pub fn fade_in(self, duration: Duration) -> Self {
self.enter_from(0, 0, 0, duration)
}
#[inline(always)]
pub fn slide_in(self, dx: i16, dy: i16, duration: Duration) -> Self {
self.enter_from(0, dx, dy, duration)
}
#[inline(always)]
pub fn ping_pong_bg(mut self, from_bg: Color, duration: Duration) -> Self {
let ms = duration.as_millis().min(u16::MAX as u128) as u16;
let mut spec = self.transition.unwrap_or_default();
spec.duration_ms = ms;
spec.animate_color = true;
spec.loop_mode = LoopMode::PingPong;
spec.easing = Easing::EaseInOut;
spec.pulse_from_bg = Some(from_bg);
self.transition = Some(spec);
self
}
#[inline(always)]
pub fn on_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
self.on_click = Some(handler);
self.focusable = true;
self
}
#[inline(always)]
pub fn on_click(
mut self,
cx: &'a FrameScope,
handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
) -> Self {
let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
let (x, y) = acx.click_position();
handler(crate::pod::ClickEvent { x, y }, acx);
});
self.on_click = Some(wrapped);
self.focusable = true;
self
}
#[inline(always)]
pub fn on_secondary_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
self.on_secondary_click = Some(handler);
self
}
#[inline(always)]
pub fn on_secondary_click(
mut self,
cx: &'a FrameScope,
handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
) -> Self {
let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
let (x, y) = acx.click_position();
handler(crate::pod::ClickEvent { x, y }, acx);
});
self.on_secondary_click = Some(wrapped);
self
}
#[inline(always)]
pub fn on_drag_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
self.on_drag = Some(handler);
self
}
#[inline(always)]
pub fn on_drag(
mut self,
cx: &'a FrameScope,
handler: &'a dyn Fn(DragEvent, &mut ActionScope),
) -> Self {
let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
let ev = acx.drag_event();
handler(ev, acx);
});
self.on_drag = Some(wrapped);
self
}
#[inline(always)]
pub fn on_hover_raw(mut self, handler: &'a dyn Fn(bool, &mut ActionScope)) -> Self {
self.on_hover = Some(handler);
self
}
#[inline(always)]
pub fn on_hover(
mut self,
_cx: &'a FrameScope,
handler: &'a dyn Fn(bool, &mut ActionScope),
) -> Self {
self.on_hover = Some(handler);
self
}
#[inline(always)]
pub fn on_pointer_move_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
self.on_pointer_move = Some(handler);
self
}
#[inline(always)]
pub fn on_pointer_move(
mut self,
cx: &'a FrameScope,
handler: &'a dyn Fn(DragEvent, &mut ActionScope),
) -> Self {
let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
let ev = acx.drag_event();
handler(ev, acx);
});
self.on_pointer_move = Some(wrapped);
self
}
#[inline(always)]
pub fn on_wheel_raw(
mut self,
handler: &'a dyn Fn(crate::pod::WheelEvent, &mut ActionScope),
) -> Self {
self.on_wheel = Some(handler);
self
}
#[inline(always)]
pub fn on_wheel(
mut self,
_cx: &'a FrameScope,
handler: &'a dyn Fn(crate::pod::WheelEvent, &mut ActionScope),
) -> Self {
self.on_wheel = Some(handler);
self
}
#[inline(always)]
pub fn on_pinch_raw(
mut self,
handler: &'a dyn Fn(crate::pod::PinchEvent, &mut ActionScope),
) -> Self {
self.on_pinch = Some(handler);
self
}
#[inline(always)]
pub fn on_pinch(
mut self,
_cx: &'a FrameScope,
handler: &'a dyn Fn(crate::pod::PinchEvent, &mut ActionScope),
) -> Self {
self.on_pinch = Some(handler);
self
}
#[inline(always)]
pub fn on_swipe(
mut self,
_cx: &'a FrameScope,
handler: &'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope),
) -> Self {
self.on_swipe = Some(handler);
self
}
#[inline(always)]
pub fn key(mut self, key: impl AsRef<[u8]>) -> Self {
let mut h: u32 = 0x811c_9dc5;
for &b in key.as_ref() {
h ^= b as u32;
h = h.wrapping_mul(0x0100_0193);
}
self.id = Some(h | 1);
self
}
#[inline(always)]
pub fn z_index(mut self, z: i8) -> Self {
self.z_index = z;
self
}
#[inline(always)]
pub fn on_key_input(
mut self,
handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
) -> Self {
self.on_key_input = Some(handler);
self.focusable = true;
self
}
#[inline(always)]
pub fn on_key_nav(
mut self,
handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
) -> Self {
self.on_key_nav = Some(handler);
self
}
#[inline(always)]
pub fn on_navigate_back(
mut self,
handler: &'a dyn Fn(&mut ActionScope),
) -> Self {
self.on_navigate_back = Some(handler);
self
}
#[inline(always)]
pub fn shadow(mut self, blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
if blur > 0 && color.a() > 0 {
self.shadow = Some((blur.min(64), offset_x, offset_y, color));
}
self
}
#[inline(always)]
pub fn grid(mut self, cols: u8) -> Self {
self.grid_cols = cols.max(1);
self
}
pub fn child(mut self, cx: &'a FrameScope, child: impl IntoElement<'a>) -> Self {
let elem = child.into_element(cx);
let node: &'a ElementNode<'a> = cx.arena().alloc_temp(ElementNode {
element: elem,
next_sibling: Cell::new(None),
});
if let Some(last) = self.last_child {
last.next_sibling.set(Some(node));
} else {
self.first_child = Some(node);
}
self.last_child = Some(node);
self
}
#[inline(always)]
pub fn child_opt(self, cx: &'a FrameScope, child: Option<impl IntoElement<'a>>) -> Self {
match child {
Some(c) => self.child(cx, c),
None => self,
}
}
pub fn children<I, E>(mut self, cx: &'a FrameScope, iter: I) -> Self
where
I: IntoIterator<Item = E>,
E: IntoElement<'a>,
{
for item in iter {
self = self.child(cx, item);
}
self
}
#[inline(always)]
pub fn justify_start_localized(mut self, cx: &FrameScope) -> Self {
if cx.is_rtl() {
self.style.set_justify(crate::style::Justify::End);
} else {
self.style.set_justify(crate::style::Justify::Start);
}
self
}
#[inline(always)]
pub fn justify_end_localized(mut self, cx: &FrameScope) -> Self {
if cx.is_rtl() {
self.style.set_justify(crate::style::Justify::Start);
} else {
self.style.set_justify(crate::style::Justify::End);
}
self
}
#[inline(always)]
pub fn ps(self, cx: &FrameScope, px: i8) -> Self {
if cx.is_rtl() {
self.pr(px)
} else {
self.pl(px)
}
}
#[inline(always)]
pub fn pe(self, cx: &FrameScope, px: i8) -> Self {
if cx.is_rtl() {
self.pl(px)
} else {
self.pr(px)
}
}
#[inline(always)]
pub fn ms(self, cx: &FrameScope, px: i16) -> Self {
if cx.is_rtl() {
self.mr(px)
} else {
self.ml(px)
}
}
#[inline(always)]
pub fn me(self, cx: &FrameScope, px: i16) -> Self {
if cx.is_rtl() {
self.ml(px)
} else {
self.mr(px)
}
}
#[inline(always)]
pub fn rounded_corners(mut self, radii: CornerRadii) -> Self {
self.effects.corner_radii = Some(radii);
self
}
#[inline(always)]
pub fn rounded_t(self, r: u8) -> Self {
self.rounded_corners(CornerRadii::top(r))
}
#[inline(always)]
pub fn rounded_b(self, r: u8) -> Self {
self.rounded_corners(CornerRadii::bottom(r))
}
#[inline(always)]
pub fn rounded_l(self, r: u8) -> Self {
self.rounded_corners(CornerRadii::left(r))
}
#[inline(always)]
pub fn rounded_r(self, r: u8) -> Self {
self.rounded_corners(CornerRadii::right(r))
}
#[inline(always)]
pub fn bg_paint(mut self, paint: Paint<'a>) -> Self {
self.effects.bg_paint = Some(paint);
self
}
#[inline]
pub fn bg_linear_gradient(
mut self,
cx: &'a FrameScope,
angle_deg: f32,
stops: &[ColorStop],
) -> Self {
let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
self.effects.bg_paint = Some(Paint::linear_angle(angle_deg, arena_stops));
self
}
#[inline]
pub fn bg_radial_gradient(mut self, cx: &'a FrameScope, stops: &[ColorStop]) -> Self {
let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
self.effects.bg_paint = Some(Paint::radial(arena_stops));
self
}
#[inline]
pub fn bg_radial_focal_gradient(
mut self,
cx: &'a FrameScope,
center: [f32; 2],
radius: f32,
focal: [f32; 2],
focal_radius: f32,
stops: &[ColorStop],
) -> Self {
let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
self.effects.bg_paint = Some(Paint::radial_focal(
center,
radius,
focal,
focal_radius,
arena_stops,
));
self
}
#[inline]
pub fn bg_conic_gradient(
mut self,
cx: &'a FrameScope,
start_angle_deg: f32,
stops: &[ColorStop],
) -> Self {
let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
self.effects.bg_paint = Some(Paint::conic(start_angle_deg, arena_stops));
self
}
#[inline]
pub fn bg_gradient_extend(mut self, mode: ExtendMode) -> Self {
if let Some(p) = self.effects.bg_paint {
self.effects.bg_paint = Some(p.with_extend(mode));
}
self
}
#[inline]
pub fn bg_gradient_space(mut self, space: GradientSpace) -> Self {
if let Some(p) = self.effects.bg_paint {
self.effects.bg_paint = Some(p.with_space(space));
}
self
}
#[inline(always)]
pub fn blend_mode(mut self, mode: BlendMode) -> Self {
self.effects.blend_mode = mode;
self
}
#[inline(always)]
pub fn isolate(mut self) -> Self {
self.effects.isolate = true;
self
}
#[inline(always)]
pub fn filter(mut self, filter: LayerFilter) -> Self {
self.effects.filter = filter;
self
}
#[inline(always)]
pub fn blur(mut self, radius: u8) -> Self {
self.effects.filter.blur_radius = radius;
self
}
#[inline]
pub fn color_matrix(mut self, matrix: ColorMatrix) -> Self {
self.effects.filter.color_matrix = Some(match self.effects.filter.color_matrix {
Some(prev) => prev.then(matrix),
None => matrix,
});
self
}
#[inline]
pub fn grayscale(self, amount: f32) -> Self {
self.color_matrix(ColorMatrix::grayscale(amount))
}
#[inline]
pub fn sepia(self, amount: f32) -> Self {
self.color_matrix(ColorMatrix::sepia(amount))
}
#[inline]
pub fn saturate(self, amount: f32) -> Self {
self.color_matrix(ColorMatrix::saturate(amount))
}
#[inline]
pub fn brightness(self, amount: f32) -> Self {
self.color_matrix(ColorMatrix::brightness(amount))
}
#[inline]
pub fn contrast(self, amount: f32) -> Self {
self.color_matrix(ColorMatrix::contrast(amount))
}
#[inline]
pub fn hue_rotate(self, deg: f32) -> Self {
self.color_matrix(ColorMatrix::hue_rotate(deg))
}
#[inline]
pub fn invert(self, amount: f32) -> Self {
self.color_matrix(ColorMatrix::invert(amount))
}
#[inline(always)]
pub fn drop_shadow(mut self, blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
self.effects.filter.drop_shadow = Some(DropShadow::new(blur, offset_x, offset_y, color));
self
}
#[inline(always)]
pub fn backdrop_filter(mut self, filter: BackdropFilter) -> Self {
self.effects.backdrop_filter = filter;
self
}
#[inline(always)]
pub fn backdrop_blur(mut self, radius: u8) -> Self {
self.effects.backdrop_filter.blur_radius = radius;
self
}
#[inline]
pub fn backdrop_acrylic(mut self, blur_radius: u8, tint: Color) -> Self {
self.effects.backdrop_filter = BackdropFilter::acrylic(blur_radius, tint);
self
}
#[inline]
pub fn transform(mut self, t: Transform2D) -> Self {
self.effects.transform = Some(match self.effects.transform {
Some(prev) => prev.concat(t),
None => t,
});
self
}
#[inline]
pub fn rotate_deg(self, deg: f32) -> Self {
self.transform(Transform2D::rotate(deg))
}
#[inline]
pub fn scale(self, sx: f32, sy: f32) -> Self {
self.transform(Transform2D::scale(sx, sy))
}
#[inline]
pub fn translate(self, tx: f32, ty: f32) -> Self {
self.transform(Transform2D::translate(tx, ty))
}
#[inline]
pub fn skew(self, ax_deg: f32, ay_deg: f32) -> Self {
let mut s = self;
if ax_deg != 0.0 {
s = s.transform(Transform2D::skew_x(ax_deg));
}
if ay_deg != 0.0 {
s = s.transform(Transform2D::skew_y(ay_deg));
}
s
}
#[inline(always)]
pub fn clip_path(mut self, path_d: &'static str) -> Self {
self.effects.clip_path = Some(StrRef::Static(path_d));
self
}
#[inline]
pub fn clip_path_str(mut self, cx: &'a FrameScope, path_d: &str) -> Self {
let buf = cx.arena().alloc_bytes(path_d.len());
buf.copy_from_slice(path_d.as_bytes());
let s = unsafe { core::str::from_utf8_unchecked(buf) };
self.effects.clip_path = Some(StrRef::Scratch(s));
self
}
#[inline(always)]
pub fn external_texture(mut self, id: crate::paint::ExternalTextureId) -> Self {
self.external_texture = Some(crate::paint::ExternalTextureSpec::new(id));
self
}
#[inline(always)]
pub fn external_texture_spec(mut self, spec: crate::paint::ExternalTextureSpec) -> Self {
self.external_texture = Some(spec);
self
}
#[inline(always)]
pub fn external_texture_uv(mut self, uv_rect: [f32; 4]) -> Self {
if let Some(ext) = &mut self.external_texture {
ext.uv_rect = uv_rect;
}
self
}
#[inline(always)]
pub fn external_texture_revision(mut self, revision: u64) -> Self {
if let Some(ext) = &mut self.external_texture {
ext.content_revision = revision;
}
self
}
#[inline(always)]
pub fn external_texture_format(mut self, premultiplied: bool, fallback_bg: Color) -> Self {
if let Some(ext) = &mut self.external_texture {
ext.premultiplied = premultiplied;
ext.fallback_bg = fallback_bg;
}
self
}
#[inline(always)]
pub fn aspect_ratio(mut self, ratio: f32) -> Self {
self.layout_ext.aspect_ratio = ratio.max(0.0);
self
}
#[inline(always)]
pub fn grid_col_span(mut self, span: u8) -> Self {
self.layout_ext.grid_col_span = span.max(1);
self
}
#[inline]
pub fn grid_template_fr(mut self, cx: &'a FrameScope, weights: &[u16]) -> Self {
if !weights.is_empty() {
self.grid_cols = weights.len().min(u8::MAX as usize) as u8;
self.layout_ext.grid_template_fr = cx.arena().alloc_iter(weights.iter().copied());
}
self
}
#[inline(always)]
pub fn sticky_top(mut self, offset: i16) -> Self {
self.layout_ext.sticky_top = Some(offset);
self
}
#[inline(always)]
pub fn rtl(mut self, rtl: bool) -> Self {
self.layout_ext.rtl = rtl;
self
}
#[inline(always)]
pub fn rtl_auto(mut self, cx: &FrameScope) -> Self {
self.layout_ext.rtl = cx.is_rtl();
self
}
#[inline(always)]
pub fn overflow_auto(mut self) -> Self {
self.scrollable_max = Some(i16::MAX);
self.clip_children = true;
self
}
#[inline(always)]
pub fn scroll_chaining(mut self, enable: bool) -> Self {
self.layout_ext.scroll_chaining = enable;
self
}
}