Skip to content

Commit

Permalink
stylo: support background-size
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Sep 4, 2016
1 parent f787e8e commit 04c5d05
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
25 changes: 24 additions & 1 deletion components/style/gecko_conversions.rs
Expand Up @@ -14,7 +14,7 @@ use gecko_bindings::structs::nsStyleCoord_CalcValue;
use gecko_bindings::sugar::refptr::HasArcFFI;
use properties::ComputedValues;
use stylesheets::Stylesheet;
use values::computed::{CalcLengthOrPercentage, LengthOrPercentage};
use values::computed::{CalcLengthOrPercentage, LengthOrPercentage, LengthOrPercentageOrAuto};

unsafe impl HasArcFFI for Stylesheet {
type FFIType = RawServoStyleSheet;
Expand Down Expand Up @@ -70,6 +70,29 @@ impl From<LengthOrPercentage> for nsStyleCoord_CalcValue {
}
}

impl LengthOrPercentageOrAuto {
pub fn to_calc_value(&self) -> Option<nsStyleCoord_CalcValue> {
match *self {
LengthOrPercentageOrAuto::Length(au) => {
Some(nsStyleCoord_CalcValue {
mLength: au.0,
mPercent: 0.0,
mHasPercent: false,
})
},
LengthOrPercentageOrAuto::Percentage(pc) => {
Some(nsStyleCoord_CalcValue {
mLength: 0,
mPercent: pc,
mHasPercent: true,
})
},
LengthOrPercentageOrAuto::Calc(calc) => Some(calc.into()),
LengthOrPercentageOrAuto::Auto => None,
}
}
}

impl From<nsStyleCoord_CalcValue> for LengthOrPercentage {
fn from(other: nsStyleCoord_CalcValue) -> LengthOrPercentage {
match (other.mHasPercent, other.mLength) {
Expand Down
74 changes: 73 additions & 1 deletion components/style/properties/gecko.mako.rs
Expand Up @@ -969,7 +969,7 @@ fn static_assert() {
<% skip_background_longhands = """background-color background-repeat
background-image background-clip
background-origin background-attachment
background-position""" %>
background-size background-position""" %>
<%self:impl_trait style_struct_name="Background"
skip_longhands="${skip_background_longhands}"
skip_additionals="*">
Expand Down Expand Up @@ -1028,6 +1028,78 @@ fn static_assert() {
}
</%self:simple_background_array_property>

<%self:simple_background_array_property name="size" field_name="mSize">
use gecko_bindings::structs::nsStyleImageLayers_Size_Dimension;
use gecko_bindings::structs::nsStyleImageLayers_Size_DimensionType;
use gecko_bindings::structs::{nsStyleCoord_CalcValue, nsStyleImageLayers_Size};
use properties::longhands::background_size::single_value::computed_value::T;
use values::computed::LengthOrPercentageOrAuto;

let mut width = nsStyleCoord_CalcValue::new();
let mut height = nsStyleCoord_CalcValue::new();

let (w_type, h_type) = match servo {
T::Explicit(size) => {
let mut w_type = nsStyleImageLayers_Size_DimensionType::eAuto;
let mut h_type = nsStyleImageLayers_Size_DimensionType::eAuto;
if let Some(w) = size.width.to_calc_value() {
width = w;
w_type = nsStyleImageLayers_Size_DimensionType::eLengthPercentage;
}
if let Some(h) = size.height.to_calc_value() {
height = h;
h_type = nsStyleImageLayers_Size_DimensionType::eLengthPercentage;
}
(w_type, h_type)
}
T::Cover => (nsStyleImageLayers_Size_DimensionType::eCover,
nsStyleImageLayers_Size_DimensionType::eCover),
T::Contain => (nsStyleImageLayers_Size_DimensionType::eContain,
nsStyleImageLayers_Size_DimensionType::eContain),
};

nsStyleImageLayers_Size {
mWidth: nsStyleImageLayers_Size_Dimension { _base: width },
mHeight: nsStyleImageLayers_Size_Dimension { _base: height },
mWidthType: w_type as u8,
mHeightType: h_type as u8,
}
</%self:simple_background_array_property>

pub fn clone_background_size(&self) -> longhands::background_size::computed_value::T {
use gecko_bindings::structs::nsStyleCoord_CalcValue as CalcValue;
use gecko_bindings::structs::nsStyleImageLayers_Size_DimensionType as DimensionType;
use properties::longhands::background_size::single_value::computed_value::{ExplicitSize, T};
use values::computed::LengthOrPercentageOrAuto;

fn to_servo(value: CalcValue, ty: u8) -> LengthOrPercentageOrAuto {
if ty == DimensionType::eAuto as u8 {
LengthOrPercentageOrAuto::Auto
} else {
debug_assert!(ty == DimensionType::eLengthPercentage as u8);
LengthOrPercentageOrAuto::Calc(value.into())
}
}

longhands::background_size::computed_value::T(
self.gecko.mImage.mLayers.iter().map(|ref layer| {
if DimensionType::eCover as u8 == layer.mSize.mWidthType {
debug_assert!(layer.mSize.mHeightType == DimensionType::eCover as u8);
return T::Cover
}
if DimensionType::eContain as u8 == layer.mSize.mWidthType {
debug_assert!(layer.mSize.mHeightType == DimensionType::eContain as u8);
return T::Contain
}

T::Explicit(ExplicitSize {
width: to_servo(layer.mSize.mWidth._base, layer.mSize.mWidthType),
height: to_servo(layer.mSize.mHeight._base, layer.mSize.mHeightType),
})
}).collect()
)
}

pub fn copy_background_position_from(&mut self, other: &Self) {
self.gecko.mImage.mPositionXCount = cmp::min(1, other.gecko.mImage.mPositionXCount);
self.gecko.mImage.mPositionYCount = cmp::min(1, other.gecko.mImage.mPositionYCount);
Expand Down
10 changes: 10 additions & 0 deletions ports/geckolib/gecko_bindings/sugar/ns_style_coord.rs
Expand Up @@ -29,6 +29,16 @@ impl CoordDataMut for nsStyleCoord {
}
}

impl nsStyleCoord_CalcValue {
pub fn new() -> Self {
nsStyleCoord_CalcValue {
mLength: 0,
mPercent: 0.0,
mHasPercent: false,
}
}
}

impl nsStyleSides {
#[inline]
pub fn data_at(&self, index: usize) -> SidesData {
Expand Down

0 comments on commit 04c5d05

Please sign in to comment.