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>
impl<T> UiInbox<T>
sourcepub fn new() -> Self
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.
sourcepub fn new_with_ctx(ctx: &impl AsRequestRepaint) -> Self
pub fn new_with_ctx(ctx: &impl AsRequestRepaint) -> Self
Create a new inbox with a context.
sourcepub fn channel() -> (UiInboxSender<T>, Self)
pub fn channel() -> (UiInboxSender<T>, Self)
Create a inbox and a sender for it.
sourcepub fn channel_with_ctx(ctx: &impl AsRequestRepaint) -> (UiInboxSender<T>, Self)
pub fn channel_with_ctx(ctx: &impl AsRequestRepaint) -> (UiInboxSender<T>, Self)
Create a inbox with a context and a sender for it.
sourcepub fn set_ctx(&mut self, ctx: &impl AsRequestRepaint)
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.
sourcepub fn read(&self, ui: &impl AsRequestRepaint) -> impl Iterator<Item = T>
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.
sourcepub fn read_without_ctx(&self) -> impl Iterator<Item = T>
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.
sourcepub fn replace(&self, ui: &impl AsRequestRepaint, target: &mut T) -> bool
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.
sourcepub fn replace_option(&self, ui: &impl AsRequestRepaint, target: &mut Option<T>)
pub fn replace_option(&self, ui: &impl AsRequestRepaint, target: &mut Option<T>)
Replaces the value of the options with Some
sourcepub fn replace_without_ctx(&self, target: &mut T) -> bool
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.
sourcepub fn sender(&self) -> UiInboxSender<T>
pub fn sender(&self) -> UiInboxSender<T>
Returns a sender for this inbox.