| 1 | use ratatui::layout::Rect; |
| 2 | |
| 3 | pub mod help; |
| 4 | pub mod input; |
| 5 | pub mod list; |
| 6 | pub mod popup; |
| 7 | pub mod table; |
| 8 | pub mod toast; |
| 9 | |
| 10 | #[allow(dead_code)] |
| 11 | #[derive(Default)] |
| 12 | pub enum Position { |
| 13 | #[default] |
| 14 | LeftTop, |
| 15 | Top, |
| 16 | RightTop, |
| 17 | |
| 18 | LeftCenter, |
| 19 | Center, |
| 20 | RightCenter, |
| 21 | |
| 22 | LeftBottom, |
| 23 | Bottom, |
| 24 | RightBottom, |
| 25 | } |
| 26 | |
| 27 | #[derive(Default)] |
| 28 | pub struct Gaps { |
| 29 | top: u16, |
| 30 | bottom: u16, |
| 31 | left: u16, |
| 32 | right: u16, |
| 33 | } |
| 34 | |
| 35 | // calculate area position |
| 36 | pub fn position_rect( |
| 37 | area: Rect, |
| 38 | mut width: u16, |
| 39 | mut height: u16, |
| 40 | position: Position, |
| 41 | gaps: Gaps, |
| 42 | ) -> Rect { |
| 43 | // Make sure that it does not pass the area width and height to prevent crash. |
| 44 | width = width.min(area.width); |
| 45 | height = height.min(area.height); |
| 46 | |
| 47 | let x = match position { |
| 48 | Position::LeftTop | Position::LeftCenter | Position::LeftBottom => { |
| 49 | 0_u16.saturating_add(gaps.left).saturating_sub(gaps.right) |
| 50 | } |
| 51 | |
| 52 | Position::Top | Position::Center | Position::Bottom => (area.width.saturating_sub(width) |
| 53 | / 2) |
| 54 | .saturating_add(gaps.left) |
| 55 | .saturating_sub(gaps.right), |
| 56 | |
| 57 | Position::RightTop | Position::RightCenter | Position::RightBottom => area |
| 58 | .width |
| 59 | .saturating_sub(width) |
| 60 | .saturating_add(gaps.left) |
| 61 | .saturating_sub(gaps.right), |
| 62 | }; |
| 63 | |
| 64 | let y = match position { |
| 65 | Position::LeftTop | Position::Top | Position::RightTop => { |
| 66 | 0_u16.saturating_add(gaps.top).saturating_sub(gaps.bottom) |
| 67 | } |
| 68 | |
| 69 | Position::LeftCenter | Position::Center | Position::RightCenter => { |
| 70 | (area.height.saturating_sub(height) / 2) |
| 71 | .saturating_add(gaps.top) |
| 72 | .saturating_sub(gaps.bottom) |
| 73 | } |
| 74 | |
| 75 | Position::LeftBottom | Position::Bottom | Position::RightBottom => area |
| 76 | .height |
| 77 | .saturating_sub(height) |
| 78 | .saturating_add(gaps.top) |
| 79 | .saturating_sub(gaps.bottom), |
| 80 | }; |
| 81 | |
| 82 | Rect::new(x, y, width, height) |
| 83 | } |
