| 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); |
| 23 |
area |
| 24 |
} |
| 25 |
|
| 26 |
|
| 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), |
| 60 |
Constraint::Length(4), |
| 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 |
|
| 77 |
let cx = input_chunks[0].x + input.cx as u16; |
| 78 |
|
| 79 |
|
| 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 |
} |