| 1 |
use nmrs::{Network, WifiSecurity}; |
| 2 |
use ratatui::{ |
| 3 |
Frame, |
| 4 |
layout::{self, Rect}, |
| 5 |
style::{Color, Style, Stylize}, |
| 6 |
widgets::{Block, Paragraph}, |
| 7 |
}; |
| 8 |
|
| 9 |
use crate::{ |
| 10 |
tui::Selected, |
| 11 |
ui::{self, Gaps, Position, Urgency, 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: Option<Gaps>, |
| 27 |
|
| 28 |
|
| 29 |
pub dim: Option<Color>, |
| 30 |
} |
| 31 |
|
| 32 |
pub fn popup_area(f: &mut Frame, opt: Options) -> Rect { |
| 33 |
if opt.dim.is_some() { |
| 34 |
|
| 35 |
let dimming_block = ratatui::widgets::Block::default().bg(opt.dim.unwrap_or_default()); |
| 36 |
f.render_widget(dimming_block, f.area()); |
| 37 |
} |
| 38 |
|
| 39 |
let area = ui::position_rect(f.area(), opt.width, opt.height, opt.position, opt.gaps); |
| 40 |
f.render_widget(ratatui::widgets::Clear, area); |
| 41 |
area |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
pub fn draw_toast(f: &mut Frame, msg: &str, urgency: Urgency) { |
| 46 |
let area = popup_area( |
| 47 |
f, |
| 48 |
Options { |
| 49 |
width: 30, |
| 50 |
height: 3, |
| 51 |
position: Position::RightBottom, |
| 52 |
gaps: Some(Gaps { |
| 53 |
bottom: 1, |
| 54 |
right: 1, |
| 55 |
..Default::default() |
| 56 |
}), |
| 57 |
..Default::default() |
| 58 |
}, |
| 59 |
); |
| 60 |
|
| 61 |
let border_style = match urgency { |
| 62 |
Urgency::Success => Style::new().green(), |
| 63 |
Urgency::Warning => Style::new().yellow(), |
| 64 |
Urgency::Critical => Style::new().red(), |
| 65 |
}; |
| 66 |
let block = Block::bordered().border_style(border_style); |
| 67 |
f.render_widget(Paragraph::new(msg).block(block), area); |
| 68 |
} |
| 69 |
|
| 70 |
pub fn draw_auth(f: &mut Frame, input: &Input, selected: &Option<Selected> ) { |
| 71 |
let input_area = popup_area( |
| 72 |
f, |
| 73 |
Options { |
| 74 |
width: 30, |
| 75 |
height: 3, |
| 76 |
position: Position::Center, |
| 77 |
dim: Some(Color::Black), |
| 78 |
..Default::default() |
| 79 |
}, |
| 80 |
); |
| 81 |
|
| 82 |
|
| 83 |
if let Some(Selected::Network(net)) = selected { |
| 84 |
let block = Block::bordered() |
| 85 |
.title(format!(" Password for {} ", net.ssid)) |
| 86 |
.border_style(Style::default().fg(Color::Yellow)); |
| 87 |
|
| 88 |
|
| 89 |
let display_pass: String = input.value.chars().map(|_| '*').collect(); |
| 90 |
let text = Paragraph::new(display_pass).block(block); |
| 91 |
f.render_widget(text, input_area); |
| 92 |
f.set_cursor_position(layout::Position::new( |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
(input_area.x + input.cx as u16 + 1).min(input_area.x + input_area.width.saturating_sub(2)), |
| 97 |
|
| 98 |
input_area.y + 1, |
| 99 |
)) |
| 100 |
} |
| 101 |
} |