Skip to content

Commit

Permalink
style: Use cbindgen for css-align types.
Browse files Browse the repository at this point in the history
This provides stronger typing and removes a bunch of subtle constants matching.

Differential Revision: https://phabricator.services.mozilla.com/D61058
  • Loading branch information
emilio committed Feb 12, 2020
1 parent 41ae92e commit 2bdcd76
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 100 deletions.
38 changes: 3 additions & 35 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -957,48 +957,16 @@ 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}
align-content justify-content align-self
justify-self align-items justify-items
grid-auto-flow">
skip_longhands="${skip_position_longhands} grid-auto-flow">
% for side in SIDES:
<% impl_split_style_coord(side.ident, "mOffset", side.index) %>
% endfor

% for kind in ["align", "justify"]:
${impl_simple_type_with_conversion(kind + "_content")}
${impl_simple_type_with_conversion(kind + "_self")}
% endfor
${impl_simple_type_with_conversion("align_items")}

pub fn set_justify_items(&mut self, v: longhands::justify_items::computed_value::T) {
self.gecko.mSpecifiedJustifyItems = v.specified.into();
self.set_computed_justify_items(v.computed);
}

${impl_simple_type_with_conversion("grid_auto_flow")}
pub fn set_computed_justify_items(&mut self, v: values::specified::JustifyItems) {
debug_assert_ne!(v.0, crate::values::specified::align::AlignFlags::LEGACY);
self.gecko.mJustifyItems = v.into();
}

pub fn reset_justify_items(&mut self, reset_style: &Self) {
self.gecko.mJustifyItems = reset_style.gecko.mJustifyItems;
self.gecko.mSpecifiedJustifyItems = reset_style.gecko.mSpecifiedJustifyItems;
}

pub fn copy_justify_items_from(&mut self, other: &Self) {
self.gecko.mJustifyItems = other.gecko.mJustifyItems;
self.gecko.mSpecifiedJustifyItems = other.gecko.mJustifyItems;
self.gecko.mJustifyItems.computed = v;
}

pub fn clone_justify_items(&self) -> longhands::justify_items::computed_value::T {
longhands::justify_items::computed_value::T {
computed: self.gecko.mJustifyItems.into(),
specified: self.gecko.mSpecifiedJustifyItems.into(),
}
}

${impl_simple_type_with_conversion("grid_auto_flow")}
</%self:impl_trait>

<% skip_outline_longhands = " ".join("outline-style outline-width".split() +
Expand Down
7 changes: 5 additions & 2 deletions components/style/values/computed/align.rs
Expand Up @@ -9,7 +9,7 @@
use crate::values::computed::{Context, ToComputedValue};
use crate::values::specified;

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

/// The computed value for the `justify-items` property.
Expand All @@ -34,7 +34,8 @@ pub use super::specified::{AlignSelf, JustifySelf};
///
/// See the discussion in https://bugzil.la/1384542.
#[derive(Clone, Copy, Debug, Eq, PartialEq, ToCss, ToResolvedValue)]
pub struct JustifyItems {
#[repr(C)]
pub struct ComputedJustifyItems {
/// The specified value for the property. Can contain the bare `legacy`
/// keyword.
#[css(skip)]
Expand All @@ -45,6 +46,8 @@ pub struct JustifyItems {
pub computed: specified::JustifyItems,
}

pub use self::ComputedJustifyItems as JustifyItems;

impl JustifyItems {
/// Returns the `legacy` value.
pub fn legacy() -> Self {
Expand Down
94 changes: 31 additions & 63 deletions components/style/values/specified/align.rs
Expand Up @@ -6,64 +6,62 @@
//!
//! https://drafts.csswg.org/css-align/

use crate::gecko_bindings::structs;
use crate::parser::{Parse, ParserContext};
use cssparser::Parser;
use std::fmt::{self, Write};
use style_traits::{CssWriter, KeywordsCollectFn, ParseError, SpecifiedValueInfo, ToCss};

bitflags! {
/// Constants shared by multiple CSS Box Alignment properties
///
/// These constants match Gecko's `NS_STYLE_ALIGN_*` constants.
#[derive(MallocSizeOf, ToComputedValue, ToResolvedValue, ToShmem)]
#[repr(C)]
pub struct AlignFlags: u8 {
// Enumeration stored in the lower 5 bits:
/// 'auto'
const AUTO = structs::NS_STYLE_ALIGN_AUTO as u8;
/// {align,justify}-{content,items,self}: 'auto'
const AUTO = 0;
/// 'normal'
const NORMAL = structs::NS_STYLE_ALIGN_NORMAL as u8;
const NORMAL = 1;
/// 'start'
const START = structs::NS_STYLE_ALIGN_START as u8;
const START = 2;
/// 'end'
const END = structs::NS_STYLE_ALIGN_END as u8;
const END = 3;
/// 'flex-start'
const FLEX_START = structs::NS_STYLE_ALIGN_FLEX_START as u8;
const FLEX_START = 4;
/// 'flex-end'
const FLEX_END = structs::NS_STYLE_ALIGN_FLEX_END as u8;
const FLEX_END = 5;
/// 'center'
const CENTER = structs::NS_STYLE_ALIGN_CENTER as u8;
const CENTER = 6;
/// 'left'
const LEFT = structs::NS_STYLE_ALIGN_LEFT as u8;
const LEFT = 7;
/// 'right'
const RIGHT = structs::NS_STYLE_ALIGN_RIGHT as u8;
const RIGHT = 8;
/// 'baseline'
const BASELINE = structs::NS_STYLE_ALIGN_BASELINE as u8;
const BASELINE = 9;
/// 'last-baseline'
const LAST_BASELINE = structs::NS_STYLE_ALIGN_LAST_BASELINE as u8;
const LAST_BASELINE = 10;
/// 'stretch'
const STRETCH = structs::NS_STYLE_ALIGN_STRETCH as u8;
const STRETCH = 11;
/// 'self-start'
const SELF_START = structs::NS_STYLE_ALIGN_SELF_START as u8;
const SELF_START = 12;
/// 'self-end'
const SELF_END = structs::NS_STYLE_ALIGN_SELF_END as u8;
const SELF_END = 13;
/// 'space-between'
const SPACE_BETWEEN = structs::NS_STYLE_ALIGN_SPACE_BETWEEN as u8;
const SPACE_BETWEEN = 14;
/// 'space-around'
const SPACE_AROUND = structs::NS_STYLE_ALIGN_SPACE_AROUND as u8;
const SPACE_AROUND = 15;
/// 'space-evenly'
const SPACE_EVENLY = structs::NS_STYLE_ALIGN_SPACE_EVENLY as u8;
const SPACE_EVENLY = 16;

// Additional flags stored in the upper bits:
/// 'legacy' (mutually exclusive w. SAFE & UNSAFE)
const LEGACY = structs::NS_STYLE_ALIGN_LEGACY as u8;
const LEGACY = 1 << 5;
/// 'safe'
const SAFE = structs::NS_STYLE_ALIGN_SAFE as u8;
const SAFE = 1 << 6;
/// 'unsafe' (mutually exclusive w. SAFE)
const UNSAFE = structs::NS_STYLE_ALIGN_UNSAFE as u8;
const UNSAFE = 1 << 7;

/// Mask for the additional flags above.
const FLAG_BITS = structs::NS_STYLE_ALIGN_FLAG_BITS as u8;
const FLAG_BITS = 0b11100000;
}
}

Expand Down Expand Up @@ -146,6 +144,7 @@ pub enum AxisDirection {
ToResolvedValue,
ToShmem,
)]
#[repr(C)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
pub struct ContentDistribution {
primary: AlignFlags,
Expand Down Expand Up @@ -270,6 +269,7 @@ impl ContentDistribution {
ToResolvedValue,
ToShmem,
)]
#[repr(transparent)]
pub struct AlignContent(pub ContentDistribution);

impl Parse for AlignContent {
Expand All @@ -292,20 +292,6 @@ impl SpecifiedValueInfo for AlignContent {
}
}

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

#[cfg(feature = "gecko")]
impl From<AlignContent> for u16 {
fn from(v: AlignContent) -> u16 {
v.0.as_bits()
}
}

/// Value for the `justify-content` property.
///
/// <https://drafts.csswg.org/css-align/#propdef-justify-content>
Expand All @@ -321,6 +307,7 @@ impl From<AlignContent> for u16 {
ToResolvedValue,
ToShmem,
)]
#[repr(transparent)]
pub struct JustifyContent(pub ContentDistribution);

impl Parse for JustifyContent {
Expand Down Expand Up @@ -370,6 +357,7 @@ impl From<JustifyContent> for u16 {
ToResolvedValue,
ToShmem,
)]
#[repr(transparent)]
pub struct SelfAlignment(pub AlignFlags);

impl SelfAlignment {
Expand Down Expand Up @@ -441,6 +429,7 @@ impl SelfAlignment {
ToResolvedValue,
ToShmem,
)]
#[repr(C)]
pub struct AlignSelf(pub SelfAlignment);

impl Parse for AlignSelf {
Expand All @@ -463,18 +452,6 @@ impl SpecifiedValueInfo for AlignSelf {
}
}

impl From<u8> for AlignSelf {
fn from(bits: u8) -> Self {
AlignSelf(SelfAlignment(AlignFlags::from_bits_truncate(bits)))
}
}

impl From<AlignSelf> for u8 {
fn from(align: AlignSelf) -> u8 {
(align.0).0.bits()
}
}

/// The specified value of the justify-self property.
///
/// <https://drafts.csswg.org/css-align/#propdef-justify-self>
Expand All @@ -490,6 +467,7 @@ impl From<AlignSelf> for u8 {
ToResolvedValue,
ToShmem,
)]
#[repr(C)]
pub struct JustifySelf(pub SelfAlignment);

impl Parse for JustifySelf {
Expand All @@ -512,18 +490,6 @@ impl SpecifiedValueInfo for JustifySelf {
}
}

impl From<u8> for JustifySelf {
fn from(bits: u8) -> Self {
JustifySelf(SelfAlignment(AlignFlags::from_bits_truncate(bits)))
}
}

impl From<JustifySelf> for u8 {
fn from(justify: JustifySelf) -> u8 {
(justify.0).0.bits()
}
}

/// Value of the `align-items` property
///
/// <https://drafts.csswg.org/css-align/#propdef-align-items>
Expand All @@ -539,6 +505,7 @@ impl From<JustifySelf> for u8 {
ToResolvedValue,
ToShmem,
)]
#[repr(C)]
pub struct AlignItems(pub AlignFlags);

impl AlignItems {
Expand Down Expand Up @@ -590,6 +557,7 @@ impl SpecifiedValueInfo for AlignItems {
///
/// <https://drafts.csswg.org/css-align/#justify-items-property>
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, ToCss, ToShmem)]
#[repr(C)]
pub struct JustifyItems(pub AlignFlags);

impl JustifyItems {
Expand Down

0 comments on commit 2bdcd76

Please sign in to comment.