layout_api/
layout_damage.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use bitflags::bitflags;
6use style::selector_parser::RestyleDamage;
7
8bitflags! {
9    /// Individual layout actions that may be necessary after restyling. This is an extension
10    /// of `RestyleDamage` from stylo, which only uses the 4 lower bits.
11    #[derive(Clone, Copy, Default, Eq, PartialEq)]
12    pub struct LayoutDamage: u16 {
13        /// Recollect the box children for this element, because some of the them will be
14        /// rebuilt.
15        const RECOLLECT_BOX_TREE_CHILDREN = 0b0111_1111_1111 << 4;
16        /// Rebuild the entire box for this element, which means that every part of layout
17        /// needs to happena again.
18        const REBUILD_BOX = 0b1111_1111_1111 << 4;
19    }
20}
21
22impl LayoutDamage {
23    pub fn recollect_box_tree_children() -> RestyleDamage {
24        RestyleDamage::from_bits_retain(LayoutDamage::RECOLLECT_BOX_TREE_CHILDREN.bits())
25    }
26
27    pub fn rebuild_box_tree() -> RestyleDamage {
28        RestyleDamage::from_bits_retain(LayoutDamage::REBUILD_BOX.bits())
29    }
30
31    pub fn has_box_damage(&self) -> bool {
32        self.intersects(Self::REBUILD_BOX)
33    }
34}
35
36impl From<RestyleDamage> for LayoutDamage {
37    fn from(restyle_damage: RestyleDamage) -> Self {
38        LayoutDamage::from_bits_retain(restyle_damage.bits())
39    }
40}
41
42impl std::fmt::Debug for LayoutDamage {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        if self.contains(Self::REBUILD_BOX) {
45            f.write_str("REBUILD_BOX")
46        } else if self.contains(Self::RECOLLECT_BOX_TREE_CHILDREN) {
47            f.write_str("RECOLLECT_BOX_TREE_CHILDREN")
48        } else {
49            f.write_str("EMPTY")
50        }
51    }
52}