1use ratatui::layout::{Constraint, Direction, Layout, Rect};
2pub struct DefaultPage {
3 pub component: Rect,
4 pub context: Rect,
5 pub shortcuts: Rect,
6}
7
8pub fn default_page(area: Rect, context_h: u16, shortcuts_h: u16) -> DefaultPage {
9 let margin_h = 1u16;
10 let component_h = area
11 .height
12 .saturating_sub(context_h.saturating_add(shortcuts_h));
13
14 let layout = Layout::default()
15 .direction(Direction::Vertical)
16 .horizontal_margin(margin_h)
17 .constraints(
18 [
19 Constraint::Length(component_h),
20 Constraint::Length(context_h),
21 Constraint::Length(shortcuts_h),
22 ]
23 .as_ref(),
24 )
25 .split(area);
26
27 DefaultPage {
28 component: layout[0],
29 context: layout[1],
30 shortcuts: layout[2],
31 }
32}
33
34pub fn centered_rect(r: Rect, percent_x: u16, percent_y: u16) -> Rect {
35 let layout = Layout::default()
36 .direction(Direction::Vertical)
37 .constraints([
38 Constraint::Percentage((100 - percent_y) / 2),
39 Constraint::Percentage(percent_y),
40 Constraint::Percentage((100 - percent_y) / 2),
41 ])
42 .split(r);
43
44 Layout::default()
45 .direction(Direction::Horizontal)
46 .constraints([
47 Constraint::Percentage((100 - percent_x) / 2),
48 Constraint::Percentage(percent_x),
49 Constraint::Percentage((100 - percent_x) / 2),
50 ])
51 .split(layout[1])[1]
52}
53
54pub fn fill() -> Layout {
55 Layout::vertical([Constraint::Fill(1)].to_vec())
56}
57
58#[derive(Default)]
59pub struct Spacing(u16);
60
61impl From<u16> for Spacing {
62 fn from(value: u16) -> Self {
63 Self(value)
64 }
65}
66
67impl From<Spacing> for u16 {
68 fn from(spacing: Spacing) -> Self {
69 spacing.0
70 }
71}