cranpose_foundation/modifier/
node_capabilities.rs1use std::{
5 fmt,
6 ops::{BitOr, BitOrAssign},
7};
8
9use super::InvalidationKind;
10
11#[derive(Clone, Copy, PartialEq, Eq, Hash)]
13pub struct NodeCapabilities(u32);
14
15impl NodeCapabilities {
16 pub const NONE: Self = Self(0);
18 pub const LAYOUT: Self = Self(1 << 0);
20 pub const DRAW: Self = Self(1 << 1);
22 pub const POINTER_INPUT: Self = Self(1 << 2);
24 pub const SEMANTICS: Self = Self(1 << 3);
26 pub const MODIFIER_LOCALS: Self = Self(1 << 4);
28 pub const FOCUS: Self = Self(1 << 5);
30 pub const WINDOW_ROOT: Self = Self(1 << 6);
34 pub const KEY_INPUT: Self = Self(1 << 7);
36 pub const PARENT_DATA: Self = Self(1 << 8);
39
40 pub const fn empty() -> Self {
42 Self::NONE
43 }
44
45 pub const fn union(self, other: Self) -> Self {
47 Self(self.0 | other.0)
48 }
49
50 pub const fn contains(self, other: Self) -> bool {
52 (self.0 & other.0) == other.0
53 }
54
55 pub const fn intersects(self, other: Self) -> bool {
57 (self.0 & other.0) != 0
58 }
59
60 pub fn insert(&mut self, other: Self) {
62 self.0 |= other.0;
63 }
64
65 pub const fn bits(self) -> u32 {
67 self.0
68 }
69
70 pub const fn is_empty(self) -> bool {
72 self.0 == 0
73 }
74
75 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}