summaryrefslogtreecommitdiff
path: root/graph-checker/src/gui.rs
blob: 2a363e2087a80030e9f20412ef9fb3c1a170da99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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);
    }
}