implement phase 4; add flags

This commit is contained in:
ms2k 2026-03-30 22:14:11 +09:00
commit 8664a80f1e

View file

@ -3,8 +3,11 @@ use std::time::Duration;
use eframe::egui;
use std::collections::HashMap;
use crate::config::Config;
use crate::countries::{get_countries, Country};
use crate::flags;
use crate::nordvpn::{self, CmdResult, Poller};
use crate::ui::country_selector::CountrySelector;
@ -14,24 +17,34 @@ pub struct App {
poller: Poller,
cmd_tx: Sender<CmdResult>,
cmd_rx: mpsc::Receiver<CmdResult>,
/// Receives rasterized flag images from the background loader thread.
flag_rx: mpsc::Receiver<(String, egui::ColorImage)>,
/// Fully loaded flag textures, keyed by nordvpn_key.
flag_textures: HashMap<String, egui::TextureHandle>,
selector: CountrySelector,
/// Last connect/disconnect result shown inline below the status bar.
notification: Option<String>,
}
impl App {
pub fn new(_cc: &eframe::CreationContext<'_>) -> Self {
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
let config = Config::load();
let countries = get_countries(&config.external_app);
let poller = Poller::new(config.external_app.clone(), config.refresh_rate);
let (cmd_tx, cmd_rx) = mpsc::channel();
// Kick off background flag rasterization.
let (flag_tx, flag_rx) = mpsc::channel();
flags::spawn_loader(flags::flag_paths(&countries), flag_tx);
Self {
config,
countries,
poller,
cmd_tx,
cmd_rx,
flag_rx,
flag_textures: HashMap::new(),
selector: CountrySelector::default(),
notification: None,
}
@ -57,6 +70,12 @@ impl App {
impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
// Upload any newly rasterized flag textures.
while let Ok((key, image)) = self.flag_rx.try_recv() {
let handle = ctx.load_texture(&key, image, egui::TextureOptions::LINEAR);
self.flag_textures.insert(key, handle);
}
// Drain completed command results.
while let Ok(result) = self.cmd_rx.try_recv() {
self.notification = Some(match result {
@ -129,6 +148,7 @@ impl eframe::App for App {
&self.countries,
&self.config.favorites,
status.country.as_deref(),
&self.flag_textures,
)
})
.inner;