1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 13:50:04 -04:00

Fix pure glow

This commit is contained in:
Konkitoman
2023-10-19 16:52:34 +03:00
parent 4f1696cf9c
commit ec37d3861a
2 changed files with 26 additions and 14 deletions

View File

@@ -17,7 +17,7 @@ impl GlutinWindowContext {
// refactor this function to use `glutin-winit` crate eventually. // refactor this function to use `glutin-winit` crate eventually.
// preferably add android support at the same time. // preferably add android support at the same time.
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn new(event_loop: &winit::event_loop::EventLoopWindowTarget<()>) -> Self { unsafe fn new(event_loop: &winit::event_loop::EventLoopWindowTarget<UserEvent>) -> Self {
use egui::NumExt; use egui::NumExt;
use glutin::context::NotCurrentGlContextSurfaceAccessor; use glutin::context::NotCurrentGlContextSurfaceAccessor;
use glutin::display::GetGlDisplay; use glutin::display::GetGlDisplay;
@@ -142,20 +142,37 @@ impl GlutinWindowContext {
} }
} }
#[derive(Debug)]
pub enum UserEvent {
Redraw(std::time::Duration),
}
fn main() { fn main() {
let mut clear_color = [0.1, 0.1, 0.1]; let mut clear_color = [0.1, 0.1, 0.1];
let event_loop = winit::event_loop::EventLoopBuilder::with_user_event().build(); let event_loop = winit::event_loop::EventLoopBuilder::<UserEvent>::with_user_event().build();
let (gl_window, gl) = create_display(&event_loop); let (gl_window, gl) = create_display(&event_loop);
let gl = std::sync::Arc::new(gl); let gl = std::sync::Arc::new(gl);
let mut egui_glow = egui_glow::EguiGlow::new(&event_loop, gl.clone(), None); let mut egui_glow = egui_glow::EguiGlow::new(&event_loop, gl.clone(), None);
let event_loop_proxy = egui::mutex::Mutex::new(event_loop.create_proxy());
egui_glow
.egui_ctx
.set_request_repaint_callback(move |info| {
event_loop_proxy
.lock()
.send_event(UserEvent::Redraw(info.after))
.expect("Cannot send event");
});
let mut repaint_after = std::time::Duration::MAX;
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
let mut redraw = || { let mut redraw = || {
let mut quit = false; let mut quit = false;
let repaint_after = egui_glow.run(gl_window.window(), |egui_ctx| { egui_glow.run(gl_window.window(), |egui_ctx| {
egui::SidePanel::left("my_side_panel").show(egui_ctx, |ui| { egui::SidePanel::left("my_side_panel").show(egui_ctx, |ui| {
ui.heading("Hello World!"); ui.heading("Hello World!");
if ui.button("Quit").clicked() { if ui.button("Quit").clicked() {
@@ -224,6 +241,10 @@ fn main() {
gl_window.window().request_redraw(); gl_window.window().request_redraw();
} }
} }
winit::event::Event::UserEvent(UserEvent::Redraw(after)) => {
repaint_after = after;
}
winit::event::Event::LoopDestroyed => { winit::event::Event::LoopDestroyed => {
egui_glow.destroy(); egui_glow.destroy();
} }
@@ -239,7 +260,7 @@ fn main() {
} }
fn create_display( fn create_display(
event_loop: &winit::event_loop::EventLoopWindowTarget<()>, event_loop: &winit::event_loop::EventLoopWindowTarget<UserEvent>,
) -> (GlutinWindowContext, glow::Context) { ) -> (GlutinWindowContext, glow::Context) {
let glutin_window_context = unsafe { GlutinWindowContext::new(event_loop) }; let glutin_window_context = unsafe { GlutinWindowContext::new(event_loop) };
let gl = unsafe { let gl = unsafe {

View File

@@ -43,15 +43,10 @@ impl EguiGlow {
/// Returns the `Duration` of the timeout after which egui should be repainted even if there's no new events. /// Returns the `Duration` of the timeout after which egui should be repainted even if there's no new events.
/// ///
/// Call [`Self::paint`] later to paint. /// Call [`Self::paint`] later to paint.
pub fn run( pub fn run(&mut self, window: &winit::window::Window, run_ui: impl FnMut(&egui::Context)) {
&mut self,
window: &winit::window::Window,
run_ui: impl FnMut(&egui::Context),
) -> std::time::Duration {
let raw_input = self.egui_winit.take_egui_input(window); let raw_input = self.egui_winit.take_egui_input(window);
let egui::FullOutput { let egui::FullOutput {
platform_output, platform_output,
repaint_after,
textures_delta, textures_delta,
shapes, shapes,
.. ..
@@ -62,10 +57,6 @@ impl EguiGlow {
self.shapes = shapes; self.shapes = shapes;
self.textures_delta.append(textures_delta); self.textures_delta.append(textures_delta);
repaint_after
.get(&egui::ViewportId::MAIN)
.cloned()
.unwrap_or_default()
} }
/// Paint the results of the last call to [`Self::run`]. /// Paint the results of the last call to [`Self::run`].