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        /// Clear the cached inline content sizes and recompute them during the next layout.
17        const RECOMPUTE_INLINE_CONTENT_SIZES = 0b1000_0000_0000 << 4;
18        /// Rebuild the entire box for this element, which means that every part of layout
19        /// needs to happen again.
20        const REBUILD_BOX = 0b1111_1111_1111 << 4;
21    }
22}
23
24impl LayoutDamage {
25    pub fn recollect_box_tree_children() -> RestyleDamage {
26        RestyleDamage::from_bits_retain(LayoutDamage::RECOLLECT_BOX_TREE_CHILDREN.bits())
27    }
28
29    pub fn recompute_inline_content_sizes() -> RestyleDamage {
30        RestyleDamage::from_bits_retain(LayoutDamage::RECOMPUTE_INLINE_CONTENT_SIZES.bits())
31    }
32
33    pub fn rebuild_box_tree() -> RestyleDamage {
34        RestyleDamage::from_bits_retain(LayoutDamage::REBUILD_BOX.bits())
35    }
36
37    pub fn has_box_damage(&self) -> bool {
38        self.intersects(Self::REBUILD_BOX)
39    }
40}
41
42impl From<RestyleDamage> for LayoutDamage {
43    fn from(restyle_damage: RestyleDamage) -> Self {
44        LayoutDamage::from_bits_retain(restyle_damage.bits())
45    }
46}
47
48impl From<LayoutDamage> for RestyleDamage {
49    fn from(layout_damage: LayoutDamage) -> Self {
50        RestyleDamage::from_bits_retain(layout_damage.bits())
51    }
52}
53
54impl std::fmt::Debug for LayoutDamage {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        if self.contains(Self::REBUILD_BOX) {
57            f.write_str("REBUILD_BOX")
58        } else if self.contains(Self::RECOLLECT_BOX_TREE_CHILDREN) {
59            f.write_str("RECOLLECT_BOX_TREE_CHILDREN")
60        } else {
61            f.write_str("EMPTY")
62        }
63    }
64}