index : nmrs-tui
A fast, Vim-friendly NetworkManager TUI built in Rust.
about summary refs log tree commit diff
nmrs-tui / src / ui / popup.rs
-rw-r--r-- blob 73b11ec08905f059f60c284e5c4528c6364ee160 3005 bytes (plain)
1
use nmrs::{Network, WifiSecurity};
2
use ratatui::{
3
    Frame,
4
    layout::{self, Rect},
5
    style::{Color, Style, Stylize},
6
    widgets::{Block, Paragraph},
7
};
8
9
use crate::{
10
    tui::Selected,
11
    ui::{self, Gaps, Position, Urgency, input::Input},
12
};
13
14
#[derive(Default)]
15
pub struct Options {
16
    /// width of the popup.
17
    pub width: u16,
18
19
    /// height of the popup.
20
    pub height: u16,
21
22
    /// position of the popup.
23
    pub position: Position,
24
25
    /// gaps of the popup.
26
    pub gaps: Option<Gaps>,
27
28
    /// dim the background.
29
    pub dim: Option<Color>,
30
}
31
32
pub fn popup_area(f: &mut Frame, opt: Options) -> Rect {
33
    if opt.dim.is_some() {
34
        // Dim the background.
35
        let dimming_block = ratatui::widgets::Block::default().bg(opt.dim.unwrap_or_default());
36
        f.render_widget(dimming_block, f.area());
37
    }
38
39
    let area = ui::position_rect(f.area(), opt.width, opt.height, opt.position, opt.gaps);
40
    f.render_widget(ratatui::widgets::Clear, area); // Clear the table behind it
41
    area
42
}
43
44
// TODO: draw toast
45
pub fn draw_toast(f: &mut Frame, msg: &str, urgency: Urgency) {
46
    let area = popup_area(
47
        f,
48
        Options {
49
            width: 30,
50
            height: 3,
51
            position: Position::RightBottom,
52
            gaps: Some(Gaps {
53
                bottom: 1,
54
                right: 1,
55
                ..Default::default()
56
            }),
57
            ..Default::default()
58
        },
59
    );
60
61
    let border_style = match urgency {
62
        Urgency::Success => Style::new().green(),
63
        Urgency::Warning => Style::new().yellow(),
64
        Urgency::Critical => Style::new().red(),
65
    };
66
    let block = Block::bordered().border_style(border_style);
67
    f.render_widget(Paragraph::new(msg).block(block), area);
68
}
69
70
pub fn draw_auth(f: &mut Frame, input: &Input, selected: &Option<Selected> ) {
71
    let input_area = popup_area(
72
        f,
73
        Options {
74
            width: 30,
75
            height: 3,
76
            position: Position::Center,
77
            dim: Some(Color::Black),
78
            ..Default::default()
79
        },
80
    );
81
82
83
    if let Some(Selected::Network(net)) = selected {
84
        let block = Block::bordered()
85
            .title(format!(" Password for {} ", net.ssid))
86
            .border_style(Style::default().fg(Color::Yellow));
87
88
        // Hide password with asterisks for security
89
        let display_pass: String = input.value.chars().map(|_| '*').collect();
90
        let text = Paragraph::new(display_pass).block(block);
91
        f.render_widget(text, input_area);
92
        f.set_cursor_position(layout::Position::new(
93
            // Draw the cursor at the current position in the input field.
94
            // Add 1 to position so it dont be on the border, min is forest the cursor can go mines
95
            // the borders
96
            (input_area.x + input.cx as u16 + 1).min(input_area.x + input_area.width.saturating_sub(2)),
97
            // Move one line down, from the border to the input line
98
            input_area.y + 1,
99
        ))
100
    }
101
}