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.
This commit is contained in:
Joscha 2023-04-14 01:51:35 +02:00
parent 810524325e
commit d0b3b9edd4

View file

@ -11,27 +11,32 @@ pub struct Predrawn {
} }
impl Predrawn { impl Predrawn {
pub fn new<E, W: Widget<E>>(inner: W, frame: &mut Frame) -> Result<Self, E> { pub fn new<E, W: Widget<E>>(inner: W, widthdb: &mut WidthDb) -> Result<Self, E> {
let mut tmp_frame = Frame::default(); 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)?; inner.draw(&mut tmp_frame)?;
mem::swap(widthdb, &mut tmp_frame.widthdb);
mem::swap(&mut frame.widthdb, &mut tmp_frame.widthdb);
let buffer = tmp_frame.buffer; let buffer = tmp_frame.buffer;
Ok(Self { buffer }) Ok(Self { buffer })
} }
pub async fn new_async<E, W: AsyncWidget<E>>(inner: W, frame: &mut Frame) -> Result<Self, E> { pub async fn new_async<E, W: AsyncWidget<E>>(
inner: W,
widthdb: &mut WidthDb,
) -> Result<Self, E> {
let mut tmp_frame = Frame::default(); 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?; inner.draw(&mut tmp_frame).await?;
mem::swap(widthdb, &mut tmp_frame.widthdb);
mem::swap(&mut frame.widthdb, &mut tmp_frame.widthdb);
let buffer = tmp_frame.buffer; let buffer = tmp_frame.buffer;
Ok(Self { buffer }) Ok(Self { buffer })