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