From e2594b64c0daf15bacdb5396d439f206fcbe11e6 Mon Sep 17 00:00:00 2001 From: Pascal Kuthe Date: Tue, 23 Apr 2024 16:32:43 +0200 Subject: [PATCH] move popup when cursor line changes Co-authored-by: Ben Fekih, Hichem" --- helix-term/src/ui/popup.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index 7ee65db59ce9..5362bdc7f537 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -124,9 +124,15 @@ impl Popup { } fn render_info(&mut self, viewport: Rect, editor: &Editor) -> RenderInfo { - let position = self + let mut position = editor.cursor().0.unwrap_or_default(); + if let Some(old_position) = self .position - .get_or_insert_with(|| editor.cursor().0.unwrap_or_default()); + .filter(|old_position| old_position.row == position.row) + { + position = old_position; + } else { + self.position = Some(position); + } let is_menu = self .contents @@ -303,14 +309,6 @@ impl Component for Popup { } = self.render_info(viewport, cx.editor); self.area = area; - let max_offset = child_height.saturating_sub(area.height) as usize; - let half_page_size = (area.height / 2) as usize; - let scroll = max_offset.min(self.scroll_half_pages * half_page_size); - if half_page_size > 0 { - self.scroll_half_pages = scroll / half_page_size; - } - cx.scroll = Some(scroll); - // clear area let background = if is_menu { // TODO: consistently style menu @@ -330,12 +328,19 @@ impl Component for Popup { } let border = usize::from(render_borders); + let max_offset = child_height.saturating_sub(inner.height) as usize; + let half_page_size = (inner.height / 2) as usize; + let scroll = max_offset.min(self.scroll_half_pages * half_page_size); + if half_page_size > 0 { + self.scroll_half_pages = scroll / half_page_size; + } + cx.scroll = Some(scroll); self.contents.render(inner, surface, cx); // render scrollbar if contents do not fit if self.has_scrollbar { - let win_height = (inner.height as usize).saturating_sub(2 * border); - let len = (child_height as usize).saturating_sub(2 * border); + let win_height = inner.height as usize; + let len = child_height as usize; let fits = len <= win_height; let scroll_style = cx.editor.theme.get("ui.menu.scroll"); @@ -350,7 +355,8 @@ impl Component for Popup { let mut cell; for i in 0..win_height { - cell = &mut surface[(inner.right() - 1, inner.top() + (border + i) as u16)]; + cell = + &mut surface[(inner.right() - 1 + border as u16, inner.top() + i as u16)]; let half_block = if render_borders { "▌" } else { "▐" };