From d0b3b9edd43d2c7a3a5bc199c8986df5a5882590 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 14 Apr 2023 01:51:35 +0200 Subject: [PATCH] Fix Predrawn size calculations Previously, Predrawn would use its parent frame's size. Now, it uses the size requested by the widget. Because of this, it no longer requires a full &mut Frame, but only a &mut WidthDb. To set a maximum size, the widget can be wrapped inside a Resize. --- src/widgets/predrawn.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/widgets/predrawn.rs b/src/widgets/predrawn.rs index e4f594a..f38166d 100644 --- a/src/widgets/predrawn.rs +++ b/src/widgets/predrawn.rs @@ -11,27 +11,32 @@ pub struct Predrawn { } impl Predrawn { - pub fn new>(inner: W, frame: &mut Frame) -> Result { + pub fn new>(inner: W, widthdb: &mut WidthDb) -> Result { let mut tmp_frame = Frame::default(); - tmp_frame.buffer.resize(frame.size()); - mem::swap(&mut frame.widthdb, &mut tmp_frame.widthdb); + let size = inner.size(widthdb, None, None)?; + tmp_frame.buffer.resize(size); + + mem::swap(widthdb, &mut tmp_frame.widthdb); inner.draw(&mut tmp_frame)?; - - mem::swap(&mut frame.widthdb, &mut tmp_frame.widthdb); + mem::swap(widthdb, &mut tmp_frame.widthdb); let buffer = tmp_frame.buffer; Ok(Self { buffer }) } - pub async fn new_async>(inner: W, frame: &mut Frame) -> Result { + pub async fn new_async>( + inner: W, + widthdb: &mut WidthDb, + ) -> Result { let mut tmp_frame = Frame::default(); - tmp_frame.buffer.resize(frame.size()); - mem::swap(&mut frame.widthdb, &mut tmp_frame.widthdb); + let size = inner.size(widthdb, None, None).await?; + tmp_frame.buffer.resize(size); + + mem::swap(widthdb, &mut tmp_frame.widthdb); inner.draw(&mut tmp_frame).await?; - - mem::swap(&mut frame.widthdb, &mut tmp_frame.widthdb); + mem::swap(widthdb, &mut tmp_frame.widthdb); let buffer = tmp_frame.buffer; Ok(Self { buffer })