From 7675b0ced07a69d21021c9b4f38195f8a5b529ae Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 5 Jun 2014 15:33:46 -0700 Subject: [PATCH] Use bitflags! for RestyleDamage --- src/components/main/css/node_util.rs | 5 +- src/components/main/layout/incremental.rs | 162 +++------------------- src/components/main/layout/inline.rs | 2 +- src/components/main/layout/layout_task.rs | 8 +- src/components/main/layout/util.rs | 3 +- 5 files changed, 30 insertions(+), 150 deletions(-) diff --git a/src/components/main/css/node_util.rs b/src/components/main/css/node_util.rs index b67098984e82..4fd5fa5d1b24 100644 --- a/src/components/main/css/node_util.rs +++ b/src/components/main/css/node_util.rs @@ -68,7 +68,7 @@ impl<'ln> NodeUtil for ThreadSafeLayoutNode<'ln> { let default = if self.node_is_element() { RestyleDamage::all() } else { - RestyleDamage::none() + RestyleDamage::empty() }; let layout_data_ref = self.borrow_layout_data(); @@ -76,7 +76,6 @@ impl<'ln> NodeUtil for ThreadSafeLayoutNode<'ln> { .get_ref() .data .restyle_damage - .map(|x| RestyleDamage::from_int(x)) .unwrap_or(default) } @@ -84,7 +83,7 @@ impl<'ln> NodeUtil for ThreadSafeLayoutNode<'ln> { fn set_restyle_damage(&self, damage: RestyleDamage) { let mut layout_data_ref = self.mutate_layout_data(); match &mut *layout_data_ref { - &Some(ref mut layout_data) => layout_data.data.restyle_damage = Some(damage.to_int()), + &Some(ref mut layout_data) => layout_data.data.restyle_damage = Some(damage), _ => fail!("no layout data for this node"), } } diff --git a/src/components/main/layout/incremental.rs b/src/components/main/layout/incremental.rs index e050418886ed..710e3979e0b9 100644 --- a/src/components/main/layout/incremental.rs +++ b/src/components/main/layout/incremental.rs @@ -4,100 +4,34 @@ use style::ComputedValues; -/// Individual layout actions that may be necessary after restyling. -/// -/// If you add to this enum, also add the value to RestyleDamage::all below. -/// (FIXME: do this automatically) -pub enum RestyleEffect { - /// Repaint the node itself. - /// Currently unused; need to decide how this propagates. - Repaint = 0x01, - - /// Recompute intrinsic widths (minimum and preferred). - /// Propagates down the flow tree because the computation is - /// bottom-up. - BubbleWidths = 0x02, - - /// Recompute actual widths and heights. - /// Propagates up the flow tree because the computation is - /// top-down. - Reflow = 0x04, -} - -/// A set of RestyleEffects. -// FIXME: Switch to librustc/util/enum_set.rs if that gets moved into -// libextra (Rust #8054) -pub struct RestyleDamage { - bits: int +bitflags! { + #[doc = "Individual layout actions that may be necessary after restyling."] + flags RestyleDamage: int { + #[doc = "Repaint the node itself."] + #[doc = "Currently unused; need to decide how this propagates."] + static Repaint = 0x01, + + #[doc = "Recompute intrinsic widths (minimum and preferred)."] + #[doc = "Propagates down the flow tree because the computation is"] + #[doc = "bottom-up."] + static BubbleWidths = 0x02, + + #[doc = "Recompute actual widths and heights."] + #[doc = "Propagates up the flow tree because the computation is"] + #[doc = "top-down."] + static Reflow = 0x04 + } } -// Provide literal syntax of the form restyle_damage!(Repaint, Reflow) -macro_rules! restyle_damage( - ( $($damage:ident),* ) => ( - RestyleDamage::none() $( .add($damage) )* - ) -) - impl RestyleDamage { - pub fn none() -> RestyleDamage { - RestyleDamage { bits: 0 } - } - - pub fn all() -> RestyleDamage { - restyle_damage!(Repaint, BubbleWidths, Reflow) - } - - /// Create a RestyleDamage from the underlying bit field. - /// We would rather not allow this, but some types in script - /// need to store RestyleDamage without depending on this crate. - pub fn from_int(n: int) -> RestyleDamage { - RestyleDamage { bits: n } - } - - pub fn to_int(self) -> int { - self.bits - } - - pub fn is_empty(self) -> bool { - self.bits == 0 - } - - pub fn is_nonempty(self) -> bool { - self.bits != 0 - } - - pub fn add(self, effect: RestyleEffect) -> RestyleDamage { - RestyleDamage { bits: self.bits | (effect as int) } - } - - pub fn has(self, effect: RestyleEffect) -> bool { - (self.bits & (effect as int)) != 0 - } - - pub fn lacks(self, effect: RestyleEffect) -> bool { - (self.bits & (effect as int)) == 0 - } - - pub fn union(self, other: RestyleDamage) -> RestyleDamage { - RestyleDamage { bits: self.bits | other.bits } - } - - pub fn union_in_place(&mut self, other: RestyleDamage) { - self.bits = self.bits | other.bits; - } - - pub fn intersect(self, other: RestyleDamage) -> RestyleDamage { - RestyleDamage { bits: self.bits & other.bits } - } - /// Elements of self which should also get set on any ancestor flow. pub fn propagate_up(self) -> RestyleDamage { - self.intersect(restyle_damage!(Reflow)) + self & Reflow } /// Elements of self which should also get set on any child flows. pub fn propagate_down(self) -> RestyleDamage { - self.intersect(restyle_damage!(BubbleWidths)) + self & BubbleWidths } } @@ -108,13 +42,13 @@ macro_rules! add_if_not_equal( ($old:ident, $new:ident, $damage:ident, [ $($effect:ident),* ], [ $($style_struct_getter:ident.$name:ident),* ]) => ({ if $( ($old.$style_struct_getter().$name != $new.$style_struct_getter().$name) )||* { - $damage.union_in_place( restyle_damage!( $($effect),* ) ); + $damage.insert($($effect)|*); } }) ) pub fn compute_damage(old: &ComputedValues, new: &ComputedValues) -> RestyleDamage { - let mut damage = RestyleDamage::none(); + let mut damage = RestyleDamage::empty(); // This checks every CSS property, as enumerated in // impl<'self> CssComputedStyle<'self> @@ -142,57 +76,3 @@ pub fn compute_damage(old: &ComputedValues, new: &ComputedValues) -> RestyleDama damage } - - -#[cfg(test)] -mod restyle_damage_tests { - use super::*; - - #[test] - fn none_is_empty() { - let d = RestyleDamage::none(); - assert!(!d.has(Repaint)); - assert!(!d.has(BubbleWidths)); - assert!(d.lacks(Repaint)); - assert!(d.lacks(BubbleWidths)); - } - - #[test] - fn all_is_full() { - let d = RestyleDamage::all(); - assert!(d.has(Repaint)); - assert!(d.has(BubbleWidths)); - assert!(!d.lacks(Repaint)); - assert!(!d.lacks(BubbleWidths)); - } - - #[test] - fn can_add() { - assert!(RestyleDamage::none().add(BubbleWidths).has(BubbleWidths)); - } - - #[test] - fn can_union() { - let d = restyle_damage!(Repaint).union(restyle_damage!(BubbleWidths)); - assert!(d.has(Repaint)); - assert!(d.has(BubbleWidths)); - } - - #[test] - fn can_union_in_place() { - let mut d = restyle_damage!(Repaint); - d.union_in_place(restyle_damage!(BubbleWidths)); - assert!(d.has(Repaint)); - assert!(d.has(BubbleWidths)); - } - - #[test] - fn can_intersect() { - let x = restyle_damage!(Repaint, BubbleWidths); - let y = restyle_damage!(Repaint, Reflow); - let d = x.intersect(y); - assert!(d.has(Repaint)); - assert!(d.lacks(BubbleWidths)); - assert!(d.lacks(Reflow)); - } -} diff --git a/src/components/main/layout/inline.rs b/src/components/main/layout/inline.rs index 5efc5002952b..d65faed56500 100644 --- a/src/components/main/layout/inline.rs +++ b/src/components/main/layout/inline.rs @@ -750,7 +750,7 @@ impl InlineFragments { style: other_style, range: mut other_range } = other_range; - + other_range.shift_by(adjustment); self.push_range(other_style, other_range) } diff --git a/src/components/main/layout/layout_task.rs b/src/components/main/layout/layout_task.rs index fe9ebc2aa65b..b5aa76a15116 100644 --- a/src/components/main/layout/layout_task.rs +++ b/src/components/main/layout/layout_task.rs @@ -118,7 +118,7 @@ impl PostorderFlowTraversal for ComputeDamageTraversal { fn process(&mut self, flow: &mut Flow) -> bool { let mut damage = flow::base(flow).restyle_damage; for child in flow::child_iter(flow) { - damage.union_in_place(flow::base(child).restyle_damage.propagate_up()) + damage.insert(flow::base(child).restyle_damage.propagate_up()) } flow::mut_base(flow).restyle_damage = damage; true @@ -136,14 +136,14 @@ impl PreorderFlowTraversal for PropagateDamageTraversal { #[inline] fn process(&mut self, flow: &mut Flow) -> bool { if self.all_style_damage { - flow::mut_base(flow).restyle_damage.union_in_place(RestyleDamage::all()) + flow::mut_base(flow).restyle_damage.insert(RestyleDamage::all()) } debug!("restyle damage = {:?}", flow::base(flow).restyle_damage); let prop = flow::base(flow).restyle_damage.propagate_down(); - if prop.is_nonempty() { + if !prop.is_empty() { for kid_ctx in flow::child_iter(flow) { - flow::mut_base(kid_ctx).restyle_damage.union_in_place(prop) + flow::mut_base(kid_ctx).restyle_damage.insert(prop) } } true diff --git a/src/components/main/layout/util.rs b/src/components/main/layout/util.rs index 3a37cdd0d03e..486cb204804e 100644 --- a/src/components/main/layout/util.rs +++ b/src/components/main/layout/util.rs @@ -3,6 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use layout::construct::{ConstructionResult, NoConstructionResult}; +use layout::incremental::RestyleDamage; use layout::parallel::DomParallelInfo; use layout::wrapper::{LayoutNode, TLayoutNode, ThreadSafeLayoutNode}; @@ -28,7 +29,7 @@ pub struct PrivateLayoutData { pub after_style: Option>, /// Description of how to account for recent style changes. - pub restyle_damage: Option, + pub restyle_damage: Option, /// The current results of flow construction for this node. This is either a flow or a /// `ConstructionItem`. See comments in `construct.rs` for more details.