Struct egui_inbox::UiInbox

source ·
pub struct UiInbox<T> { /* 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 sender = inbox.sender();
                    std::thread::spawn(move || {
                        std::thread::sleep(std::time::Duration::from_secs(1));
                        sender.send(Some("Hello from another thread!".to_string())).ok();
                    });
                }
            });
        },
    )
}

Implementations§

source§

impl<T> UiInbox<T>

source

pub fn new() -> Self

Create a new inbox. The context is grabbed from the [Ui] passed to UiInbox::read, so if you call [UiInbox::send] before UiInbox::read, no repaint is requested. If you want to set the context on creation, use UiInbox::new_with_ctx.

source

pub fn new_with_ctx(ctx: &impl AsRequestRepaint) -> Self

Create a new inbox with a context.

source

pub fn channel() -> (UiInboxSender<T>, Self)

Create a inbox and a sender for it.

source

pub fn channel_with_ctx(ctx: &impl AsRequestRepaint) -> (UiInboxSender<T>, Self)

Create a inbox with a context and a sender for it.

source

pub fn set_ctx(&mut self, ctx: &impl AsRequestRepaint)

Set the [Context] to use for requesting repaints. Usually this is not needed, since the [Context] is grabbed from the [Ui] passed to UiInbox::read.

source

pub fn read(&self, ui: &impl AsRequestRepaint) -> 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 read_without_ctx(&self) -> impl Iterator<Item = T>

Same as UiInbox::read, but you don’t need to pass a reference to [Ui]. If you use this, make sure you set the [Context] with UiInbox::set_ctx or UiInbox::new_with_ctx manually.

source

pub fn replace(&self, ui: &impl AsRequestRepaint, 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.

source

pub fn replace_option(&self, ui: &impl AsRequestRepaint, target: &mut Option<T>)

Replaces the value of the options with Some if there is an item in the inbox. Otherwise, similar to UiInbox::replace.

source

pub fn replace_without_ctx(&self, target: &mut T) -> bool

Same as UiInbox::replace, but you don’t need to pass a reference to [Ui]. If you use this, make sure you set the [Context] with UiInbox::set_ctx or UiInbox::new_with_ctx manually.

source

pub fn sender(&self) -> UiInboxSender<T>

Returns a sender for this inbox.

Trait Implementations§

source§

impl<T> Debug for UiInbox<T>

source§

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

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

impl<T> Default for UiInbox<T>

source§

fn default() -> Self

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

impl<T> Drop for UiInbox<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for UiInbox<T>

§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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, U> TryFrom<U> for T
where 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 T
where 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.