aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 08c1ded9778999c3c66cc563fc2b33241d4bb441 (plain) (blame)
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
84
85
86
87
mod eval;
mod lexer;
mod parser;

use gdk4::{Key, ModifierType};
use gtk4::glib::{clone, ExitCode};
use gtk4::prelude::{
    ApplicationExt, ApplicationExtManual, EditableExt, GridExt, GtkWindowExt, WidgetExt,
};
use gtk4::{glib, Application, ApplicationWindow, Entry, EventControllerKey, Grid};
use std::sync::{Arc, Mutex};

fn send_mail<T>(m: &Mutex<Option<T>>, msg: T) {
    let mut guard = m.lock().unwrap();
    *guard = Some(msg);
}

fn get_mail<T>(m: &Mutex<Option<T>>) -> Option<T> {
    let mut guard = m.lock().unwrap();
    std::mem::take(&mut guard)
}

fn main() -> ExitCode {
    let app = Application::builder()
        .application_id("com.github.rhogenson.qc")
        .build();
    app.connect_activate(|app| {
        let window = ApplicationWindow::builder()
            .application(app)
            .decorated(false)
            .resizable(false)
            .build();

        let grid = Grid::new();
        window.set_child(Some(&grid));

        let input = Entry::new();
        grid.attach(&input, 0, 0, 1, 1);

        let output = Entry::builder().editable(false).build();
        grid.attach(&output, 0, 1, 1, 1);

        let (ping_send, ping_recv) = async_channel::bounded(1);
        let mailbox = Arc::new(Mutex::new(None));

        let key_controller = EventControllerKey::new();
        key_controller.connect_key_released(
            clone!(@weak window, @weak input, @strong mailbox => move |_, k, _, modifiers| {
                if k == Key::Escape
                    || k == Key::Return
                    || k == Key::bracketleft && modifiers.contains(ModifierType::CONTROL_MASK)
                {
                    window.destroy();
                }
                send_mail(mailbox.as_ref(), format!("{}", input.text()));
                let _ = ping_send.try_send(());
            }),
        );
        window.add_controller(key_controller);

        glib::spawn_future_local(async move {
            loop {
                if ping_recv.recv().await.is_err() {
                    break;
                }
                let Some(input) = get_mail(mailbox.as_ref()) else {
                    continue;
                };
                let (output_send, output_recv) = async_channel::bounded(1);
                let thread_handle = gtk4::gio::spawn_blocking(move || {
                    if let Some(result) = parser::parse(&input) {
                        output_send.send_blocking(result).unwrap();
                    }
                });
                let _ = thread_handle.await;
                let Ok(result) = output_recv.try_recv() else {
                    continue;
                };
                output.set_text(&format!("{result}"));
            }
        });

        window.present();
    });

    app.run()
}