mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 04:40:03 -04:00
Refactor integrations (#871)
* Unify code in egui_glium and egui_glow into egui_winit::EpiIntegration * Simplify `EguiGlium` interface * Simplify `EguiGlow` interface * egui_web refactor: merge `WebBackend` into `AppRunner`
This commit is contained in:
@@ -3,6 +3,7 @@ All notable changes to the `egui_glow` integration will be noted in this file.
|
||||
|
||||
|
||||
## Unreleased
|
||||
* Simplify `EguiGlow` interface ([#871](https://github.com/emilk/egui/pull/871)).
|
||||
|
||||
|
||||
## 0.15.0 - 2021-10-24
|
||||
|
||||
@@ -40,23 +40,21 @@ fn main() {
|
||||
let event_loop = glutin::event_loop::EventLoop::with_user_event();
|
||||
let (gl_window, gl) = create_display(&event_loop);
|
||||
|
||||
let mut egui = egui_glow::EguiGlow::new(&gl_window, &gl);
|
||||
let mut egui_glow = egui_glow::EguiGlow::new(&gl_window, &gl);
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
let mut redraw = || {
|
||||
egui.begin_frame(gl_window.window());
|
||||
|
||||
let mut quit = false;
|
||||
|
||||
egui::SidePanel::left("my_side_panel").show(egui.ctx(), |ui| {
|
||||
ui.heading("Hello World!");
|
||||
if ui.button("Quit").clicked() {
|
||||
quit = true;
|
||||
}
|
||||
let (needs_repaint, shapes) = egui_glow.run(gl_window.window(), |egui_ctx| {
|
||||
egui::SidePanel::left("my_side_panel").show(egui_ctx, |ui| {
|
||||
ui.heading("Hello World!");
|
||||
if ui.button("Quit").clicked() {
|
||||
quit = true;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let (needs_repaint, shapes) = egui.end_frame(gl_window.window());
|
||||
|
||||
*control_flow = if quit {
|
||||
glutin::event_loop::ControlFlow::Exit
|
||||
} else if needs_repaint {
|
||||
@@ -76,7 +74,7 @@ fn main() {
|
||||
|
||||
// draw things behind egui here
|
||||
|
||||
egui.paint(&gl_window, &gl, shapes);
|
||||
egui_glow.paint(&gl_window, &gl, shapes);
|
||||
|
||||
// draw things on top of egui here
|
||||
|
||||
@@ -92,7 +90,7 @@ fn main() {
|
||||
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
|
||||
|
||||
glutin::event::Event::WindowEvent { event, .. } => {
|
||||
if egui.is_quit_event(&event) {
|
||||
if egui_glow.is_quit_event(&event) {
|
||||
*control_flow = glutin::event_loop::ControlFlow::Exit;
|
||||
}
|
||||
|
||||
@@ -100,12 +98,12 @@ fn main() {
|
||||
gl_window.resize(physical_size);
|
||||
}
|
||||
|
||||
egui.on_event(&event);
|
||||
egui_glow.on_event(&event);
|
||||
|
||||
gl_window.window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
|
||||
}
|
||||
glutin::event::Event::LoopDestroyed => {
|
||||
egui.destroy(&gl);
|
||||
egui_glow.destroy(&gl);
|
||||
}
|
||||
|
||||
_ => (),
|
||||
|
||||
@@ -2,7 +2,6 @@ use crate::*;
|
||||
use egui::Color32;
|
||||
#[cfg(target_os = "windows")]
|
||||
use glutin::platform::windows::WindowBuilderExtWindows;
|
||||
use std::time::Instant;
|
||||
|
||||
impl epi::TextureAllocator for Painter {
|
||||
fn alloc_srgba_premultiplied(
|
||||
@@ -60,84 +59,36 @@ fn create_display(
|
||||
(gl_window, gl)
|
||||
}
|
||||
|
||||
fn integration_info(
|
||||
window: &glutin::window::Window,
|
||||
previous_frame_time: Option<f32>,
|
||||
) -> epi::IntegrationInfo {
|
||||
epi::IntegrationInfo {
|
||||
name: "egui_glow",
|
||||
web_info: None,
|
||||
prefer_dark_mode: None, // TODO: figure out system default
|
||||
cpu_usage: previous_frame_time,
|
||||
native_pixels_per_point: Some(egui_winit::native_pixels_per_point(window)),
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
pub use epi::NativeOptions;
|
||||
|
||||
/// Run an egui app
|
||||
#[allow(unsafe_code)]
|
||||
pub fn run(mut app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||
let mut persistence = egui_winit::epi::Persistence::from_app_name(app.name());
|
||||
|
||||
pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||
let persistence = egui_winit::epi::Persistence::from_app_name(app.name());
|
||||
let window_settings = persistence.load_window_settings();
|
||||
let event_loop = glutin::event_loop::EventLoop::with_user_event();
|
||||
let window_builder =
|
||||
egui_winit::epi::window_builder(native_options, &window_settings).with_title(app.name());
|
||||
let event_loop = glutin::event_loop::EventLoop::with_user_event();
|
||||
let (gl_window, gl) = create_display(window_builder, &event_loop);
|
||||
|
||||
let repaint_signal = std::sync::Arc::new(GlowRepaintSignal(std::sync::Mutex::new(
|
||||
event_loop.create_proxy(),
|
||||
)));
|
||||
|
||||
let mut egui = EguiGlow::new(&gl_window, &gl);
|
||||
*egui.ctx().memory() = persistence.load_memory().unwrap_or_default();
|
||||
|
||||
{
|
||||
let (ctx, painter) = egui.ctx_and_painter_mut();
|
||||
let mut app_output = epi::backend::AppOutput::default();
|
||||
let mut frame = epi::backend::FrameBuilder {
|
||||
info: integration_info(gl_window.window(), None),
|
||||
tex_allocator: painter,
|
||||
output: &mut app_output,
|
||||
repaint_signal: repaint_signal.clone(),
|
||||
}
|
||||
.build();
|
||||
app.setup(ctx, &mut frame, persistence.storage());
|
||||
}
|
||||
|
||||
let mut previous_frame_time = None;
|
||||
let mut painter = crate::Painter::new(&gl);
|
||||
let mut integration = egui_winit::epi::EpiIntegration::new(
|
||||
"egui_glow",
|
||||
gl_window.window(),
|
||||
&mut painter,
|
||||
repaint_signal,
|
||||
persistence,
|
||||
app,
|
||||
);
|
||||
|
||||
let mut is_focused = true;
|
||||
|
||||
if app.warm_up_enabled() {
|
||||
let saved_memory = egui.ctx().memory().clone();
|
||||
egui.ctx().memory().set_everything_is_visible(true);
|
||||
|
||||
egui.begin_frame(gl_window.window());
|
||||
let (ctx, painter) = egui.ctx_and_painter_mut();
|
||||
let mut app_output = epi::backend::AppOutput::default();
|
||||
let mut frame = epi::backend::FrameBuilder {
|
||||
info: integration_info(gl_window.window(), None),
|
||||
tex_allocator: painter,
|
||||
output: &mut app_output,
|
||||
repaint_signal: repaint_signal.clone(),
|
||||
}
|
||||
.build();
|
||||
|
||||
app.update(ctx, &mut frame);
|
||||
|
||||
let _ = egui.end_frame(gl_window.window());
|
||||
|
||||
*egui.ctx().memory() = saved_memory; // We don't want to remember that windows were huge.
|
||||
egui.ctx().clear_animations();
|
||||
|
||||
// TODO: handle app_output
|
||||
// eprintln!("Warmed up in {} ms", warm_up_start.elapsed().as_millis())
|
||||
}
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
let mut redraw = || {
|
||||
if !is_focused {
|
||||
@@ -149,44 +100,31 @@ pub fn run(mut app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> !
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
|
||||
let frame_start = std::time::Instant::now();
|
||||
|
||||
egui.begin_frame(gl_window.window());
|
||||
let (ctx, painter) = egui.ctx_and_painter_mut();
|
||||
let mut app_output = epi::backend::AppOutput::default();
|
||||
let mut frame = epi::backend::FrameBuilder {
|
||||
info: integration_info(gl_window.window(), previous_frame_time),
|
||||
tex_allocator: painter,
|
||||
output: &mut app_output,
|
||||
repaint_signal: repaint_signal.clone(),
|
||||
}
|
||||
.build();
|
||||
app.update(ctx, &mut frame);
|
||||
let (needs_repaint, shapes) = egui.end_frame(gl_window.window());
|
||||
|
||||
let frame_time = (Instant::now() - frame_start).as_secs_f64() as f32;
|
||||
previous_frame_time = Some(frame_time);
|
||||
let (needs_repaint, shapes) = integration.update(gl_window.window(), &mut painter);
|
||||
let clipped_meshes = integration.egui_ctx.tessellate(shapes);
|
||||
|
||||
{
|
||||
let color = app.clear_color();
|
||||
let color = integration.app.clear_color();
|
||||
unsafe {
|
||||
use glow::HasContext as _;
|
||||
gl.disable(glow::SCISSOR_TEST);
|
||||
gl.clear_color(color[0], color[1], color[2], color[3]);
|
||||
gl.clear(glow::COLOR_BUFFER_BIT);
|
||||
}
|
||||
egui.paint(&gl_window, &gl, shapes);
|
||||
|
||||
painter.paint_meshes(
|
||||
&gl_window,
|
||||
&gl,
|
||||
integration.egui_ctx.pixels_per_point(),
|
||||
clipped_meshes,
|
||||
&integration.egui_ctx.texture(),
|
||||
);
|
||||
|
||||
gl_window.swap_buffers().unwrap();
|
||||
}
|
||||
|
||||
{
|
||||
egui_winit::epi::handle_app_output(
|
||||
gl_window.window(),
|
||||
egui.ctx().pixels_per_point(),
|
||||
app_output.clone(),
|
||||
);
|
||||
|
||||
*control_flow = if app_output.quit {
|
||||
*control_flow = if integration.should_quit() {
|
||||
glutin::event_loop::ControlFlow::Exit
|
||||
} else if needs_repaint {
|
||||
gl_window.window().request_redraw();
|
||||
@@ -196,7 +134,7 @@ pub fn run(mut app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> !
|
||||
};
|
||||
}
|
||||
|
||||
persistence.maybe_autosave(&mut *app, egui.ctx(), gl_window.window());
|
||||
integration.maybe_autosave(gl_window.window());
|
||||
};
|
||||
|
||||
match event {
|
||||
@@ -207,10 +145,6 @@ pub fn run(mut app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> !
|
||||
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
|
||||
|
||||
glutin::event::Event::WindowEvent { event, .. } => {
|
||||
if egui.is_quit_event(&event) {
|
||||
*control_flow = glutin::event_loop::ControlFlow::Exit;
|
||||
}
|
||||
|
||||
if let glutin::event::WindowEvent::Focused(new_focused) = event {
|
||||
is_focused = new_focused;
|
||||
}
|
||||
@@ -219,20 +153,20 @@ pub fn run(mut app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> !
|
||||
gl_window.resize(physical_size);
|
||||
}
|
||||
|
||||
egui.on_event(&event);
|
||||
integration.on_event(&event);
|
||||
if integration.should_quit() {
|
||||
*control_flow = glutin::event_loop::ControlFlow::Exit;
|
||||
}
|
||||
|
||||
gl_window.window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
|
||||
}
|
||||
glutin::event::Event::LoopDestroyed => {
|
||||
app.on_exit();
|
||||
persistence.save(&mut *app, egui.ctx(), gl_window.window());
|
||||
egui.destroy(&gl);
|
||||
integration.on_exit(gl_window.window());
|
||||
painter.destroy(&gl);
|
||||
}
|
||||
|
||||
glutin::event::Event::UserEvent(RequestRepaintEvent) => {
|
||||
gl_window.window().request_redraw();
|
||||
}
|
||||
|
||||
_ => (),
|
||||
}
|
||||
});
|
||||
|
||||
@@ -101,9 +101,9 @@ pub use egui_winit;
|
||||
|
||||
/// Use [`egui`] from a [`glow`] app.
|
||||
pub struct EguiGlow {
|
||||
egui_ctx: egui::CtxRef,
|
||||
egui_winit: egui_winit::State,
|
||||
painter: crate::Painter,
|
||||
pub egui_ctx: egui::CtxRef,
|
||||
pub egui_winit: egui_winit::State,
|
||||
pub painter: crate::Painter,
|
||||
}
|
||||
|
||||
impl EguiGlow {
|
||||
@@ -118,26 +118,6 @@ impl EguiGlow {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ctx(&self) -> &egui::CtxRef {
|
||||
&self.egui_ctx
|
||||
}
|
||||
|
||||
pub fn painter_mut(&mut self) -> &mut crate::Painter {
|
||||
&mut self.painter
|
||||
}
|
||||
|
||||
pub fn ctx_and_painter_mut(&mut self) -> (&egui::CtxRef, &mut crate::Painter) {
|
||||
(&self.egui_ctx, &mut self.painter)
|
||||
}
|
||||
|
||||
pub fn pixels_per_point(&self) -> f32 {
|
||||
self.egui_winit.pixels_per_point()
|
||||
}
|
||||
|
||||
pub fn egui_input(&self) -> &egui::RawInput {
|
||||
self.egui_winit.egui_input()
|
||||
}
|
||||
|
||||
/// Returns `true` if egui wants exclusive use of this event
|
||||
/// (e.g. a mouse click on an egui window, or entering text into a text field).
|
||||
/// For instance, if you use egui for a game, you want to first call this
|
||||
@@ -153,34 +133,22 @@ impl EguiGlow {
|
||||
self.egui_winit.is_quit_event(event)
|
||||
}
|
||||
|
||||
pub fn begin_frame(&mut self, window: &glutin::window::Window) {
|
||||
let raw_input = self.take_raw_input(window);
|
||||
self.begin_frame_with_input(raw_input);
|
||||
}
|
||||
|
||||
pub fn begin_frame_with_input(&mut self, raw_input: egui::RawInput) {
|
||||
self.egui_ctx.begin_frame(raw_input);
|
||||
}
|
||||
|
||||
/// Prepare for a new frame. Normally you would call [`Self::begin_frame`] instead.
|
||||
pub fn take_raw_input(&mut self, window: &glutin::window::Window) -> egui::RawInput {
|
||||
self.egui_winit.take_egui_input(window)
|
||||
}
|
||||
|
||||
/// Returns `needs_repaint` and shapes to draw.
|
||||
pub fn end_frame(
|
||||
pub fn run(
|
||||
&mut self,
|
||||
window: &glutin::window::Window,
|
||||
mut run_ui: impl FnMut(&egui::CtxRef),
|
||||
) -> (bool, Vec<egui::epaint::ClippedShape>) {
|
||||
let raw_input = self.egui_winit.take_egui_input(window);
|
||||
self.egui_ctx.begin_frame(raw_input);
|
||||
|
||||
run_ui(&self.egui_ctx);
|
||||
|
||||
let (egui_output, shapes) = self.egui_ctx.end_frame();
|
||||
let needs_repaint = egui_output.needs_repaint;
|
||||
self.handle_output(window, egui_output);
|
||||
(needs_repaint, shapes)
|
||||
}
|
||||
|
||||
pub fn handle_output(&mut self, window: &glutin::window::Window, output: egui::Output) {
|
||||
self.egui_winit
|
||||
.handle_output(window, &self.egui_ctx, output);
|
||||
.handle_output(window, &self.egui_ctx, egui_output);
|
||||
(needs_repaint, shapes)
|
||||
}
|
||||
|
||||
pub fn paint(
|
||||
@@ -199,6 +167,7 @@ impl EguiGlow {
|
||||
);
|
||||
}
|
||||
|
||||
/// Call to release the allocated graphics resources.
|
||||
pub fn destroy(&mut self, gl: &glow::Context) {
|
||||
self.painter.destroy(gl);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user