Skip to content

Commit

Permalink
style: Use rust lengths for row-gap / column-gap.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio committed Apr 12, 2019
1 parent 0f57b7b commit 0a84073
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 72 deletions.
61 changes: 1 addition & 60 deletions components/style/gecko/values.rs
Expand Up @@ -18,7 +18,7 @@ use crate::values::generics::gecko::ScrollSnapPoint;
use crate::values::generics::grid::{TrackBreadth, TrackKeyword};
use crate::values::generics::length::LengthPercentageOrAuto;
use crate::values::generics::{CounterStyleOrNone, NonNegative};
use crate::values::{Auto, Either, None_, Normal};
use crate::values::Either;
use crate::{Atom, Zero};
use app_units::Au;
use cssparser::RGBA;
Expand All @@ -44,23 +44,6 @@ impl nsStyleCoord {
}
}

impl<A: GeckoStyleCoordConvertible, B: GeckoStyleCoordConvertible> GeckoStyleCoordConvertible
for Either<A, B>
{
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match *self {
Either::First(ref v) => v.to_gecko_style_coord(coord),
Either::Second(ref v) => v.to_gecko_style_coord(coord),
}
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
A::from_gecko_style_coord(coord)
.map(Either::First)
.or_else(|| B::from_gecko_style_coord(coord).map(Either::Second))
}
}

impl<Inner> GeckoStyleCoordConvertible for NonNegative<Inner>
where
Inner: GeckoStyleCoordConvertible,
Expand Down Expand Up @@ -265,48 +248,6 @@ impl GeckoStyleCoordConvertible for Angle {
}
}

impl GeckoStyleCoordConvertible for Auto {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
coord.set_value(CoordDataValue::Auto)
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
if let CoordDataValue::Auto = coord.as_value() {
Some(Auto)
} else {
None
}
}
}

impl GeckoStyleCoordConvertible for None_ {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
coord.set_value(CoordDataValue::None)
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
if let CoordDataValue::None = coord.as_value() {
Some(None_)
} else {
None
}
}
}

impl GeckoStyleCoordConvertible for Normal {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
coord.set_value(CoordDataValue::Normal)
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
if let CoordDataValue::Normal = coord.as_value() {
Some(Normal)
} else {
None
}
}
}

impl GeckoStyleCoordConvertible for ScrollSnapPoint<LengthPercentage> {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match self.repeated() {
Expand Down
1 change: 0 additions & 1 deletion components/style/properties/gecko.mako.rs
Expand Up @@ -1190,7 +1190,6 @@ impl Clone for ${style_struct.gecko_struct_name} {

# Types used with predefined_type()-defined properties that we can auto-generate.
predefined_types = {
"length::NonNegativeLengthPercentageOrNormal": impl_style_coord,
"MozScriptMinSize": impl_absolute_length,
"SVGLength": impl_svg_length,
"SVGOpacity": impl_svg_opacity,
Expand Down
4 changes: 2 additions & 2 deletions components/style/properties/helpers.mako.rs
Expand Up @@ -105,7 +105,7 @@
#[allow(unused_imports)]
use crate::values::{computed, specified};
#[allow(unused_imports)]
use crate::values::{Auto, Either, None_, Normal};
use crate::values::{Auto, Either, None_};
${caller.body()}
}

Expand Down Expand Up @@ -292,7 +292,7 @@
#[allow(unused_imports)]
use crate::properties::{UnparsedValue, ShorthandId};
#[allow(unused_imports)]
use crate::values::{Auto, Either, None_, Normal};
use crate::values::{Auto, Either, None_};
#[allow(unused_imports)]
use crate::error_reporting::ParseErrorReporter;
#[allow(unused_imports)]
Expand Down
4 changes: 2 additions & 2 deletions components/style/properties/longhands/position.mako.rs
Expand Up @@ -372,7 +372,7 @@ ${helpers.predefined_type(
${helpers.predefined_type(
"column-gap",
"length::NonNegativeLengthPercentageOrNormal",
"Either::Second(Normal)",
"computed::length::NonNegativeLengthPercentageOrNormal::normal()",
alias="grid-column-gap" if product == "gecko" else "",
extra_prefixes="moz",
servo_pref="layout.columns.enabled",
Expand All @@ -385,7 +385,7 @@ ${helpers.predefined_type(
${helpers.predefined_type(
"row-gap",
"length::NonNegativeLengthPercentageOrNormal",
"Either::Second(Normal)",
"computed::length::NonNegativeLengthPercentageOrNormal::normal()",
alias="grid-row-gap",
products="gecko",
spec="https://drafts.csswg.org/css-align-3/#propdef-row-gap",
Expand Down
7 changes: 4 additions & 3 deletions components/style/values/computed/length.rs
Expand Up @@ -10,12 +10,13 @@ use crate::values::computed::NonNegativeNumber;
use crate::values::distance::{ComputeSquaredDistance, SquaredDistance};
use crate::values::generics::length as generics;
use crate::values::generics::length::{
GenericLengthOrNumber, MaxSize as GenericMaxSize, Size as GenericSize,
GenericLengthPercentageOrNormal, GenericLengthOrNumber,
GenericMaxSize, GenericSize,
};
use crate::values::generics::NonNegative;
use crate::values::specified::length::ViewportPercentageLength;
use crate::values::specified::length::{AbsoluteLength, FontBaseSize, FontRelativeLength};
use crate::values::{specified, CSSFloat, Either, Normal};
use crate::values::{specified, CSSFloat};
use crate::Zero;
use app_units::Au;
use ordered_float::NotNan;
Expand Down Expand Up @@ -782,7 +783,7 @@ impl From<NonNegativeLength> for Au {
}

/// Either a computed NonNegativeLengthPercentage or the `normal` keyword.
pub type NonNegativeLengthPercentageOrNormal = Either<NonNegativeLengthPercentage, Normal>;
pub type NonNegativeLengthPercentageOrNormal = GenericLengthPercentageOrNormal<NonNegativeLengthPercentage>;

/// Either a non-negative `<length>` or a `<number>`.
pub type NonNegativeLengthOrNumber = GenericLengthOrNumber<NonNegativeLength, NonNegativeNumber>;
Expand Down
35 changes: 35 additions & 0 deletions components/style/values/generics/length.rs
Expand Up @@ -219,3 +219,38 @@ impl<L, N: Zero> Zero for LengthOrNumber<L, N> {
}
}
}

/// A generic `<length-percentage>` | normal` value.
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
Parse,
PartialEq,
SpecifiedValueInfo,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[repr(C, u8)]
#[allow(missing_docs)]
pub enum GenericLengthPercentageOrNormal<LengthPercent> {
LengthPercentage(LengthPercent),
Normal,
}

pub use self::GenericLengthPercentageOrNormal as LengthPercentageOrNormal;

impl<LengthPercent> LengthPercentageOrNormal<LengthPercent> {
/// Returns the normal value.
#[inline]
pub fn normal() -> Self {
LengthPercentageOrNormal::Normal
}
}
1 change: 0 additions & 1 deletion components/style/values/mod.rs
Expand Up @@ -39,7 +39,6 @@ pub type CSSInteger = i32;

define_keyword_type!(None_, "none");
define_keyword_type!(Auto, "auto");
define_keyword_type!(Normal, "normal");

/// Serialize an identifier which is represented as an atom.
#[cfg(feature = "gecko")]
Expand Down
7 changes: 4 additions & 3 deletions components/style/values/specified/length.rs
Expand Up @@ -13,12 +13,13 @@ use crate::properties::computed_value_flags::ComputedValueFlags;
use crate::values::computed::{self, CSSPixelLength, Context};
use crate::values::generics::length as generics;
use crate::values::generics::length::{
GenericLengthOrNumber, MaxSize as GenericMaxSize, Size as GenericSize,
GenericLengthPercentageOrNormal, GenericLengthOrNumber,
GenericMaxSize, GenericSize,
};
use crate::values::generics::NonNegative;
use crate::values::specified::calc::CalcNode;
use crate::values::specified::NonNegativeNumber;
use crate::values::{CSSFloat, Either, Normal};
use crate::values::CSSFloat;
use crate::Zero;
use app_units::Au;
use cssparser::{Parser, Token};
Expand Down Expand Up @@ -957,7 +958,7 @@ impl NonNegativeLengthPercentageOrAuto {
pub type NonNegativeLengthPercentage = NonNegative<LengthPercentage>;

/// Either a NonNegativeLengthPercentage or the `normal` keyword.
pub type NonNegativeLengthPercentageOrNormal = Either<NonNegativeLengthPercentage, Normal>;
pub type NonNegativeLengthPercentageOrNormal = GenericLengthPercentageOrNormal<NonNegativeLengthPercentage>;

impl From<NoCalcLength> for NonNegativeLengthPercentage {
#[inline]
Expand Down

0 comments on commit 0a84073

Please sign in to comment.