diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2024-06-07 09:01:40 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2024-06-07 09:01:40 -0700 |
| commit | 6f49283f8a8cd60b329a63fa46c7112c4e4bbb4f (patch) | |
| tree | ec82142d7981eee19875a761370748363362154d /src/main.rs | |
| parent | dbc36c472daeab34f7f71193d3b69edea19d9a23 (diff) | |
| download | qc-6f49283f8a8cd60b329a63fa46c7112c4e4bbb4f.tar.zst | |
Bring back bigint.
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 9c1e107..a372735 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use gtk4::prelude::{ ApplicationExt, ApplicationExtManual, EditableExt, GridExt, GtkWindowExt, WidgetExt, }; use gtk4::{glib, Application, ApplicationWindow, Entry, EventControllerKey, Grid}; +use std::sync::{Arc, Mutex}; fn main() -> ExitCode { let app = Application::builder() @@ -30,18 +31,43 @@ fn main() -> ExitCode { let output = Entry::builder().editable(false).build(); grid.attach(&output, 0, 1, 1, 1); + let mailbox = Arc::new(Mutex::new(String::from(""))); + let (flag_send, flag_recv) = async_channel::bounded(1); + + glib::spawn_future_local({ + let mailbox = mailbox.clone(); + async move { + while let Ok(()) = flag_recv.recv().await { + let input = std::mem::take(&mut *mailbox.lock().unwrap()); + let (result_send, result_recv) = async_channel::bounded(1); + gio::spawn_blocking(move || { + let Some(bytecode) = parser::parse(&input) else { + return; + }; + result_send + .send_blocking(format!("{}", op::eval(bytecode.into_iter()))) + .unwrap(); + }) + .await + .unwrap(); + if let Ok(result) = result_recv.try_recv() { + output.set_text(&result); + } + } + } + }); + let key_controller = EventControllerKey::new(); key_controller.connect_key_released( - clone!(@weak window, @weak input, @weak output => move |_, k, _, modifiers| { + clone!(@weak window, @weak input => move |_, k, _, modifiers| { if k == Key::Escape || k == Key::Return || k == Key::bracketleft && modifiers.contains(ModifierType::CONTROL_MASK) { window.destroy(); } - if let Some(bytecode) = parser::parse(&input.text()) { - output.set_text(&format!("{}", op::eval(&bytecode))); - } + *mailbox.lock().unwrap() = input.text().to_string(); + let _ = flag_send.try_send(()); }), ); window.add_controller(key_controller); |
