mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
Deduplicate code found in both egui_glium and egui_glow (#819)
* Move window building to egui-winit * Move icon loading to egui-winit * `use glow::HasContext;` -> `use glow::HasContext as _;` * Move FileStorage into epi behind a feature flag * De-duplicate screen_size_in_pixels and native_pixels_per_point * Move creation of FileStorage to epi * Handle epi app output (window size changes etc) in egui-winit * Move app and memory persistence and autosave logic to egui-winit * fix check.sh * Make the epi backend opt-in for egui_glium and egui_glow * Fix persistence * Add integration name to epi::IntegrationInfo and the demo * Clean up Cargo.toml files and fix making egui_glium optional * fix typo * Make egui_glium compile without the `epi` feature
This commit is contained in:
239
egui_glow/src/epi_backend.rs
Normal file
239
egui_glow/src/epi_backend.rs
Normal file
@@ -0,0 +1,239 @@
|
||||
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(
|
||||
&mut self,
|
||||
size: (usize, usize),
|
||||
srgba_pixels: &[Color32],
|
||||
) -> egui::TextureId {
|
||||
let id = self.alloc_user_texture();
|
||||
self.set_user_texture(id, size, srgba_pixels);
|
||||
id
|
||||
}
|
||||
|
||||
fn free(&mut self, id: egui::TextureId) {
|
||||
self.free_user_texture(id)
|
||||
}
|
||||
}
|
||||
|
||||
struct RequestRepaintEvent;
|
||||
|
||||
struct GlowRepaintSignal(std::sync::Mutex<glutin::event_loop::EventLoopProxy<RequestRepaintEvent>>);
|
||||
|
||||
impl epi::RepaintSignal for GlowRepaintSignal {
|
||||
fn request_repaint(&self) {
|
||||
self.0.lock().unwrap().send_event(RequestRepaintEvent).ok();
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
fn create_display(
|
||||
window_builder: glutin::window::WindowBuilder,
|
||||
event_loop: &glutin::event_loop::EventLoop<RequestRepaintEvent>,
|
||||
) -> (
|
||||
glutin::WindowedContext<glutin::PossiblyCurrent>,
|
||||
glow::Context,
|
||||
) {
|
||||
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 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());
|
||||
|
||||
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 (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 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 {
|
||||
// On Mac, a minimized Window uses up all CPU: https://github.com/emilk/egui/issues/325
|
||||
// We can't know if we are minimized: https://github.com/rust-windowing/winit/issues/208
|
||||
// But we know if we are focused (in foreground). When minimized, we are not focused.
|
||||
// However, a user may want an egui with an animation in the background,
|
||||
// so we still need to repaint quite fast.
|
||||
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 color = 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);
|
||||
gl_window.swap_buffers().unwrap();
|
||||
}
|
||||
|
||||
{
|
||||
egui_winit::epi::handle_app_output(
|
||||
gl_window.window(),
|
||||
egui.ctx().pixels_per_point(),
|
||||
app_output,
|
||||
);
|
||||
|
||||
*control_flow = if app_output.quit {
|
||||
glutin::event_loop::ControlFlow::Exit
|
||||
} else if needs_repaint {
|
||||
gl_window.window().request_redraw();
|
||||
glutin::event_loop::ControlFlow::Poll
|
||||
} else {
|
||||
glutin::event_loop::ControlFlow::Wait
|
||||
};
|
||||
}
|
||||
|
||||
persistence.maybe_autosave(&mut *app, egui.ctx(), gl_window.window());
|
||||
};
|
||||
|
||||
match event {
|
||||
// Platform-dependent event handlers to workaround a winit bug
|
||||
// See: https://github.com/rust-windowing/winit/issues/987
|
||||
// See: https://github.com/rust-windowing/winit/issues/1619
|
||||
glutin::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
|
||||
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;
|
||||
}
|
||||
|
||||
if let glutin::event::WindowEvent::Resized(physical_size) = event {
|
||||
gl_window.resize(physical_size);
|
||||
}
|
||||
|
||||
egui.on_event(&event);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
glutin::event::Event::UserEvent(RequestRepaintEvent) => {
|
||||
gl_window.window().request_redraw();
|
||||
}
|
||||
|
||||
_ => (),
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user