Skip to main content

egui_async/egui/
mod.rs

1//! `egui` integration for `egui-async`.
2//!
3//! This module provides the [`EguiAsyncPlugin`], which is necessary to integrate
4//! `egui-async` into an `egui` application, alongside async immediate mode widgets.
5
6pub mod bind_ext;
7pub mod widgets;
8
9pub use widgets::*;
10
11use crate::bind;
12
13/// The plugin that drives `egui-async`'s per-frame updates.
14///
15/// This plugin **must be registered** with `egui` for `egui-async` to work.
16/// It is responsible for updating frame timers and setting the global `egui::Context`
17/// so that background tasks can request repaints.
18///
19/// The easiest way to register it is to call `ctx.plugin_or_default::<EguiAsyncPlugin>();`
20/// in your `eframe::App::update` method or equivalent. `egui` ensures this is a
21/// cheap, idempotent operation.
22#[derive(Default)]
23pub struct EguiAsyncPlugin;
24
25impl egui::Plugin for EguiAsyncPlugin {
26    fn debug_name(&self) -> &'static str {
27        "egui_async"
28    }
29
30    fn on_begin_pass(&mut self, ctx: &egui::Context) {
31        bind::CTX.get_or_init(|| ctx.clone());
32        let time = ctx.input(|i| i.time);
33
34        let last_frame = bind::CURR_FRAME.swap(time, std::sync::atomic::Ordering::Relaxed);
35        bind::LAST_FRAME.store(last_frame, std::sync::atomic::Ordering::Relaxed);
36    }
37}