diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 48 |
1 files changed, 4 insertions, 44 deletions
diff --git a/src/main.rs b/src/main.rs index 9fe62b3..8976e17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,17 +8,6 @@ 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() @@ -40,51 +29,22 @@ fn main() -> ExitCode { 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| { + clone!(@weak window, @weak input, @weak output => 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(()); + if let Some(n) = parser::parse(&input.text()) { + output.set_text(&format!("{n}")); + } }), ); window.add_controller(key_controller); - glib::spawn_future_local(async move { - let mut prev_input = String::from(""); - loop { - if ping_recv.recv().await.is_err() { - break; - } - let Some(input) = get_mail(mailbox.as_ref()) else { - continue; - }; - if input == prev_input { - continue; - } - prev_input = input.clone(); - 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(format!("{result}")).unwrap(); - } - }); - let _ = thread_handle.await; - let Ok(result) = output_recv.try_recv() else { - continue; - }; - output.set_text(&result); - } - }); - window.present(); }); |
