fix: panic on small terminal when textinput for stash name is open

This commit is contained in:
Arnaud Locquet 2020-10-27 17:40:42 +01:00 committed by Stephan Dilly
parent a75f43de77
commit 603fd75703
3 changed files with 35 additions and 10 deletions

View File

@ -23,6 +23,7 @@ use tui::{
Frame, Frame,
}; };
use crate::ui::Size;
use anyhow::Result; use anyhow::Result;
use ui::style::SharedTheme; use ui::style::SharedTheme;
@ -43,15 +44,16 @@ impl DrawableComponent for SelectBranchComponent {
rect: Rect, rect: Rect,
) -> Result<()> { ) -> Result<()> {
if self.visible { if self.visible {
const PERCENT_SIZE: (u16, u16) = (60, 25); const PERCENT_SIZE: Size = Size::new(60, 25);
const MIN_SIZE: (u16, u16) = (50, 20); const MIN_SIZE: Size = Size::new(50, 20);
let area = ui::centered_rect( let area = ui::centered_rect(
PERCENT_SIZE.0, PERCENT_SIZE.width,
PERCENT_SIZE.1, PERCENT_SIZE.height,
f.size(), 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 area = area.intersection(rect);
let scroll_threshold = area.height / 3; let scroll_threshold = area.height / 3;

View File

@ -1,3 +1,4 @@
use crate::ui::Size;
use crate::{ use crate::{
components::{ components::{
popup_paragraph, visibility_blocking, CommandBlocking, popup_paragraph, visibility_blocking, CommandBlocking,
@ -200,7 +201,11 @@ impl DrawableComponent for TextInputComponent {
let area = match self.input_type { let area = match self.input_type {
InputType::Multiline => { InputType::Multiline => {
let area = ui::centered_rect(60, 20, f.size()); 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()), _ => ui::centered_rect_absolute(32, 3, f.size()),
}; };

View File

@ -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<Size> for Rect {
fn into(self) -> Size {
Size::new(self.width, self.height)
}
}
/// use layouts to create a rects that /// use layouts to create a rects that
/// centers inside `r` and sizes `percent_x`/`percent_x` of `r` /// centers inside `r` and sizes `percent_x`/`percent_x` of `r`
pub fn centered_rect( pub fn centered_rect(
@ -53,10 +71,10 @@ pub fn centered_rect(
.split(popup_layout[1])[1] .split(popup_layout[1])[1]
} }
/// makes sure Rect `r` at least stays as big as `width` & `height` /// makes sure Rect `r` at least stays as big as min and not bigger than max
pub fn rect_min(width: u16, height: u16, r: Rect) -> Rect { pub fn rect_inside(min: &Size, max: &Size, r: Rect) -> Rect {
let new_width = r.width.max(width); let new_width = r.width.max(min.width).min(max.width);
let new_height = r.height.max(height); let new_height = r.height.max(min.height).min(max.height);
let diff_width = new_width.saturating_sub(r.width); let diff_width = new_width.saturating_sub(r.width);
let diff_height = new_height.saturating_sub(r.height); let diff_height = new_height.saturating_sub(r.height);