terminal_ui/ui/
ui_popup.rs

1use tui::layout::{Layout, Rect, Constraint, Direction};
2
3/// helper function to create a centered rect using up certain percentage of the available rect `r`
4pub fn centered_rect(percent_x: u16, y_size: u16, r: Rect) -> Rect {
5    let popup_layout = Layout::default()
6        .direction(Direction::Vertical)
7        .constraints(
8            [
9                Constraint::Length(i32::max((r.height as i32 - y_size as i32) / 2, 0) as u16),
10                Constraint::Length(y_size),
11                Constraint::Length(i32::max((r.height as i32 - y_size as i32) / 2, 0) as u16),
12            ]
13            .as_ref(),
14        )
15        .split(r);
16
17    Layout::default()
18        .direction(Direction::Horizontal)
19        .constraints(
20            [
21                Constraint::Percentage((100 - percent_x) / 2),
22                Constraint::Percentage(percent_x),
23                Constraint::Percentage((100 - percent_x) / 2),
24            ]
25            .as_ref(),
26        )
27        .split(popup_layout[1])[1]
28}