diff options
| author | Andrew Guschin <guschin@altlinux.org> | 2024-09-11 18:12:42 +0400 |
|---|---|---|
| committer | Andrew Guschin <guschin@altlinux.org> | 2024-09-11 18:12:42 +0400 |
| commit | 67b2ce202097c4ce0db3ea81df775405608d527f (patch) | |
| tree | 636c0526df705597acc00b48a411307e8480d0e2 /graph-checker/src/gui.rs | |
| parent | b9796c72d952517e866f729691389723fa381bc0 (diff) | |
wip: add egui
Diffstat (limited to 'graph-checker/src/gui.rs')
| -rw-r--r-- | graph-checker/src/gui.rs | 83 |
1 files changed, 83 insertions, 0 deletions
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<graph::GraphProfile>, + rx: mpsc::Receiver<graph::GraphProfile>, + tx: mpsc::Sender<graph::GraphProfile>, +} + +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::<Vec<_>>(); + // println!("len = {}", res.len()); + // println!("Time elapsed: {}s", time as f64 / 1e9); + } +} |