| 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::{ |
| 12 |
action::{Action, ActionHandler}, |
| 13 |
app::Focus, |
| 14 |
ui::{ |
| 15 |
Ui, |
| 16 |
area::{self, Position}, |
| 17 |
input::Input, |
| 18 |
margin::Margin, |
| 19 |
}, |
| 20 |
}; |
| 21 |
|
| 22 |
#[derive(Debug, Clone)] |
| 23 |
pub enum Popups { |
| 24 |
Password(Input, Box<Network>), |
| 25 |
} |
| 26 |
|
| 27 |
impl Popups { |
| 28 |
pub fn open(self, action: &ActionHandler) { |
| 29 |
action.send(Action::SetFocus(Focus::Popup(self))); |
| 30 |
} |
| 31 |
|
| 32 |
pub fn close(&self, action: &ActionHandler, last_focus: Focus) { |
| 33 |
action.send(Action::SetFocus(last_focus)); |
| 34 |
} |
| 35 |
|
| 36 |
pub fn rect(f: &mut Frame, config: &PopupConfig) -> Rect { |
| 37 |
let area = area::anchor_rect( |
| 38 |
f.area(), |
| 39 |
config.width, |
| 40 |
config.height, |
| 41 |
config.position, |
| 42 |
config.margin, |
| 43 |
); |
| 44 |
f.render_widget(widgets::Clear, area); |
| 45 |
area |
| 46 |
} |
| 47 |
|
| 48 |
pub fn draw(&self, f: &mut Frame, config: &Ui) { |
| 49 |
match self { |
| 50 |
Self::Password(input, network) => { |
| 51 |
let rect = Popups::rect(f, &config.password_popup); |
| 52 |
|
| 53 |
let block = Block::bordered() |
| 54 |
.border_style(Style::default().green().bold()) |
| 55 |
.border_type(BorderType::Thick); |
| 56 |
|
| 57 |
let title = Line::from(vec![ |
| 58 |
Span::raw("Enter the password for "), |
| 59 |
Span::styled(&network.ssid, Style::new().bold()), |
| 60 |
]) |
| 61 |
.alignment(Alignment::Center); |
| 62 |
|
| 63 |
let title_widget = Paragraph::new(title).block(block); |
| 64 |
f.render_widget(title_widget, rect); |
| 65 |
|
| 66 |
let input_area = Rect::new(rect.x + 1, rect.y + 3, rect.width.saturating_sub(2), 1); |
| 67 |
|
| 68 |
let input_chunks = Layout::horizontal([ |
| 69 |
Constraint::Min(0), |
| 70 |
Constraint::Length(4), |
| 71 |
]) |
| 72 |
.split(input_area); |
| 73 |
|
| 74 |
let (mut password, icon): (String, &'static str) = if input.hidden { |
| 75 |
(input.value.chars().map(|_| '*').collect(), " ") |
| 76 |
} else { |
| 77 |
(input.value.to_string(), " ") |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
let string_offset = password |
| 82 |
.len() |
| 83 |
.saturating_sub(input_chunks[0].width.saturating_sub(1) as usize); |
| 84 |
if string_offset > 0 { |
| 85 |
password = password.split_at(string_offset).1.to_string(); |
| 86 |
} |
| 87 |
|
| 88 |
let password_widget = Paragraph::new(password).style(Style::new().on_dark_gray()); |
| 89 |
f.render_widget(password_widget, input_chunks[0]); |
| 90 |
|
| 91 |
let icon_widget = Paragraph::new(icon).style(Style::new().green()); |
| 92 |
f.render_widget(icon_widget, input_chunks[1]); |
| 93 |
|
| 94 |
|
| 95 |
let cx = input_chunks[0].x + input.cx as u16; |
| 96 |
|
| 97 |
|
| 98 |
let cx_max = input_chunks[0].x + input_chunks[0].width.saturating_sub(1); |
| 99 |
f.set_cursor_position(layout::Position::new(cx.min(cx_max), input_chunks[0].y)) |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
#[derive(Deserialize, Serialize)] |
| 106 |
pub struct PopupConfig { |
| 107 |
pub width: u16, |
| 108 |
pub height: u16, |
| 109 |
#[serde(default)] |
| 110 |
pub position: Position, |
| 111 |
#[serde(default)] |
| 112 |
pub margin: Margin, |
| 113 |
} |