mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
style: Introduce a generic way of gathering information from the cascade, and use it for viewport units.
This commit is contained in:
parent
40c04b4c6b
commit
9e88a495c8
12 changed files with 205 additions and 23 deletions
78
components/style/cascade_info.rs
Normal file
78
components/style/cascade_info.rs
Normal file
|
@ -0,0 +1,78 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* 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 dom::TNode;
|
||||
use properties::{DeclaredValue, PropertyDeclaration};
|
||||
use values::HasViewportPercentage;
|
||||
|
||||
/// A structure to collect information about the cascade.
|
||||
///
|
||||
/// This is useful to gather information about what an element is affected by,
|
||||
/// and can be used in the future to track optimisations like when a
|
||||
/// non-inherited property is explicitly inherited, in order to cut-off the
|
||||
/// traversal.
|
||||
pub struct CascadeInfo {
|
||||
pub saw_viewport_units: bool,
|
||||
#[cfg(debug_assertions)]
|
||||
finished: bool,
|
||||
}
|
||||
|
||||
impl CascadeInfo {
|
||||
#[cfg(debug_assertions)]
|
||||
pub fn new() -> Self {
|
||||
CascadeInfo {
|
||||
saw_viewport_units: false,
|
||||
finished: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
pub fn new() -> Self {
|
||||
CascadeInfo {
|
||||
saw_viewport_units: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Called when a property is cascaded.
|
||||
///
|
||||
/// NOTE: We can add a vast amount of information here.
|
||||
#[inline]
|
||||
pub fn on_cascade_property<T>(&mut self,
|
||||
_property_declaration: &PropertyDeclaration,
|
||||
value: &DeclaredValue<T>)
|
||||
where T: HasViewportPercentage
|
||||
{
|
||||
// TODO: we can be smarter and keep a property bitfield to keep track of
|
||||
// the last applying rule.
|
||||
if value.has_viewport_percentage() {
|
||||
self.saw_viewport_units = true;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
fn mark_as_finished_if_appropriate(&mut self) {
|
||||
self.finished = true;
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn mark_as_finished_if_appropriate(&mut self) {}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub fn finish<N: TNode>(mut self, node: &N) {
|
||||
self.mark_as_finished_if_appropriate();
|
||||
|
||||
if self.saw_viewport_units {
|
||||
unsafe {
|
||||
node.set_dirty_on_viewport_size_changed();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
impl Drop for CascadeInfo {
|
||||
fn drop(&mut self) {
|
||||
debug_assert!(self.finished,
|
||||
"Didn't use the result of CascadeInfo, if you don't need it, consider passing None");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue