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
88
89
|
mod lexer;
mod num;
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) = std::sync::mpsc::sync_channel(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);
let (output_send, output_recv) = async_channel::bounded(1);
let (confirm_send, confirm_recv) = async_channel::bounded(1);
gtk4::gio::spawn_blocking(move || loop {
let Ok(_) = ping_recv.recv() else {
return;
};
let Some(input) = get_mail(mailbox.as_ref()) else {
continue;
};
let Some(result) = parser::parse(&input) else {
continue;
};
output_send.send_blocking(result).unwrap();
confirm_recv.recv_blocking().unwrap();
});
glib::spawn_future_local(clone!(@weak output => async move {
while let Ok(result) = output_recv.recv().await {
output.set_text(&format!("{}", result));
confirm_send.send(()).await.unwrap();
}
}));
window.present();
});
app.run()
}
|