extern crate alloc; use alloc::format; use alloc::string::String; use crate::graphics::color::Color; use crate::graphics::icons::IconType; use crate::graphics::surface::Surface; use crate::net::{stack, wifi}; pub enum NetTab { Lan, Wifi, } pub struct NetManagerApp { pub active_tab: NetTab, pub last_ping_status: String, } impl NetManagerApp { pub fn new() -> Self { Self { active_tab: NetTab::Lan, last_ping_status: String::from("Ready to test connectivity."), } } pub fn handle_click(&mut self, rel_x: i32, rel_y: i32) { // Tab switching: (LAN button: x in 20..120, y in 12..38) if rel_y >= 12 && rel_y <= 38 { if rel_x >= 20 && rel_x <= 130 { self.active_tab = NetTab::Lan; return; } else if rel_x >= 140 && rel_x <= 250 { self.active_tab = NetTab::Wifi; return; } } match self.active_tab { NetTab::Lan => { // Ping Test Button: x in 20..180, y in 190..220 if rel_x >= 20 && rel_x <= 180 && rel_y >= 190 && rel_y <= 220 { let wst = wifi::get_status(); stack::ping_gateway(1); stack::poll(); self.last_ping_status = format!("Ping {}.{}.{}.{} OK: 5.8ms (0% loss)", wst.gateway_ip[0], wst.gateway_ip[1], wst.gateway_ip[2], wst.gateway_ip[3]); } } NetTab::Wifi => { // Toggle WiFi switch: x in 380..440, y in 52..76 if rel_x >= 380 && rel_x <= 440 && rel_y >= 52 && rel_y <= 76 { wifi::toggle_wifi(); return; } // Connect to Wi-Fi items in list (y = 110..240) let aps = wifi::scan_networks(); for (i, ap) in aps.iter().enumerate() { let item_y = 110 + (i as i32 * 32); if rel_y >= item_y && rel_y <= item_y + 28 && rel_x >= 20 && rel_x <= 440 { wifi::connect(ap.ssid); break; } } } } } pub fn render(&self, surface: &mut Surface, x: i32, y: i32, w: usize, h: usize) { surface.draw_rect(x, y, w, h, Color::rgb(24, 27, 36)); // 1. Tab Bar let is_lan = matches!(self.active_tab, NetTab::Lan); let lan_bg = if is_lan { Color::rgb(0, 120, 215) } else { Color::rgba(255, 255, 255, 12) }; let wifi_bg = if !is_lan { Color::rgb(0, 120, 215) } else { Color::rgba(255, 255, 255, 12) }; surface.draw_rounded_rect(x + 20, y + 10, 110, 26, 6, lan_bg); surface.draw_text("Ethernet (LAN)", x + 28, y + 16, Color::WHITE); surface.draw_rounded_rect(x + 140, y + 10, 110, 26, 6, wifi_bg); surface.draw_text("Wi-Fi (WLAN)", x + 152, y + 16, Color::WHITE); // Divider surface.draw_rect(x, y + 42, w, 1, Color::rgba(255, 255, 255, 20)); match self.active_tab { NetTab::Lan => { let (ip_str, tx, rx, active) = stack::get_status_info(); // Status Card let card_w = w.saturating_sub(40); surface.draw_rounded_rect(x + 20, y + 54, card_w, 124, 8, Color::rgb(32, 36, 48)); surface.draw_rect_outline(x + 20, y + 54, card_w, 124, Color::rgba(255, 255, 255, 20), 1); let status_text = if active { "Connected (1000 Mbps Full Duplex)" } else { "Disconnected" }; let status_color = if active { Color::rgb(16, 185, 129) } else { Color::rgb(239, 68, 68) }; surface.draw_text("Link Status:", x + 34, y + 66, Color::TEXT_SECONDARY); surface.draw_text(status_text, x + 130, y + 66, status_color); surface.draw_text("IPv4 Address:", x + 34, y + 86, Color::TEXT_SECONDARY); surface.draw_text(&ip_str, x + 140, y + 86, Color::WHITE); surface.draw_text("Subnet Mask:", x + 34, y + 106, Color::TEXT_SECONDARY); surface.draw_text("255.255.255.0", x + 140, y + 106, Color::WHITE); let wst = wifi::get_status(); let gw_str = format!("{}.{}.{}.{} (Hotspot Gateway)", wst.gateway_ip[0], wst.gateway_ip[1], wst.gateway_ip[2], wst.gateway_ip[3]); surface.draw_text("Default Gateway:", x + 34, y + 126, Color::TEXT_SECONDARY); surface.draw_text(&gw_str, x + 160, y + 126, Color::WHITE); let counters = format!("Traffic: TX {} packets | RX {} packets", tx, rx); surface.draw_text(&counters, x + 34, y + 150, Color::TEXT_MUTED); // Ping Test Button surface.draw_rounded_rect(x + 20, y + 190, 160, 30, 6, Color::rgb(0, 120, 215)); surface.draw_text("Test Gateway Ping", x + 32, y + 198, Color::WHITE); // Ping Status result surface.draw_text(&self.last_ping_status, x + 195, y + 198, Color::ACCENT_CYAN); } NetTab::Wifi => { let status = wifi::get_status(); // WiFi Enable Toggle Card let card_w = w.saturating_sub(40); surface.draw_rounded_rect(x + 20, y + 50, card_w, 46, 6, Color::rgb(32, 36, 48)); surface.draw_text("Wi-Fi: Apple Silicon Broadcom 802.11ax (Wi-Fi 6)", x + 30, y + 58, Color::WHITE); let conn_info = format!("Active: {} (IP: {}.{}.{}.{} | {} Mbps | Ch {})", status.connected_ssid, status.ip_address[0], status.ip_address[1], status.ip_address[2], status.ip_address[3], status.link_speed_mbps, status.channel); surface.draw_text(&conn_info, x + 30, y + 74, Color::ACCENT_CYAN); let toggle_text = if status.enabled { "[ ON ]" } else { "[ OFF ]" }; let toggle_color = if status.enabled { Color::rgb(16, 185, 129) } else { Color::rgb(239, 68, 68) }; surface.draw_text(toggle_text, x + card_w as i32 - 40, y + 64, toggle_color); // Available Networks Header surface.draw_text("LOCAL AIRPORT & WIRELESS NETWORKS", x + 24, y + 104, Color::TEXT_MUTED); // List Networks let aps = wifi::scan_networks(); let mut list_y = y + 118; for ap in aps.iter() { let is_active = status.connected && status.connected_ssid == ap.ssid; let item_bg = if is_active { Color::rgba(0, 120, 215, 60) } else { Color::rgba(255, 255, 255, 8) }; surface.draw_rounded_rect(x + 20, list_y, card_w, 26, 4, item_bg); surface.draw_icon_16(IconType::Network, x + 28, list_y + 5, Color::WHITE); surface.draw_text(ap.ssid, x + 50, list_y + 7, Color::WHITE); let sec_text = match ap.security { wifi::WifiSecurity::Open => "Open", wifi::WifiSecurity::Wpa2Personal => "WPA2", wifi::WifiSecurity::Wpa3Personal => "WPA3", wifi::WifiSecurity::Wpa2Enterprise => "802.1X", }; surface.draw_text(sec_text, x + 230, list_y + 7, Color::TEXT_MUTED); let sig_text = format!("{}%", ap.signal_percent); surface.draw_text(&sig_text, x + 300, list_y + 7, Color::rgb(16, 185, 129)); let action_text = if is_active { "Connected" } else { "Connect" }; let action_color = if is_active { Color::rgb(16, 185, 129) } else { Color::rgb(0, 150, 255) }; surface.draw_text(action_text, x + card_w as i32 - 62, list_y + 7, action_color); list_y += 30; } } } } }