| 1 |
use ratatui::{ |
| 2 |
Frame, |
| 3 |
layout::{self, Alignment, Constraint, Layout, Rect}, |
| 4 |
style::{Color, Style}, |
| 5 |
text::{Line, Span}, |
| 6 |
widgets::{self, Block, Paragraph}, |
| 7 |
}; |
| 8 |
|
| 9 |
use crate::{ |
| 10 |
tui::Selected, |
| 11 |
ui::{self, Gaps, Position, input::Input}, |
| 12 |
}; |
| 13 |
|
| 14 |
#[derive(Default)] |
| 15 |
pub struct Options { |
| 16 |
|
| 17 |
pub width: u16, |
| 18 |
|
| 19 |
|
| 20 |
pub height: u16, |
| 21 |
|
| 22 |
|
| 23 |
pub position: Position, |
| 24 |
|
| 25 |
|
| 26 |
pub gaps: Gaps, |
| 27 |
} |
| 28 |
|
| 29 |
pub fn popup_area(f: &mut Frame, opt: Options) -> Rect { |
| 30 |
let area = ui::position_rect(f.area(), opt.width, opt.height, opt.position, opt.gaps); |
| 31 |
f.render_widget(widgets::Clear, area); |
| 32 |
area |
| 33 |
} |
| 34 |
|
| 35 |
pub fn draw_auth(f: &mut Frame, input: &Input, selected: &Option<Selected>, hidden_password: bool) { |
| 36 |
let popup_area = popup_area( |
| 37 |
f, |
| 38 |
Options { |
| 39 |
width: 60, |
| 40 |
height: 7, |
| 41 |
position: Position::Center, |
| 42 |
..Default::default() |
| 43 |
}, |
| 44 |
); |
| 45 |
|
| 46 |
if let Some(Selected::Network(net)) = selected { |
| 47 |
let block = Block::bordered().border_style(Style::default().fg(Color::Yellow).bold()); |
| 48 |
|
| 49 |
let title = Line::from(vec![ |
| 50 |
Span::raw("Enter the password for "), |
| 51 |
Span::styled(&net.ssid, Style::new().bold()), |
| 52 |
]).alignment(Alignment::Center); |
| 53 |
|
| 54 |
let title_widget = Paragraph::new(title).block(block); |
| 55 |
f.render_widget(title_widget, popup_area); |
| 56 |
|
| 57 |
let input_area = Rect::new( |
| 58 |
popup_area.x + 1, |
| 59 |
popup_area.y + 3, |
| 60 |
popup_area.width.saturating_sub(2), |
| 61 |
1, |
| 62 |
); |
| 63 |
|
| 64 |
let input_chunks = Layout::horizontal([ |
| 65 |
Constraint::Min(0), |
| 66 |
Constraint::Length(4), |
| 67 |
]) |
| 68 |
.split(input_area); |
| 69 |
|
| 70 |
let (password, icon): (String, &'static str) = if hidden_password { |
| 71 |
(input.value.chars().map(|_| '*').collect(), " ") |
| 72 |
} else { |
| 73 |
(input.value.to_string(), " ") |
| 74 |
}; |
| 75 |
|
| 76 |
let password_widget = Paragraph::new(password).style(Style::new().on_dark_gray()); |
| 77 |
f.render_widget(password_widget, input_chunks[0]); |
| 78 |
|
| 79 |
let icon_widget = Paragraph::new(icon).style(Style::new().green()); |
| 80 |
f.render_widget(icon_widget, input_chunks[1]); |
| 81 |
|
| 82 |
let cx = input_chunks[0].x + input.cx as u16; |
| 83 |
let cx_max = input_chunks[0].x + input_chunks[0].width.saturating_sub(1) as u16; |
| 84 |
f.set_cursor_position(layout::Position::new( |
| 85 |
cx.min(cx_max), |
| 86 |
input_chunks[0].y, |
| 87 |
)) |
| 88 |
} |
| 89 |
} |