use std::{
fmt,
ops::{BitOr, BitOrAssign},
};
use super::InvalidationKind;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct NodeCapabilities(u32);
impl NodeCapabilities {
pub const NONE: Self = Self(0);
pub const LAYOUT: Self = Self(1 << 0);
pub const DRAW: Self = Self(1 << 1);
pub const POINTER_INPUT: Self = Self(1 << 2);
pub const SEMANTICS: Self = Self(1 << 3);
pub const MODIFIER_LOCALS: Self = Self(1 << 4);
pub const FOCUS: Self = Self(1 << 5);
pub const WINDOW_ROOT: Self = Self(1 << 6);
pub const KEY_INPUT: Self = Self(1 << 7);
pub const PARENT_DATA: Self = Self(1 << 8);
pub const fn empty() -> Self {
Self::NONE
}
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
pub const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
pub const fn intersects(self, other: Self) -> bool {
(self.0 & other.0) != 0
}
pub fn insert(&mut self, other: Self) {
self.0 |= other.0;
}
pub const fn bits(self) -> u32 {
self.0
}
pub const fn is_empty(self) -> bool {
self.0 == 0
}
pub const fn for_invalidation(kind: InvalidationKind) -> Self {
match kind {
InvalidationKind::Layout => Self::LAYOUT,
InvalidationKind::Draw => Self::DRAW,
InvalidationKind::PointerInput => Self::POINTER_INPUT,
InvalidationKind::Semantics => Self::SEMANTICS,
InvalidationKind::Focus => Self::FOCUS,
}
}
}
impl Default for NodeCapabilities {
fn default() -> Self {
Self::NONE
}
}
impl fmt::Debug for NodeCapabilities {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("NodeCapabilities")
.field("layout", &self.contains(Self::LAYOUT))
.field("draw", &self.contains(Self::DRAW))
.field("pointer_input", &self.contains(Self::POINTER_INPUT))
.field("semantics", &self.contains(Self::SEMANTICS))
.field("modifier_locals", &self.contains(Self::MODIFIER_LOCALS))
.field("focus", &self.contains(Self::FOCUS))
.field("window_root", &self.contains(Self::WINDOW_ROOT))
.field("key_input", &self.contains(Self::KEY_INPUT))
.field("parent_data", &self.contains(Self::PARENT_DATA))
.finish()
}
}
impl BitOr for NodeCapabilities {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}
impl BitOrAssign for NodeCapabilities {
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}