use {
keyboard_types::{
Code,
NamedKey,
},
smol_str::SmolStr,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum NativeKey {
Unidentified,
Key(u32),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum NativeCode {
Unidentified,
Code(u32),
}
impl From<NativeCode> for NativeKey {
#[inline]
fn from(code: NativeCode) -> Self {
match code {
NativeCode::Unidentified => NativeKey::Unidentified,
NativeCode::Code(x) => NativeKey::Key(x),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum LogicalKey<Str = SmolStr> {
Named(NamedKey),
Character(Str),
Unidentified(NativeKey),
Dead(Option<char>),
}
impl LogicalKey<SmolStr> {
pub fn as_ref(&self) -> LogicalKey<&str> {
match self {
Self::Named(a) => LogicalKey::Named(*a),
Self::Character(ch) => LogicalKey::Character(ch.as_str()),
Self::Dead(d) => LogicalKey::Dead(*d),
Self::Unidentified(u) => LogicalKey::Unidentified(*u),
}
}
}
impl From<NamedKey> for LogicalKey {
#[inline]
fn from(action: NamedKey) -> Self {
Self::Named(action)
}
}
impl From<NativeKey> for LogicalKey {
#[inline]
fn from(code: NativeKey) -> Self {
Self::Unidentified(code)
}
}
impl<Str> PartialEq<NamedKey> for LogicalKey<Str> {
#[inline]
fn eq(&self, rhs: &NamedKey) -> bool {
match self {
Self::Named(a) => a == rhs,
_ => false,
}
}
}
impl<Str: PartialEq<str>> PartialEq<str> for LogicalKey<Str> {
#[inline]
fn eq(&self, rhs: &str) -> bool {
match self {
Self::Character(s) => s == rhs,
_ => false,
}
}
}
impl<Str: PartialEq<str>> PartialEq<&str> for LogicalKey<Str> {
#[inline]
fn eq(&self, rhs: &&str) -> bool {
self == *rhs
}
}
impl<Str> PartialEq<NativeKey> for LogicalKey<Str> {
#[inline]
fn eq(&self, rhs: &NativeKey) -> bool {
match self {
Self::Unidentified(code) => code == rhs,
_ => false,
}
}
}
impl<Str> PartialEq<LogicalKey<Str>> for NativeKey {
#[inline]
fn eq(&self, rhs: &LogicalKey<Str>) -> bool {
rhs == self
}
}
impl LogicalKey {
pub fn to_text(&self) -> Option<&str> {
match self {
Self::Named(action) => match action {
NamedKey::Enter => Some("\r"),
NamedKey::Backspace => Some("\x08"),
NamedKey::Tab => Some("\t"),
NamedKey::Escape => Some("\x1b"),
_ => None,
},
Self::Character(ch) => Some(ch.as_str()),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PhysicalKey {
Code(Code),
Unidentified(NativeCode),
}
impl From<Code> for PhysicalKey {
#[inline]
fn from(code: Code) -> Self {
Self::Code(code)
}
}
impl From<PhysicalKey> for Code {
#[inline]
fn from(key: PhysicalKey) -> Self {
match key {
PhysicalKey::Code(code) => code,
PhysicalKey::Unidentified(_) => Code::Unidentified,
}
}
}
impl From<NativeCode> for PhysicalKey {
#[inline]
fn from(code: NativeCode) -> Self {
Self::Unidentified(code)
}
}
impl PartialEq<Code> for PhysicalKey {
#[inline]
fn eq(&self, rhs: &Code) -> bool {
match self {
Self::Code(code) => code == rhs,
_ => false,
}
}
}
impl PartialEq<PhysicalKey> for Code {
#[inline]
fn eq(&self, rhs: &PhysicalKey) -> bool {
rhs == self
}
}
impl PartialEq<NativeCode> for PhysicalKey {
#[inline]
fn eq(&self, rhs: &NativeCode) -> bool {
match self {
Self::Unidentified(code) => code == rhs,
_ => false,
}
}
}
impl PartialEq<PhysicalKey> for NativeCode {
#[inline]
fn eq(&self, rhs: &PhysicalKey) -> bool {
rhs == self
}
}