style: Use cbindgen for grid track sizing.

Differential Revision: https://phabricator.services.mozilla.com/D36118
This commit is contained in:
Emilio Cobos Álvarez 2019-06-28 09:46:02 +00:00
parent 034557a717
commit cc15afa348
No known key found for this signature in database
GPG key ID: E1152D0994E4BF8A
6 changed files with 75 additions and 257 deletions

View file

@ -7,8 +7,8 @@
use self::transform::DirectionVector;
use super::animated::ToAnimatedValue;
use super::generics::grid::GridTemplateComponent as GenericGridTemplateComponent;
use super::generics::grid::{GenericGridLine, TrackBreadth as GenericTrackBreadth};
use super::generics::grid::{TrackList as GenericTrackList, TrackSize as GenericTrackSize};
use super::generics::grid::{GenericGridLine, GenericTrackBreadth};
use super::generics::grid::{TrackList as GenericTrackList, GenericTrackSize};
use super::generics::transform::IsParallelTo;
use super::generics::{self, GreaterThanOrEqualToOne, NonNegative, ZeroToOne};
use super::specified;

View file

@ -7,7 +7,6 @@
use crate::{Atom, Zero};
use crate::parser::{Parse, ParserContext};
use crate::values::computed::{Context, ToComputedValue};
use crate::values::specified;
use crate::values::specified::grid::parse_line_names;
use crate::values::{CSSFloat, CustomIdent};
@ -180,33 +179,12 @@ impl Parse for GridLine<specified::Integer> {
}
}
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(
Animate,
Clone,
Copy,
Debug,
Eq,
MallocSizeOf,
Parse,
PartialEq,
SpecifiedValueInfo,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
pub enum TrackKeyword {
Auto,
MaxContent,
MinContent,
}
/// A track breadth for explicit grid track sizing. It's generic solely to
/// avoid re-implementing it for the computed type.
///
/// <https://drafts.csswg.org/css-grid/#typedef-track-breadth>
///
/// cbindgen:derive-tagged-enum-copy-constructor=true
#[derive(
Animate,
Clone,
@ -219,16 +197,23 @@ pub enum TrackKeyword {
ToResolvedValue,
ToShmem,
)]
pub enum TrackBreadth<L> {
#[repr(C, u8)]
pub enum GenericTrackBreadth<L> {
/// The generic type is almost always a non-negative `<length-percentage>`
Breadth(L),
/// A flex fraction specified in `fr` units.
#[css(dimension)]
Fr(CSSFloat),
/// One of the track-sizing keywords (`auto`, `min-content`, `max-content`)
Keyword(TrackKeyword),
/// `auto`
Auto,
/// `min-content`
MinContent,
/// `max-content`
MaxContent,
}
pub use self::GenericTrackBreadth as TrackBreadth;
impl<L> TrackBreadth<L> {
/// Check whether this is a `<fixed-breadth>` (i.e., it only has `<length-percentage>`)
///
@ -243,23 +228,31 @@ impl<L> TrackBreadth<L> {
/// generic only to avoid code bloat. It only takes `<length-percentage>`
///
/// <https://drafts.csswg.org/css-grid/#typedef-track-size>
#[derive(Clone, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToResolvedValue, ToShmem)]
pub enum TrackSize<L> {
///
/// cbindgen:derive-tagged-enum-copy-constructor=true
#[derive(Clone, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToComputedValue, ToResolvedValue, ToShmem)]
#[repr(C, u8)]
pub enum GenericTrackSize<L> {
/// A flexible `<track-breadth>`
Breadth(TrackBreadth<L>),
Breadth(GenericTrackBreadth<L>),
/// A `minmax` function for a range over an inflexible `<track-breadth>`
/// and a flexible `<track-breadth>`
///
/// <https://drafts.csswg.org/css-grid/#valdef-grid-template-columns-minmax>
#[css(function)]
Minmax(TrackBreadth<L>, TrackBreadth<L>),
Minmax(GenericTrackBreadth<L>, GenericTrackBreadth<L>),
/// A `fit-content` function.
///
/// This stores a TrackBreadth<L> for convenience, but it can only be a
/// LengthPercentage.
///
/// <https://drafts.csswg.org/css-grid/#valdef-grid-template-columns-fit-content>
#[css(function)]
FitContent(L),
FitContent(GenericTrackBreadth<L>),
}
pub use self::GenericTrackSize as TrackSize;
impl<L> TrackSize<L> {
/// Check whether this is a `<fixed-size>`
///
@ -288,7 +281,7 @@ impl<L> TrackSize<L> {
impl<L> Default for TrackSize<L> {
fn default() -> Self {
TrackSize::Breadth(TrackBreadth::Keyword(TrackKeyword::Auto))
TrackSize::Breadth(TrackBreadth::Auto)
}
}
@ -309,7 +302,7 @@ impl<L: ToCss> ToCss for TrackSize<L> {
TrackSize::Minmax(ref min, ref max) => {
// According to gecko minmax(auto, <flex>) is equivalent to <flex>,
// and both are serialized as <flex>.
if let TrackBreadth::Keyword(TrackKeyword::Auto) = *min {
if let TrackBreadth::Auto = *min {
if let TrackBreadth::Fr(_) = *max {
return max.to_css(dest);
}
@ -330,48 +323,6 @@ impl<L: ToCss> ToCss for TrackSize<L> {
}
}
impl<L: ToComputedValue> ToComputedValue for TrackSize<L> {
type ComputedValue = TrackSize<L::ComputedValue>;
#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
match *self {
TrackSize::Breadth(TrackBreadth::Fr(ref f)) => {
// <flex> outside `minmax()` expands to `mimmax(auto, <flex>)`
// https://drafts.csswg.org/css-grid/#valdef-grid-template-columns-flex
// FIXME(nox): This sounds false, the spec just says that <flex>
// implies `minmax(auto, <flex>)`, not that it should be changed
// into `minmax` at computed value time.
TrackSize::Minmax(
TrackBreadth::Keyword(TrackKeyword::Auto),
TrackBreadth::Fr(f.to_computed_value(context)),
)
},
TrackSize::Breadth(ref b) => TrackSize::Breadth(b.to_computed_value(context)),
TrackSize::Minmax(ref b1, ref b2) => {
TrackSize::Minmax(b1.to_computed_value(context), b2.to_computed_value(context))
},
TrackSize::FitContent(ref lp) => TrackSize::FitContent(lp.to_computed_value(context)),
}
}
#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
match *computed {
TrackSize::Breadth(ref b) => {
TrackSize::Breadth(ToComputedValue::from_computed_value(b))
},
TrackSize::Minmax(ref b1, ref b2) => TrackSize::Minmax(
ToComputedValue::from_computed_value(b1),
ToComputedValue::from_computed_value(b2),
),
TrackSize::FitContent(ref lp) => {
TrackSize::FitContent(ToComputedValue::from_computed_value(lp))
},
}
}
}
/// Helper function for serializing identifiers with a prefix and suffix, used
/// for serializing <line-names> (in grid).
pub fn concat_serialize_idents<W>(

View file

@ -8,7 +8,7 @@
use crate::parser::{Parse, ParserContext};
use crate::values::computed::{self, Context, ToComputedValue};
use crate::values::generics::grid::{GridTemplateComponent, RepeatCount, TrackBreadth};
use crate::values::generics::grid::{LineNameList, TrackKeyword, TrackRepeat, TrackSize};
use crate::values::generics::grid::{LineNameList, TrackRepeat, TrackSize};
use crate::values::generics::grid::{TrackList, TrackListType, TrackListValue};
use crate::values::specified::{Integer, LengthPercentage};
use crate::values::{CSSFloat, CustomIdent};
@ -27,11 +27,34 @@ pub fn parse_flex<'i, 't>(input: &mut Parser<'i, 't>) -> Result<CSSFloat, ParseE
}
}
impl<L> TrackBreadth<L> {
fn parse_keyword<'i, 't>(
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
#[derive(Parse)]
enum TrackKeyword {
Auto,
MaxContent,
MinContent,
}
Ok(match TrackKeyword::parse(input)? {
TrackKeyword::Auto => TrackBreadth::Auto,
TrackKeyword::MaxContent => TrackBreadth::MaxContent,
TrackKeyword::MinContent => TrackBreadth::MinContent,
})
}
}
impl Parse for TrackBreadth<LengthPercentage> {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
// FIXME: This and other callers in this file should use
// NonNegativeLengthPercentage instead.
//
// Though it seems these cannot be animated so it's ~ok.
if let Ok(lp) = input.try(|i| LengthPercentage::parse_non_negative(context, i)) {
return Ok(TrackBreadth::Breadth(lp));
}
@ -40,7 +63,7 @@ impl Parse for TrackBreadth<LengthPercentage> {
return Ok(TrackBreadth::Fr(f));
}
TrackKeyword::parse(input).map(TrackBreadth::Keyword)
Self::parse_keyword(input)
}
}
@ -58,10 +81,7 @@ impl Parse for TrackSize<LengthPercentage> {
let inflexible_breadth =
match input.try(|i| LengthPercentage::parse_non_negative(context, i)) {
Ok(lp) => TrackBreadth::Breadth(lp),
Err(..) => {
let keyword = TrackKeyword::parse(input)?;
TrackBreadth::Keyword(keyword)
},
Err(..) => TrackBreadth::parse_keyword(input)?,
};
input.expect_comma()?;
@ -74,7 +94,7 @@ impl Parse for TrackSize<LengthPercentage> {
input.expect_function_matching("fit-content")?;
let lp = input.parse_nested_block(|i| LengthPercentage::parse_non_negative(context, i))?;
Ok(TrackSize::FitContent(lp))
Ok(TrackSize::FitContent(TrackBreadth::Breadth(lp)))
}
}