| 1 | //! Mouse event handling |
| 2 | |
| 3 | #![allow(dead_code)] |
| 4 | |
| 5 | use crossterm::event::{KeyModifiers, MouseButton, MouseEvent, MouseEventKind}; |
| 6 | |
| 7 | /// Mouse button types |
| 8 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 9 | pub enum Button { |
| 10 | Left, |
| 11 | Right, |
| 12 | Middle, |
| 13 | } |
| 14 | |
| 15 | /// Modifiers held during mouse event |
| 16 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 17 | pub struct MouseModifiers { |
| 18 | pub ctrl: bool, |
| 19 | pub alt: bool, |
| 20 | pub shift: bool, |
| 21 | } |
| 22 | |
| 23 | impl From<KeyModifiers> for MouseModifiers { |
| 24 | fn from(m: KeyModifiers) -> Self { |
| 25 | Self { |
| 26 | ctrl: m.contains(KeyModifiers::CONTROL), |
| 27 | alt: m.contains(KeyModifiers::ALT), |
| 28 | shift: m.contains(KeyModifiers::SHIFT), |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /// Abstracted mouse event |
| 34 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 35 | pub enum Mouse { |
| 36 | /// Click at (column, row) - 0-indexed |
| 37 | Click { button: Button, col: u16, row: u16, modifiers: MouseModifiers }, |
| 38 | /// Drag to (column, row) |
| 39 | Drag { button: Button, col: u16, row: u16, modifiers: MouseModifiers }, |
| 40 | /// Scroll up at (column, row) |
| 41 | ScrollUp { col: u16, row: u16 }, |
| 42 | /// Scroll down at (column, row) |
| 43 | ScrollDown { col: u16, row: u16 }, |
| 44 | } |
| 45 | |
| 46 | impl Mouse { |
| 47 | pub fn from_crossterm(event: MouseEvent) -> Option<Self> { |
| 48 | let col = event.column; |
| 49 | let row = event.row; |
| 50 | let modifiers = MouseModifiers::from(event.modifiers); |
| 51 | |
| 52 | match event.kind { |
| 53 | MouseEventKind::Down(button) => { |
| 54 | let button = match button { |
| 55 | MouseButton::Left => Button::Left, |
| 56 | MouseButton::Right => Button::Right, |
| 57 | MouseButton::Middle => Button::Middle, |
| 58 | }; |
| 59 | Some(Mouse::Click { button, col, row, modifiers }) |
| 60 | } |
| 61 | MouseEventKind::Drag(button) => { |
| 62 | let button = match button { |
| 63 | MouseButton::Left => Button::Left, |
| 64 | MouseButton::Right => Button::Right, |
| 65 | MouseButton::Middle => Button::Middle, |
| 66 | }; |
| 67 | Some(Mouse::Drag { button, col, row, modifiers }) |
| 68 | } |
| 69 | MouseEventKind::ScrollUp => Some(Mouse::ScrollUp { col, row }), |
| 70 | MouseEventKind::ScrollDown => Some(Mouse::ScrollDown { col, row }), |
| 71 | _ => None, // Ignore Up, Moved events for now |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// Get the column position |
| 76 | pub fn col(&self) -> u16 { |
| 77 | match self { |
| 78 | Mouse::Click { col, .. } => *col, |
| 79 | Mouse::Drag { col, .. } => *col, |
| 80 | Mouse::ScrollUp { col, .. } => *col, |
| 81 | Mouse::ScrollDown { col, .. } => *col, |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// Get the row position |
| 86 | pub fn row(&self) -> u16 { |
| 87 | match self { |
| 88 | Mouse::Click { row, .. } => *row, |
| 89 | Mouse::Drag { row, .. } => *row, |
| 90 | Mouse::ScrollUp { row, .. } => *row, |
| 91 | Mouse::ScrollDown { row, .. } => *row, |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 |