style: Generalise BorderCornerRadius as Size.

This commit is contained in:
Emilio Cobos Álvarez 2017-09-15 18:11:27 +02:00
parent 4936314b7e
commit f9c06d7932
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
6 changed files with 122 additions and 60 deletions

View file

@ -7,11 +7,11 @@
use values::animated::ToAnimatedZero; use values::animated::ToAnimatedZero;
use values::computed::{Number, NumberOrPercentage}; use values::computed::{Number, NumberOrPercentage};
use values::computed::length::LengthOrPercentage; use values::computed::length::LengthOrPercentage;
use values::generics::border::BorderCornerRadius as GenericBorderCornerRadius;
use values::generics::border::BorderImageSideWidth as GenericBorderImageSideWidth; use values::generics::border::BorderImageSideWidth as GenericBorderImageSideWidth;
use values::generics::border::BorderImageSlice as GenericBorderImageSlice; use values::generics::border::BorderImageSlice as GenericBorderImageSlice;
use values::generics::border::BorderRadius as GenericBorderRadius; use values::generics::border::BorderRadius as GenericBorderRadius;
use values::generics::rect::Rect; use values::generics::rect::Rect;
use values::generics::size::Size;
/// A computed value for the `border-image-width` property. /// A computed value for the `border-image-width` property.
pub type BorderImageWidth = Rect<BorderImageSideWidth>; pub type BorderImageWidth = Rect<BorderImageSideWidth>;
@ -26,7 +26,7 @@ pub type BorderImageSlice = GenericBorderImageSlice<NumberOrPercentage>;
pub type BorderRadius = GenericBorderRadius<LengthOrPercentage>; pub type BorderRadius = GenericBorderRadius<LengthOrPercentage>;
/// A computed value for the `border-*-radius` longhand properties. /// A computed value for the `border-*-radius` longhand properties.
pub type BorderCornerRadius = GenericBorderCornerRadius<LengthOrPercentage>; pub type BorderCornerRadius = Size<LengthOrPercentage>;
impl BorderImageSideWidth { impl BorderImageSideWidth {
/// Returns `1`. /// Returns `1`.

View file

@ -4,10 +4,10 @@
//! Generic types for CSS values related to borders. //! Generic types for CSS values related to borders.
use euclid::Size2D;
use std::fmt; use std::fmt;
use style_traits::ToCss; use style_traits::ToCss;
use values::generics::rect::Rect; use values::generics::rect::Rect;
use values::generics::size::Size;
/// A generic value for a single side of a `border-image-width` property. /// A generic value for a single side of a `border-image-width` property.
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))] #[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
@ -42,22 +42,15 @@ pub struct BorderImageSlice<NumberOrPercentage> {
#[derive(PartialEq, ToComputedValue)] #[derive(PartialEq, ToComputedValue)]
pub struct BorderRadius<LengthOrPercentage> { pub struct BorderRadius<LengthOrPercentage> {
/// The top left radius. /// The top left radius.
pub top_left: BorderCornerRadius<LengthOrPercentage>, pub top_left: Size<LengthOrPercentage>,
/// The top right radius. /// The top right radius.
pub top_right: BorderCornerRadius<LengthOrPercentage>, pub top_right: Size<LengthOrPercentage>,
/// The bottom right radius. /// The bottom right radius.
pub bottom_right: BorderCornerRadius<LengthOrPercentage>, pub bottom_right: Size<LengthOrPercentage>,
/// The bottom left radius. /// The bottom left radius.
pub bottom_left: BorderCornerRadius<LengthOrPercentage>, pub bottom_left: Size<LengthOrPercentage>,
} }
/// A generic value for `border-*-radius` longhand properties.
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug)]
#[derive(PartialEq, ToComputedValue)]
pub struct BorderCornerRadius<L>(pub Size2D<L>);
impl<N> From<N> for BorderImageSlice<N> impl<N> From<N> for BorderImageSlice<N>
where N: Clone, where N: Clone,
{ {
@ -87,11 +80,7 @@ impl<N> ToCss for BorderImageSlice<N>
impl<L> BorderRadius<L> { impl<L> BorderRadius<L> {
/// Returns a new `BorderRadius<L>`. /// Returns a new `BorderRadius<L>`.
#[inline] #[inline]
pub fn new(tl: BorderCornerRadius<L>, pub fn new(tl: Size<L>, tr: Size<L>, br: Size<L>, bl: Size<L>) -> Self {
tr: BorderCornerRadius<L>,
br: BorderCornerRadius<L>,
bl: BorderCornerRadius<L>)
-> Self {
BorderRadius { BorderRadius {
top_left: tl, top_left: tl,
top_right: tr, top_right: tr,
@ -135,34 +124,3 @@ impl<L> ToCss for BorderRadius<L>
Self::serialize_rects(widths, heights, dest) Self::serialize_rects(widths, heights, dest)
} }
} }
impl<L> BorderCornerRadius<L> {
#[inline]
/// Create a new `BorderCornerRadius` for an area of given width and height.
pub fn new(width: L, height: L) -> BorderCornerRadius<L> {
BorderCornerRadius(Size2D::new(width, height))
}
}
impl<L: Clone> From<L> for BorderCornerRadius<L> {
fn from(radius: L) -> Self {
Self::new(radius.clone(), radius)
}
}
impl<L> ToCss for BorderCornerRadius<L>
where L: ToCss + PartialEq,
{
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write
{
self.0.width.to_css(dest)?;
if self.0.height != self.0.width {
dest.write_str(" ")?;
self.0.height.to_css(dest)?;
}
Ok(())
}
}

View file

@ -25,6 +25,7 @@ pub mod grid;
pub mod image; pub mod image;
pub mod position; pub mod position;
pub mod rect; pub mod rect;
pub mod size;
pub mod svg; pub mod svg;
pub mod text; pub mod text;
pub mod transform; pub mod transform;

View file

@ -0,0 +1,103 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Generic type for CSS properties that are composed by two dimensions.
use cssparser::Parser;
use euclid::Size2D;
use parser::ParserContext;
use std::fmt;
use style_traits::{ToCss, ParseError};
use values::animated::ToAnimatedValue;
/// A generic size, for `border-*-radius` longhand properties, or
/// `border-spacing`.
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug)]
#[derive(PartialEq, ToComputedValue)]
pub struct Size<L>(pub Size2D<L>);
impl<L> Size<L> {
#[inline]
/// Create a new `Size` for an area of given width and height.
pub fn new(width: L, height: L) -> Size<L> {
Size(Size2D::new(width, height))
}
/// Returns the width component.
pub fn width(&self) -> &L {
&self.0.width
}
/// Returns the height component.
pub fn height(&self) -> &L {
&self.0.height
}
/// Parse a `Size` with a given parsing function.
pub fn parse_with<'i, 't, F>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
parse_one: F,
) -> Result<Self, ParseError<'i>>
where
L: Clone,
F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<L, ParseError<'i>>
{
let first = parse_one(context, input)?;
let second = input
.try(|i| parse_one(context, i))
.unwrap_or_else(|_| first.clone());
Ok(Self::new(first, second))
}
}
impl<L: Clone> From<L> for Size<L> {
fn from(size: L) -> Self {
Self::new(size.clone(), size)
}
}
impl<L> ToCss for Size<L>
where L:
ToCss + PartialEq,
{
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W:
fmt::Write
{
self.0.width.to_css(dest)?;
if self.0.height != self.0.width {
dest.write_str(" ")?;
self.0.height.to_css(dest)?;
}
Ok(())
}
}
impl<L> ToAnimatedValue for Size<L>
where L:
ToAnimatedValue,
{
type AnimatedValue = Size<L::AnimatedValue>;
#[inline]
fn to_animated_value(self) -> Self::AnimatedValue {
Size(Size2D::new(
self.0.width.to_animated_value(),
self.0.height.to_animated_value(),
))
}
#[inline]
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
Size(Size2D::new(
L::from_animated_value(animated.0.width),
L::from_animated_value(animated.0.height),
))
}
}

View file

@ -8,7 +8,7 @@ use cssparser::Parser;
use parser::{Parse, ParserContext}; use parser::{Parse, ParserContext};
use style_traits::ParseError; use style_traits::ParseError;
use values::computed::{Context, NonNegativeLength, ToComputedValue}; use values::computed::{Context, NonNegativeLength, ToComputedValue};
use values::generics::border::BorderCornerRadius as GenericBorderCornerRadius; use values::generics::size::Size;
use values::generics::border::BorderImageSideWidth as GenericBorderImageSideWidth; use values::generics::border::BorderImageSideWidth as GenericBorderImageSideWidth;
use values::generics::border::BorderImageSlice as GenericBorderImageSlice; use values::generics::border::BorderImageSlice as GenericBorderImageSlice;
use values::generics::border::BorderRadius as GenericBorderRadius; use values::generics::border::BorderRadius as GenericBorderRadius;
@ -44,7 +44,7 @@ pub type BorderImageSlice = GenericBorderImageSlice<NumberOrPercentage>;
pub type BorderRadius = GenericBorderRadius<LengthOrPercentage>; pub type BorderRadius = GenericBorderRadius<LengthOrPercentage>;
/// A specified value for the `border-*-radius` longhand properties. /// A specified value for the `border-*-radius` longhand properties.
pub type BorderCornerRadius = GenericBorderCornerRadius<LengthOrPercentage>; pub type BorderCornerRadius = Size<LengthOrPercentage>;
impl BorderSideWidth { impl BorderSideWidth {
/// Parses, with quirks. /// Parses, with quirks.

View file

@ -29,14 +29,14 @@ macro_rules! assert_border_radius_values {
let input = parse(BorderRadius::parse, $input) let input = parse(BorderRadius::parse, $input)
.expect(&format!("Failed parsing {} as border radius", .expect(&format!("Failed parsing {} as border radius",
$input)); $input));
assert_eq!(::style_traits::ToCss::to_css_string(&input.top_left.0.width), $tlw); assert_eq!(::style_traits::ToCss::to_css_string(&input.top_left.0.width()), $tlw);
assert_eq!(::style_traits::ToCss::to_css_string(&input.top_right.0.width), $trw); assert_eq!(::style_traits::ToCss::to_css_string(&input.top_right.0.width()), $trw);
assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_right.0.width), $brw); assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_right.0.width()), $brw);
assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_left.0.width), $blw); assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_left.0.width()), $blw);
assert_eq!(::style_traits::ToCss::to_css_string(&input.top_left.0.height), $tlh); assert_eq!(::style_traits::ToCss::to_css_string(&input.top_left.0.height()), $tlh);
assert_eq!(::style_traits::ToCss::to_css_string(&input.top_right.0.height), $trh); assert_eq!(::style_traits::ToCss::to_css_string(&input.top_right.0.height()), $trh);
assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_right.0.height), $brh); assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_right.0.height()), $brh);
assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_left.0.height), $blh); assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_left.0.height()), $blh);
} }
} }