style: Use cbindgen for cursors.

Pretty straight-forward.

Differential Revision: https://phabricator.services.mozilla.com/D63777
This commit is contained in:
Emilio Cobos Álvarez 2020-02-23 13:07:30 +00:00
parent 787ac98d18
commit 18570bf077
4 changed files with 39 additions and 77 deletions

View file

@ -22,7 +22,6 @@ use crate::gecko_bindings::bindings::Gecko_CopyConstruct_${style_struct.gecko_ff
use crate::gecko_bindings::bindings::Gecko_Destroy_${style_struct.gecko_ffi_name}; use crate::gecko_bindings::bindings::Gecko_Destroy_${style_struct.gecko_ffi_name};
% endfor % endfor
use crate::gecko_bindings::bindings::Gecko_CopyCounterStyle; use crate::gecko_bindings::bindings::Gecko_CopyCounterStyle;
use crate::gecko_bindings::bindings::Gecko_CopyCursorArrayFrom;
use crate::gecko_bindings::bindings::Gecko_CopyFontFamilyFrom; use crate::gecko_bindings::bindings::Gecko_CopyFontFamilyFrom;
use crate::gecko_bindings::bindings::Gecko_EnsureImageLayersLength; use crate::gecko_bindings::bindings::Gecko_EnsureImageLayersLength;
use crate::gecko_bindings::bindings::Gecko_nsStyleFont_SetLang; use crate::gecko_bindings::bindings::Gecko_nsStyleFont_SetLang;
@ -2263,61 +2262,7 @@ mask-mode mask-repeat mask-clip mask-origin mask-composite mask-position-x mask-
} }
</%self:impl_trait> </%self:impl_trait>
<%self:impl_trait style_struct_name="InheritedUI" skip_longhands="cursor"> <%self:impl_trait style_struct_name="InheritedUI">
pub fn set_cursor(&mut self, v: longhands::cursor::computed_value::T) {
self.gecko.mCursor = v.keyword;
unsafe {
bindings::Gecko_SetCursorArrayCapacity(&mut *self.gecko, v.images.len());
}
for i in 0..v.images.len() {
unsafe {
bindings::Gecko_AppendCursorImage(&mut *self.gecko, &v.images[i].url);
}
match v.images[i].hotspot {
Some((x, y)) => {
self.gecko.mCursorImages[i].mHaveHotspot = true;
self.gecko.mCursorImages[i].mHotspotX = x;
self.gecko.mCursorImages[i].mHotspotY = y;
},
_ => {
self.gecko.mCursorImages[i].mHaveHotspot = false;
}
}
}
}
pub fn copy_cursor_from(&mut self, other: &Self) {
self.gecko.mCursor = other.gecko.mCursor;
unsafe {
Gecko_CopyCursorArrayFrom(&mut *self.gecko, &*other.gecko);
}
}
pub fn reset_cursor(&mut self, other: &Self) {
self.copy_cursor_from(other)
}
pub fn clone_cursor(&self) -> longhands::cursor::computed_value::T {
use crate::values::computed::ui::CursorImage;
let keyword = self.gecko.mCursor;
let images = self.gecko.mCursorImages.iter().map(|gecko_cursor_image| {
let url = gecko_cursor_image.mImage.clone();
let hotspot =
if gecko_cursor_image.mHaveHotspot {
Some((gecko_cursor_image.mHotspotX, gecko_cursor_image.mHotspotY))
} else {
None
};
CursorImage { url, hotspot }
}).collect::<Vec<_>>().into_boxed_slice();
longhands::cursor::computed_value::T { images, keyword }
}
</%self:impl_trait> </%self:impl_trait>
<%self:impl_trait style_struct_name="Column" <%self:impl_trait style_struct_name="Column"

View file

@ -13,10 +13,10 @@ pub use crate::values::specified::ui::CursorKind;
pub use crate::values::specified::ui::{MozForceBrokenImageIcon, UserSelect}; pub use crate::values::specified::ui::{MozForceBrokenImageIcon, UserSelect};
/// A computed value for the `cursor` property. /// A computed value for the `cursor` property.
pub type Cursor = generics::Cursor<CursorImage>; pub type Cursor = generics::GenericCursor<CursorImage>;
/// A computed value for item of `image cursors`. /// A computed value for item of `image cursors`.
pub type CursorImage = generics::CursorImage<ComputedImageUrl, Number>; pub type CursorImage = generics::GenericCursorImage<ComputedImageUrl, Number>;
/// A computed value for `scrollbar-color` property. /// A computed value for `scrollbar-color` property.
pub type ScrollbarColor = generics::GenericScrollbarColor<Color>; pub type ScrollbarColor = generics::GenericScrollbarColor<Color>;

View file

@ -21,19 +21,22 @@ use values::specified::ui::CursorKind;
ToResolvedValue, ToResolvedValue,
ToShmem, ToShmem,
)] )]
pub struct Cursor<Image> { #[repr(C)]
pub struct GenericCursor<Image> {
/// The parsed images for the cursor. /// The parsed images for the cursor.
pub images: Box<[Image]>, pub images: crate::OwnedSlice<Image>,
/// The kind of the cursor [default | help | ...]. /// The kind of the cursor [default | help | ...].
pub keyword: CursorKind, pub keyword: CursorKind,
} }
pub use self::GenericCursor as Cursor;
impl<Image> Cursor<Image> { impl<Image> Cursor<Image> {
/// Set `cursor` to `auto` /// Set `cursor` to `auto`
#[inline] #[inline]
pub fn auto() -> Self { pub fn auto() -> Self {
Self { Self {
images: vec![].into_boxed_slice(), images: Default::default(),
keyword: CursorKind::Auto, keyword: CursorKind::Auto,
} }
} }
@ -63,24 +66,31 @@ impl<Image: ToCss> ToCss for Cursor<Image> {
ToResolvedValue, ToResolvedValue,
ToShmem, ToShmem,
)] )]
pub struct CursorImage<ImageUrl, Number> { #[repr(C)]
pub struct GenericCursorImage<ImageUrl, Number> {
/// The url to parse images from. /// The url to parse images from.
pub url: ImageUrl, pub url: ImageUrl,
/// The <x> and <y> coordinates. /// Whether the image has a hotspot or not.
pub hotspot: Option<(Number, Number)>, pub has_hotspot: bool,
/// The x coordinate.
pub hotspot_x: Number,
/// The y coordinate.
pub hotspot_y: Number,
} }
pub use self::GenericCursorImage as CursorImage;
impl<ImageUrl: ToCss, Number: ToCss> ToCss for CursorImage<ImageUrl, Number> { impl<ImageUrl: ToCss, Number: ToCss> ToCss for CursorImage<ImageUrl, Number> {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where where
W: Write, W: Write,
{ {
self.url.to_css(dest)?; self.url.to_css(dest)?;
if let Some((ref x, ref y)) = self.hotspot { if self.has_hotspot {
dest.write_str(" ")?; dest.write_str(" ")?;
x.to_css(dest)?; self.hotspot_x.to_css(dest)?;
dest.write_str(" ")?; dest.write_str(" ")?;
y.to_css(dest)?; self.hotspot_y.to_css(dest)?;
} }
Ok(()) Ok(())
} }

View file

@ -14,10 +14,10 @@ use std::fmt::{self, Write};
use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss}; use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};
/// A specified value for the `cursor` property. /// A specified value for the `cursor` property.
pub type Cursor = generics::Cursor<CursorImage>; pub type Cursor = generics::GenericCursor<CursorImage>;
/// A specified value for item of `image cursors`. /// A specified value for item of `image cursors`.
pub type CursorImage = generics::CursorImage<SpecifiedImageUrl, Number>; pub type CursorImage = generics::GenericCursorImage<SpecifiedImageUrl, Number>;
impl Parse for Cursor { impl Parse for Cursor {
/// cursor: [<url> [<number> <number>]?]# [auto | default | ...] /// cursor: [<url> [<number> <number>]?]# [auto | default | ...]
@ -34,7 +34,7 @@ impl Parse for Cursor {
input.expect_comma()?; input.expect_comma()?;
} }
Ok(Self { Ok(Self {
images: images.into_boxed_slice(), images: images.into(),
keyword: CursorKind::parse(input)?, keyword: CursorKind::parse(input)?,
}) })
} }
@ -45,13 +45,20 @@ impl Parse for CursorImage {
context: &ParserContext, context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> { ) -> Result<Self, ParseError<'i>> {
Ok(Self { use crate::Zero;
url: SpecifiedImageUrl::parse(context, input)?,
hotspot: match input.try(|input| Number::parse(context, input)) { let url = SpecifiedImageUrl::parse(context, input)?;
Ok(number) => Some((number, Number::parse(context, input)?)), let mut has_hotspot = false;
Err(_) => None, let mut hotspot_x = Number::zero();
}, let mut hotspot_y = Number::zero();
})
if let Ok(x) = input.try(|input| Number::parse(context, input)) {
has_hotspot = true;
hotspot_x = x;
hotspot_y = Number::parse(context, input)?;
}
Ok(Self { url, has_hotspot, hotspot_x, hotspot_y })
} }
} }