Factor out opacity property to use an Opacity type.

This commit is contained in:
Cameron McCormack 2016-05-01 17:33:43 +10:00
parent d69763b0c1
commit 0f2dc53b0a
3 changed files with 35 additions and 41 deletions

View file

@ -7,46 +7,9 @@
// Box-shadow, etc.
<% data.new_style_struct("Effects", inherited=False) %>
<%helpers:longhand name="opacity">
use cssparser::ToCss;
use std::fmt;
use values::CSSFloat;
impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}
#[derive(Debug, Clone, PartialEq, HeapSizeOf)]
pub struct SpecifiedValue(pub CSSFloat);
pub mod computed_value {
use values::CSSFloat;
pub type T = CSSFloat;
}
#[inline]
pub fn get_initial_value() -> computed_value::T {
1.0
}
impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T;
#[inline]
fn to_computed_value<Cx: TContext>(&self, _context: &Cx) -> computed_value::T {
if self.0 < 0.0 {
0.0
} else if self.0 > 1.0 {
1.0
} else {
self.0
}
}
}
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
specified::parse_number(input).map(SpecifiedValue)
}
</%helpers:longhand>
${helpers.predefined_type("opacity",
"Opacity",
"1.0")}
<%helpers:longhand name="box-shadow">
use cssparser::{self, ToCss};

View file

@ -1475,6 +1475,36 @@ pub mod specified {
write!(dest, "{}s", self.0)
}
}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, HeapSizeOf)]
pub struct Opacity(pub CSSFloat);
impl Opacity {
pub fn parse(input: &mut Parser) -> Result<Opacity, ()> {
parse_number(input).map(Opacity)
}
}
impl ToComputedValue for Opacity {
type ComputedValue = CSSFloat;
#[inline]
fn to_computed_value<Cx: TContext>(&self, _: &Cx) -> CSSFloat {
if self.0 < 0.0 {
0.0
} else if self.0 > 1.0 {
1.0
} else {
self.0
}
}
}
impl ToCss for Opacity {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}
}
pub mod computed {
@ -2087,4 +2117,5 @@ pub mod computed {
}
}
pub type Length = Au;
pub type Opacity = CSSFloat;
}

View file

@ -295,7 +295,7 @@ impl Debug for ${style_struct.gecko_ffi_name} {
# These are booleans.
force_stub += ["page-break-after", "page-break-before"]
simple_types = []
simple_types = ["Opacity"]
keyword_longhands = [x for x in longhands if x.keyword and not x.name in force_stub]
simple_longhands = [x for x in longhands