| 1 |
use std::hash::Hash; |
| 2 |
|
| 3 |
use crate::{ |
| 4 |
app::{App, Focus, Tabs}, |
| 5 |
ui::{ |
| 6 |
area::{self, Border}, |
| 7 |
list::StatefulList, |
| 8 |
margin::Margin, |
| 9 |
style_config::StyleConfig, |
| 10 |
}, |
| 11 |
}; |
| 12 |
use ratatui::{ |
| 13 |
Frame, |
| 14 |
layout::{Constraint, Flex, Rect}, |
| 15 |
style::Style, |
| 16 |
text::Line, |
| 17 |
widgets::{Block, BorderType, Row, Table}, |
| 18 |
}; |
| 19 |
use serde::{Deserialize, Serialize}; |
| 20 |
|
| 21 |
#[derive(Deserialize, Serialize)] |
| 22 |
pub struct TableConfig<K> { |
| 23 |
pub title: String, |
| 24 |
pub header: Header<K>, |
| 25 |
pub border: Border, |
| 26 |
#[serde(default)] |
| 27 |
pub margin: Margin, |
| 28 |
} |
| 29 |
|
| 30 |
#[derive(Deserialize, Serialize)] |
| 31 |
pub struct Header<K> { |
| 32 |
pub columns: Vec<Column<K>>, |
| 33 |
#[serde(default)] |
| 34 |
pub style_normal: StyleConfig, |
| 35 |
#[serde(default)] |
| 36 |
pub style_active: StyleConfig, |
| 37 |
} |
| 38 |
|
| 39 |
#[derive(Deserialize, Serialize)] |
| 40 |
pub struct Column<K> { |
| 41 |
pub title: String, |
| 42 |
pub value: K, |
| 43 |
pub constraint: Constraint, |
| 44 |
} |
| 45 |
|
| 46 |
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Hash)] |
| 47 |
pub enum NetworkColumnKind { |
| 48 |
Name, |
| 49 |
Security, |
| 50 |
Signal, |
| 51 |
} |
| 52 |
|
| 53 |
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Hash)] |
| 54 |
pub enum DeviceColumnKind { |
| 55 |
Name, |
| 56 |
Powered, |
| 57 |
State, |
| 58 |
Frequency, |
| 59 |
Address, |
| 60 |
Security, |
| 61 |
} |
| 62 |
|
| 63 |
pub struct Data<'a, T> { |
| 64 |
pub cells: Vec<Row<'a>>, |
| 65 |
pub list: &'a mut StatefulList<T>, |
| 66 |
} |
| 67 |
|
| 68 |
pub fn draw<Any, K: Eq + Hash>( |
| 69 |
f: &mut Frame, |
| 70 |
area: Rect, |
| 71 |
config: &TableConfig<K>, |
| 72 |
data: &mut Data<Any>, |
| 73 |
is_active: bool, |
| 74 |
) { |
| 75 |
let area = area::fill_rect(area, config.margin); |
| 76 |
|
| 77 |
let border_style: Style = if is_active { |
| 78 |
config.border.style_active.format().unwrap_or_default() |
| 79 |
} else { |
| 80 |
config.border.style_normal.format().unwrap_or_default() |
| 81 |
}; |
| 82 |
|
| 83 |
let border_type: BorderType = if is_active { |
| 84 |
config.border.type_active |
| 85 |
} else { |
| 86 |
config.border.type_normal |
| 87 |
}; |
| 88 |
|
| 89 |
let header_style: Style = if is_active { |
| 90 |
config.header.style_active.format().unwrap_or_default() |
| 91 |
} else { |
| 92 |
config.header.style_normal.format().unwrap_or_default() |
| 93 |
}; |
| 94 |
|
| 95 |
let row_style = if is_active { |
| 96 |
Style::new().on_dark_gray() |
| 97 |
} else { |
| 98 |
Style::new() |
| 99 |
}; |
| 100 |
|
| 101 |
let (columns, constraints): (Vec<Line>, Vec<Constraint>) = config |
| 102 |
.header |
| 103 |
.columns |
| 104 |
.iter() |
| 105 |
.map(|c| (Line::from(c.title.clone()).centered(), c.constraint)) |
| 106 |
.unzip(); |
| 107 |
let table = Table::new(data.cells.clone(), constraints) |
| 108 |
.header(Row::new(columns).style(header_style).bottom_margin(1)) |
| 109 |
.block( |
| 110 |
Block::bordered() |
| 111 |
.title(config.title.clone()) |
| 112 |
.border_style(border_style) |
| 113 |
.border_type(border_type), |
| 114 |
) |
| 115 |
.flex(Flex::Center) |
| 116 |
.row_highlight_style(row_style); |
| 117 |
|
| 118 |
f.render_stateful_widget(table, area, &mut data.list.state); |
| 119 |
} |
| 120 |
|
| 121 |
pub fn draw_known_network(f: &mut Frame<'_>, area: Rect, app: &mut App) { |
| 122 |
let active = if let Focus::Tab(tab) = app.focus |
| 123 |
&& tab == Tabs::KnownNetworks |
| 124 |
{ |
| 125 |
true |
| 126 |
} else { |
| 127 |
false |
| 128 |
}; |
| 129 |
|
| 130 |
let rows: Vec<Row> = app |
| 131 |
.known_networks |
| 132 |
.items |
| 133 |
.iter() |
| 134 |
.map(|net| -> Row<'_> { |
| 135 |
let security = if net.is_psk { |
| 136 |
"Psk" |
| 137 |
} else if net.is_eap { |
| 138 |
"Enterprise" |
| 139 |
} else if net.secured { |
| 140 |
"Other" |
| 141 |
} else { |
| 142 |
"Open" |
| 143 |
}; |
| 144 |
|
| 145 |
let (strength, bars) = if let Some(strength) = net.strength { |
| 146 |
let bars = if strength > 80 { |
| 147 |
"" |
| 148 |
} else if strength > 60 { |
| 149 |
"" |
| 150 |
} else if strength > 40 { |
| 151 |
"" |
| 152 |
} else if strength > 20 { |
| 153 |
"" |
| 154 |
} else { |
| 155 |
"" |
| 156 |
}; |
| 157 |
(strength.to_string(), bars) |
| 158 |
} else { |
| 159 |
("Unknown".to_string(), "") |
| 160 |
}; |
| 161 |
|
| 162 |
Row::new(vec![ |
| 163 |
Line::from(net.ssid.clone()).centered(), |
| 164 |
Line::from(security).centered(), |
| 165 |
Line::from( |
| 166 |
if app |
| 167 |
.network_manager |
| 168 |
.current_network |
| 169 |
.as_ref() |
| 170 |
.is_some_and(|current| current.ssid == net.ssid) |
| 171 |
{ |
| 172 |
"Connected" |
| 173 |
} else { |
| 174 |
"-" |
| 175 |
}, |
| 176 |
) |
| 177 |
.centered(), |
| 178 |
Line::from(format!("{}% {}", strength, bars)).centered(), |
| 179 |
]) |
| 180 |
}) |
| 181 |
.collect(); |
| 182 |
draw( |
| 183 |
f, |
| 184 |
area, |
| 185 |
&app.config.ui.known_networks, |
| 186 |
&mut Data { |
| 187 |
cells: rows, |
| 188 |
list: &mut app.known_networks, |
| 189 |
}, |
| 190 |
active, |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
pub fn draw_available_network(f: &mut Frame<'_>, area: Rect, app: &mut App) { |
| 195 |
let active = if let Focus::Tab(tab) = app.focus |
| 196 |
&& tab == Tabs::AvailableNetworks |
| 197 |
{ |
| 198 |
true |
| 199 |
} else { |
| 200 |
false |
| 201 |
}; |
| 202 |
let rows: Vec<Row> = app |
| 203 |
.available_networks |
| 204 |
.items |
| 205 |
.iter() |
| 206 |
.map(|net| -> Row<'_> { |
| 207 |
let security = if net.is_psk { |
| 208 |
"Psk" |
| 209 |
} else if net.is_eap { |
| 210 |
"Enterprise" |
| 211 |
} else if net.secured { |
| 212 |
"Other" |
| 213 |
} else { |
| 214 |
"Open" |
| 215 |
}; |
| 216 |
|
| 217 |
let (strength, bars) = if let Some(strength) = net.strength { |
| 218 |
let bars = if strength > 80 { |
| 219 |
"" |
| 220 |
} else if strength > 60 { |
| 221 |
"" |
| 222 |
} else if strength > 40 { |
| 223 |
"" |
| 224 |
} else if strength > 20 { |
| 225 |
"" |
| 226 |
} else { |
| 227 |
"" |
| 228 |
}; |
| 229 |
(strength.to_string(), bars) |
| 230 |
} else { |
| 231 |
("Unknown".to_string(), "") |
| 232 |
}; |
| 233 |
|
| 234 |
Row::new(vec![ |
| 235 |
Line::from(net.ssid.clone()).centered(), |
| 236 |
Line::from(security).centered(), |
| 237 |
Line::from(format!("{}% {}", strength, bars)).centered(), |
| 238 |
]) |
| 239 |
}) |
| 240 |
.collect(); |
| 241 |
draw( |
| 242 |
f, |
| 243 |
area, |
| 244 |
&app.config.ui.available_networks, |
| 245 |
&mut Data { |
| 246 |
cells: rows, |
| 247 |
list: &mut app.available_networks, |
| 248 |
}, |
| 249 |
active, |
| 250 |
); |
| 251 |
} |
| 252 |
|
| 253 |
pub fn draw_devices(f: &mut Frame<'_>, area: Rect, app: &mut App) { |
| 254 |
let active = if let Focus::Tab(tab) = app.focus |
| 255 |
&& tab == Tabs::Devices |
| 256 |
{ |
| 257 |
true |
| 258 |
} else { |
| 259 |
false |
| 260 |
}; |
| 261 |
|
| 262 |
let security = if let Some(network_info) = &app.network_manager.current_network_info { |
| 263 |
&network_info.security |
| 264 |
} else { |
| 265 |
&"-".to_string() |
| 266 |
}; |
| 267 |
let rows: Vec<Row> = app |
| 268 |
.devices |
| 269 |
.items |
| 270 |
.iter() |
| 271 |
.map(|dev| -> Row<'_> { |
| 272 |
Row::new(vec![ |
| 273 |
Line::from(dev.interface.clone()).centered(), |
| 274 |
Line::from(if dev.state.is_enabled() { "On" } else { "Off" }).centered(), |
| 275 |
Line::from(format!("{}", dev.state)).centered(), |
| 276 |
Line::from(format!("{} MHz", dev.active_frequency_mhz.unwrap_or(0))).centered(), |
| 277 |
Line::from(dev.hw_address.to_string()).centered(), |
| 278 |
Line::from(security.clone()).centered(), |
| 279 |
]) |
| 280 |
}) |
| 281 |
.collect(); |
| 282 |
draw( |
| 283 |
f, |
| 284 |
area, |
| 285 |
&app.config.ui.devices, |
| 286 |
&mut Data { |
| 287 |
cells: rows, |
| 288 |
list: &mut app.devices, |
| 289 |
}, |
| 290 |
active, |
| 291 |
); |
| 292 |
} |