1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 06:40:06 -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:
Emil Ernerfeldt
2022-03-16 15:39:48 +01:00
committed by GitHub
parent c768d1d48e
commit c8f6cae362
26 changed files with 387 additions and 444 deletions

View File

@@ -2,42 +2,6 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
fn create_display(
event_loop: &glutin::event_loop::EventLoop<()>,
) -> (
glutin::WindowedContext<glutin::PossiblyCurrent>,
glow::Context,
) {
let window_builder = glutin::window::WindowBuilder::new()
.with_resizable(true)
.with_inner_size(glutin::dpi::LogicalSize {
width: 800.0,
height: 600.0,
})
.with_title("egui_glow example");
let gl_window = unsafe {
glutin::ContextBuilder::new()
.with_depth_buffer(0)
.with_srgb(true)
.with_stencil_buffer(0)
.with_vsync(true)
.build_windowed(window_builder, event_loop)
.unwrap()
.make_current()
.unwrap()
};
let gl = unsafe { glow::Context::from_loader_function(|s| gl_window.get_proc_address(s)) };
unsafe {
use glow::HasContext as _;
gl.enable(glow::FRAMEBUFFER_SRGB);
}
(gl_window, gl)
}
fn main() {
let mut clear_color = [0.1, 0.1, 0.1];
@@ -116,3 +80,39 @@ fn main() {
}
});
}
fn create_display(
event_loop: &glutin::event_loop::EventLoop<()>,
) -> (
glutin::WindowedContext<glutin::PossiblyCurrent>,
glow::Context,
) {
let window_builder = glutin::window::WindowBuilder::new()
.with_resizable(true)
.with_inner_size(glutin::dpi::LogicalSize {
width: 800.0,
height: 600.0,
})
.with_title("egui_glow example");
let gl_window = unsafe {
glutin::ContextBuilder::new()
.with_depth_buffer(0)
.with_srgb(true)
.with_stencil_buffer(0)
.with_vsync(true)
.build_windowed(window_builder, event_loop)
.unwrap()
.make_current()
.unwrap()
};
let gl = unsafe { glow::Context::from_loader_function(|s| gl_window.get_proc_address(s)) };
unsafe {
use glow::HasContext as _;
gl.enable(glow::FRAMEBUFFER_SRGB);
}
(gl_window, gl)
}

View File

@@ -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) => {