Skip to content

Commit

Permalink
Continue execution after closing native eframe window (#1889)
Browse files Browse the repository at this point in the history
This adds `NativeOptions::run_and_return` with default value `true`.

If `true`, execution will continue after the eframe window is closed. This is a new behavior introduced in this PR.
If `false`, the app will close once the eframe window is closed. The is the old behavior.

This is `true` by default, and the `false` option is only there so we can revert if we find any bugs.

When `true`, [`winit::platform::run_return::EventLoopExtRunReturn::run_return`](https://docs.rs/winit/latest/winit/platform/run_return/trait.EventLoopExtRunReturn.html#tymethod.run_return) is used. The winit docs warns of its usage, recommending `EventLoop::run`, but 🤷 
When `false`, [`winit::event_loop::EventLoop::run`](https://docs.rs/winit/latest/winit/event_loop/struct.EventLoop.html#method.run) is used.

This is a really useful feature. You can now use `eframe` to quickly open up a window and show some data or options, and then continue your program after the `eframe` window is closed

My previous attempt at this caused some problems, but my new attempt seems to be working much better, at least on my Mac.
  • Loading branch information
emilk committed Aug 5, 2022
1 parent e3f993d commit 66c601f
Show file tree
Hide file tree
Showing 3 changed files with 524 additions and 242 deletions.
15 changes: 15 additions & 0 deletions eframe/src/epi.rs
Expand Up @@ -269,6 +269,20 @@ pub struct NativeOptions {
///
/// Default: `Theme::Dark`.
pub default_theme: Theme,

/// This controls what happens when you close the main eframe window.
///
/// If `true`, execution will continue after the eframe window is closed.
/// If `false`, the app will close once the eframe window is closed.
///
/// This is `true` by default, and the `false` option is only there
/// so we can revert if we find any bugs.
///
/// This feature was introduced in <https://github.com/emilk/egui/pull/1889>.
///
/// When `true`, [`winit::platform::run_return::EventLoopExtRunReturn::run_return`] is used.
/// When `false`, [`winit::event_loop::EventLoop::run`] is used.
pub run_and_return: bool,
}

#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -295,6 +309,7 @@ impl Default for NativeOptions {
renderer: Renderer::default(),
follow_system_theme: cfg!(target_os = "macos") || cfg!(target_os = "windows"),
default_theme: Theme::Dark,
run_and_return: true,
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions eframe/src/lib.rs
Expand Up @@ -161,20 +161,20 @@ mod native;
/// ```
#[cfg(not(target_arch = "wasm32"))]
#[allow(clippy::needless_pass_by_value)]
pub fn run_native(app_name: &str, native_options: NativeOptions, app_creator: AppCreator) -> ! {
pub fn run_native(app_name: &str, native_options: NativeOptions, app_creator: AppCreator) {
let renderer = native_options.renderer;

match renderer {
#[cfg(feature = "glow")]
Renderer::Glow => {
tracing::debug!("Using the glow renderer");
native::run::run_glow(app_name, &native_options, app_creator)
native::run::run_glow(app_name, &native_options, app_creator);
}

#[cfg(feature = "wgpu")]
Renderer::Wgpu => {
tracing::debug!("Using the wgpu renderer");
native::run::run_wgpu(app_name, &native_options, app_creator)
native::run::run_wgpu(app_name, &native_options, app_creator);
}
}
}
Expand Down

0 comments on commit 66c601f

Please sign in to comment.