use super::{names, Chord, Mods, Platform};
use crate::KeyBinding;
use winit::keyboard::KeyCode;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Stroke {
pub mods: Mods,
pub key: KeyBinding,
}
impl Stroke {
pub fn new(mods: Mods, key: impl Into<KeyBinding>) -> Self {
Self {
mods,
key: key.into(),
}
}
pub fn code(&self) -> Option<KeyCode> {
match self.key {
KeyBinding::Key(code) => Some(code),
_ => None,
}
}
pub fn is_valid(&self) -> bool {
self.code().is_some()
}
pub fn concrete(self, platform: Platform) -> Self {
Self {
mods: self.mods.concrete(platform),
key: self.key,
}
}
pub fn then(self, next: Stroke) -> Chord {
Chord::new([self, next])
}
}
impl Mods {
pub fn key(self, key: impl Into<KeyBinding>) -> Stroke {
Stroke::new(self, key)
}
}
impl From<KeyCode> for Stroke {
fn from(code: KeyCode) -> Self {
Self::new(Mods::NONE, code)
}
}
impl From<KeyBinding> for Stroke {
fn from(key: KeyBinding) -> Self {
Self::new(Mods::NONE, key)
}
}
impl std::fmt::Display for Stroke {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for name in self.mods.names() {
write!(f, "{name}+")?;
}
match self.key {
KeyBinding::Key(code) => match names::name(code) {
Some(name) => f.write_str(name),
None => write!(f, "{}", format!("{code:?}").to_ascii_lowercase()),
},
_ => f.write_str("none"),
}
}
}