Skip to content

Commit

Permalink
style: Implement style system support for Masonry layout.
Browse files Browse the repository at this point in the history
This implements support for this CSS Masonry layout proposal:
w3c/csswg-drafts#4650

I've intentionally left out a shorthand (place-tracks?) for now until
we have a draft CSS spec for this.

Differential Revision: https://phabricator.services.mozilla.com/D67061
  • Loading branch information
Mats Palmgren authored and emilio committed Jun 3, 2020
1 parent 21d48e0 commit 6f58c66
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 30 deletions.
1 change: 1 addition & 0 deletions components/style/properties/data.py
Expand Up @@ -356,6 +356,7 @@ def specified_is_copy(self):
"JustifyItems",
"JustifySelf",
"LineBreak",
"MasonryAutoFlow",
"MozForceBrokenImageIcon",
"MozListReversed",
"MozScriptLevel",
Expand Down
4 changes: 3 additions & 1 deletion components/style/properties/gecko.mako.rs
Expand Up @@ -802,7 +802,8 @@ fn static_assert() {

<% skip_position_longhands = " ".join(x.ident for x in SIDES) %>
<%self:impl_trait style_struct_name="Position"
skip_longhands="${skip_position_longhands}">
skip_longhands="${skip_position_longhands}
masonry-auto-flow">
% for side in SIDES:
<% impl_split_style_coord(side.ident, "mOffset", side.index) %>
% endfor
Expand All @@ -811,6 +812,7 @@ fn static_assert() {
self.gecko.mJustifyItems.computed = v;
}

${impl_simple_type_with_conversion("masonry_auto_flow", "mMasonryAutoFlow")}
</%self:impl_trait>

<% skip_outline_longhands = " ".join("outline-style outline-width".split() +
Expand Down
32 changes: 32 additions & 0 deletions components/style/properties/longhands/position.mako.rs
Expand Up @@ -114,6 +114,17 @@ ${helpers.single_keyword(
animation_value_type="discrete",
servo_restyle_damage="reflow",
)}

${helpers.predefined_type(
"justify-tracks",
"JustifyTracks",
"specified::JustifyTracks::default()",
engines="gecko",
gecko_pref="layout.css.grid-template-masonry-value.enabled",
animation_value_type="discrete",
servo_restyle_damage="reflow",
spec="https://github.com/w3c/csswg-drafts/issues/4650",
)}
% endif

% if engine in ["servo-2013", "servo-2020"]:
Expand Down Expand Up @@ -151,6 +162,17 @@ ${helpers.single_keyword(
servo_restyle_damage="reflow",
)}

${helpers.predefined_type(
"align-tracks",
"AlignTracks",
"specified::AlignTracks::default()",
engines="gecko",
gecko_pref="layout.css.grid-template-masonry-value.enabled",
animation_value_type="discrete",
servo_restyle_damage="reflow",
spec="https://github.com/w3c/csswg-drafts/issues/4650",
)}

${helpers.predefined_type(
"align-items",
"AlignItems",
Expand Down Expand Up @@ -372,6 +394,16 @@ ${helpers.predefined_type(

% endfor

${helpers.predefined_type(
"masonry-auto-flow",
"MasonryAutoFlow",
"computed::MasonryAutoFlow::initial()",
engines="gecko",
gecko_pref="layout.css.grid-template-masonry-value.enabled",
animation_value_type="discrete",
spec="https://github.com/w3c/csswg-drafts/issues/4650",
)}

${helpers.predefined_type(
"grid-auto-flow",
"GridAutoFlow",
Expand Down
2 changes: 1 addition & 1 deletion components/style/values/computed/align.rs
Expand Up @@ -10,7 +10,7 @@ use crate::values::computed::{Context, ToComputedValue};
use crate::values::specified;

pub use super::specified::{
AlignContent, AlignItems, ContentDistribution, JustifyContent, SelfAlignment,
AlignContent, AlignItems, AlignTracks, ContentDistribution, JustifyContent, JustifyTracks, SelfAlignment,
};
pub use super::specified::{AlignSelf, JustifySelf};

Expand Down
4 changes: 2 additions & 2 deletions components/style/values/computed/mod.rs
Expand Up @@ -29,7 +29,7 @@ use std::cmp;
use std::f32;

#[cfg(feature = "gecko")]
pub use self::align::{AlignContent, AlignItems, JustifyContent, JustifyItems, SelfAlignment};
pub use self::align::{AlignContent, AlignItems, AlignTracks, JustifyContent, JustifyItems, JustifyTracks, SelfAlignment};
#[cfg(feature = "gecko")]
pub use self::align::{AlignSelf, JustifySelf};
pub use self::angle::Angle;
Expand Down Expand Up @@ -68,7 +68,7 @@ pub use self::list::Quotes;
pub use self::motion::{OffsetPath, OffsetRotate};
pub use self::outline::OutlineStyle;
pub use self::percentage::{NonNegativePercentage, Percentage};
pub use self::position::{GridAutoFlow, GridTemplateAreas, Position, PositionOrAuto, ZIndex};
pub use self::position::{GridAutoFlow, GridTemplateAreas, MasonryAutoFlow, Position, PositionOrAuto, ZIndex};
pub use self::rect::NonNegativeLengthOrNumberRect;
pub use self::resolution::Resolution;
pub use self::svg::MozContextProperties;
Expand Down
2 changes: 1 addition & 1 deletion components/style/values/computed/position.rs
Expand Up @@ -12,7 +12,7 @@ use crate::values::generics::position::Position as GenericPosition;
use crate::values::generics::position::PositionComponent as GenericPositionComponent;
use crate::values::generics::position::PositionOrAuto as GenericPositionOrAuto;
use crate::values::generics::position::ZIndex as GenericZIndex;
pub use crate::values::specified::position::{GridAutoFlow, GridTemplateAreas};
pub use crate::values::specified::position::{GridAutoFlow, GridTemplateAreas, MasonryAutoFlow};
use crate::Zero;
use std::fmt::{self, Write};
use style_traits::{CssWriter, ToCss};
Expand Down
3 changes: 3 additions & 0 deletions components/style/values/generics/grid.rs
Expand Up @@ -786,6 +786,9 @@ pub enum GenericGridTemplateComponent<L, I> {
/// TODO: Support animations for this after subgrid is addressed in [grid-2] spec.
#[animation(error)]
Subgrid(Box<LineNameList>),
/// `masonry` value.
/// https://github.com/w3c/csswg-drafts/issues/4650
Masonry,
}

pub use self::GenericGridTemplateComponent as GridTemplateComponent;
Expand Down
87 changes: 66 additions & 21 deletions components/style/values/specified/align.rs
Expand Up @@ -171,16 +171,6 @@ impl ContentDistribution {
Self { primary }
}

fn from_bits(bits: u16) -> Self {
Self {
primary: AlignFlags::from_bits_truncate(bits as u8),
}
}

fn as_bits(&self) -> u16 {
self.primary.bits() as u16
}

/// Returns whether this value is a <baseline-position>.
pub fn is_baseline_position(&self) -> bool {
matches!(
Expand Down Expand Up @@ -292,6 +282,41 @@ impl SpecifiedValueInfo for AlignContent {
}
}

/// Value for the `align-tracks` property.
///
/// <https://github.com/w3c/csswg-drafts/issues/4650>
#[derive(
Clone,
Debug,
Default,
Eq,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[repr(transparent)]
#[css(comma)]
pub struct AlignTracks(
#[css(iterable, if_empty = "normal")]
pub crate::OwnedSlice<AlignContent>
);

impl Parse for AlignTracks {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let values = input.parse_comma_separated(|input| {
AlignContent::parse(context, input)
})?;
Ok(AlignTracks(values.into()))
}
}

/// Value for the `justify-content` property.
///
/// <https://drafts.csswg.org/css-align/#propdef-justify-content>
Expand Down Expand Up @@ -329,18 +354,38 @@ impl SpecifiedValueInfo for JustifyContent {
ContentDistribution::list_keywords(f, AxisDirection::Inline);
}
}
/// Value for the `justify-tracks` property.
///
/// <https://github.com/w3c/csswg-drafts/issues/4650>
#[derive(
Clone,
Debug,
Default,
Eq,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[repr(transparent)]
#[css(comma)]
pub struct JustifyTracks(
#[css(iterable, if_empty = "normal")]
pub crate::OwnedSlice<JustifyContent>
);

#[cfg(feature = "gecko")]
impl From<u16> for JustifyContent {
fn from(bits: u16) -> Self {
JustifyContent(ContentDistribution::from_bits(bits))
}
}

#[cfg(feature = "gecko")]
impl From<JustifyContent> for u16 {
fn from(v: JustifyContent) -> u16 {
v.0.as_bits()
impl Parse for JustifyTracks {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let values = input.parse_comma_separated(|input| {
JustifyContent::parse(context, input)
})?;
Ok(JustifyTracks(values.into()))
}
}

Expand Down
18 changes: 17 additions & 1 deletion components/style/values/specified/grid.rs
Expand Up @@ -295,6 +295,18 @@ fn allow_grid_template_subgrids() -> bool {
false
}

#[cfg(feature = "gecko")]
#[inline]
fn allow_grid_template_masonry() -> bool {
static_prefs::pref!("layout.css.grid-template-masonry-value.enabled")
}

#[cfg(feature = "servo")]
#[inline]
fn allow_grid_template_masonry() -> bool {
false
}

impl Parse for GridTemplateComponent<LengthPercentage, Integer> {
fn parse<'i, 't>(
context: &ParserContext,
Expand All @@ -319,7 +331,11 @@ impl GridTemplateComponent<LengthPercentage, Integer> {
return Ok(GridTemplateComponent::Subgrid(Box::new(t)));
}
}

if allow_grid_template_masonry() {
if input.try(|i| i.expect_ident_matching("masonry")).is_ok() {
return Ok(GridTemplateComponent::Masonry);
}
}
let track_list = TrackList::parse(context, input)?;
Ok(GridTemplateComponent::TrackList(Box::new(track_list)))
}
Expand Down
6 changes: 3 additions & 3 deletions components/style/values/specified/mod.rs
Expand Up @@ -28,9 +28,9 @@ use style_traits::values::specified::AllowedNumericType;
use style_traits::{CssWriter, ParseError, SpecifiedValueInfo, StyleParseErrorKind, ToCss};

#[cfg(feature = "gecko")]
pub use self::align::{AlignContent, AlignItems, AlignSelf, ContentDistribution};
pub use self::align::{AlignContent, AlignItems, AlignSelf, AlignTracks, ContentDistribution};
#[cfg(feature = "gecko")]
pub use self::align::{JustifyContent, JustifyItems, JustifySelf, SelfAlignment};
pub use self::align::{JustifyContent, JustifyItems, JustifySelf, JustifyTracks, SelfAlignment};
pub use self::angle::{AllowUnitlessZeroAngle, Angle};
pub use self::background::{BackgroundRepeat, BackgroundSize};
pub use self::basic_shape::FillRule;
Expand Down Expand Up @@ -72,7 +72,7 @@ pub use self::list::Quotes;
pub use self::motion::{OffsetPath, OffsetRotate};
pub use self::outline::OutlineStyle;
pub use self::percentage::Percentage;
pub use self::position::{GridAutoFlow, GridTemplateAreas, Position, PositionOrAuto};
pub use self::position::{GridAutoFlow, GridTemplateAreas, MasonryAutoFlow, Position, PositionOrAuto};
pub use self::position::{PositionComponent, ZIndex};
pub use self::rect::NonNegativeLengthOrNumberRect;
pub use self::resolution::Resolution;
Expand Down

0 comments on commit 6f58c66

Please sign in to comment.