use crate::values::generics::rect::Rect;
use crate::values::generics::size::Size2D;
use crate::Zero;
use std::fmt::{self, Write};
use style_traits::{CssWriter, ToCss};
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
Parse,
PartialEq,
SpecifiedValueInfo,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[repr(C, u8)]
pub enum GenericBorderImageSideWidth<LP, N> {
Number(N),
LengthPercentage(LP),
Auto,
}
pub use self::GenericBorderImageSideWidth as BorderImageSideWidth;
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[repr(C)]
pub struct GenericBorderImageSlice<NumberOrPercentage> {
#[css(field_bound)]
pub offsets: Rect<NumberOrPercentage>,
#[animation(constant)]
#[css(represents_keyword)]
pub fill: bool,
}
pub use self::GenericBorderImageSlice as BorderImageSlice;
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
Deserialize,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
Serialize,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[repr(C)]
pub struct GenericBorderCornerRadius<L>(
#[css(field_bound)]
#[shmem(field_bound)]
pub Size2D<L>,
);
pub use self::GenericBorderCornerRadius as BorderCornerRadius;
impl<L> BorderCornerRadius<L> {
pub fn new(w: L, h: L) -> Self {
BorderCornerRadius(Size2D::new(w, h))
}
}
impl<L: Zero> Zero for BorderCornerRadius<L> {
fn zero() -> Self {
BorderCornerRadius(Size2D::zero())
}
fn is_zero(&self) -> bool {
self.0.is_zero()
}
}
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[repr(transparent)]
pub struct GenericBorderSpacing<L>(
#[css(field_bound)]
#[shmem(field_bound)]
pub Size2D<L>,
);
pub use self::GenericBorderSpacing as BorderSpacing;
impl<L> BorderSpacing<L> {
pub fn new(w: L, h: L) -> Self {
BorderSpacing(Size2D::new(w, h))
}
}
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
Deserialize,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
Serialize,
ToAnimatedValue,
ToComputedValue,
ToResolvedValue,
ToShmem,
)]
#[repr(C)]
pub struct GenericBorderRadius<LengthPercentage> {
#[shmem(field_bound)]
pub top_left: GenericBorderCornerRadius<LengthPercentage>,
pub top_right: GenericBorderCornerRadius<LengthPercentage>,
pub bottom_right: GenericBorderCornerRadius<LengthPercentage>,
pub bottom_left: GenericBorderCornerRadius<LengthPercentage>,
}
pub use self::GenericBorderRadius as BorderRadius;
impl<L> BorderRadius<L> {
#[inline]
pub fn new(
tl: BorderCornerRadius<L>,
tr: BorderCornerRadius<L>,
br: BorderCornerRadius<L>,
bl: BorderCornerRadius<L>,
) -> Self {
BorderRadius {
top_left: tl,
top_right: tr,
bottom_right: br,
bottom_left: bl,
}
}
pub fn serialize_rects<W>(
widths: Rect<&L>,
heights: Rect<&L>,
dest: &mut CssWriter<W>,
) -> fmt::Result
where
L: PartialEq + ToCss,
W: Write,
{
widths.to_css(dest)?;
if widths != heights {
dest.write_str(" / ")?;
heights.to_css(dest)?;
}
Ok(())
}
}
impl<L: Zero> Zero for BorderRadius<L> {
fn zero() -> Self {
Self::new(
BorderCornerRadius::<L>::zero(),
BorderCornerRadius::<L>::zero(),
BorderCornerRadius::<L>::zero(),
BorderCornerRadius::<L>::zero(),
)
}
fn is_zero(&self) -> bool {
self.top_left.is_zero() &&
self.top_right.is_zero() &&
self.bottom_right.is_zero() &&
self.bottom_left.is_zero()
}
}
impl<L> ToCss for BorderRadius<L>
where
L: PartialEq + ToCss,
{
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
let BorderRadius {
top_left: BorderCornerRadius(ref tl),
top_right: BorderCornerRadius(ref tr),
bottom_right: BorderCornerRadius(ref br),
bottom_left: BorderCornerRadius(ref bl),
} = *self;
let widths = Rect::new(&tl.width, &tr.width, &br.width, &bl.width);
let heights = Rect::new(&tl.height, &tr.height, &br.height, &bl.height);
Self::serialize_rects(widths, heights, dest)
}
}