1
Fork 0

handle close requests (slightly bodgey)

This commit is contained in:
Andy Killorin 2025-03-08 02:04:06 -05:00
parent 0bef5cc6e7
commit 81f55cc0e8
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
2 changed files with 18 additions and 9 deletions

View file

@ -1,16 +1,18 @@
use std::cell::OnceCell;
use std::{cell::OnceCell, time::Duration};
use common::{ControlPacket, TelemetryPacket};
use eframe::egui::{self, containers, Align2, Checkbox, Context, Label};
use tokio::sync::{mpsc, watch::Receiver};
use tokio::{runtime::Runtime, sync::{mpsc, watch::Receiver}};
use egui_toast::{Toast, Toasts};
pub const GUI: OnceCell<Context> = OnceCell::new();
pub fn gui(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>) -> eframe::Result {
pub fn gui(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>, executor: Runtime) -> eframe::Result {
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
viewport: egui::ViewportBuilder::default()
.with_inner_size([700.0, 480.0])
.with_app_id("cruisecontrol"),
..Default::default()
};
eframe::run_native(
@ -20,7 +22,7 @@ pub fn gui(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>) -> eframe::Re
// This gives us image support:
egui_extras::install_image_loaders(&cc.egui_ctx);
Ok(Box::new(GUI::with_receivers(data, toasts)))
Ok(Box::new(GUI::with_receivers(data, toasts, executor)))
}),
)
}
@ -34,13 +36,15 @@ pub struct GUIData {
struct GUI {
data: Receiver<GUIData>,
toasts: mpsc::Receiver<Toast>,
executor: Option<Runtime>,
}
impl GUI {
fn with_receivers(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>) -> Self {
fn with_receivers(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>, executor: Runtime) -> Self {
Self {
data,
toasts,
executor: Some(executor),
}
}
}
@ -74,5 +78,9 @@ impl eframe::App for GUI {
}
toasts.show(ctx);
if ctx.input(|i| i.viewport().close_requested()) {
self.executor.take().unwrap().shutdown_timeout(Duration::from_millis(100));
}
}
}

View file

@ -81,8 +81,9 @@ fn main() -> Result<()> {
let (data_sender, gui_data) =watch::channel(GUIData::default());
let spawner = executor.handle().clone();
thread::spawn(move || {
let control = executor.block_on(async {
let control = spawner.block_on(async {
toast_sender.send(Toast::new().text("connecting to bot").kind(ToastKind::Info)).await.unwrap();
let cruisecontrol = TcpStream::connect("192.168.1.2:1234").await?;
println!("connected");
@ -95,11 +96,11 @@ fn main() -> Result<()> {
Ok(control)
}).unwrap();
executor.block_on(controller(notes, control, data_sender.clone())).unwrap()
spawner.block_on(controller(notes, control, data_sender.clone())).unwrap()
});
println!("launching gui");
gui(gui_data, toasts).unwrap();
gui(gui_data, toasts, executor).unwrap();
drop(stream);