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>

source

pub fn new() -> Self

source

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).

source

pub fn read(&self, ui: &mut Ui) -> impl Iterator<Item = T>

Returns an iterator over all items sent to the inbox. The inbox is cleared after this call.

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.

source

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§

source§

impl<T: Debug> Clone for UiInbox<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug + Debug> Debug for UiInbox<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Debug> Default for UiInbox<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> SerializableAny for Twhere T: 'static + Any + Clone + for<'a> Send + Sync,