mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 05:40:03 -04:00
eframe app creation refactor (#1363)
* Change how eframe apps are created * eframe: re-export epi::* so users don't need to care about what epi is
This commit is contained in:
@@ -39,24 +39,23 @@ pub use epi::NativeOptions;
|
||||
|
||||
/// Run an egui app
|
||||
#[allow(unsafe_code)]
|
||||
pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||
let persistence = egui_winit::epi::Persistence::from_app_name(app.name());
|
||||
pub fn run(app_name: &str, native_options: &epi::NativeOptions, app_creator: epi::AppCreator) -> ! {
|
||||
let persistence = egui_winit::epi::Persistence::from_app_name(app_name);
|
||||
let window_settings = persistence.load_window_settings();
|
||||
let window_builder =
|
||||
egui_winit::epi::window_builder(native_options, &window_settings).with_title(app.name());
|
||||
egui_winit::epi::window_builder(native_options, &window_settings).with_title(app_name);
|
||||
let event_loop = winit::event_loop::EventLoop::with_user_event();
|
||||
let (gl_window, gl) = create_display(window_builder, &event_loop);
|
||||
let gl = std::rc::Rc::new(gl);
|
||||
|
||||
let mut painter = crate::Painter::new(gl.clone(), None, "")
|
||||
.unwrap_or_else(|error| panic!("some OpenGL error occurred {}\n", error));
|
||||
|
||||
let mut integration = egui_winit::epi::EpiIntegration::new(
|
||||
"egui_glow",
|
||||
painter.max_texture_side(),
|
||||
gl_window.window(),
|
||||
&gl,
|
||||
persistence,
|
||||
app,
|
||||
);
|
||||
|
||||
{
|
||||
@@ -66,6 +65,17 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||
});
|
||||
}
|
||||
|
||||
let mut app = app_creator(&epi::CreationContext {
|
||||
egui_ctx: integration.egui_ctx.clone(),
|
||||
integration_info: integration.frame.info(),
|
||||
storage: integration.persistence.storage(),
|
||||
gl: gl.clone(),
|
||||
});
|
||||
|
||||
if app.warm_up_enabled() {
|
||||
integration.warm_up(app.as_mut(), gl_window.window());
|
||||
}
|
||||
|
||||
let mut is_focused = true;
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
@@ -84,7 +94,7 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||
needs_repaint,
|
||||
textures_delta,
|
||||
shapes,
|
||||
} = integration.update(gl_window.window());
|
||||
} = integration.update(app.as_mut(), gl_window.window());
|
||||
|
||||
integration.handle_platform_output(gl_window.window(), platform_output);
|
||||
|
||||
@@ -92,7 +102,7 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||
|
||||
// paint:
|
||||
{
|
||||
let color = integration.app.clear_color();
|
||||
let color = app.clear_color();
|
||||
unsafe {
|
||||
use glow::HasContext as _;
|
||||
gl.disable(glow::SCISSOR_TEST);
|
||||
@@ -120,7 +130,7 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||
};
|
||||
}
|
||||
|
||||
integration.maybe_autosave(gl_window.window());
|
||||
integration.maybe_autosave(app.as_mut(), gl_window.window());
|
||||
};
|
||||
|
||||
match event {
|
||||
@@ -139,7 +149,7 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||
gl_window.resize(physical_size);
|
||||
}
|
||||
|
||||
integration.on_event(&event);
|
||||
integration.on_event(app.as_mut(), &event);
|
||||
if integration.should_quit() {
|
||||
*control_flow = winit::event_loop::ControlFlow::Exit;
|
||||
}
|
||||
@@ -147,7 +157,10 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||
gl_window.window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
|
||||
}
|
||||
winit::event::Event::LoopDestroyed => {
|
||||
integration.on_exit(gl_window.window());
|
||||
integration
|
||||
.persistence
|
||||
.save(&mut *app, &integration.egui_ctx, gl_window.window());
|
||||
app.on_exit(&gl);
|
||||
painter.destroy();
|
||||
}
|
||||
winit::event::Event::UserEvent(RequestRepaintEvent) => {
|
||||
|
||||
Reference in New Issue
Block a user