popup: call required_size only once while rendering

to speed up the rendering a little

Signed-off-by: Ben Fekih, Hichem <hichem.f@live.de>
This commit is contained in:
Ben Fekih, Hichem 2024-04-19 20:54:09 +02:00 committed by Michael Davis
parent af4ff80524
commit 4b8bcd2773

View file

@ -118,13 +118,22 @@ impl<T: Component> Popup<T> {
} }
pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect { pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect {
// trigger required_size so we recalculate if the child changed let child_size = self
let (width, height) = self .contents
.required_size((viewport.width, viewport.height)) .required_size((viewport.width, viewport.height))
.expect("Component needs required_size implemented in order to be embedded in a popup"); .expect("Component needs required_size implemented in order to be embedded in a popup");
let width = width.min(viewport.width); self.area_internal(viewport, editor, child_size)
let height = height.min(viewport.height.saturating_sub(2)); // add some spacing in the viewport }
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 let position = self
.position .position
@ -270,14 +279,14 @@ impl<T: Component> Component for Popup<T> {
} }
fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) { 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 let child_size = self
.contents .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"); .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 max_offset = child_size.1.saturating_sub(area.height) as usize;
let half_page_size = (area.height / 2) as usize; let half_page_size = (area.height / 2) as usize;
let scroll = max_offset.min(self.scroll_half_pages * half_page_size); let scroll = max_offset.min(self.scroll_half_pages * half_page_size);