Skip to content

Commit

Permalink
Make most background- properties handle arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Aug 22, 2016
1 parent ce9640a commit 65a8a8d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 67 deletions.
121 changes: 59 additions & 62 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -937,6 +937,32 @@ fn static_assert() {

</%self:impl_trait>

<%def name="simple_background_array_property(name, field_name)">
pub fn copy_background_${name}_from(&mut self, other: &Self) {
unsafe {
Gecko_EnsureImageLayersLength(&mut self.gecko.mImage, other.gecko.mImage.mLayers.len());
}
for (layer, other) in self.gecko.mImage.mLayers.iter_mut()
.zip(other.gecko.mImage.mLayers.iter())
.take(other.gecko.mImage.${field_name}Count) {
layer.${field_name} = other.${field_name};
}
self.gecko.mImage.${field_name}Count = other.gecko.mImage.${field_name}Count;
}

pub fn set_background_${name}(&mut self, v: longhands::background_${name}::computed_value::T) {
unsafe {
Gecko_EnsureImageLayersLength(&mut self.gecko.mImage, v.0.len());
}

self.gecko.mImage.${field_name}Count = v.0.len() as u32;
for (servo, geckolayer) in v.0.into_iter().zip(self.gecko.mImage.mLayers.iter_mut()) {
geckolayer.${field_name} = {
${caller.body()}
};
}
}
</%def>
// TODO: Gecko accepts lists in most background-related properties. We just use
// the first element (which is the common case), but at some point we want to
// add support for parsing these lists in servo and pushing to nsTArray's.
Expand All @@ -950,86 +976,57 @@ fn static_assert() {

<% impl_color("background_color", "mBackgroundColor", need_clone=True) %>

pub fn copy_background_repeat_from(&mut self, other: &Self) {
self.gecko.mImage.mRepeatCount = cmp::min(1, other.gecko.mImage.mRepeatCount);
self.gecko.mImage.mLayers.mFirstElement.mRepeat =
other.gecko.mImage.mLayers.mFirstElement.mRepeat;
}

pub fn set_background_repeat(&mut self, v: longhands::background_repeat::computed_value::T) {
use properties::longhands::background_repeat::computed_value::T;
use gecko_bindings::structs::{NS_STYLE_IMAGELAYER_REPEAT_REPEAT, NS_STYLE_IMAGELAYER_REPEAT_NO_REPEAT};
<%self:simple_background_array_property name="repeat" field_name="mRepeat">
use properties::longhands::background_repeat::single_value::computed_value::T;
use gecko_bindings::structs::nsStyleImageLayers_Repeat;
let (repeat_x, repeat_y) = match v {
T::repeat_x => (NS_STYLE_IMAGELAYER_REPEAT_REPEAT,
NS_STYLE_IMAGELAYER_REPEAT_NO_REPEAT),
T::repeat_y => (NS_STYLE_IMAGELAYER_REPEAT_NO_REPEAT,
NS_STYLE_IMAGELAYER_REPEAT_REPEAT),
T::repeat => (NS_STYLE_IMAGELAYER_REPEAT_REPEAT,
NS_STYLE_IMAGELAYER_REPEAT_REPEAT),
T::no_repeat => (NS_STYLE_IMAGELAYER_REPEAT_NO_REPEAT,
NS_STYLE_IMAGELAYER_REPEAT_NO_REPEAT),
};
use gecko_bindings::structs::NS_STYLE_IMAGELAYER_REPEAT_REPEAT;
use gecko_bindings::structs::NS_STYLE_IMAGELAYER_REPEAT_NO_REPEAT;

self.gecko.mImage.mRepeatCount = 1;
self.gecko.mImage.mLayers.mFirstElement.mRepeat = nsStyleImageLayers_Repeat {
mXRepeat: repeat_x as u8,
mYRepeat: repeat_y as u8,
let (repeat_x, repeat_y) = match servo {
T::repeat_x => (NS_STYLE_IMAGELAYER_REPEAT_REPEAT,
NS_STYLE_IMAGELAYER_REPEAT_NO_REPEAT),
T::repeat_y => (NS_STYLE_IMAGELAYER_REPEAT_NO_REPEAT,
NS_STYLE_IMAGELAYER_REPEAT_REPEAT),
T::repeat => (NS_STYLE_IMAGELAYER_REPEAT_REPEAT,
NS_STYLE_IMAGELAYER_REPEAT_REPEAT),
T::no_repeat => (NS_STYLE_IMAGELAYER_REPEAT_NO_REPEAT,
NS_STYLE_IMAGELAYER_REPEAT_NO_REPEAT),
};
}

pub fn copy_background_clip_from(&mut self, other: &Self) {
self.gecko.mImage.mClipCount = cmp::min(1, other.gecko.mImage.mClipCount);
self.gecko.mImage.mLayers.mFirstElement.mClip =
other.gecko.mImage.mLayers.mFirstElement.mClip;
}
nsStyleImageLayers_Repeat {
mXRepeat: repeat_x as u8,
mYRepeat: repeat_y as u8,
}
</%self:simple_background_array_property>

pub fn set_background_clip(&mut self, v: longhands::background_clip::computed_value::T) {
use properties::longhands::background_clip::computed_value::T;
self.gecko.mImage.mClipCount = 1;
<%self:simple_background_array_property name="clip" field_name="mClip">
use properties::longhands::background_clip::single_value::computed_value::T;

// TODO: Gecko supports background-clip: text, but just on -webkit-
// prefixed properties.
self.gecko.mImage.mLayers.mFirstElement.mClip = match v {
match servo {
T::border_box => structs::NS_STYLE_IMAGELAYER_CLIP_BORDER as u8,
T::padding_box => structs::NS_STYLE_IMAGELAYER_CLIP_PADDING as u8,
T::content_box => structs::NS_STYLE_IMAGELAYER_CLIP_CONTENT as u8,
};
}

pub fn copy_background_origin_from(&mut self, other: &Self) {
self.gecko.mImage.mOriginCount = cmp::min(1, other.gecko.mImage.mOriginCount);
self.gecko.mImage.mLayers.mFirstElement.mOrigin =
other.gecko.mImage.mLayers.mFirstElement.mOrigin;
}
}
</%self:simple_background_array_property>

pub fn set_background_origin(&mut self, v: longhands::background_origin::computed_value::T) {
use properties::longhands::background_origin::computed_value::T;
<%self:simple_background_array_property name="origin" field_name="mOrigin">
use properties::longhands::background_origin::single_value::computed_value::T;

self.gecko.mImage.mOriginCount = 1;
self.gecko.mImage.mLayers.mFirstElement.mOrigin = match v {
match servo {
T::border_box => structs::NS_STYLE_IMAGELAYER_ORIGIN_BORDER as u8,
T::padding_box => structs::NS_STYLE_IMAGELAYER_ORIGIN_PADDING as u8,
T::content_box => structs::NS_STYLE_IMAGELAYER_ORIGIN_CONTENT as u8,
};
}

pub fn copy_background_attachment_from(&mut self, other: &Self) {
self.gecko.mImage.mAttachmentCount = cmp::min(1, other.gecko.mImage.mAttachmentCount);
self.gecko.mImage.mLayers.mFirstElement.mAttachment =
other.gecko.mImage.mLayers.mFirstElement.mAttachment;
}
}
</%self:simple_background_array_property>

pub fn set_background_attachment(&mut self, v: longhands::background_attachment::computed_value::T) {
use properties::longhands::background_attachment::computed_value::T;
<%self:simple_background_array_property name="attachment" field_name="mAttachment">
use properties::longhands::background_attachment::single_value::computed_value::T;

self.gecko.mImage.mAttachmentCount = 1;
self.gecko.mImage.mLayers.mFirstElement.mAttachment = match v {
match servo {
T::scroll => structs::NS_STYLE_IMAGELAYER_ATTACHMENT_SCROLL as u8,
T::fixed => structs::NS_STYLE_IMAGELAYER_ATTACHMENT_FIXED as u8,
T::local => structs::NS_STYLE_IMAGELAYER_ATTACHMENT_LOCAL as u8,
};
}
}
</%self:simple_background_array_property>

pub fn copy_background_position_from(&mut self, other: &Self) {
self.gecko.mImage.mPositionXCount = cmp::min(1, other.gecko.mImage.mPositionXCount);
Expand Down
5 changes: 1 addition & 4 deletions components/style/properties/helpers.mako.rs
Expand Up @@ -285,9 +285,6 @@
}
</%def>

<%def name="maybe_vector_longhand(name, maybe, **kwargs)">

</%def>
<%def name="single_keyword(name, values, vector=False, **kwargs)">
<%call expr="single_keyword_computed(name, values, vector, **kwargs)">
use values::computed::ComputedValueAsSpecified;
Expand All @@ -304,7 +301,7 @@
'extra_gecko_values', 'extra_servo_values',
]}
%>

<%def name="inner_body()">
pub use self::computed_value::T as SpecifiedValue;
pub mod computed_value {
Expand Down
8 changes: 8 additions & 0 deletions components/style/properties/longhand/background.mako.rs
Expand Up @@ -107,18 +107,26 @@ ${helpers.predefined_type("background-color", "CSSColor",

${helpers.single_keyword("background-repeat",
"repeat repeat-x repeat-y no-repeat",
vector=True,
gecko_only=True,
animatable=False)}

${helpers.single_keyword("background-attachment",
"scroll fixed" + (" local" if product == "gecko" else ""),
vector=True,
gecko_only=True,
animatable=False)}

${helpers.single_keyword("background-clip",
"border-box padding-box content-box",
vector=True,
gecko_only=True,
animatable=False)}

${helpers.single_keyword("background-origin",
"padding-box border-box content-box",
vector=True,
gecko_only=True,
animatable=False)}

<%helpers:longhand name="background-size" animatable="True">
Expand Down
5 changes: 4 additions & 1 deletion ports/geckolib/gecko_bindings/sugar/ns_style_auto_array.rs
Expand Up @@ -3,13 +3,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::iter::{once, Chain, Once, IntoIterator};
use std::slice::IterMut;
use std::slice::{Iter, IterMut};
use structs::nsStyleAutoArray;

impl<T> nsStyleAutoArray<T> {
pub fn iter_mut(&mut self) -> Chain<Once<&mut T>, IterMut<T>> {
once(&mut self.mFirstElement).chain(self.mOtherElements.iter_mut())
}
pub fn iter(&self) -> Chain<Once<&T>, Iter<T>> {
once(&self.mFirstElement).chain(self.mOtherElements.iter())
}

// Note that often structs containing autoarrays will have
// additional member fields that contain the length, which must be kept
Expand Down

0 comments on commit 65a8a8d

Please sign in to comment.