Create a module for generics and make BorderRadiusSize generic

This commit is contained in:
Ravi Shankar 2017-04-12 21:02:43 +05:30
parent c5c6e8c442
commit 0249270f61
9 changed files with 91 additions and 74 deletions

View file

@ -16,6 +16,7 @@ use style::logical_geometry::{LogicalMargin, WritingMode};
use style::properties::ServoComputedValues;
use style::values::computed::{BorderRadiusSize, LengthOrPercentageOrAuto};
use style::values::computed::{LengthOrPercentage, LengthOrPercentageOrNone};
use style::values::generics;
/// A collapsible margin. See CSS 2.1 § 8.3.1.
#[derive(Copy, Clone, Debug)]
@ -478,7 +479,7 @@ pub fn specified(length: LengthOrPercentage, containing_length: Au) -> Au {
}
pub fn specified_border_radius(radius: BorderRadiusSize, containing_length: Au) -> Size2D<Au> {
let BorderRadiusSize(size) = radius;
let generics::BorderRadiusSize(size) = radius;
let w = specified(size.width, containing_length);
let h = specified(size.height, containing_length);
Size2D::new(w, h)

View file

@ -318,7 +318,6 @@ impl nsStyleImage {
pub mod basic_shape {
//! Conversions from and to CSS shape representations.
use euclid::size::Size2D;
use gecko::values::GeckoStyleCoordConvertible;
use gecko_bindings::structs;
use gecko_bindings::structs::{StyleBasicShape, StyleBasicShapeType, StyleFillRule};
@ -329,6 +328,7 @@ pub mod basic_shape {
use values::computed::{BorderRadiusSize, LengthOrPercentage};
use values::computed::basic_shape::*;
use values::computed::position;
use values::generics::BorderRadiusSize as GenericBorderRadiusSize;
// using Borrow so that we can have a non-moving .into()
impl<T: Borrow<StyleBasicShape>> From<T> for BasicShape {
@ -391,11 +391,11 @@ pub mod basic_shape {
fn from(other: T) -> Self {
let other = other.borrow();
let get_corner = |index| {
BorderRadiusSize(Size2D::new(
GenericBorderRadiusSize::new(
LengthOrPercentage::from_gecko_style_coord(&other.data_at(index))
.expect("<border-radius> should be a length, percentage, or calc value"),
LengthOrPercentage::from_gecko_style_coord(&other.data_at(index + 1))
.expect("<border-radius> should be a length, percentage, or calc value")))
.expect("<border-radius> should be a length, percentage, or calc value"))
};
BorderRadius {

View file

@ -543,15 +543,14 @@ fn color_to_nscolor_zero_currentcolor(color: Color) -> structs::nscolor {
% if need_clone:
#[allow(non_snake_case)]
pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T {
use properties::longhands::${ident}::computed_value::T;
use euclid::Size2D;
use values::generics::BorderRadiusSize;
let width = GeckoStyleCoordConvertible::from_gecko_style_coord(
&self.gecko.${gecko_ffi_name}.data_at(${x_index}))
.expect("Failed to clone ${ident}");
let height = GeckoStyleCoordConvertible::from_gecko_style_coord(
&self.gecko.${gecko_ffi_name}.data_at(${y_index}))
.expect("Failed to clone ${ident}");
T(Size2D::new(width, height))
BorderRadiusSize::new(width, height)
}
% endif
</%def>

View file

@ -31,7 +31,7 @@ use std::fmt;
use style_traits::ToCss;
use super::ComputedValues;
use values::CSSFloat;
use values::{Auto, Either, Normal};
use values::{Auto, Either, Normal, generics};
use values::computed::{Angle, LengthOrPercentageOrAuto, LengthOrPercentageOrNone};
use values::computed::{BorderRadiusSize, ClipRect, LengthOrNone};
use values::computed::{CalcLengthOrPercentage, Context, LengthOrPercentage};
@ -711,7 +711,7 @@ impl<T: Interpolate + Copy> Interpolate for Point2D<T> {
impl Interpolate for BorderRadiusSize {
#[inline]
fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
self.0.interpolate(&other.0, progress).map(BorderRadiusSize)
self.0.interpolate(&other.0, progress).map(generics::BorderRadiusSize)
}
}

View file

@ -11,7 +11,9 @@ use media_queries::Device;
use properties::ComputedValues;
use std::fmt;
use style_traits::ToCss;
use super::{CSSFloat, CSSInteger, RGBA, specified};
use super::{CSSFloat, CSSInteger, RGBA};
use super::generics::BorderRadiusSize as GenericBorderRadiusSize;
use super::specified;
use super::specified::grid::{TrackBreadth as GenericTrackBreadth, TrackSize as GenericTrackSize};
pub use cssparser::Color as CSSColor;
@ -297,43 +299,19 @@ impl ComputedValueAsSpecified for specified::AlignJustifyContent {}
impl ComputedValueAsSpecified for specified::AlignJustifySelf {}
impl ComputedValueAsSpecified for specified::BorderStyle {}
#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)]
pub struct BorderRadiusSize(pub Size2D<LengthOrPercentage>);
/// The computed value of `BorderRadiusSize`
pub type BorderRadiusSize = GenericBorderRadiusSize<LengthOrPercentage>;
impl BorderRadiusSize {
#[allow(missing_docs)]
/// Create a null value.
#[inline]
pub fn zero() -> BorderRadiusSize {
BorderRadiusSize(Size2D::new(LengthOrPercentage::Length(Au(0)), LengthOrPercentage::Length(Au(0))))
let zero = LengthOrPercentage::zero();
GenericBorderRadiusSize(Size2D::new(zero.clone(), zero))
}
}
impl ToComputedValue for specified::BorderRadiusSize {
type ComputedValue = BorderRadiusSize;
#[inline]
fn to_computed_value(&self, context: &Context) -> BorderRadiusSize {
let w = self.0.width.to_computed_value(context);
let h = self.0.height.to_computed_value(context);
BorderRadiusSize(Size2D::new(w, h))
}
#[inline]
fn from_computed_value(computed: &BorderRadiusSize) -> Self {
let w = ToComputedValue::from_computed_value(&computed.0.width);
let h = ToComputedValue::from_computed_value(&computed.0.height);
specified::BorderRadiusSize(Size2D::new(w, h))
}
}
impl ToCss for BorderRadiusSize {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.0.width.to_css(dest));
try!(dest.write_str("/"));
self.0.height.to_css(dest)
}
}
impl Copy for BorderRadiusSize {}
#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]

View file

@ -0,0 +1,65 @@
/* 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 types that share their serialization implementations
//! for both specified and computed values.
use euclid::size::Size2D;
use std::fmt;
use style_traits::ToCss;
use super::HasViewportPercentage;
use super::computed::{Context, ToComputedValue};
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
/// A type for representing CSS `widthh` and `height` values.
pub struct BorderRadiusSize<L>(pub Size2D<L>);
impl<L> HasViewportPercentage for BorderRadiusSize<L> {
#[inline]
fn has_viewport_percentage(&self) -> bool { false }
}
impl<L> BorderRadiusSize<L> {
#[inline]
/// Create a new `BorderRadiusSize` for an area of given width and height.
pub fn new(width: L, height: L) -> BorderRadiusSize<L> {
BorderRadiusSize(Size2D::new(width, height))
}
}
impl<L: Clone> BorderRadiusSize<L> {
#[inline]
/// Create a new `BorderRadiusSize` for a circle of given radius.
pub fn circle(radius: L) -> BorderRadiusSize<L> {
BorderRadiusSize(Size2D::new(radius.clone(), radius))
}
}
impl<L: ToCss> ToCss for BorderRadiusSize<L> {
#[inline]
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.width.to_css(dest)?;
dest.write_str(" ")?;
self.0.height.to_css(dest)
}
}
impl<L: ToComputedValue> ToComputedValue for BorderRadiusSize<L> {
type ComputedValue = BorderRadiusSize<L::ComputedValue>;
#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
let w = self.0.width.to_computed_value(context);
let h = self.0.height.to_computed_value(context);
BorderRadiusSize(Size2D::new(w, h))
}
#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
let w = ToComputedValue::from_computed_value(&computed.0.width);
let h = ToComputedValue::from_computed_value(&computed.0.height);
BorderRadiusSize(Size2D::new(w, h))
}
}

View file

@ -82,6 +82,7 @@ macro_rules! add_impls_for_keyword_enum {
}
pub mod computed;
pub mod generics;
pub mod specified;
/// A CSS float value.

View file

@ -1146,7 +1146,9 @@ impl ToCss for LengthOrPercentage {
}
}
}
impl LengthOrPercentage {
#[inline]
/// Returns a `zero` length.
pub fn zero() -> LengthOrPercentage {
LengthOrPercentage::Length(NoCalcLength::zero())

View file

@ -22,6 +22,7 @@ use style_traits::values::specified::AllowedNumericType;
use super::{Auto, CSSFloat, CSSInteger, HasViewportPercentage, Either, None_};
use super::computed::{self, Context};
use super::computed::{Shadow as ComputedShadow, ToComputedValue};
use super::generics::BorderRadiusSize as GenericBorderRadiusSize;
#[cfg(feature = "gecko")]
pub use self::align::{AlignItems, AlignJustifyContent, AlignJustifySelf, JustifyItems};
@ -282,30 +283,8 @@ pub fn parse_number_with_clamping_mode(context: &ParserContext,
}
}
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)]
pub struct BorderRadiusSize(pub Size2D<LengthOrPercentage>);
no_viewport_percentage!(BorderRadiusSize);
impl BorderRadiusSize {
#[allow(missing_docs)]
pub fn zero() -> BorderRadiusSize {
let zero = LengthOrPercentage::Length(NoCalcLength::zero());
BorderRadiusSize(Size2D::new(zero.clone(), zero))
}
#[allow(missing_docs)]
pub fn new(width: LengthOrPercentage, height: LengthOrPercentage) -> BorderRadiusSize {
BorderRadiusSize(Size2D::new(width, height))
}
#[allow(missing_docs)]
pub fn circle(radius: LengthOrPercentage) -> BorderRadiusSize {
BorderRadiusSize(Size2D::new(radius.clone(), radius))
}
}
/// The specified value of `BorderRadiusSize`
pub type BorderRadiusSize = GenericBorderRadiusSize<LengthOrPercentage>;
impl Parse for BorderRadiusSize {
#[inline]
@ -313,15 +292,7 @@ impl Parse for BorderRadiusSize {
let first = try!(LengthOrPercentage::parse_non_negative(context, input));
let second = input.try(|i| LengthOrPercentage::parse_non_negative(context, i))
.unwrap_or_else(|()| first.clone());
Ok(BorderRadiusSize(Size2D::new(first, second)))
}
}
impl ToCss for BorderRadiusSize {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.0.width.to_css(dest));
try!(dest.write_str(" "));
self.0.height.to_css(dest)
Ok(GenericBorderRadiusSize(Size2D::new(first, second)))
}
}