| 1 |
use ratatui::{layout::Rect, widgets::BorderType}; |
| 2 |
use serde::{Deserialize, Serialize}; |
| 3 |
|
| 4 |
use crate::ui::{margin::Margin, style_config::StyleConfig}; |
| 5 |
|
| 6 |
#[derive(Default, Clone, Copy, Deserialize, Serialize)] |
| 7 |
pub enum Position { |
| 8 |
LeftTop, |
| 9 |
Top, |
| 10 |
RightTop, |
| 11 |
|
| 12 |
LeftCenter, |
| 13 |
#[default] |
| 14 |
Center, |
| 15 |
RightCenter, |
| 16 |
|
| 17 |
LeftBottom, |
| 18 |
Bottom, |
| 19 |
RightBottom, |
| 20 |
} |
| 21 |
|
| 22 |
#[derive(Default, Deserialize, Serialize)] |
| 23 |
#[serde(default)] |
| 24 |
pub struct Border { |
| 25 |
pub style_normal: StyleConfig, |
| 26 |
pub style_active: StyleConfig, |
| 27 |
pub type_normal: BorderType, |
| 28 |
pub type_active: BorderType, |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
pub fn anchor_rect( |
| 33 |
area: Rect, |
| 34 |
mut width: u16, |
| 35 |
mut height: u16, |
| 36 |
position: Position, |
| 37 |
margin: Margin, |
| 38 |
) -> Rect { |
| 39 |
|
| 40 |
width = width.min(area.width); |
| 41 |
height = height.min(area.height); |
| 42 |
|
| 43 |
let x = match position { |
| 44 |
Position::LeftTop | Position::LeftCenter | Position::LeftBottom => 0_u16 |
| 45 |
.saturating_add(margin.left) |
| 46 |
.saturating_sub(margin.right), |
| 47 |
|
| 48 |
Position::Top | Position::Center | Position::Bottom => (area.width.saturating_sub(width) |
| 49 |
/ 2) |
| 50 |
.saturating_add(margin.left) |
| 51 |
.saturating_sub(margin.right), |
| 52 |
|
| 53 |
Position::RightTop | Position::RightCenter | Position::RightBottom => area |
| 54 |
.width |
| 55 |
.saturating_sub(width) |
| 56 |
.saturating_add(margin.left) |
| 57 |
.saturating_sub(margin.right), |
| 58 |
}; |
| 59 |
|
| 60 |
let y = match position { |
| 61 |
Position::LeftTop | Position::Top | Position::RightTop => 0_u16 |
| 62 |
.saturating_add(margin.top) |
| 63 |
.saturating_sub(margin.bottom), |
| 64 |
|
| 65 |
Position::LeftCenter | Position::Center | Position::RightCenter => { |
| 66 |
(area.height.saturating_sub(height) / 2) |
| 67 |
.saturating_add(margin.top) |
| 68 |
.saturating_sub(margin.bottom) |
| 69 |
} |
| 70 |
|
| 71 |
Position::LeftBottom | Position::Bottom | Position::RightBottom => area |
| 72 |
.height |
| 73 |
.saturating_sub(height) |
| 74 |
.saturating_add(margin.top) |
| 75 |
.saturating_sub(margin.bottom), |
| 76 |
}; |
| 77 |
|
| 78 |
Rect::new(x, y, width, height) |
| 79 |
} |
| 80 |
|
| 81 |
pub fn fill_rect(area: Rect, margin: Margin) -> Rect { |
| 82 |
Rect::new( |
| 83 |
area.x.saturating_add(margin.left), |
| 84 |
area.y.saturating_add(margin.top), |
| 85 |
area.width |
| 86 |
.saturating_sub(margin.left) |
| 87 |
.saturating_sub(margin.right), |
| 88 |
area.height |
| 89 |
.saturating_sub(margin.top) |
| 90 |
.saturating_sub(margin.bottom), |
| 91 |
) |
| 92 |
} |