use super::Platform;
use std::ops::BitOr;
use winit::keyboard::ModifiersState;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Mods(u8);
impl Mods {
pub const NONE: Self = Self(0);
pub const CTRL: Self = Self(1);
pub const SHIFT: Self = Self(2);
pub const ALT: Self = Self(4);
pub const META: Self = Self(8);
pub const PRIMARY: Self = Self(16);
pub const fn is_empty(self) -> bool {
self.0 == 0
}
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
pub fn concrete(self, platform: Platform) -> Self {
if self.contains(Self::PRIMARY) {
Self(self.0 & !Self::PRIMARY.0) | platform.primary()
} else {
self
}
}
pub fn from_state(state: ModifiersState) -> Self {
let mut mods = Self::NONE;
for (held, flag) in [
(state.control_key(), Self::CTRL),
(state.shift_key(), Self::SHIFT),
(state.alt_key(), Self::ALT),
(state.super_key(), Self::META),
] {
if held {
mods = mods | flag;
}
}
mods
}
pub(crate) fn names(self) -> impl Iterator<Item = &'static str> {
[
(Self::PRIMARY, "mod"),
(Self::CTRL, "ctrl"),
(Self::ALT, "alt"),
(Self::SHIFT, "shift"),
(Self::META, "meta"),
]
.into_iter()
.filter(move |(flag, _)| self.contains(*flag))
.map(|(_, name)| name)
}
pub(crate) fn from_name(name: &str) -> Option<Self> {
Some(match name {
"mod" | "primary" => Self::PRIMARY,
"ctrl" | "control" => Self::CTRL,
"shift" => Self::SHIFT,
"alt" | "option" | "opt" => Self::ALT,
"meta" | "cmd" | "command" | "super" | "win" => Self::META,
_ => return None,
})
}
}
impl BitOr for Mods {
type Output = Self;
fn bitor(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}