From 9d596967b415d337bb686d0e4934a8a5a1a20d96 Mon Sep 17 00:00:00 2001 From: triangle drawer <48007646+t18b219k@users.noreply.github.com> Date: Tue, 25 Jan 2022 01:08:27 +0900 Subject: [PATCH] Reducing glutin dependency in egui_glow (#1151) Use winit wherever possible --- egui_glow/CHANGELOG.md | 3 ++- egui_glow/examples/pure_glow.rs | 4 ++-- egui_glow/src/epi_backend.rs | 31 ++++++++++++++++--------------- egui_glow/src/lib.rs | 21 ++++++++------------- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/egui_glow/CHANGELOG.md b/egui_glow/CHANGELOG.md index 45ddd2db274..a5eaa4b2182 100644 --- a/egui_glow/CHANGELOG.md +++ b/egui_glow/CHANGELOG.md @@ -6,7 +6,8 @@ All notable changes to the `egui_glow` integration will be noted in this file. * `EguiGlow::run` no longer returns the shapes to paint, but stores them internally until you call `EguiGlow::paint` ([#1110](https://github.com/emilk/egui/pull/1110)). * Added `set_texture_filter` method to `Painter` ([#1041](https://github.com/emilk/egui/pull/1041)). * Fix failure to run in Chrome ([#1092](https://github.com/emilk/egui/pull/1092)). - +* `EguiGlow::new` now takes `&winit::Window` because there are no reason to use `&glutin::WindowedContext` ([#1151](https://github.com/emilk/egui/pull/1151)). +* `EguiGlow::paint` now takes `&winit::Window` because there are no reason to use `&glutin::WindowedContext` ([#1151](https://github.com/emilk/egui/pull/1151)). ## 0.16.0 - 2021-12-29 * Made winit/glutin an optional dependency ([#868](https://github.com/emilk/egui/pull/868)). diff --git a/egui_glow/examples/pure_glow.rs b/egui_glow/examples/pure_glow.rs index 7fec582adca..a6196dbd6c3 100644 --- a/egui_glow/examples/pure_glow.rs +++ b/egui_glow/examples/pure_glow.rs @@ -44,7 +44,7 @@ fn main() { let event_loop = glutin::event_loop::EventLoop::with_user_event(); let (gl_window, gl) = create_display(&event_loop); - let mut egui_glow = egui_glow::EguiGlow::new(&gl_window, &gl); + let mut egui_glow = egui_glow::EguiGlow::new(gl_window.window(), &gl); event_loop.run(move |event, _, control_flow| { let mut redraw = || { @@ -78,7 +78,7 @@ fn main() { // draw things behind egui here - egui_glow.paint(&gl_window, &gl); + egui_glow.paint(gl_window.window(), &gl); // draw things on top of egui here diff --git a/egui_glow/src/epi_backend.rs b/egui_glow/src/epi_backend.rs index 0e2cac489ea..0a9f381a671 100644 --- a/egui_glow/src/epi_backend.rs +++ b/egui_glow/src/epi_backend.rs @@ -1,8 +1,9 @@ use crate::*; +use egui_winit::winit; struct RequestRepaintEvent; -struct GlowRepaintSignal(std::sync::Mutex>); +struct GlowRepaintSignal(std::sync::Mutex>); impl epi::backend::RepaintSignal for GlowRepaintSignal { fn request_repaint(&self) { @@ -12,8 +13,8 @@ impl epi::backend::RepaintSignal for GlowRepaintSignal { #[allow(unsafe_code)] fn create_display( - window_builder: glutin::window::WindowBuilder, - event_loop: &glutin::event_loop::EventLoop, + window_builder: winit::window::WindowBuilder, + event_loop: &winit::event_loop::EventLoop, ) -> ( glutin::WindowedContext, glow::Context, @@ -51,7 +52,7 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { let window_settings = persistence.load_window_settings(); 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 event_loop = winit::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( @@ -116,12 +117,12 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { { *control_flow = if integration.should_quit() { - glutin::event_loop::ControlFlow::Exit + winit::event_loop::ControlFlow::Exit } else if needs_repaint { gl_window.window().request_redraw(); - glutin::event_loop::ControlFlow::Poll + winit::event_loop::ControlFlow::Poll } else { - glutin::event_loop::ControlFlow::Wait + winit::event_loop::ControlFlow::Wait }; } @@ -132,30 +133,30 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { // 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(), + winit::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(), + winit::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(), - glutin::event::Event::WindowEvent { event, .. } => { - if let glutin::event::WindowEvent::Focused(new_focused) = event { + winit::event::Event::WindowEvent { event, .. } => { + if let winit::event::WindowEvent::Focused(new_focused) = event { is_focused = new_focused; } - if let glutin::event::WindowEvent::Resized(physical_size) = event { + if let winit::event::WindowEvent::Resized(physical_size) = event { gl_window.resize(physical_size); } integration.on_event(&event); if integration.should_quit() { - *control_flow = glutin::event_loop::ControlFlow::Exit; + *control_flow = winit::event_loop::ControlFlow::Exit; } gl_window.window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead } - glutin::event::Event::LoopDestroyed => { + winit::event::Event::LoopDestroyed => { integration.on_exit(gl_window.window()); painter.destroy(&gl); } - glutin::event::Event::UserEvent(RequestRepaintEvent) => { + winit::event::Event::UserEvent(RequestRepaintEvent) => { gl_window.window().request_redraw(); } _ => (), diff --git a/egui_glow/src/lib.rs b/egui_glow/src/lib.rs index 5a8c85e40d0..39c493c2c88 100644 --- a/egui_glow/src/lib.rs +++ b/egui_glow/src/lib.rs @@ -88,6 +88,8 @@ #![allow(clippy::manual_range_contains)] pub mod painter; +#[cfg(feature = "winit")] +use egui_winit::winit; pub use glow; pub use painter::Painter; #[cfg(feature = "winit")] @@ -119,10 +121,7 @@ pub struct EguiGlow { #[cfg(feature = "winit")] impl EguiGlow { - pub fn new( - gl_window: &glutin::WindowedContext, - gl: &glow::Context, - ) -> Self { + pub fn new(window: &winit::window::Window, gl: &glow::Context) -> Self { let painter = crate::Painter::new(gl, None, "") .map_err(|error| { crate::misc_util::glow_print_error(format!( @@ -134,7 +133,7 @@ impl EguiGlow { Self { egui_ctx: Default::default(), - egui_winit: egui_winit::State::new(painter.max_texture_side(), gl_window.window()), + egui_winit: egui_winit::State::new(painter.max_texture_side(), window), painter, shapes: Default::default(), textures_delta: Default::default(), @@ -147,7 +146,7 @@ impl EguiGlow { /// and only when this returns `false` pass on the events to your game. /// /// Note that egui uses `tab` to move focus between elements, so this will always return `true` for tabs. - pub fn on_event(&mut self, event: &glutin::event::WindowEvent<'_>) -> bool { + pub fn on_event(&mut self, event: &winit::event::WindowEvent<'_>) -> bool { self.egui_winit.on_event(&self.egui_ctx, event) } @@ -156,7 +155,7 @@ impl EguiGlow { /// Call [`Self::paint`] later to paint. pub fn run( &mut self, - window: &glutin::window::Window, + window: &winit::window::Window, run_ui: impl FnMut(&egui::Context), ) -> bool { let raw_input = self.egui_winit.take_egui_input(window); @@ -172,11 +171,7 @@ impl EguiGlow { } /// Paint the results of the last call to [`Self::run`]. - pub fn paint( - &mut self, - gl_window: &glutin::WindowedContext, - gl: &glow::Context, - ) { + pub fn paint(&mut self, window: &winit::window::Window, gl: &glow::Context) { let shapes = std::mem::take(&mut self.shapes); let mut textures_delta = std::mem::take(&mut self.textures_delta); @@ -185,7 +180,7 @@ impl EguiGlow { } let clipped_meshes = self.egui_ctx.tessellate(shapes); - let dimensions: [u32; 2] = gl_window.window().inner_size().into(); + let dimensions: [u32; 2] = window.inner_size().into(); self.painter.paint_meshes( gl, dimensions,