| 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 | /// Mouse button released at (column, row) |
| 41 | Up { button: Button, col: u16, row: u16 }, |
| 42 | /// Scroll up at (column, row) |
| 43 | ScrollUp { col: u16, row: u16 }, |
| 44 | /// Scroll down at (column, row) |
| 45 | ScrollDown { col: u16, row: u16 }, |
| 46 | } |
| 47 | |
| 48 | impl Mouse { |
| 49 | pub fn from_crossterm(event: MouseEvent) -> Option<Self> { |
| 50 | let col = event.column; |
| 51 | let row = event.row; |
| 52 | let modifiers = MouseModifiers::from(event.modifiers); |
| 53 | |
| 54 | match event.kind { |
| 55 | MouseEventKind::Down(button) => { |
| 56 | let button = match button { |
| 57 | MouseButton::Left => Button::Left, |
| 58 | MouseButton::Right => Button::Right, |
| 59 | MouseButton::Middle => Button::Middle, |
| 60 | }; |
| 61 | Some(Mouse::Click { button, col, row, modifiers }) |
| 62 | } |
| 63 | MouseEventKind::Drag(button) => { |
| 64 | let button = match button { |
| 65 | MouseButton::Left => Button::Left, |
| 66 | MouseButton::Right => Button::Right, |
| 67 | MouseButton::Middle => Button::Middle, |
| 68 | }; |
| 69 | Some(Mouse::Drag { button, col, row, modifiers }) |
| 70 | } |
| 71 | MouseEventKind::Up(button) => { |
| 72 | let button = match button { |
| 73 | MouseButton::Left => Button::Left, |
| 74 | MouseButton::Right => Button::Right, |
| 75 | MouseButton::Middle => Button::Middle, |
| 76 | }; |
| 77 | Some(Mouse::Up { button, col, row }) |
| 78 | } |
| 79 | MouseEventKind::ScrollUp => Some(Mouse::ScrollUp { col, row }), |
| 80 | MouseEventKind::ScrollDown => Some(Mouse::ScrollDown { col, row }), |
| 81 | _ => None, // Ignore Moved events for now |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// Get the column position |
| 86 | pub fn col(&self) -> u16 { |
| 87 | match self { |
| 88 | Mouse::Click { col, .. } => *col, |
| 89 | Mouse::Drag { col, .. } => *col, |
| 90 | Mouse::Up { col, .. } => *col, |
| 91 | Mouse::ScrollUp { col, .. } => *col, |
| 92 | Mouse::ScrollDown { col, .. } => *col, |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// Get the row position |
| 97 | pub fn row(&self) -> u16 { |
| 98 | match self { |
| 99 | Mouse::Click { row, .. } => *row, |
| 100 | Mouse::Drag { row, .. } => *row, |
| 101 | Mouse::Up { row, .. } => *row, |
| 102 | Mouse::ScrollUp { row, .. } => *row, |
| 103 | Mouse::ScrollDown { row, .. } => *row, |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 |