tuyere 0.1.0

A powerful TUI framework based on The Elm Architecture 🔮
Documentation
//! Event types for user input.

use crate::key::{Key, KeyModifiers};

/// An event from the terminal.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
    /// A key press event.
    Key(Key),
    /// A key press with modifiers.
    KeyWithModifiers(Key, KeyModifiers),
    /// A mouse event.
    Mouse(MouseEvent),
    /// The terminal was resized.
    Resize(u16, u16),
    /// Focus gained.
    FocusGained,
    /// Focus lost.
    FocusLost,
    /// A paste event (bracketed paste mode).
    Paste(String),
}

/// A mouse event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MouseEvent {
    /// The kind of mouse event.
    pub kind: MouseEventKind,
    /// The column (x position).
    pub column: u16,
    /// The row (y position).
    pub row: u16,
    /// Key modifiers held during the event.
    pub modifiers: KeyModifiers,
}

/// The kind of mouse event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MouseEventKind {
    /// Mouse button pressed.
    Down(MouseButton),
    /// Mouse button released.
    Up(MouseButton),
    /// Mouse dragged with button held.
    Drag(MouseButton),
    /// Mouse moved (no button pressed).
    Moved,
    /// Scroll up.
    ScrollUp,
    /// Scroll down.
    ScrollDown,
    /// Scroll left.
    ScrollLeft,
    /// Scroll right.
    ScrollRight,
}

/// A mouse button.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MouseButton {
    /// Left mouse button.
    Left,
    /// Right mouse button.
    Right,
    /// Middle mouse button.
    Middle,
}

impl Event {
    /// Check if this is a key event.
    #[must_use]
    pub const fn is_key(&self) -> bool {
        matches!(self, Event::Key(_) | Event::KeyWithModifiers(_, _))
    }

    /// Check if this is a mouse event.
    #[must_use]
    pub const fn is_mouse(&self) -> bool {
        matches!(self, Event::Mouse(_))
    }

    /// Check if this is a resize event.
    #[must_use]
    pub const fn is_resize(&self) -> bool {
        matches!(self, Event::Resize(_, _))
    }

    /// Get the key if this is a key event.
    #[must_use]
    pub const fn key(&self) -> Option<Key> {
        match self {
            Event::Key(k) | Event::KeyWithModifiers(k, _) => Some(*k),
            _ => None,
        }
    }
}