use std::ops::Add;
use winit::keyboard::KeyCode;
use winit::keyboard::ModifiersState;
pub const ALT: Modifier = Modifier(ModifiersState::ALT);
pub const CONTROL: Modifier = Modifier(ModifiersState::CONTROL);
pub const CTRL: Modifier = Modifier(ModifiersState::CONTROL);
pub const META: Modifier = Modifier(ModifiersState::SUPER);
pub const SHIFT: Modifier = Modifier(ModifiersState::SHIFT);
pub const SUPER: Modifier = Modifier(ModifiersState::SUPER);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Modifier(ModifiersState);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Key {
key_code: KeyCode,
modifiers: ModifiersState,
}
impl Add<Modifier> for Key {
type Output = Key;
fn add(mut self, rhs: Modifier) -> Self::Output {
self.modifiers |= rhs.0;
self
}
}
impl Add<KeyCode> for Modifier {
type Output = Key;
fn add(self, key_code: KeyCode) -> Self::Output {
Key {
key_code,
modifiers: self.0,
}
}
}
impl Key {
pub fn with_shift(mut self) -> Self {
self.modifiers |= ModifiersState::SHIFT;
self
}
pub fn with_control(mut self) -> Self {
self.modifiers |= ModifiersState::CONTROL;
self
}
pub fn with_alt(mut self) -> Self {
self.modifiers |= ModifiersState::ALT;
self
}
pub fn with_super(mut self) -> Self {
self.modifiers |= ModifiersState::SUPER;
self
}
pub(crate) fn with_modifiers(mut self, modifiers: ModifiersState) -> Self {
self.modifiers = modifiers;
self
}
}
impl From<KeyCode> for Key {
fn from(key_code: KeyCode) -> Self {
Self {
key_code,
modifiers: ModifiersState::default(),
}
}
}
impl From<(KeyCode, ModifiersState)> for Key {
fn from((key_code, modifiers): (KeyCode, ModifiersState)) -> Self {
Self {
key_code,
modifiers,
}
}
}
pub mod prelude {
pub use super::ALT;
pub use super::CONTROL;
pub use super::CTRL;
pub use super::Key;
pub use super::META;
pub use super::SHIFT;
pub use super::SUPER;
pub use winit::keyboard::KeyCode;
}