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); } }