use postcard::experimental::max_size::MaxSize;
use serde::{Deserialize, Serialize};
use super::Action;
#[derive(Debug, Copy, Clone, Eq, Serialize, Deserialize, MaxSize)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub enum KeyAction {
No,
Transparent,
Single(Action),
Tap(Action),
TapHold(Action, Action, u8),
Morse(u8),
}
impl KeyAction {
pub fn to_action(self) -> Action {
match self {
KeyAction::Single(a) | KeyAction::Tap(a) => a,
_ => Action::No,
}
}
pub fn is_morse(&self) -> bool {
matches!(self, KeyAction::TapHold(_, _, _) | KeyAction::Morse(_))
}
pub fn is_empty(&self) -> bool {
matches!(self, KeyAction::No)
}
}
impl PartialEq for KeyAction {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(KeyAction::No, KeyAction::No) => true,
(KeyAction::Transparent, KeyAction::Transparent) => true,
(KeyAction::Single(a), KeyAction::Single(b)) => a == b,
(KeyAction::Tap(a), KeyAction::Tap(b)) => a == b,
(KeyAction::TapHold(a, b, _), KeyAction::TapHold(c, d, _)) => a == c && b == d,
(KeyAction::Morse(a), KeyAction::Morse(b)) => a == b,
_ => false,
}
}
}