| 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 Margin { |
| 29 |
top: u16, |
| 30 |
bottom: u16, |
| 31 |
left: u16, |
| 32 |
right: u16, |
| 33 |
} |
| 34 |
|
| 35 |
#[allow(dead_code)] |
| 36 |
impl Margin { |
| 37 |
pub fn new(value: u16) -> Self { |
| 38 |
Self { |
| 39 |
top: value, |
| 40 |
bottom: value, |
| 41 |
left: value, |
| 42 |
right: value, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
pub fn horizontal(mut self, value: u16) -> Self { |
| 47 |
self.left = value; |
| 48 |
self.right = value; |
| 49 |
self |
| 50 |
} |
| 51 |
|
| 52 |
pub fn vertical(mut self, value: u16) -> Self { |
| 53 |
self.top = value; |
| 54 |
self.bottom = value; |
| 55 |
self |
| 56 |
} |
| 57 |
|
| 58 |
pub fn top(mut self, value: u16) -> Self { |
| 59 |
self.top = value; |
| 60 |
self |
| 61 |
} |
| 62 |
|
| 63 |
pub fn bottom(mut self, value: u16) -> Self { |
| 64 |
self.bottom = value; |
| 65 |
self |
| 66 |
} |
| 67 |
|
| 68 |
pub fn left(mut self, value: u16) -> Self { |
| 69 |
self.left = value; |
| 70 |
self |
| 71 |
} |
| 72 |
|
| 73 |
pub fn right(mut self, value: u16) -> Self { |
| 74 |
self.right = value; |
| 75 |
self |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
pub fn anchor_rect( |
| 81 |
area: Rect, |
| 82 |
mut width: u16, |
| 83 |
mut height: u16, |
| 84 |
position: Position, |
| 85 |
margin: Margin, |
| 86 |
) -> Rect { |
| 87 |
|
| 88 |
width = width.min(area.width); |
| 89 |
height = height.min(area.height); |
| 90 |
|
| 91 |
let x = match position { |
| 92 |
Position::LeftTop | Position::LeftCenter | Position::LeftBottom => 0_u16 |
| 93 |
.saturating_add(margin.left) |
| 94 |
.saturating_sub(margin.right), |
| 95 |
|
| 96 |
Position::Top | Position::Center | Position::Bottom => (area.width.saturating_sub(width) |
| 97 |
/ 2) |
| 98 |
.saturating_add(margin.left) |
| 99 |
.saturating_sub(margin.right), |
| 100 |
|
| 101 |
Position::RightTop | Position::RightCenter | Position::RightBottom => area |
| 102 |
.width |
| 103 |
.saturating_sub(width) |
| 104 |
.saturating_add(margin.left) |
| 105 |
.saturating_sub(margin.right), |
| 106 |
}; |
| 107 |
|
| 108 |
let y = match position { |
| 109 |
Position::LeftTop | Position::Top | Position::RightTop => 0_u16 |
| 110 |
.saturating_add(margin.top) |
| 111 |
.saturating_sub(margin.bottom), |
| 112 |
|
| 113 |
Position::LeftCenter | Position::Center | Position::RightCenter => { |
| 114 |
(area.height.saturating_sub(height) / 2) |
| 115 |
.saturating_add(margin.top) |
| 116 |
.saturating_sub(margin.bottom) |
| 117 |
} |
| 118 |
|
| 119 |
Position::LeftBottom | Position::Bottom | Position::RightBottom => area |
| 120 |
.height |
| 121 |
.saturating_sub(height) |
| 122 |
.saturating_add(margin.top) |
| 123 |
.saturating_sub(margin.bottom), |
| 124 |
}; |
| 125 |
|
| 126 |
Rect::new(x, y, width, height) |
| 127 |
} |
| 128 |
|
| 129 |
pub fn fill_rect(area: Rect, margin: Margin) -> Rect { |
| 130 |
Rect::new( |
| 131 |
area.x.saturating_add(margin.left), |
| 132 |
area.y.saturating_add(margin.top), |
| 133 |
area.width |
| 134 |
.saturating_sub(margin.left) |
| 135 |
.saturating_sub(margin.right), |
| 136 |
area.height |
| 137 |
.saturating_sub(margin.top) |
| 138 |
.saturating_sub(margin.bottom), |
| 139 |
) |
| 140 |
} |