tui_components/
rect_ext.rs

1use tui::layout::Rect;
2
3pub trait RectExt {
4    /// Calculates a rectangle centered on `self`, with the same size as `other`.
5    ///
6    /// Width and height of other rectangle are truncated to that of `self`
7    fn centered(self, other: Rect) -> Rect;
8
9    /// Calculates a rectangle with a new height and width
10    fn scaled(self, x_scale: f64, y_scale: f64) -> Rect;
11}
12
13impl RectExt for Rect {
14    fn centered(self, other: Rect) -> Rect {
15        let width = self.width.min(other.width);
16        let height = self.height.min(other.height);
17        Rect {
18            x: self.x + ((self.width - width) as f64 / 2.0) as u16,
19            y: self.y + ((self.height - height) as f64 / 2.0) as u16,
20            width,
21            height,
22        }
23    }
24
25    fn scaled(self, x_scale: f64, y_scale: f64) -> Rect {
26        Rect {
27            x: self.x,
28            y: self.y,
29            width: (self.width as f64 * x_scale) as u16,
30            height: (self.height as f64 * y_scale) as u16,
31        }
32    }
33}