use serde::{Deserialize, Serialize};
use crate::*;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DialogEvent {
ActivateModal,
EnterPressed,
EscPressed,
None,
}
mod dialog_event_impl {
use super::*;
impl DialogEvent {
pub fn should_activate_modal(
input_event: &InputEvent,
modal_keypress: KeyPress,
) -> Self {
if let InputEvent::Keyboard(keypress) = input_event {
if keypress == &modal_keypress {
return Self::ActivateModal;
}
}
Self::None
}
pub fn from(input_event: &InputEvent) -> Self {
if let InputEvent::Keyboard(keypress) = input_event {
match keypress {
KeyPress::Plain {
key: Key::SpecialKey(SpecialKey::Enter),
} => {
return Self::EnterPressed;
}
KeyPress::Plain {
key: Key::SpecialKey(SpecialKey::Esc),
} => {
return Self::EscPressed;
}
_ => {}
}
}
Self::None
}
}
}
#[cfg(test)]
mod test_dialog_event {
use r3bl_rs_utils_core::*;
use crate::*;
#[test]
fn dialog_event_handles_enter() {
let input_event = InputEvent::Keyboard(keypress!(@special SpecialKey::Enter));
let dialog_event = DialogEvent::from(&input_event);
assert_eq2!(dialog_event, DialogEvent::EnterPressed);
}
#[test]
fn dialog_event_handles_esc() {
let input_event = InputEvent::Keyboard(keypress!(@special SpecialKey::Esc));
let dialog_event = DialogEvent::from(&input_event);
assert_eq2!(dialog_event, DialogEvent::EscPressed);
}
#[test]
fn dialog_event_handles_modal_keypress() {
let modal_keypress = keypress!(@char ModifierKeysMask::CTRL, 'l');
let input_event = InputEvent::Keyboard(modal_keypress);
let dialog_event =
DialogEvent::should_activate_modal(&input_event, modal_keypress);
assert_eq2!(dialog_event, DialogEvent::ActivateModal);
}
}