mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-21 20:23:11 +00:00
fix(rect): prevent overflow in inner() and area() (#523)
This commit is contained in:
parent
c9b8e7cf41
commit
21303f2167
1 changed files with 9 additions and 6 deletions
|
@ -206,7 +206,7 @@ impl Rect {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn area(self) -> u16 {
|
pub const fn area(self) -> u16 {
|
||||||
self.width * self.height
|
self.width.saturating_mul(self.height)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn left(self) -> u16 {
|
pub const fn left(self) -> u16 {
|
||||||
|
@ -226,14 +226,17 @@ impl Rect {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn inner(self, margin: &Margin) -> Rect {
|
pub fn inner(self, margin: &Margin) -> Rect {
|
||||||
if self.width < 2 * margin.horizontal || self.height < 2 * margin.vertical {
|
let doubled_margin_horizontal = margin.horizontal.saturating_mul(2);
|
||||||
|
let doubled_margin_vertical = margin.vertical.saturating_mul(2);
|
||||||
|
|
||||||
|
if self.width < doubled_margin_horizontal || self.height < doubled_margin_vertical {
|
||||||
Rect::default()
|
Rect::default()
|
||||||
} else {
|
} else {
|
||||||
Rect {
|
Rect {
|
||||||
x: self.x + margin.horizontal,
|
x: self.x.saturating_add(margin.horizontal),
|
||||||
y: self.y + margin.vertical,
|
y: self.y.saturating_add(margin.vertical),
|
||||||
width: self.width - 2 * margin.horizontal,
|
width: self.width.saturating_sub(doubled_margin_horizontal),
|
||||||
height: self.height - 2 * margin.vertical,
|
height: self.height.saturating_sub(doubled_margin_vertical),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue