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 0df0502f497f981e39808e914564656e6b9e120d 2434 bytes (plain)
1
use nmrs::Network;
2
use ratatui::{
3
    Frame,
4
    layout::{self, Alignment, Constraint, Layout, Rect},
5
    style::Style,
6
    text::{Line, Span},
7
    widgets::{self, Block, BorderType, Paragraph},
8
};
9
10
use crate::ui::{self, Margin, Position, input::Input};
11
12
#[derive(Default)]
13
pub struct Options {
14
    pub width: u16,
15
    pub height: u16,
16
    pub position: Position,
17
    pub margin: Margin,
18
}
19
20
pub fn popup_rect(f: &mut Frame, opt: Options) -> Rect {
21
    let area = ui::anchor_rect(f.area(), opt.width, opt.height, opt.position, opt.margin);
22
    f.render_widget(widgets::Clear, area); // Clear the area behind it
23
    area
24
}
25
26
// TODO: padding.
27
pub fn draw_auth(f: &mut Frame, input: &Input, network: Network) {
28
    let popup_rect = popup_rect(
29
        f,
30
        Options {
31
            width: 60,
32
            height: 7,
33
            position: Position::Center,
34
            ..Default::default()
35
        },
36
    );
37
38
    let block = Block::bordered()
39
        .border_style(Style::default().green().bold())
40
        .border_type(BorderType::Thick);
41
42
    let title = Line::from(vec![
43
        Span::raw("Enter the password for "),
44
        Span::styled(&network.ssid, Style::new().bold()),
45
    ])
46
    .alignment(Alignment::Center);
47
48
    let title_widget = Paragraph::new(title).block(block);
49
    f.render_widget(title_widget, popup_rect);
50
51
    let input_area = Rect::new(
52
        popup_rect.x + 1,
53
        popup_rect.y + 3,
54
        popup_rect.width.saturating_sub(2),
55
        1,
56
    );
57
58
    let input_chunks = Layout::horizontal([
59
        Constraint::Min(0),    // Password input
60
        Constraint::Length(4), // eye icon
61
    ])
62
    .split(input_area);
63
64
    let (password, icon): (String, &'static str) = if input.hidden_password {
65
        (input.value.chars().map(|_| '*').collect(), " 󰈉  ")
66
    } else {
67
        (input.value.to_string(), " 󰈈   ")
68
    };
69
70
    let password_widget = Paragraph::new(password).style(Style::new().on_dark_gray());
71
    f.render_widget(password_widget, input_chunks[0]);
72
73
    let icon_widget = Paragraph::new(icon).style(Style::new().green());
74
    f.render_widget(icon_widget, input_chunks[1]);
75
76
    // Cursor position x
77
    let cx = input_chunks[0].x + input.cx as u16;
78
79
    // cx_max = position + width - 1 (so the cursor does overlap on eye icon)
80
    let cx_max = input_chunks[0].x + input_chunks[0].width.saturating_sub(1);
81
    f.set_cursor_position(layout::Position::new(cx.min(cx_max), input_chunks[0].y))
82
}