2026-10-02 19:23:06 +01:00
|
|
|
//! The eframe application behind the window: the main view, the per-network
|
2026-10-02 15:37:08 +01:00
|
|
|
//! devices windows, and the agent worker.
|
2026-10-02 19:23:06 +01:00
|
|
|
//!
|
|
|
|
|
//! It runs in its own process, started by the tray when the user chooses Open
|
|
|
|
|
//! and gone when the window is closed. The tray, which has to outlive the
|
|
|
|
|
//! window, lives in the other process; see [`crate::tray`].
|
2026-10-02 14:48:52 +01:00
|
|
|
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
use eframe::egui;
|
|
|
|
|
|
2026-10-02 20:16:01 +01:00
|
|
|
use crate::about;
|
2026-10-02 23:29:51 +01:00
|
|
|
use crate::agent::AgentClient;
|
2026-10-02 15:37:08 +01:00
|
|
|
use crate::devices;
|
2026-10-02 15:08:29 +01:00
|
|
|
use crate::stats::Traffic;
|
2026-10-02 15:37:08 +01:00
|
|
|
use crate::ui::{self, UiState};
|
2026-10-02 14:48:52 +01:00
|
|
|
|
2026-10-14 13:50:05 +01:00
|
|
|
fn app_fonts() -> egui::FontDefinitions {
|
|
|
|
|
let mut fonts = egui::FontDefinitions::default();
|
|
|
|
|
egui_phosphor::add_to_fonts(&mut fonts, egui_phosphor::Variant::Regular);
|
|
|
|
|
fonts
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 19:23:06 +01:00
|
|
|
/// The window's state.
|
2026-10-02 14:48:52 +01:00
|
|
|
pub(crate) struct App {
|
|
|
|
|
agent: AgentClient,
|
2026-10-02 15:37:08 +01:00
|
|
|
state: UiState,
|
2026-10-02 15:08:29 +01:00
|
|
|
/// Traffic rates derived from successive snapshots.
|
|
|
|
|
traffic: Traffic,
|
2026-10-02 15:37:08 +01:00
|
|
|
/// The snapshot generation last folded into `traffic`.
|
2026-10-02 15:08:29 +01:00
|
|
|
sampled: u64,
|
2026-10-02 14:48:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl App {
|
2026-10-02 19:23:06 +01:00
|
|
|
/// Builds the app: resolves the socket and starts the worker.
|
2026-10-02 14:48:52 +01:00
|
|
|
pub(crate) fn new(
|
|
|
|
|
cc: &eframe::CreationContext<'_>,
|
|
|
|
|
runtime: tokio::runtime::Handle,
|
|
|
|
|
) -> Result<Self, String> {
|
|
|
|
|
let ctx = cc.egui_ctx.clone();
|
2026-10-02 16:00:23 +01:00
|
|
|
// Add the Phosphor icon glyphs to the font set so the icon buttons
|
|
|
|
|
// render instead of showing as tofu.
|
2026-10-14 13:50:05 +01:00
|
|
|
ctx.set_fonts(app_fonts());
|
2026-10-02 16:00:23 +01:00
|
|
|
|
2026-10-02 23:29:51 +01:00
|
|
|
let agent = AgentClient::spawn(&runtime, ctx);
|
2026-10-02 14:48:52 +01:00
|
|
|
Ok(Self {
|
|
|
|
|
agent,
|
2026-10-02 15:37:08 +01:00
|
|
|
state: UiState::default(),
|
2026-10-02 15:08:29 +01:00
|
|
|
traffic: Traffic::default(),
|
|
|
|
|
sampled: 0,
|
2026-10-02 14:48:52 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl eframe::App for App {
|
|
|
|
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
|
|
|
|
let snapshot = self.agent.snapshot();
|
2026-10-02 15:08:29 +01:00
|
|
|
if snapshot.generation != self.sampled
|
|
|
|
|
&& let (Some(Ok(report)), Some(at)) = (&snapshot.status, snapshot.at)
|
|
|
|
|
{
|
|
|
|
|
self.traffic.observe(report, at);
|
|
|
|
|
self.sampled = snapshot.generation;
|
|
|
|
|
}
|
2026-10-02 14:48:52 +01:00
|
|
|
|
2026-10-02 15:37:08 +01:00
|
|
|
ui::draw(ctx, &self.agent, &mut self.state, &snapshot, &self.traffic);
|
|
|
|
|
self.show_devices_windows(ctx, &snapshot);
|
2026-10-21 15:40:45 +01:00
|
|
|
self.show_vip_windows(ctx, &snapshot);
|
2026-10-02 20:16:01 +01:00
|
|
|
self.show_about_window(ctx, &snapshot);
|
2026-10-02 15:37:08 +01:00
|
|
|
|
2026-10-02 15:48:32 +01:00
|
|
|
// A secret the worker fetched for the clipboard: copy it here, on the
|
|
|
|
|
// UI thread that owns the clipboard, exactly once.
|
|
|
|
|
if let Some(secret) = self.agent.take_pending_copy() {
|
|
|
|
|
ctx.copy_text(secret);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 19:23:06 +01:00
|
|
|
// Keep ticking so fresh status is picked up.
|
2026-10-02 14:48:52 +01:00
|
|
|
ctx.request_repaint_after(Duration::from_millis(500));
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-02 15:37:08 +01:00
|
|
|
|
|
|
|
|
impl App {
|
2026-10-21 15:40:45 +01:00
|
|
|
fn show_vip_windows(&mut self, ctx: &egui::Context, snapshot: &crate::agent::Snapshot) {
|
|
|
|
|
let Some(Ok(report)) = &snapshot.status else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let ids: Vec<_> = self.state.vip_windows.keys().cloned().collect();
|
|
|
|
|
for id in ids {
|
|
|
|
|
let Some(network) = report.networks.iter().find(|n| n.network_id == id) else {
|
|
|
|
|
self.state.vip_windows.remove(&id);
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
let Some(state) = self.state.vip_windows.get_mut(&id) else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
let builder = egui::ViewportBuilder::default()
|
|
|
|
|
.with_title(format!("tsunagi · VIP · {}", network.name))
|
|
|
|
|
.with_app_id(crate::icon::APP_ID)
|
|
|
|
|
.with_icon(crate::icon::window())
|
2026-10-21 16:57:27 +01:00
|
|
|
.with_inner_size([1280., 860.])
|
2026-10-21 15:40:45 +01:00
|
|
|
.with_min_inner_size([650., 400.]);
|
|
|
|
|
let keep = ctx.show_viewport_immediate(
|
|
|
|
|
egui::ViewportId::from_hash_of(("vip", &id)),
|
|
|
|
|
builder,
|
|
|
|
|
|vctx, _| {
|
|
|
|
|
let close = crate::vip::show(vctx, &self.agent, state, network, snapshot);
|
|
|
|
|
if close {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if vctx.input(|i| i.viewport().close_requested()) {
|
|
|
|
|
if state.dirty() {
|
|
|
|
|
state.request_close();
|
|
|
|
|
vctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
if !keep {
|
|
|
|
|
self.state.vip_windows.remove(&id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-02 20:16:01 +01:00
|
|
|
/// Renders the About window while it is open.
|
|
|
|
|
fn show_about_window(&mut self, ctx: &egui::Context, snapshot: &crate::agent::Snapshot) {
|
|
|
|
|
if !self.state.about_open {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let builder = egui::ViewportBuilder::default()
|
|
|
|
|
.with_title("About tsunagi")
|
2026-10-03 03:30:47 +01:00
|
|
|
.with_app_id(crate::icon::APP_ID)
|
|
|
|
|
.with_icon(crate::icon::window())
|
2026-10-02 20:16:01 +01:00
|
|
|
.with_inner_size([560.0, 520.0])
|
|
|
|
|
.with_min_inner_size([380.0, 280.0]);
|
|
|
|
|
let socket = self.agent.socket();
|
|
|
|
|
let keep = ctx.show_viewport_immediate(
|
|
|
|
|
egui::ViewportId::from_hash_of("about"),
|
|
|
|
|
builder,
|
|
|
|
|
|vctx, _class| {
|
2026-10-02 23:29:51 +01:00
|
|
|
about::show(vctx, &socket, snapshot);
|
2026-10-02 20:16:01 +01:00
|
|
|
!vctx.input(|i| i.viewport().close_requested())
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
if !keep {
|
|
|
|
|
self.state.about_open = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 15:37:08 +01:00
|
|
|
/// Renders one separate window per open network-devices view.
|
|
|
|
|
fn show_devices_windows(&mut self, ctx: &egui::Context, snapshot: &crate::agent::Snapshot) {
|
|
|
|
|
let Some(Ok(report)) = &snapshot.status else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let open: Vec<String> = self.state.open_devices.iter().cloned().collect();
|
|
|
|
|
for id in open {
|
|
|
|
|
let Some(network) = report.networks.iter().find(|n| n.network_id == id) else {
|
|
|
|
|
// The network is gone (left/forgotten); close its window.
|
|
|
|
|
self.state.open_devices.remove(&id);
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
let viewport_id = egui::ViewportId::from_hash_of(("devices", &id));
|
|
|
|
|
let builder = egui::ViewportBuilder::default()
|
|
|
|
|
.with_title(devices::title(network))
|
2026-10-03 03:30:47 +01:00
|
|
|
.with_app_id(crate::icon::APP_ID)
|
|
|
|
|
.with_icon(crate::icon::window())
|
2026-10-12 18:04:29 +01:00
|
|
|
.with_inner_size([1440.0, 640.0])
|
|
|
|
|
.with_min_inner_size([800.0, 400.0]);
|
2026-10-02 15:37:08 +01:00
|
|
|
let agent = &self.agent;
|
|
|
|
|
let traffic = &self.traffic;
|
2026-10-02 16:04:39 +01:00
|
|
|
// The window shares the single unit setting, so its toggle and the
|
|
|
|
|
// main window's stay in step.
|
|
|
|
|
let unit = &mut self.state.unit;
|
2026-10-03 01:14:51 +01:00
|
|
|
let confirm = &mut self.state.exit_confirm;
|
2026-10-12 18:04:29 +01:00
|
|
|
let forget_confirm = &mut self.state.forget_confirm;
|
2026-10-02 15:37:08 +01:00
|
|
|
let keep = ctx.show_viewport_immediate(viewport_id, builder, |vctx, _class| {
|
2026-10-03 01:14:51 +01:00
|
|
|
devices::show(
|
|
|
|
|
vctx,
|
|
|
|
|
agent,
|
|
|
|
|
unit,
|
2026-10-12 18:04:29 +01:00
|
|
|
(confirm, forget_confirm),
|
2026-10-03 01:14:51 +01:00
|
|
|
network,
|
|
|
|
|
&report.endpoint_id,
|
|
|
|
|
traffic,
|
|
|
|
|
);
|
2026-10-02 15:37:08 +01:00
|
|
|
!vctx.input(|i| i.viewport().close_requested())
|
|
|
|
|
});
|
|
|
|
|
if !keep {
|
|
|
|
|
self.state.open_devices.remove(&id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-14 13:50:05 +01:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn route_icons_have_an_embedded_font_in_the_app() {
|
|
|
|
|
let fonts = app_fonts();
|
|
|
|
|
assert!(fonts.font_data.contains_key("phosphor"));
|
|
|
|
|
assert!(
|
|
|
|
|
fonts
|
|
|
|
|
.families
|
|
|
|
|
.get(&egui::FontFamily::Proportional)
|
|
|
|
|
.is_some_and(|family| family.iter().any(|name| name == "phosphor"))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|