envelope_cli/tui/
layout.rs1use ratatui::layout::{Constraint, Direction, Layout, Rect};
6
7pub struct AppLayout {
9 pub sidebar: Rect,
11 pub main: Rect,
13 pub status_bar: Rect,
15}
16
17impl AppLayout {
18 pub fn new(area: Rect) -> Self {
20 let vertical = Layout::default()
22 .direction(Direction::Vertical)
23 .constraints([
24 Constraint::Min(3), Constraint::Length(1), ])
27 .split(area);
28
29 let horizontal = Layout::default()
31 .direction(Direction::Horizontal)
32 .constraints([
33 Constraint::Length(30), Constraint::Min(40), ])
36 .split(vertical[0]);
37
38 Self {
39 sidebar: horizontal[0],
40 main: horizontal[1],
41 status_bar: vertical[1],
42 }
43 }
44}
45
46pub struct SidebarLayout {
48 pub header: Rect,
50 pub accounts: Rect,
52 pub view_switcher: Rect,
54}
55
56impl SidebarLayout {
57 pub fn new(area: Rect) -> Self {
59 let chunks = Layout::default()
60 .direction(Direction::Vertical)
61 .constraints([
62 Constraint::Length(3), Constraint::Min(5), Constraint::Length(5), ])
66 .split(area);
67
68 Self {
69 header: chunks[0],
70 accounts: chunks[1],
71 view_switcher: chunks[2],
72 }
73 }
74}
75
76pub struct MainPanelLayout {
78 pub header: Rect,
80 pub content: Rect,
82}
83
84impl MainPanelLayout {
85 pub fn new(area: Rect) -> Self {
87 let chunks = Layout::default()
88 .direction(Direction::Vertical)
89 .constraints([
90 Constraint::Length(3), Constraint::Min(3), ])
93 .split(area);
94
95 Self {
96 header: chunks[0],
97 content: chunks[1],
98 }
99 }
100}
101
102pub struct BudgetLayout {
104 pub atb_header: Rect,
106 pub categories: Rect,
108}
109
110impl BudgetLayout {
111 pub fn new(area: Rect) -> Self {
113 let chunks = Layout::default()
114 .direction(Direction::Vertical)
115 .constraints([
116 Constraint::Length(3), Constraint::Min(3), ])
119 .split(area);
120
121 Self {
122 atb_header: chunks[0],
123 categories: chunks[1],
124 }
125 }
126}
127
128pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
130 let popup_layout = Layout::default()
131 .direction(Direction::Vertical)
132 .constraints([
133 Constraint::Percentage((100 - percent_y) / 2),
134 Constraint::Percentage(percent_y),
135 Constraint::Percentage((100 - percent_y) / 2),
136 ])
137 .split(r);
138
139 Layout::default()
140 .direction(Direction::Horizontal)
141 .constraints([
142 Constraint::Percentage((100 - percent_x) / 2),
143 Constraint::Percentage(percent_x),
144 Constraint::Percentage((100 - percent_x) / 2),
145 ])
146 .split(popup_layout[1])[1]
147}
148
149pub fn centered_rect_fixed(width: u16, height: u16, r: Rect) -> Rect {
151 let x = r.x + (r.width.saturating_sub(width)) / 2;
152 let y = r.y + (r.height.saturating_sub(height)) / 2;
153 Rect::new(x, y, width.min(r.width), height.min(r.height))
154}