1
0
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:
Emil Ernerfeldt
2021-11-03 13:45:51 +01:00
committed by GitHub
parent b1716be745
commit 1dbe608e73
14 changed files with 381 additions and 479 deletions

View File

@@ -3,6 +3,7 @@ All notable changes to the `egui_glium` integration will be noted in this file.
## Unreleased
* Simplify `EguiGlium` interface ([#871](https://github.com/emilk/egui/pull/871)).
## 0.15.0 - 2021-10-24

View File

@@ -41,7 +41,7 @@ fn main() {
let event_loop = glutin::event_loop::EventLoop::with_user_event();
let display = create_display(&event_loop);
let mut egui = egui_glium::EguiGlium::new(&display);
let mut egui_glium = egui_glium::EguiGlium::new(&display);
let png_data = include_bytes!("../../eframe/examples/rust-logo-256x256.png");
let image = load_glium_image(png_data);
@@ -51,24 +51,22 @@ fn main() {
// Allow us to share the texture with egui:
let glium_texture = std::rc::Rc::new(glium_texture);
// Allocate egui's texture id for GL texture
let texture_id = egui.painter_mut().register_native_texture(glium_texture);
let texture_id = egui_glium.painter.register_native_texture(glium_texture);
event_loop.run(move |event, _, control_flow| {
let mut redraw = || {
egui.begin_frame(&display);
let mut quit = false;
egui::SidePanel::left("my_side_panel").show(egui.ctx(), |ui| {
ui.heading("");
if ui.button("Quit").clicked() {
quit = true;
}
let (needs_repaint, shapes) = egui_glium.run(&display, |egui_ctx| {
egui::SidePanel::left("my_side_panel").show(egui_ctx, |ui| {
if ui.button("Quit").clicked() {
quit = true;
}
});
egui::Window::new("NativeTextureDisplay").show(egui_ctx, |ui| {
ui.image(texture_id, image_size);
});
});
egui::Window::new("NativeTextureDisplay").show(egui.ctx(), |ui| {
ui.image(texture_id, image_size);
});
let (needs_repaint, shapes) = egui.end_frame(&display);
*control_flow = if quit {
glutin::event_loop::ControlFlow::Exit
@@ -83,17 +81,12 @@ fn main() {
use glium::Surface as _;
let mut target = display.draw();
let clear_color = egui::Rgba::from_rgb(0.1, 0.3, 0.2);
target.clear_color(
clear_color[0],
clear_color[1],
clear_color[2],
clear_color[3],
);
let color = egui::Rgba::from_rgb(0.1, 0.3, 0.2);
target.clear_color(color[0], color[1], color[2], color[3]);
// draw things behind egui here
egui.paint(&display, &mut target, shapes);
egui_glium.paint(&display, &mut target, shapes);
// draw things on top of egui here
@@ -109,11 +102,11 @@ fn main() {
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
glutin::event::Event::WindowEvent { event, .. } => {
if egui.is_quit_event(&event) {
if egui_glium.is_quit_event(&event) {
*control_flow = glium::glutin::event_loop::ControlFlow::Exit;
}
egui.on_event(&event);
egui_glium.on_event(&event);
display.gl_window().window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
}

View File

@@ -23,23 +23,21 @@ fn main() {
let event_loop = glutin::event_loop::EventLoop::with_user_event();
let display = create_display(&event_loop);
let mut egui = egui_glium::EguiGlium::new(&display);
let mut egui_glium = egui_glium::EguiGlium::new(&display);
event_loop.run(move |event, _, control_flow| {
let mut redraw = || {
egui.begin_frame(&display);
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_glium.run(&display, |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(&display);
*control_flow = if quit {
glutin::event_loop::ControlFlow::Exit
} else if needs_repaint {
@@ -58,7 +56,7 @@ fn main() {
// draw things behind egui here
egui.paint(&display, &mut target, shapes);
egui_glium.paint(&display, &mut target, shapes);
// draw things on top of egui here
@@ -74,11 +72,11 @@ fn main() {
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
glutin::event::Event::WindowEvent { event, .. } => {
if egui.is_quit_event(&event) {
if egui_glium.is_quit_event(&event) {
*control_flow = glium::glutin::event_loop::ControlFlow::Exit;
}
egui.on_event(&event);
egui_glium.on_event(&event);
display.gl_window().window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
}

View File

@@ -2,7 +2,6 @@ use crate::*;
use egui::Color32;
#[cfg(target_os = "windows")]
use glium::glutin::platform::windows::WindowBuilderExtWindows;
use std::time::Instant;
impl epi::TextureAllocator for Painter {
fn alloc_srgba_premultiplied(
@@ -45,85 +44,35 @@ fn create_display(
glium::Display::new(window_builder, context_builder, event_loop).unwrap()
}
fn integration_info(
display: &glium::Display,
previous_frame_time: Option<f32>,
) -> epi::IntegrationInfo {
epi::IntegrationInfo {
name: "egui_glium",
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(
display.gl_window().window(),
)),
}
}
// ----------------------------------------------------------------------------
pub use epi::NativeOptions;
/// Run an egui app
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 display = create_display(window_builder, &event_loop);
let repaint_signal = std::sync::Arc::new(GliumRepaintSignal(std::sync::Mutex::new(
event_loop.create_proxy(),
)));
let mut egui = EguiGlium::new(&display);
*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(&display, 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(&display);
let mut integration = egui_winit::epi::EpiIntegration::new(
"egui_glium",
display.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(&display);
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(&display, None),
tex_allocator: painter,
output: &mut app_output,
repaint_signal: repaint_signal.clone(),
}
.build();
app.update(ctx, &mut frame);
let _ = egui.end_frame(&display);
*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 {
@@ -135,41 +84,29 @@ 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(&display);
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(&display, 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(&display);
let frame_time = (Instant::now() - frame_start).as_secs_f64() as f32;
previous_frame_time = Some(frame_time);
let (needs_repaint, shapes) =
integration.update(display.gl_window().window(), &mut painter);
let clipped_meshes = integration.egui_ctx.tessellate(shapes);
{
use glium::Surface as _;
let mut target = display.draw();
let color = app.clear_color();
let color = integration.app.clear_color();
target.clear_color(color[0], color[1], color[2], color[3]);
egui.paint(&display, &mut target, shapes);
painter.paint_meshes(
&display,
&mut target,
integration.egui_ctx.pixels_per_point(),
clipped_meshes,
&integration.egui_ctx.texture(),
);
target.finish().unwrap();
}
{
egui_winit::epi::handle_app_output(
display.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 {
display.gl_window().window().request_redraw();
@@ -179,7 +116,7 @@ pub fn run(mut app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> !
};
}
persistence.maybe_autosave(&mut *app, egui.ctx(), display.gl_window().window());
integration.maybe_autosave(display.gl_window().window());
};
match event {
@@ -190,27 +127,23 @@ 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 = glium::glutin::event_loop::ControlFlow::Exit;
}
if let glutin::event::WindowEvent::Focused(new_focused) = event {
is_focused = new_focused;
}
egui.on_event(&event);
integration.on_event(&event);
if integration.should_quit() {
*control_flow = glium::glutin::event_loop::ControlFlow::Exit;
}
display.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(), display.gl_window().window());
integration.on_exit(display.gl_window().window());
}
glutin::event::Event::UserEvent(RequestRepaintEvent) => {
display.gl_window().window().request_redraw();
}
_ => (),
}
});

View File

@@ -103,9 +103,9 @@ use glium::glutin;
/// Use [`egui`] from a [`glium`] app.
pub struct EguiGlium {
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 EguiGlium {
@@ -117,27 +117,6 @@ impl EguiGlium {
}
}
pub fn ctx(&self) -> &egui::CtxRef {
&self.egui_ctx
}
/// useful for calling e.g. [`crate::Painter::alloc_user_texture`].
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,35 +132,24 @@ impl EguiGlium {
self.egui_winit.is_quit_event(event)
}
pub fn begin_frame(&mut self, display: &glium::Display) {
let raw_input = self.take_raw_input(display);
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, display: &glium::Display) -> egui::RawInput {
self.egui_winit
.take_egui_input(display.gl_window().window())
}
/// Returns `needs_repaint` and shapes to draw.
pub fn end_frame(
pub fn run(
&mut self,
display: &glium::Display,
mut run_ui: impl FnMut(&egui::CtxRef),
) -> (bool, Vec<egui::epaint::ClippedShape>) {
let raw_input = self
.egui_winit
.take_egui_input(display.gl_window().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(display, egui_output);
(needs_repaint, shapes)
}
pub fn handle_output(&mut self, display: &glium::Display, output: egui::Output) {
self.egui_winit
.handle_output(display.gl_window().window(), &self.egui_ctx, output);
.handle_output(display.gl_window().window(), &self.egui_ctx, egui_output);
(needs_repaint, shapes)
}
pub fn paint<T: glium::Surface>(