diff options
author | extrawurst <mail@rusticorn.com> | 2022-12-18 20:07:18 +0100 |
---|---|---|
committer | extrawurst <mail@rusticorn.com> | 2022-12-18 20:07:26 +0100 |
commit | 9ca6068a177d314524a50e7dba81a954723398b5 (patch) | |
tree | 33f70fc3470488d2c27b0e548b4495e56e28547a /src/ui | |
parent | b59526c86c3faaac29716fe472f322c134a52305 (diff) |
fix crash in small window and branches
fixes #1470
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/mod.rs | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 8f11dc9b..72b74cfa 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -87,8 +87,18 @@ pub fn centered_rect( /// makes sure Rect `r` at least stays as big as min and not bigger than max pub fn rect_inside(min: Size, max: Size, r: Rect) -> Rect { - let new_width = r.width.clamp(min.width, max.width); - let new_height = r.height.clamp(min.height, max.height); + let new_width = if min.width > max.width { + max.width + } else { + r.width.clamp(min.width, max.width) + }; + + let new_height = if min.height > max.height { + max.width + } else { + r.height.clamp(min.height, max.height) + }; + let diff_width = new_width.saturating_sub(r.width); let diff_height = new_height.saturating_sub(r.height); @@ -142,3 +152,40 @@ pub fn common_nav( None } } + +#[cfg(test)] +mod test { + use super::{rect_inside, Size}; + use pretty_assertions::assert_eq; + use tui::layout::Rect; + + #[test] + fn test_small_rect_in_rect() { + let rect = rect_inside( + Size { + width: 2, + height: 2, + }, + Size { + width: 1, + height: 1, + }, + Rect { + x: 0, + y: 0, + width: 10, + height: 10, + }, + ); + + assert_eq!( + rect, + Rect { + x: 0, + y: 0, + width: 1, + height: 1 + } + ); + } +} |