| 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); |
| 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), |
| 64 |
Constraint::Length(4), |
| 65 |
]) |
| 66 |
.split(input_area); |
| 67 |
|
| 68 |
let (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 |
let password_widget = Paragraph::new(password).style(Style::new().on_dark_gray()); |
| 75 |
f.render_widget(password_widget, input_chunks[0]); |
| 76 |
|
| 77 |
let icon_widget = Paragraph::new(icon).style(Style::new().green()); |
| 78 |
f.render_widget(icon_widget, input_chunks[1]); |
| 79 |
|
| 80 |
|
| 81 |
let cx = input_chunks[0].x + input.cx as u16; |
| 82 |
|
| 83 |
|
| 84 |
let cx_max = input_chunks[0].x + input_chunks[0].width.saturating_sub(1); |
| 85 |
f.set_cursor_position(layout::Position::new(cx.min(cx_max), input_chunks[0].y)) |
| 86 |
} |