use super::*;
impl Element {
pub fn padding_axis(mut self, vertical: f32, horizontal: f32) -> Self {
self.layout.padding = TaffyRect {
left: LengthPercentage::length(horizontal),
right: LengthPercentage::length(horizontal),
top: LengthPercentage::length(vertical),
bottom: LengthPercentage::length(vertical),
};
self
}
pub fn bg(mut self, color: Color) -> Self {
self.visual.background = Some(color);
self
}
pub fn opacity(mut self, opacity: f32) -> Self {
self.visual.opacity = finite_opacity(opacity);
self
}
pub fn border(mut self, width: f32, color: Color) -> Self {
let width = width.max(0.0);
self.visual.border_width = width;
self.visual.border_color = Some(color);
self.layout.border = TaffyRect {
left: LengthPercentage::length(width),
right: LengthPercentage::length(width),
top: LengthPercentage::length(width),
bottom: LengthPercentage::length(width),
};
self
}
pub fn rounded(mut self, radius: f32) -> Self {
self.visual.radius = radius.max(0.0);
self
}
pub fn rounded_sm(self) -> Self {
self.rounded(4.0)
}
pub fn rounded_md(self) -> Self {
self.rounded(6.0)
}
pub fn rounded_lg(self) -> Self {
self.rounded(8.0)
}
pub fn rounded_xl(self) -> Self {
self.rounded(12.0)
}
pub fn rounded_2xl(self) -> Self {
self.rounded(16.0)
}
pub fn shadow(mut self, shadow: BoxShadow) -> Self {
self.visual.shadows = Some(Arc::from([shadow]));
self
}
pub fn shadows(mut self, shadows: impl IntoIterator<Item = BoxShadow>) -> Self {
self.visual.shadows = Some(collect_box_shadows(shadows));
self
}
pub fn shadow_none(mut self) -> Self {
self.visual.shadows = None;
self
}
pub fn shadow_sm(self) -> Self {
self.shadow(shadow_sm_preset())
}
pub fn shadow_md(self) -> Self {
self.shadows(shadow_md_preset())
}
pub fn shadow_lg(self) -> Self {
self.shadows(shadow_lg_preset())
}
pub fn shadow_xl(self) -> Self {
self.shadows(shadow_xl_preset())
}
pub fn shadow_2xl(self) -> Self {
self.shadow(shadow_2xl_preset())
}
pub fn object_fit(mut self, fit: ObjectFit) -> Self {
match &mut self.kind {
ElementKind::Image(image) => image.object_fit = fit,
ElementKind::Svg(svg) => svg.object_fit = fit,
ElementKind::Path(path) => path.object_fit = fit,
_ => {}
}
self
}
pub fn path_background(mut self, background: impl Into<Background>) -> Self {
if let ElementKind::Path(path) = &mut self.kind {
path.background = Some(background.into());
}
self
}
pub fn shader_parameters(mut self, parameters: impl Into<ShaderParameters>) -> Self {
let ElementKind::CustomShader(shader) = &mut self.kind else {
panic!("shader_parameters can only be applied to a custom shader element");
};
shader.parameters = parameters.into();
self
}
pub fn svg_transform(mut self, transform: SvgTransform) -> Self {
if let ElementKind::Svg(svg) = &mut self.kind {
svg.transform = transform;
}
self
}
pub fn grayscale(mut self, grayscale: bool) -> Self {
if let ElementKind::Image(image) = &mut self.kind {
image.grayscale = grayscale;
}
self
}
pub fn with_loading<E: IntoElement + 'static>(
mut self,
render: impl Fn() -> E + 'static,
) -> Self {
if let ElementKind::Image(image) = &mut self.kind {
image.loading = Some(ImageReplacement::new(render));
}
self
}
pub fn with_fallback<E: IntoElement + 'static>(
mut self,
render: impl Fn() -> E + 'static,
) -> Self {
if let ElementKind::Image(image) = &mut self.kind {
image.fallback = Some(ImageReplacement::new(render));
}
self
}
pub fn text_color(mut self, color: Color) -> Self {
self.typography.color = Some(color);
self
}
pub fn text_size(mut self, size: f32) -> Self {
self.typography.font_size = Some(size.max(1.0));
self
}
pub fn line_height(mut self, height: f32) -> Self {
self.typography.line_height = Some(height.max(1.0));
self
}
pub fn monospace_width(mut self, width: f32) -> Self {
self.typography.monospace_width = Some(width.max(1.0));
self
}
pub fn text_xs(self) -> Self {
self.text_size(12.0).line_height(16.0)
}
pub fn text_sm(self) -> Self {
self.text_size(14.0).line_height(20.0)
}
pub fn text_base(self) -> Self {
self.text_size(16.0).line_height(24.0)
}
pub fn text_lg(self) -> Self {
self.text_size(18.0).line_height(26.0)
}
pub fn text_xl(self) -> Self {
self.text_size(20.0).line_height(28.0)
}
pub fn text_2xl(self) -> Self {
self.text_size(24.0).line_height(32.0)
}
pub fn text_3xl(self) -> Self {
self.text_size(30.0).line_height(36.0)
}
pub fn font_weight(mut self, weight: Weight) -> Self {
self.typography.weight = Some(weight);
self
}
pub fn font_normal(self) -> Self {
self.font_weight(Weight::NORMAL)
}
pub fn font_medium(self) -> Self {
self.font_weight(Weight::MEDIUM)
}
pub fn font_semibold(self) -> Self {
self.font_weight(Weight::SEMIBOLD)
}
pub fn font_bold(self) -> Self {
self.font_weight(Weight::BOLD)
}
pub fn font_style(mut self, style: GlyphStyle) -> Self {
self.typography.font_style = Some(style);
self
}
pub fn italic(self) -> Self {
self.font_style(GlyphStyle::Italic)
}
pub fn not_italic(self) -> Self {
self.font_style(GlyphStyle::Normal)
}
pub fn font_thicken(mut self, thicken: bool) -> Self {
self.typography.font_thicken = Some(thicken);
self
}
pub fn underline(mut self) -> Self {
self.typography.underline = Some(TextUnderline::Single);
self.typography.underline_wavy = Some(false);
self.typography.underline_thickness = Some(1.0);
self
}
pub fn double_underline(mut self) -> Self {
self.typography.underline = Some(TextUnderline::Double);
self.typography.underline_wavy = Some(false);
self.typography.underline_thickness = Some(1.0);
self
}
pub fn line_through(mut self) -> Self {
self.typography.strikethrough = Some(true);
self
}
pub fn text_decoration_none(mut self) -> Self {
self.typography.underline = Some(TextUnderline::None);
self.typography.underline_color = Some(None);
self.typography.underline_wavy = Some(false);
self.typography.underline_thickness = Some(1.0);
self.typography.strikethrough = Some(false);
self.typography.strikethrough_color = Some(None);
self
}
pub fn text_decoration_color(mut self, color: Color) -> Self {
if self
.typography
.underline
.is_none_or(|underline| underline == TextUnderline::None)
{
self.typography.underline = Some(TextUnderline::Single);
self.typography.underline_wavy = Some(false);
self.typography.underline_thickness = Some(1.0);
}
self.typography.underline_color = Some(Some(color));
self
}
pub fn text_decoration_solid(mut self) -> Self {
let enables_underline = self
.typography
.underline
.is_none_or(|underline| underline == TextUnderline::None);
if enables_underline {
self.typography.underline = Some(TextUnderline::Single);
self.typography.underline_thickness = Some(1.0);
}
self.typography.underline_wavy = Some(false);
self
}
pub fn text_decoration_wavy(mut self) -> Self {
let enables_underline = self
.typography
.underline
.is_none_or(|underline| underline == TextUnderline::None);
if enables_underline {
self.typography.underline = Some(TextUnderline::Single);
self.typography.underline_thickness = Some(1.0);
}
self.typography.underline_wavy = Some(true);
self
}
fn text_decoration_thickness(mut self, thickness: f32) -> Self {
if self
.typography
.underline
.is_none_or(|underline| underline == TextUnderline::None)
{
self.typography.underline = Some(TextUnderline::Single);
}
self.typography.underline_thickness = Some(thickness);
self
}
pub fn text_decoration_0(self) -> Self {
self.text_decoration_thickness(0.0)
}
pub fn text_decoration_1(self) -> Self {
self.text_decoration_thickness(1.0)
}
pub fn text_decoration_2(self) -> Self {
self.text_decoration_thickness(2.0)
}
pub fn text_decoration_4(self) -> Self {
self.text_decoration_thickness(4.0)
}
pub fn text_decoration_8(self) -> Self {
self.text_decoration_thickness(8.0)
}
pub fn font_family(mut self, family: impl Into<FontFamily>) -> Self {
let family = family.into();
assert_valid_font_family(&family);
self.typography.family = Some(family);
self
}
pub fn font_features(mut self, features: FontFeatures) -> Self {
self.typography.features = Some(features);
self
}
pub fn font_fallbacks(mut self, fallbacks: FontFallbacks) -> Self {
self.typography.fallbacks = Some((!fallbacks.is_empty()).then_some(fallbacks));
self
}
pub fn font(mut self, font: Font) -> Self {
assert_valid_font_family(&font.family);
self.typography.family = Some(font.family);
self.typography.features = Some(font.features);
self.typography.fallbacks = Some(normalize_fallbacks(font.fallbacks));
self.typography.weight = Some(font.weight);
self.typography.font_style = Some(font.style);
self
}
pub fn text_align(mut self, align: TextAlign) -> Self {
self.typography.align = Some(align);
self
}
pub fn text_left(self) -> Self {
self.text_align(TextAlign::Left)
}
pub fn text_center(self) -> Self {
self.text_align(TextAlign::Center)
}
pub fn text_right(self) -> Self {
self.text_align(TextAlign::Right)
}
pub fn text_justify(self) -> Self {
self.text_align(TextAlign::Justify)
}
pub fn no_wrap(mut self) -> Self {
self.typography.wrap = Some(TextWrap::None);
self
}
pub fn white_space(mut self, white_space: WhiteSpace) -> Self {
self.typography.wrap = Some(white_space.into());
self
}
pub fn whitespace_normal(self) -> Self {
self.white_space(WhiteSpace::Normal)
}
pub fn whitespace_nowrap(self) -> Self {
self.white_space(WhiteSpace::Nowrap)
}
pub fn text_overflow(mut self, overflow: TextOverflow) -> Self {
self.typography.text_overflow = Some(overflow);
self
}
pub fn text_ellipsis(self) -> Self {
self.text_overflow(TextOverflow::ellipsis())
}
pub fn text_ellipsis_start(self) -> Self {
self.text_overflow(TextOverflow::ellipsis_start())
}
pub fn text_ellipsis_middle(self) -> Self {
self.text_overflow(TextOverflow::ellipsis_middle())
}
pub fn line_clamp(mut self, lines: usize) -> Self {
self.typography.line_clamp = Some(lines.max(1));
self.overflow_hidden()
}
pub fn truncate(self) -> Self {
self.overflow_hidden().whitespace_nowrap().text_ellipsis()
}
pub fn text_shaping_basic(mut self) -> Self {
self.typography.shaping = Some(TextShaping::Basic);
self
}
pub fn text_shaping(mut self, shaping: TextShaping) -> Self {
self.typography.shaping = Some(shaping);
self
}
pub fn wrap(mut self) -> Self {
self.typography.wrap = Some(TextWrap::Word);
self
}
pub fn overflow_hidden(mut self) -> Self {
self.layout.overflow = TaffyPoint {
x: Overflow::Hidden,
y: Overflow::Hidden,
};
self
}
pub fn overflow_y_scroll(mut self) -> Self {
self.layout.overflow = TaffyPoint {
x: Overflow::Hidden,
y: Overflow::Scroll,
};
self
}
pub fn scroll_to_end(mut self, revision: u64) -> Self {
self.scroll_to_end_revision = Some(revision);
self
}
pub fn virtual_scroll(mut self, list: &VirtualList) -> Self {
self.layout.overflow.y = Overflow::Hidden;
self.virtual_scroll = Some(VirtualScrollStyle {
handle: list.scroll_handle(),
max_offset_y: list.max_scroll_offset(),
measurement_revision: 0,
mount: list.scroll_mount(),
});
self
}
pub fn variable_virtual_scroll(mut self, list: &ListState) -> Self {
self.layout.overflow.y = Overflow::Hidden;
let (handle, max_offset_y, measurement_revision, mount) = list.scroll_binding();
self.virtual_scroll = Some(VirtualScrollStyle {
handle,
max_offset_y,
measurement_revision,
mount,
});
self
}
pub fn absolute(mut self) -> Self {
self.layout.position = Position::Absolute;
self
}
pub fn z_index(mut self, value: i16) -> Self {
self.z_index = Some(value);
self
}
pub fn overlay(mut self) -> Self {
self.layout.position = Position::Absolute;
self.plane = Some(ScenePlane::Overlay);
self.portal = true;
self.blocks_pointer = true;
self
}
pub fn anchor_to(mut self, target: impl Into<ElementId>, placement: AnchorPlacement) -> Self {
self = self.overlay();
self.anchor = Some(AnchorStyle {
target: AnchorTarget::Element(target.into()),
placement,
gap: DEFAULT_ANCHOR_GAP,
viewport_margin: DEFAULT_VIEWPORT_MARGIN,
});
self
}
pub fn anchor_at(mut self, point: crate::Point, placement: AnchorPlacement) -> Self {
self = self.overlay();
let point = crate::Point::new(
if point.x.is_finite() { point.x } else { 0.0 },
if point.y.is_finite() { point.y } else { 0.0 },
);
self.anchor = Some(AnchorStyle {
target: AnchorTarget::Point(point),
placement,
gap: 0.0,
viewport_margin: DEFAULT_VIEWPORT_MARGIN,
});
self
}
pub fn anchor_gap(mut self, gap: f32) -> Self {
if let Some(anchor) = &mut self.anchor {
anchor.gap = gap.max(0.0);
}
self
}
pub fn viewport_margin(mut self, margin: f32) -> Self {
if let Some(anchor) = &mut self.anchor {
anchor.viewport_margin = margin.max(0.0);
}
self
}
pub fn tooltip(mut self, tooltip: impl Into<Tooltip>) -> Self {
let tooltip = tooltip.into();
if self.accessibility.description.is_none() {
self.accessibility.description = tooltip.accessibility_description.clone();
}
self.tooltip = Some(tooltip);
self
}
pub fn block_pointer(mut self) -> Self {
self.blocks_pointer = true;
self
}
pub fn app_region(mut self, region: AppRegion) -> Self {
self.app_region = Some(region);
self
}
pub fn app_region_drag(self) -> Self {
self.app_region(AppRegion::Drag)
}
pub fn app_region_no_drag(self) -> Self {
self.app_region(AppRegion::NoDrag)
}
pub fn relative(mut self) -> Self {
self.layout.position = Position::Relative;
self
}
pub fn top(mut self, value: f32) -> Self {
self.layout.inset.top = LengthPercentageAuto::length(value);
self
}
pub fn right(mut self, value: f32) -> Self {
self.layout.inset.right = LengthPercentageAuto::length(value);
self
}
pub fn bottom(mut self, value: f32) -> Self {
self.layout.inset.bottom = LengthPercentageAuto::length(value);
self
}
pub fn left(mut self, value: f32) -> Self {
self.layout.inset.left = LengthPercentageAuto::length(value);
self
}
pub fn inset_0(mut self) -> Self {
let zero = LengthPercentageAuto::length(0.0);
self.layout.inset = TaffyRect {
left: zero,
right: zero,
top: zero,
bottom: zero,
};
self
}
}