mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
style: Implement css(dimension) and derive ToCss for a bunch of stuff.
For css(dimension), it'd be nice to derive(Parse) too, I think...
This commit is contained in:
parent
1b533f9bdc
commit
4927786d90
10 changed files with 98 additions and 153 deletions
|
@ -5,9 +5,8 @@
|
|||
//! Computed angles.
|
||||
|
||||
use euclid::Radians;
|
||||
use std::{f32, f64, fmt};
|
||||
use std::{f32, f64};
|
||||
use std::f64::consts::PI;
|
||||
use style_traits::ToCss;
|
||||
use values::CSSFloat;
|
||||
use values::animated::{Animate, Procedure};
|
||||
use values::distance::{ComputeSquaredDistance, SquaredDistance};
|
||||
|
@ -15,15 +14,16 @@ use values::distance::{ComputeSquaredDistance, SquaredDistance};
|
|||
/// A computed angle.
|
||||
#[animate(fallback = "Self::animate_fallback")]
|
||||
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
|
||||
#[derive(Animate, Clone, Copy, Debug, MallocSizeOf, PartialEq)]
|
||||
#[derive(Animate, Clone, Copy, Debug, MallocSizeOf, PartialEq, ToCss)]
|
||||
#[derive(PartialOrd, ToAnimatedZero)]
|
||||
#[css(dimension)]
|
||||
pub enum Angle {
|
||||
/// An angle with degree unit.
|
||||
Degree(CSSFloat),
|
||||
Deg(CSSFloat),
|
||||
/// An angle with gradian unit.
|
||||
Gradian(CSSFloat),
|
||||
Grad(CSSFloat),
|
||||
/// An angle with radian unit.
|
||||
Radian(CSSFloat),
|
||||
Rad(CSSFloat),
|
||||
/// An angle with turn unit.
|
||||
Turn(CSSFloat),
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ pub enum Angle {
|
|||
impl Angle {
|
||||
/// Creates a computed `Angle` value from a radian amount.
|
||||
pub fn from_radians(radians: CSSFloat) -> Self {
|
||||
Angle::Radian(radians)
|
||||
Angle::Rad(radians)
|
||||
}
|
||||
|
||||
/// Returns the amount of radians this angle represents.
|
||||
|
@ -53,17 +53,17 @@ impl Angle {
|
|||
const RAD_PER_TURN: f64 = PI * 2.0;
|
||||
|
||||
let radians = match *self {
|
||||
Angle::Degree(val) => val as f64 * RAD_PER_DEG,
|
||||
Angle::Gradian(val) => val as f64 * RAD_PER_GRAD,
|
||||
Angle::Deg(val) => val as f64 * RAD_PER_DEG,
|
||||
Angle::Grad(val) => val as f64 * RAD_PER_GRAD,
|
||||
Angle::Turn(val) => val as f64 * RAD_PER_TURN,
|
||||
Angle::Radian(val) => val as f64,
|
||||
Angle::Rad(val) => val as f64,
|
||||
};
|
||||
radians.min(f64::MAX).max(f64::MIN)
|
||||
}
|
||||
|
||||
/// Returns an angle that represents a rotation of zero radians.
|
||||
pub fn zero() -> Self {
|
||||
Angle::Radian(0.0)
|
||||
Self::from_radians(0.0)
|
||||
}
|
||||
|
||||
/// <https://drafts.csswg.org/css-transitions/#animtype-number>
|
||||
|
@ -82,25 +82,6 @@ impl ComputeSquaredDistance for Angle {
|
|||
}
|
||||
}
|
||||
|
||||
impl ToCss for Angle {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
|
||||
where
|
||||
W: fmt::Write,
|
||||
{
|
||||
let mut write = |value: CSSFloat, unit: &str| {
|
||||
value.to_css(dest)?;
|
||||
dest.write_str(unit)
|
||||
};
|
||||
|
||||
match *self {
|
||||
Angle::Degree(val) => write(val, "deg"),
|
||||
Angle::Gradian(val) => write(val, "grad"),
|
||||
Angle::Radian(val) => write(val, "rad"),
|
||||
Angle::Turn(val) => write(val, "turn"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Angle> for Radians<CSSFloat> {
|
||||
#[inline]
|
||||
fn from(a: Angle) -> Self {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue