forked from pythops/tenere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.rs
99 lines (85 loc) · 2.74 KB
/
ui.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std;
use crate::app::{App, FocusedBlock};
use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
Frame,
};
pub type AppResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
pub fn help_rect(r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage(35),
Constraint::Min(10),
Constraint::Percentage(35),
]
.as_ref(),
)
.split(r);
Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Length((r.width - 80) / 2),
Constraint::Min(80),
Constraint::Length((r.width - 80) / 2),
]
.as_ref(),
)
.split(popup_layout[1])[1]
}
pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage((100 - percent_y) / 2),
Constraint::Percentage(percent_y),
Constraint::Percentage((100 - percent_y) / 2),
]
.as_ref(),
)
.split(r);
Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Percentage((100 - percent_x) / 2),
Constraint::Percentage(percent_x),
Constraint::Percentage((100 - percent_x) / 2),
]
.as_ref(),
)
.split(popup_layout[1])[1]
}
pub fn render(app: &mut App, frame: &mut Frame) {
let frame_size = frame.size();
let prompt_block_height = app.prompt.height(&frame_size) + 3;
let (chat_block, prompt_block) = {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(1), Constraint::Length(prompt_block_height)].as_ref())
.split(frame.size());
(chunks[0], chunks[1])
};
// Chat
app.chat.render(frame, chat_block);
// Prompt
app.prompt.render(frame, prompt_block);
// History
if let FocusedBlock::History | FocusedBlock::Preview = app.focused_block {
let area = centered_rect(80, 80, frame_size);
app.history.render(frame, area, app.focused_block.clone());
}
// Help
if let FocusedBlock::Help = app.focused_block {
app.prompt.update(&FocusedBlock::Help);
let area = help_rect(frame_size);
app.help.render(frame, area);
}
// Notifications
for (index, notification) in app.notifications.iter().enumerate() {
notification.render(index, frame);
}
}