Skip to main content

cranpose_foundation/modifier/
node_capabilities.rs

1// The capability flags of a modifier node: which specialized traits it
2// implements.
3
4use std::{
5    fmt,
6    ops::{BitOr, BitOrAssign},
7};
8
9use super::InvalidationKind;
10
11/// Capability flags indicating which specialized traits a modifier node implements.
12#[derive(Clone, Copy, PartialEq, Eq, Hash)]
13pub struct NodeCapabilities(u32);
14
15impl NodeCapabilities {
16    /// No capabilities.
17    pub const NONE: Self = Self(0);
18    /// Modifier participates in measure/layout.
19    pub const LAYOUT: Self = Self(1 << 0);
20    /// Modifier participates in draw.
21    pub const DRAW: Self = Self(1 << 1);
22    /// Modifier participates in pointer input.
23    pub const POINTER_INPUT: Self = Self(1 << 2);
24    /// Modifier participates in semantics tree construction.
25    pub const SEMANTICS: Self = Self(1 << 3);
26    /// Modifier participates in modifier locals.
27    pub const MODIFIER_LOCALS: Self = Self(1 << 4);
28    /// Modifier participates in focus management.
29    pub const FOCUS: Self = Self(1 << 5);
30    /// Modifier makes its node the root of a separate window: the node's
31    /// subtree is laid out into that window's size and drawn into that
32    /// window's scene, and the parent's scene skips it.
33    pub const WINDOW_ROOT: Self = Self(1 << 6);
34    /// Modifier handles hardware keyboard events.
35    pub const KEY_INPUT: Self = Self(1 << 7);
36    /// Modifier tells the parent's layout about its node, as a weight or an
37    /// alignment does: Compose's `ParentDataModifierNode`.
38    pub const PARENT_DATA: Self = Self(1 << 8);
39
40    /// Returns an empty capability set.
41    pub const fn empty() -> Self {
42        Self::NONE
43    }
44
45    /// The capabilities of `self` and those of `other`.
46    pub const fn union(self, other: Self) -> Self {
47        Self(self.0 | other.0)
48    }
49
50    /// Returns whether all bits in `other` are present in `self`.
51    pub const fn contains(self, other: Self) -> bool {
52        (self.0 & other.0) == other.0
53    }
54
55    /// Returns whether any bit in `other` is present in `self`.
56    pub const fn intersects(self, other: Self) -> bool {
57        (self.0 & other.0) != 0
58    }
59
60    /// Inserts the requested capability bits.
61    pub fn insert(&mut self, other: Self) {
62        self.0 |= other.0;
63    }
64
65    /// Returns the raw bit representation.
66    pub const fn bits(self) -> u32 {
67        self.0
68    }
69
70    /// Returns true when no capabilities are set.
71    pub const fn is_empty(self) -> bool {
72        self.0 == 0
73    }
74
75    /// Returns the capability bit mask required for the given invalidation.
76    pub const fn for_invalidation(kind: InvalidationKind) -> Self {
77        match kind {
78            InvalidationKind::Layout => Self::LAYOUT,
79            InvalidationKind::Draw => Self::DRAW,
80            InvalidationKind::PointerInput => Self::POINTER_INPUT,
81            InvalidationKind::Semantics => Self::SEMANTICS,
82            InvalidationKind::Focus => Self::FOCUS,
83        }
84    }
85}
86
87impl Default for NodeCapabilities {
88    fn default() -> Self {
89        Self::NONE
90    }
91}
92
93impl fmt::Debug for NodeCapabilities {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        f.debug_struct("NodeCapabilities")
96            .field("layout", &self.contains(Self::LAYOUT))
97            .field("draw", &self.contains(Self::DRAW))
98            .field("pointer_input", &self.contains(Self::POINTER_INPUT))
99            .field("semantics", &self.contains(Self::SEMANTICS))
100            .field("modifier_locals", &self.contains(Self::MODIFIER_LOCALS))
101            .field("focus", &self.contains(Self::FOCUS))
102            .field("window_root", &self.contains(Self::WINDOW_ROOT))
103            .field("key_input", &self.contains(Self::KEY_INPUT))
104            .field("parent_data", &self.contains(Self::PARENT_DATA))
105            .finish()
106    }
107}
108
109impl BitOr for NodeCapabilities {
110    type Output = Self;
111
112    fn bitor(self, rhs: Self) -> Self::Output {
113        Self(self.0 | rhs.0)
114    }
115}
116
117impl BitOrAssign for NodeCapabilities {
118    fn bitor_assign(&mut self, rhs: Self) {
119        self.0 |= rhs.0;
120    }
121}