Skip to content

Commit

Permalink
Add style_traits crate to improve crate separation,
Browse files Browse the repository at this point in the history
closes #7353
  • Loading branch information
jxs committed Sep 4, 2015
1 parent 8d21a79 commit 4a305d1
Show file tree
Hide file tree
Showing 21 changed files with 274 additions and 156 deletions.
7 changes: 3 additions & 4 deletions components/compositing/Cargo.toml
Expand Up @@ -19,6 +19,9 @@ path = "../layout_traits"
[dependencies.script_traits]
path = "../script_traits"

[dependencies.style_traits]
path = "../style_traits"

[dependencies.msg]
path = "../msg"

Expand All @@ -28,9 +31,6 @@ path = "../profile_traits"
[dependencies.net_traits]
path = "../net_traits"

[dependencies.style]
path = "../style"

[dependencies.util]
path = "../util"

Expand Down Expand Up @@ -79,4 +79,3 @@ core-graphics = "0.1"

[target.x86_64-apple-darwin.dependencies.core-text]
git = "https://github.com/servo/core-text-rs"

2 changes: 1 addition & 1 deletion components/compositing/compositor.rs
Expand Up @@ -46,7 +46,7 @@ use std::mem as std_mem;
use std::rc::Rc;
use std::slice::bytes::copy_memory;
use std::sync::mpsc::Sender;
use style::viewport::ViewportConstraints;
use style_traits::viewport::ViewportConstraints;
use time::{precise_time_ns, precise_time_s};
use url::Url;
use util::geometry::{Au, PagePx, ScreenPx, ViewportPx};
Expand Down
2 changes: 1 addition & 1 deletion components/compositing/compositor_task.rs
Expand Up @@ -25,7 +25,7 @@ use profile_traits::time;
use std::fmt::{Error, Formatter, Debug};
use std::rc::Rc;
use std::sync::mpsc::{channel, Sender, Receiver};
use style::viewport::ViewportConstraints;
use style_traits::viewport::ViewportConstraints;
use url::Url;
use util::cursor::Cursor;

Expand Down
2 changes: 1 addition & 1 deletion components/compositing/constellation.rs
Expand Up @@ -50,7 +50,7 @@ use std::marker::PhantomData;
use std::mem::replace;
use std::process;
use std::sync::mpsc::{Receiver, Sender, channel};
use style::viewport::ViewportConstraints;
use style_traits::viewport::ViewportConstraints;
use url::Url;
use util::cursor::Cursor;
use util::geometry::PagePx;
Expand Down
3 changes: 1 addition & 2 deletions components/compositing/lib.rs
Expand Up @@ -30,7 +30,7 @@ extern crate num;
extern crate profile_traits;
extern crate net_traits;
extern crate gfx_traits;
extern crate style;
extern crate style_traits;
#[macro_use]
extern crate util;
extern crate gleam;
Expand Down Expand Up @@ -58,4 +58,3 @@ mod scrolling;
pub mod pipeline;
pub mod constellation;
pub mod windowing;

7 changes: 3 additions & 4 deletions components/msg/Cargo.toml
Expand Up @@ -7,15 +7,15 @@ authors = ["The Servo Project Developers"]
name = "msg"
path = "lib.rs"

[dependencies.style]
path = "../style"

[dependencies.util]
path = "../util"

[dependencies.canvas_traits]
path = "../canvas_traits"

[dependencies.style_traits]
path = "../style_traits"

[dependencies.azure]
git = "https://github.com/servo/rust-azure"

Expand Down Expand Up @@ -56,4 +56,3 @@ core-foundation = "0.1"

[target.x86_64-apple-darwin.dependencies.io-surface]
git = "https://github.com/servo/io-surface-rs"

2 changes: 1 addition & 1 deletion components/msg/constellation_msg.rs
Expand Up @@ -18,7 +18,7 @@ use offscreen_gl_context::GLContextAttributes;
use png::Image;
use std::collections::HashMap;
use std::sync::mpsc::{channel, Sender, Receiver};
use style::viewport::ViewportConstraints;
use style_traits::viewport::ViewportConstraints;
use url::Url;
use util::cursor::Cursor;
use util::geometry::{PagePx, ViewportPx};
Expand Down
2 changes: 1 addition & 1 deletion components/msg/lib.rs
Expand Up @@ -18,7 +18,7 @@ extern crate rustc_serialize;
extern crate serde;
extern crate util;
extern crate url;
extern crate style;
extern crate style_traits;

#[cfg(target_os = "macos")]
extern crate core_foundation;
Expand Down
24 changes: 22 additions & 2 deletions components/servo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion components/style/Cargo.toml
Expand Up @@ -15,6 +15,9 @@ path = "../plugins"
[dependencies.util]
path = "../util"

[dependencies.style_traits]
path = "../style_traits"

[dependencies.selectors]
git = "https://github.com/servo/rust-selectors"
features = ["unstable"]
Expand Down Expand Up @@ -42,4 +45,3 @@ string_cache_plugin = "0.1"
euclid = "0.1"
serde = "0.5"
serde_macros = "0.5"

2 changes: 2 additions & 0 deletions components/style/lib.rs
Expand Up @@ -42,6 +42,8 @@ extern crate lazy_static;
extern crate num;
extern crate util;

#[macro_use]
extern crate style_traits;

mod custom_properties;
pub mod stylesheets;
Expand Down
3 changes: 2 additions & 1 deletion components/style/selector_matching.rs
Expand Up @@ -18,8 +18,9 @@ use legacy::PresentationalHintSynthesis;
use media_queries::Device;
use node::TElementAttributes;
use properties::{PropertyDeclaration, PropertyDeclarationBlock};
use style_traits::viewport::ViewportConstraints;
use stylesheets::{Stylesheet, CSSRuleIteratorExt, Origin};
use viewport::{ViewportConstraints, ViewportRuleCascade};
use viewport::{MaybeNew, ViewportRuleCascade};


pub type DeclarationBlock = GenericDeclarationBlock<Vec<PropertyDeclaration>>;
Expand Down
47 changes: 1 addition & 46 deletions components/style/values.rs
Expand Up @@ -6,37 +6,6 @@

pub use cssparser::RGBA;

macro_rules! define_css_keyword_enum {
($name: ident: $( $css: expr => $variant: ident ),+,) => {
define_css_keyword_enum!($name: $( $css => $variant ),+);
};
($name: ident: $( $css: expr => $variant: ident ),+) => {
#[allow(non_camel_case_types)]
#[derive(Clone, Eq, PartialEq, Copy, Hash, RustcEncodable, Debug, HeapSizeOf)]
#[derive(Deserialize, Serialize)]
pub enum $name {
$( $variant ),+
}

impl $name {
pub fn parse(input: &mut ::cssparser::Parser) -> Result<$name, ()> {
match_ignore_ascii_case! { try!(input.expect_ident()),
$( $css => Ok($name::$variant) ),+
_ => Err(())
}
}
}

impl ::cssparser::ToCss for $name {
fn to_css<W>(&self, dest: &mut W) -> ::std::fmt::Result
where W: ::std::fmt::Write {
match *self {
$( $name::$variant => dest.write_str($css) ),+
}
}
}
}
}

macro_rules! define_numbered_css_keyword_enum {
($name: ident: $( $css: expr => $variant: ident = $value: expr ),+,) => {
Expand Down Expand Up @@ -83,25 +52,11 @@ pub mod specified {
use std::fmt;
use std::fmt::Write;
use std::ops::Mul;
use style_traits::values::specified::AllowedNumericType;
use super::CSSFloat;
use url::Url;
use util::geometry::Au;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AllowedNumericType {
All,
NonNegative
}

impl AllowedNumericType {
#[inline]
pub fn is_ok(&self, value: f32) -> bool {
match *self {
AllowedNumericType::All => true,
AllowedNumericType::NonNegative => value >= 0.,
}
}
}

#[derive(Clone, PartialEq, Debug, HeapSizeOf)]
pub struct CSSColor {
Expand Down
102 changes: 11 additions & 91 deletions components/style/viewport.rs
Expand Up @@ -2,19 +2,20 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use cssparser::{Parser, DeclarationListParser, AtRuleParser, DeclarationParser, ToCss, parse_important};
use cssparser::{Parser, DeclarationListParser, AtRuleParser, DeclarationParser, parse_important};
use euclid::scale_factor::ScaleFactor;
use euclid::size::{Size2D, TypedSize2D};
use parser::{ParserContext, log_css_error};
use properties::longhands;
use style_traits::viewport::{UserZoom, Zoom, Orientation, ViewportConstraints};
use stylesheets::Origin;
use util::geometry::{Au, PagePx, ViewportPx};
use util::geometry::{Au, ViewportPx};
use values::computed::{Context, ToComputedValue};
use values::specified::{AllowedNumericType, LengthOrPercentageOrAuto};
use values::specified::LengthOrPercentageOrAuto;


use std::ascii::AsciiExt;
use std::collections::hash_map::{Entry, HashMap};
use std::fmt;
use std::intrinsics;

#[derive(Copy, Clone, Debug, PartialEq)]
Expand All @@ -33,61 +34,6 @@ pub enum ViewportDescriptor {
Orientation(Orientation)
}

/// Zoom is a number | percentage | auto
/// See http://dev.w3.org/csswg/css-device-adapt/#descdef-viewport-zoom
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Zoom {
Number(f32),
Percentage(f32),
Auto,
}

impl ToCss for Zoom {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write
{
match *self {
Zoom::Number(number) => write!(dest, "{}", number),
Zoom::Percentage(percentage) => write!(dest, "{}%", percentage * 100.),
Zoom::Auto => write!(dest, "auto")
}
}
}

impl Zoom {
pub fn parse(input: &mut Parser) -> Result<Zoom, ()> {
use cssparser::Token;

match try!(input.next()) {
Token::Percentage(ref value) if AllowedNumericType::NonNegative.is_ok(value.unit_value) =>
Ok(Zoom::Percentage(value.unit_value)),
Token::Number(ref value) if AllowedNumericType::NonNegative.is_ok(value.value) =>
Ok(Zoom::Number(value.value)),
Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") =>
Ok(Zoom::Auto),
_ => Err(())
}
}

#[inline]
pub fn to_f32(&self) -> Option<f32> {
match *self {
Zoom::Number(number) => Some(number as f32),
Zoom::Percentage(percentage) => Some(percentage as f32),
Zoom::Auto => None
}
}
}

define_css_keyword_enum!(UserZoom:
"zoom" => Zoom,
"fixed" => Fixed);

define_css_keyword_enum!(Orientation:
"auto" => Auto,
"portrait" => Portrait,
"landscape" => Landscape);

struct ViewportRuleParser<'a, 'b: 'a> {
context: &'a ParserContext<'b>
}
Expand Down Expand Up @@ -306,40 +252,14 @@ impl<'a, I> ViewportDescriptorDeclarationCascade for I
}
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct ViewportConstraints {
pub size: TypedSize2D<ViewportPx, f32>,

pub initial_zoom: ScaleFactor<PagePx, ViewportPx, f32>,
pub min_zoom: Option<ScaleFactor<PagePx, ViewportPx, f32>>,
pub max_zoom: Option<ScaleFactor<PagePx, ViewportPx, f32>>,

pub user_zoom: UserZoom,
pub orientation: Orientation
}

impl ToCss for ViewportConstraints {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write
{
try!(write!(dest, "@viewport {{"));
try!(write!(dest, " width: {}px;", self.size.width.get()));
try!(write!(dest, " height: {}px;", self.size.height.get()));
try!(write!(dest, " zoom: {};", self.initial_zoom.get()));
if let Some(min_zoom) = self.min_zoom {
try!(write!(dest, " min-zoom: {};", min_zoom.get()));
}
if let Some(max_zoom) = self.max_zoom {
try!(write!(dest, " max-zoom: {};", max_zoom.get()));
}
try!(write!(dest, " user-zoom: ")); try!(self.user_zoom.to_css(dest));
try!(write!(dest, "; orientation: ")); try!(self.orientation.to_css(dest));
write!(dest, "; }}")
}
pub trait MaybeNew {
fn maybe_new(initial_viewport: TypedSize2D<ViewportPx, f32>,
rule: &ViewportRule)
-> Option<ViewportConstraints>;
}

impl ViewportConstraints {
pub fn maybe_new(initial_viewport: TypedSize2D<ViewportPx, f32>,
impl MaybeNew for ViewportConstraints {
fn maybe_new(initial_viewport: TypedSize2D<ViewportPx, f32>,
rule: &ViewportRule)
-> Option<ViewportConstraints>
{
Expand Down

0 comments on commit 4a305d1

Please sign in to comment.