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