use ratatui::layout::Rect;
#[allow(unused)]
pub trait RectExt {
fn x_margin(&self, margin: u16) -> Rect;
fn x_h_margin(&self, h_margin: u16) -> Rect;
#[allow(unused)]
fn x_v_margin(&self, v_margin: u16) -> Rect;
fn x_move_down(&self, y: u16) -> Rect;
fn x_shrink_from_top(&self, height_to_remove: u16) -> Rect;
fn x_shrink_left(&self, width: u16) -> Rect;
fn x_row(&self, row_num: u16) -> Rect;
fn x_top_right(&self, width: u16, height: u16) -> Rect;
fn x_bottom_right(&self, width: u16, height: u16) -> Rect;
fn x_with_x(&self, x: u16) -> Rect;
fn x_with_y(&self, y: u16) -> Rect;
fn x_width(&self, width: u16) -> Rect;
fn x_height(&self, height: u16) -> Rect;
}
impl RectExt for Rect {
fn x_margin(&self, margin: u16) -> Rect {
let x = (self.x + margin).min(self.x + self.width);
let y = (self.y + margin).min(self.y + self.height);
let width = self.width.saturating_sub(2 * margin);
let height = self.height.saturating_sub(2 * margin);
Rect { x, y, width, height }
}
fn x_h_margin(&self, h_margin: u16) -> Rect {
let x = (self.x + h_margin).min(self.x + self.width);
let width = self.width.saturating_sub(2 * h_margin);
Rect {
x,
y: self.y,
width,
height: self.height,
}
}
fn x_v_margin(&self, v_margin: u16) -> Rect {
let y = (self.y + v_margin).min(self.y + self.height);
let height = self.height.saturating_sub(2 * v_margin);
Rect {
x: self.x,
y,
width: self.width,
height,
}
}
fn x_shrink_from_top(&self, height_to_remove: u16) -> Rect {
let new_height = self.height.saturating_sub(height_to_remove);
Rect {
x: self.x,
y: self.y + height_to_remove,
width: self.width,
height: new_height,
}
}
fn x_shrink_left(&self, width: u16) -> Rect {
let new_width = self.width.saturating_sub(width);
let x = self.x + width;
Rect {
x,
y: self.y,
width: new_width,
height: self.height,
}
}
fn x_move_down(&self, y_offset: u16) -> Rect {
Rect {
x: self.x,
y: self.y + y_offset,
width: self.width,
height: self.height,
}
}
fn x_row(&self, row_num: u16) -> Rect {
Rect {
x: self.x,
y: self.y + row_num - 1,
width: self.width,
height: 1.min(self.height),
}
}
fn x_bottom_right(&self, width: u16, height: u16) -> Rect {
Rect {
x: self.x + self.width - width,
y: self.y + self.height - height,
width,
height,
}
}
fn x_top_right(&self, width: u16, height: u16) -> Rect {
Rect {
x: self.x + self.width - width,
y: self.y,
width,
height,
}
}
fn x_with_x(&self, x: u16) -> Rect {
Rect {
x,
y: self.y,
width: self.width,
height: self.height,
}
}
fn x_with_y(&self, y: u16) -> Rect {
Rect {
x: self.x,
y,
width: self.width,
height: self.height,
}
}
fn x_width(&self, width: u16) -> Rect {
Rect {
x: self.x,
y: self.y,
width,
height: self.height,
}
}
fn x_height(&self, height: u16) -> Rect {
Rect {
x: self.x,
y: self.y,
width: self.width,
height,
}
}
}