use crate::input::keyboard::KeyEvent;
use crate::terminal::MouseEvent;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event {
Key(KeyEvent),
Mouse(MouseEvent),
Resize(ResizeEvent),
FocusGained,
FocusLost,
Paste(PasteEvent),
}
impl Event {
#[must_use]
pub fn is_key(&self) -> bool {
matches!(self, Self::Key(_))
}
#[must_use]
pub fn is_mouse(&self) -> bool {
matches!(self, Self::Mouse(_))
}
#[must_use]
pub fn is_resize(&self) -> bool {
matches!(self, Self::Resize(_))
}
#[must_use]
pub fn key(&self) -> Option<&KeyEvent> {
match self {
Self::Key(e) => Some(e),
_ => None,
}
}
#[must_use]
pub fn mouse(&self) -> Option<&MouseEvent> {
match self {
Self::Mouse(e) => Some(e),
_ => None,
}
}
#[must_use]
pub fn resize(&self) -> Option<&ResizeEvent> {
match self {
Self::Resize(e) => Some(e),
_ => None,
}
}
#[must_use]
pub fn paste(&self) -> Option<&PasteEvent> {
match self {
Self::Paste(e) => Some(e),
_ => None,
}
}
#[must_use]
pub fn is_paste(&self) -> bool {
matches!(self, Self::Paste(_))
}
}
impl From<KeyEvent> for Event {
fn from(e: KeyEvent) -> Self {
Self::Key(e)
}
}
impl From<MouseEvent> for Event {
fn from(e: MouseEvent) -> Self {
Self::Mouse(e)
}
}
impl From<ResizeEvent> for Event {
fn from(e: ResizeEvent) -> Self {
Self::Resize(e)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ResizeEvent {
pub width: u16,
pub height: u16,
}
impl ResizeEvent {
#[must_use]
pub fn new(width: u16, height: u16) -> Self {
Self { width, height }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FocusEvent {
Gained,
Lost,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PasteEvent {
pub content: String,
}
impl PasteEvent {
#[must_use]
pub fn new(content: String) -> Self {
Self { content }
}
#[must_use]
pub fn content(&self) -> &str {
&self.content
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.content.is_empty()
}
#[must_use]
pub fn len(&self) -> usize {
self.content.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::input::{KeyCode, KeyModifiers};
use crate::terminal::MouseButton;
#[test]
fn test_event_key() {
let key = KeyEvent::new(KeyCode::Enter, KeyModifiers::empty());
let event = Event::Key(key);
assert!(event.is_key());
assert!(!event.is_mouse());
assert_eq!(event.key(), Some(&key));
}
#[test]
fn test_event_mouse() {
let mouse = MouseEvent::press(10, 5, MouseButton::Left);
let event = Event::Mouse(mouse);
assert!(event.is_mouse());
assert!(!event.is_key());
assert_eq!(event.mouse(), Some(&mouse));
}
#[test]
fn test_resize_event() {
let resize = ResizeEvent::new(80, 24);
assert_eq!(resize.width, 80);
assert_eq!(resize.height, 24);
}
#[test]
fn test_paste_event() {
let paste = PasteEvent::new("Hello, World!".to_string());
assert_eq!(paste.content(), "Hello, World!");
assert!(!paste.is_empty());
assert_eq!(paste.len(), 13);
}
#[test]
fn test_event_from_conversions() {
let key = KeyEvent::char('a');
let event: Event = key.into();
assert!(event.is_key());
let resize = ResizeEvent::new(100, 50);
let event: Event = resize.into();
assert!(event.is_resize());
}
}