Struct egui_inbox::UiInbox 
source · pub struct UiInbox<T: Debug>(/* private fields */);Expand description
Utility to send messages to egui views from async functions, callbacks, etc. without having to use interior mutability. Example:
use eframe::egui;
use egui::CentralPanel;
use egui_inbox::UiInbox;
pub fn main() -> eframe::Result<()> {
    let mut inbox = UiInbox::new();
    let mut state = None;
    eframe::run_simple_native(
        "DnD Simple Example",
        Default::default(),
        move |ctx, _frame| {
            CentralPanel::default().show(ctx, |ui| {
                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 mut inbox_clone = inbox.clone();
                    std::thread::spawn(move || {
                        std::thread::sleep(std::time::Duration::from_secs(1));
                        inbox_clone.send(Some("Hello from another thread!".to_string()));
                    });
                }
            });
        },
    )
}Implementations§
source§impl<T: Debug> UiInbox<T>
 
impl<T: Debug> UiInbox<T>
pub fn new() -> Self
sourcepub fn send(&self, item: T)
 
pub fn send(&self, item: T)
Send an item to the inbox.
Calling this will request a repaint from egui.
If this is called before a call to UiInbox::read was done, no repaint is requested
(Since we didn’t have a chance to get a reference to Context yet).
sourcepub fn replace(&self, ui: &mut Ui, target: &mut T) -> bool
 
pub fn replace(&self, ui: &mut Ui, target: &mut T) -> bool
Replaces the value of target with the last item sent to the inbox.
Any other updates are discarded.
If no item was sent to the inbox, target is not updated.
Returns true if target was updated.
The ui is only passed here so we can grab a reference to Context. This is mostly done for convenience, so you don’t have to pass a reference to Context to every struct that uses an inbox on creation.
Trait Implementations§
Auto Trait Implementations§
impl<T> RefUnwindSafe for UiInbox<T>
impl<T> Send for UiInbox<T>where T: Send,
impl<T> Sync for UiInbox<T>where T: Send,
impl<T> Unpin for UiInbox<T>
impl<T> UnwindSafe for UiInbox<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more