From 57788a9dd9688bdf3e59bf7366ba6276fc660715 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 17 Apr 2023 16:48:12 +0200 Subject: [PATCH] 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. --- src/widgets/resize.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/widgets/resize.rs b/src/widgets/resize.rs index 5a92e05..81e30b5 100644 --- a/src/widgets/resize.rs +++ b/src/widgets/resize.rs @@ -42,6 +42,20 @@ impl Resize { self } + fn presize( + &self, + mut width: Option, + mut height: Option, + ) -> (Option, Option) { + 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, max_height: Option, ) -> Result { + 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, max_height: Option, ) -> Result { + 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)) }