Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow eframe apps to be detached #3340

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions crates/eframe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ pub use web::{WebLogger, WebRunner};
#[cfg(any(feature = "glow", feature = "wgpu"))]
mod native;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "glow", feature = "wgpu"))]
pub use native::run::{Detached, DetachedResult};

#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "glow", feature = "wgpu"))]
#[cfg(feature = "persistence")]
Expand Down Expand Up @@ -295,6 +299,94 @@ pub fn run_simple_native(

// ----------------------------------------------------------------------------

/// Execute an app in a detached state.
///
/// This allows you to control the event loop itself, which is necessary in a few
/// occasions, like when you need a screen not managed by `egui`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// occasions, like when you need a screen not managed by `egui`.
/// occasions, like when you need a screen not managed by `eframe`.

///
/// See [`Detached`] for more info on how to run detached apps.
///
/// # Example
/// ``` no_run
/// use eframe::DetachedResult;
/// use winit::event_loop::ControlFlow;
///
/// fn main() {
/// let native_options = eframe::NativeOptions::default();
/// let event_loop = eframe::EventLoopBuilder::<eframe::UserEvent>::with_user_event().build();
/// let mut detached = eframe::run_detached_native(
/// "MyApp",
/// &event_loop,
/// native_options,
/// Box::new(|cc| Box::new(MyEguiApp::new(cc))),
/// );
/// event_loop.run(move |event, event_loop, control_flow| {
/// *control_flow = match detached.on_event(&event, event_loop).unwrap() {
/// DetachedResult::UpdateNext => ControlFlow::Poll,
/// DetachedResult::UpdateAt(next_repaint) => ControlFlow::WaitUntil(next_repaint),
/// DetachedResult::Exit => ControlFlow::Exit,
/// }
/// })
/// }
///
/// #[derive(Default)]
/// struct MyEguiApp {}
///
/// impl MyEguiApp {
/// fn new(cc: &eframe::CreationContext<'_>) -> Self {
/// // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals.
/// // Restore app state using cc.storage (requires the "persistence" feature).
/// // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
/// // for e.g. egui::PaintCallback.
/// Self::default()
/// }
/// }
///
/// impl eframe::App for MyEguiApp {
/// fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
/// egui::CentralPanel::default().show(ctx, |ui| {
/// ui.heading("Hello World!");
/// });
/// }
/// }
/// ```
#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "glow", feature = "wgpu"))]
#[allow(clippy::needless_pass_by_value)]
pub fn run_detached_native(
app_name: &str,
event_loop: &winit::event_loop::EventLoop<UserEvent>,
native_options: NativeOptions,
app_creator: AppCreator,
) -> Box<dyn Detached> {
let renderer = native_options.renderer;

match renderer {
#[cfg(feature = "glow")]
Renderer::Glow => {
log::debug!("Using the glow renderer");
Box::new(native::run::detached_glow(
app_name,
event_loop,
native_options,
app_creator,
))
}
#[cfg(feature = "wgpu")]
Renderer::Wgpu => {
log::debug!("Using the wgpu renderer");
Box::new(native::run::detached_wgpu(
app_name,
event_loop,
native_options,
app_creator,
))
}
}
}

// ----------------------------------------------------------------------------

/// The different problems that can occur when trying to run `eframe`.
#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down
Loading