From 4b8bcd2773459f663aa1f641dd6ff49461e04542 Mon Sep 17 00:00:00 2001 From: "Ben Fekih, Hichem" Date: Fri, 19 Apr 2024 20:54:09 +0200 Subject: [PATCH] popup: call required_size only once while rendering to speed up the rendering a little Signed-off-by: Ben Fekih, Hichem --- helix-term/src/ui/popup.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index d1d7d6c50..8d436fda9 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -118,13 +118,22 @@ impl Popup { } pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect { - // trigger required_size so we recalculate if the child changed - let (width, height) = self + let child_size = self + .contents .required_size((viewport.width, viewport.height)) .expect("Component needs required_size implemented in order to be embedded in a popup"); - let width = width.min(viewport.width); - let height = height.min(viewport.height.saturating_sub(2)); // add some spacing in the viewport + self.area_internal(viewport, editor, child_size) + } + + pub fn area_internal( + &mut self, + viewport: Rect, + editor: &Editor, + child_size: (u16, u16), + ) -> Rect { + let width = child_size.0.min(viewport.width); + let height = child_size.1.min(viewport.height.saturating_sub(2)); // add some spacing in the viewport let position = self .position @@ -270,14 +279,14 @@ 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; - let child_size = self .contents - .required_size((area.width, area.height)) + .required_size((viewport.width, viewport.height)) .expect("Component needs required_size implemented in order to be embedded in a popup"); + let area = self.area_internal(viewport, cx.editor, child_size); + self.area = area; + let max_offset = child_size.1.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);