From 563bc2f10b2ca4ac494bba68d3f0897a6bd21ded Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Sat, 30 Mar 2024 22:38:38 +0530 Subject: [PATCH 1/4] Extract popup scrolling code into named functions --- helix-term/src/ui/popup.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index b38b8b6e3fbc..8ff9cc7d491f 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -146,6 +146,14 @@ impl Popup { } } + pub fn scroll_half_page_down(&mut self) { + self.scroll(self.size.1 as usize / 2, true) + } + + pub fn scroll_half_page_up(&mut self) { + self.scroll(self.size.1 as usize / 2, false) + } + /// Toggles the Popup's scrollbar. /// Consider disabling the scrollbar in case the child /// already has its own. @@ -200,11 +208,11 @@ impl Component for Popup { EventResult::Consumed(Some(close_fn)) } ctrl!('d') => { - self.scroll(self.size.1 as usize / 2, true); + self.scroll_half_page_down(); EventResult::Consumed(None) } ctrl!('u') => { - self.scroll(self.size.1 as usize / 2, false); + self.scroll_half_page_up(); EventResult::Consumed(None) } _ => { From 4dfa235d9ae9dd7cec2cfddce4ff139ef2554b2f Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Sun, 31 Mar 2024 00:07:13 +0530 Subject: [PATCH 2/4] Scroll popup contents on mouse scroll event --- helix-term/src/ui/popup.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index 8ff9cc7d491f..242370e3e354 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -11,6 +11,7 @@ use tui::{ use helix_core::Position; use helix_view::{ graphics::{Margin, Rect}, + input::{MouseEvent, MouseEventKind}, Editor, }; @@ -179,12 +180,27 @@ impl Popup { // clip to viewport viewport.intersection(Rect::new(rel_x, rel_y, self.size.0, self.size.1)) } + + fn handle_mouse_event(&mut self, event: &MouseEvent) -> EventResult { + return match event.kind { + MouseEventKind::ScrollDown if self.has_scrollbar => { + self.scroll_half_page_down(); + EventResult::Consumed(None) + } + MouseEventKind::ScrollUp if self.has_scrollbar => { + self.scroll_half_page_up(); + EventResult::Consumed(None) + } + _ => EventResult::Ignored(None), + }; + } } impl Component for Popup { fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult { let key = match event { Event::Key(event) => *event, + Event::Mouse(event) => return self.handle_mouse_event(event), Event::Resize(_, _) => { // TODO: calculate inner area, call component's handle_event with that area return EventResult::Ignored(None); From 7c6f50bba053224f5f610290962a95cdbc6f4e98 Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Sun, 31 Mar 2024 01:32:50 +0530 Subject: [PATCH 3/4] Ignore mouse events outside the popup --- helix-term/src/ui/popup.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index 242370e3e354..90bd7532ed1a 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -24,6 +24,7 @@ pub struct Popup { margin: Margin, size: (u16, u16), child_size: (u16, u16), + area: Rect, position_bias: Open, scroll: usize, auto_close: bool, @@ -41,6 +42,7 @@ impl Popup { size: (0, 0), position_bias: Open::Below, child_size: (0, 0), + area: Rect::new(0, 0, 0, 0), scroll: 0, auto_close: false, ignore_escape_key: false, @@ -181,8 +183,25 @@ impl Popup { viewport.intersection(Rect::new(rel_x, rel_y, self.size.0, self.size.1)) } - fn handle_mouse_event(&mut self, event: &MouseEvent) -> EventResult { - return match event.kind { + fn handle_mouse_event( + &mut self, + &MouseEvent { + kind, + column: x, + row: y, + .. + }: &MouseEvent, + ) -> EventResult { + let mouse_is_within_popup = x >= self.area.left() + && x < self.area.right() + && y >= self.area.top() + && y < self.area.bottom(); + + if !mouse_is_within_popup { + return EventResult::Ignored(None); + } + + return match kind { MouseEventKind::ScrollDown if self.has_scrollbar => { self.scroll_half_page_down(); EventResult::Consumed(None) @@ -273,6 +292,7 @@ impl Component for Popup { fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) { let area = self.area(viewport, cx.editor); + self.area = area; cx.scroll = Some(self.scroll); // clear area From 96edc845063cba53d1ef3808686e29b39c283ccd Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Sun, 31 Mar 2024 01:58:44 +0530 Subject: [PATCH 4/4] Remove unneeded return statement --- helix-term/src/ui/popup.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index 90bd7532ed1a..4f379b4a1d5b 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -201,7 +201,7 @@ impl Popup { return EventResult::Ignored(None); } - return match kind { + match kind { MouseEventKind::ScrollDown if self.has_scrollbar => { self.scroll_half_page_down(); EventResult::Consumed(None) @@ -211,7 +211,7 @@ impl Popup { EventResult::Consumed(None) } _ => EventResult::Ignored(None), - }; + } } }