diff --git a/src/components/select_branch.rs b/src/components/select_branch.rs index 4cc404b5..f73558b5 100644 --- a/src/components/select_branch.rs +++ b/src/components/select_branch.rs @@ -23,6 +23,7 @@ use tui::{ Frame, }; +use crate::ui::Size; use anyhow::Result; use ui::style::SharedTheme; @@ -43,15 +44,16 @@ impl DrawableComponent for SelectBranchComponent { rect: Rect, ) -> Result<()> { if self.visible { - const PERCENT_SIZE: (u16, u16) = (60, 25); - const MIN_SIZE: (u16, u16) = (50, 20); + const PERCENT_SIZE: Size = Size::new(60, 25); + const MIN_SIZE: Size = Size::new(50, 20); let area = ui::centered_rect( - PERCENT_SIZE.0, - PERCENT_SIZE.1, + PERCENT_SIZE.width, + PERCENT_SIZE.height, f.size(), ); - let area = ui::rect_min(MIN_SIZE.0, MIN_SIZE.1, area); + let area = + ui::rect_inside(&MIN_SIZE, &f.size().into(), area); let area = area.intersection(rect); let scroll_threshold = area.height / 3; diff --git a/src/components/textinput.rs b/src/components/textinput.rs index 267c9578..c965e826 100644 --- a/src/components/textinput.rs +++ b/src/components/textinput.rs @@ -1,3 +1,4 @@ +use crate::ui::Size; use crate::{ components::{ popup_paragraph, visibility_blocking, CommandBlocking, @@ -200,7 +201,11 @@ impl DrawableComponent for TextInputComponent { let area = match self.input_type { InputType::Multiline => { let area = ui::centered_rect(60, 20, f.size()); - ui::rect_min(10, 3, area) + ui::rect_inside( + &Size::new(10, 3), + &f.size().into(), + area, + ) } _ => ui::centered_rect_absolute(32, 3, f.size()), }; diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 606dad98..3cba131e 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -21,6 +21,24 @@ pub const fn calc_scroll_top( } } +/// ui component size representation +pub struct Size { + pub width: u16, + pub height: u16, +} + +impl Size { + pub const fn new(width: u16, height: u16) -> Self { + Self { width, height } + } +} + +impl Into for Rect { + fn into(self) -> Size { + Size::new(self.width, self.height) + } +} + /// use layouts to create a rects that /// centers inside `r` and sizes `percent_x`/`percent_x` of `r` pub fn centered_rect( @@ -53,10 +71,10 @@ pub fn centered_rect( .split(popup_layout[1])[1] } -/// makes sure Rect `r` at least stays as big as `width` & `height` -pub fn rect_min(width: u16, height: u16, r: Rect) -> Rect { - let new_width = r.width.max(width); - let new_height = r.height.max(height); +/// 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.max(min.width).min(max.width); + let new_height = r.height.max(min.height).min(max.height); let diff_width = new_width.saturating_sub(r.width); let diff_height = new_height.saturating_sub(r.height);