From 67b2ce202097c4ce0db3ea81df775405608d527f Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Wed, 11 Sep 2024 18:12:42 +0400 Subject: wip: add egui --- graph-checker/src/gui.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 graph-checker/src/gui.rs (limited to 'graph-checker/src/gui.rs') diff --git a/graph-checker/src/gui.rs b/graph-checker/src/gui.rs new file mode 100644 index 0000000..2a363e2 --- /dev/null +++ b/graph-checker/src/gui.rs @@ -0,0 +1,83 @@ +use crate::compute; +use crate::geng::GengIterator; +use crate::graph; +use eframe::egui; +use std::sync::mpsc; +use std::time::{Duration, Instant}; +use tokio; + +pub fn run_gui(rt: tokio::runtime::Runtime) { + let options = eframe::NativeOptions::default(); + let _ = eframe::run_native( + "Graph checker", + options, + Box::new(|_cc| Ok(Box::new(Application::new(rt)))), + ); +} + +struct Application { + rt: tokio::runtime::Runtime, +} + +impl Application { + fn new(rt: tokio::runtime::Runtime) -> Self { + Self { rt } + } +} + +impl eframe::App for Application { + fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + let mut c = ComputeWindow::new(&self.rt); + egui::Window::new("Spawn tasks").show(ctx, |ui| { + c.update(ui); + }); + } +} + +struct ComputeWindow<'a> { + rt: &'a tokio::runtime::Runtime, + computed: Vec, + rx: mpsc::Receiver, + tx: mpsc::Sender, +} + +impl<'a> ComputeWindow<'a> { + fn new(rt: &'a tokio::runtime::Runtime) -> Self { + let (tx, rx) = mpsc::channel(); + Self { + rt, + computed: Vec::new(), + rx, + tx, + } + } + + fn update(&mut self, ui: &mut egui::Ui) { + if ui.button("Посчитать графы").clicked() { + let gi = GengIterator::new(7); + let _tasks: Vec<_> = gi + .map(|g| { + // let db = db.clone(); + let tx = self.tx.clone(); + self.rt.spawn(async move { + tx.send(compute::async_theorems1(g).await).unwrap(); + }) + }) + .collect(); + } + + if let Ok(p) = self.rx.recv_timeout(Duration::from_millis(10)) { + self.computed.push(p); + } + + ui.label(format!("Посчитано графов: {}", self.computed.len())); + + // let start = Instant::now(); + // let res = futures::future::join_all(tasks).await; + // let elapsed = start.elapsed(); + // let time = elapsed.as_nanos(); + // let res = res.iter().map(|e| e.as_ref().unwrap()).collect::>(); + // println!("len = {}", res.len()); + // println!("Time elapsed: {}s", time as f64 / 1e9); + } +} -- cgit v1.2.3