From 2491522c87d654ad2ec4f509d0f7bca44eea2a6d Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Sun, 12 Jan 2025 17:40:50 +0000 Subject: [PATCH] feat: basic MouseClicks struct --- helix-term/src/ui/editor.rs | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 5d028415e..2bb4d4d01 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -46,6 +46,50 @@ pub struct EditorView { terminal_focused: bool, } +/// Tracks the character positions where the mouse was last clicked +/// +/// If we double click, select that word +/// If we triple click, select full line +struct MouseClicks { + clicks: [Option; 2], + count: usize, +} + +impl MouseClicks { + fn new() -> Self { + Self { + clicks: [None, None], + count: 0, + } + } + + fn register_click(&mut self, char_idx: usize) { + match self.count { + 0 => { + self.clicks[0] = Some(char_idx); + self.count = 1; + } + 1 => { + self.clicks[1] = Some(char_idx); + self.count = 2; + } + 2 => { + self.clicks[0] = self.clicks[1]; + self.clicks[1] = Some(char_idx); + } + _ => unreachable!("Mouse click count should never exceed 2"), + } + } + + fn has_double_click(&mut self) -> Option { + self.clicks[0].filter(|first_click| Some(first_click) != self.clicks[1].as_ref()) + } + + fn has_single_click(&mut self) -> Option { + self.clicks[1] + } +} + #[derive(Debug, Clone)] pub enum InsertEvent { Key(KeyEvent),