Apply Resize's max size to available size too

I'm not sure if the input max size and the output max size should be
separate, and I'm not sure whether the min size should also have an
effect on the input. For now, this works well enough, but I may need to
adjust it in the future as I stumble across new edge cases.

This change was made because I was using Resize as a way to set the size
of widgets containing text that were rendered inside Predraw widgets.
After this change, setting a max_width but no max_height has the desired
effect of making the inner widgets perform word wrapping. The resulting
Predrawn is then as high as it needs to be to contain the wrapped text.
This commit is contained in:
Joscha 2023-04-17 16:48:12 +02:00
parent 59710c8162
commit 57788a9dd9

View file

@ -42,6 +42,20 @@ impl<I> Resize<I> {
self
}
fn presize(
&self,
mut width: Option<u16>,
mut height: Option<u16>,
) -> (Option<u16>, Option<u16>) {
if let Some(mw) = self.max_width {
width = Some(width.unwrap_or(mw).min(mw));
}
if let Some(mh) = self.max_height {
height = Some(height.unwrap_or(mh).max(mh));
}
(width, height)
}
fn resize(&self, size: Size) -> Size {
let mut width = size.width;
let mut height = size.height;
@ -74,6 +88,7 @@ where
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
let (max_width, max_height) = self.presize(max_width, max_height);
let size = self.inner.size(widthdb, max_width, max_height)?;
Ok(self.resize(size))
}
@ -94,6 +109,7 @@ where
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
let (max_width, max_height) = self.presize(max_width, max_height);
let size = self.inner.size(widthdb, max_width, max_height).await?;
Ok(self.resize(size))
}