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        /// Clear the cached inline content sizes and recompute them during the next layout.
14        const RECOMPUTE_INLINE_CONTENT_SIZES = 0b1000_0000_0000 << 4;
15        /// Rebuild this box and all of its ancestors. Do not rebuild any children. This
16        /// is used when a box's content (such as text content) changes or a descendant
17        /// has box damage ([`Self::BOX_DAMAGE`]).
18        const DESCENDANT_HAS_BOX_DAMAGE = 0b0111_1111_1111 << 4;
19        /// Rebuild this box, all of its ancestors and all of its descendants. This is the
20        /// most a box can be damaged.
21        const BOX_DAMAGE = 0b1111_1111_1111 << 4;
22    }
23}
24
25impl LayoutDamage {
26    pub fn descendant_has_box_damage() -> RestyleDamage {
27        RestyleDamage::from_bits_retain(LayoutDamage::DESCENDANT_HAS_BOX_DAMAGE.bits())
28    }
29
30    pub fn box_damage() -> RestyleDamage {
31        RestyleDamage::from_bits_retain(LayoutDamage::BOX_DAMAGE.bits())
32    }
33
34    pub fn needs_new_box(&self) -> bool {
35        self.contains(Self::DESCENDANT_HAS_BOX_DAMAGE)
36    }
37
38    pub fn recompute_inline_content_sizes() -> RestyleDamage {
39        RestyleDamage::from_bits_retain(LayoutDamage::RECOMPUTE_INLINE_CONTENT_SIZES.bits())
40    }
41}
42
43impl From<RestyleDamage> for LayoutDamage {
44    fn from(restyle_damage: RestyleDamage) -> Self {
45        LayoutDamage::from_bits_retain(restyle_damage.bits())
46    }
47}
48
49impl From<LayoutDamage> for RestyleDamage {
50    fn from(layout_damage: LayoutDamage) -> Self {
51        RestyleDamage::from_bits_retain(layout_damage.bits())
52    }
53}
54
55impl std::fmt::Debug for LayoutDamage {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        if self.contains(Self::BOX_DAMAGE) {
58            f.write_str("REBUILD_BOX")
59        } else if self.contains(Self::DESCENDANT_HAS_BOX_DAMAGE) {
60            f.write_str("RECOLLECT_BOX_TREE_CHILDREN")
61        } else {
62            f.write_str("EMPTY")
63        }
64    }
65}