#egui #interior-mutability #async #send-message #callback #views #update

egui_inbox

Utility to send messages to egui views from async functions, callbacks, etc. without having to use interior mutability.

8 releases (5 breaking)

0.6.0 Oct 3, 2024
0.5.0 Jul 3, 2024
0.4.1 May 6, 2024
0.4.0 Apr 2, 2024
0.1.0 Aug 20, 2023

#220 in Concurrency

Download history 17/week @ 2024-08-23 41/week @ 2024-08-30 13/week @ 2024-09-06 31/week @ 2024-09-13 26/week @ 2024-09-20 136/week @ 2024-09-27 78/week @ 2024-10-04 47/week @ 2024-10-11 21/week @ 2024-10-18 20/week @ 2024-10-25 44/week @ 2024-11-01 10/week @ 2024-11-08 58/week @ 2024-11-15 51/week @ 2024-11-22 49/week @ 2024-11-29 56/week @ 2024-12-06

216 downloads per month
Used in 7 crates (6 directly)

MIT license

42KB
493 lines

egui_inbox

egui_ver Latest version Documentation unsafe forbidden License

Channel to send messages to egui views from async functions, callbacks, etc. without having to use interior mutability. Will automatically call request_repaint() on the Ui when a message is received.

The goal of this crate is to make interfacing with egui from asynchronous code as easy as possible. Currently it is not optimized for performance, so if you expect to send 1000s of updates per frame you might want to use e.g. std::sync::mpsc instead. Performance might still be improved in the future though.

Example:

use eframe::egui;
use egui::CentralPanel;
use egui_inbox::UiInbox;

pub fn main() -> eframe::Result<()> {
    let inbox = UiInbox::new();
    let mut state = None;

    eframe::run_simple_native(
        "DnD Simple Example",
        Default::default(),
        move |ctx, _frame| {
            CentralPanel::default().show(ctx, |ui| {
                // `read` will return an iterator over all pending messages
                if let Some(last) = inbox.read(ui).last() {
                    state = last;
                }
                // There also is a `replace` method that you can use as a shorthand for the above:
                // inbox.replace(ui, &mut state);

                ui.label(format!("State: {:?}", state));
                if ui.button("Async Task").clicked() {
                    state = Some("Waiting for async task to complete".to_string());
                    let tx = inbox.sender();
                    std::thread::spawn(move || {
                        std::thread::sleep(std::time::Duration::from_secs(1));
                        // Send will return an error if the receiver has been dropped
                        // but unless you have a long running task that will send multiple messages
                        // you can just ignore the error
                        tx.send(Some("Hello from another thread!".to_string())).ok();
                    });
                }
            });
        },
    )
}

Dependencies

~4–11MB
~131K SLoC