From 641e9c2d263772da57f4756083691401a750c6d1 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 22 Apr 2021 20:12:49 +0200 Subject: [PATCH] egui_glium: sleep a bit when not focused This is to stop using all of the CPU when in minimized in continious mode. Fixes https://github.com/emilk/egui/issues/325 --- egui_glium/src/backend.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/egui_glium/src/backend.rs b/egui_glium/src/backend.rs index 82f3235f..95f0bdf4 100644 --- a/egui_glium/src/backend.rs +++ b/egui_glium/src/backend.rs @@ -189,6 +189,8 @@ pub fn run(mut app: Box) -> ! { let mut clipboard = init_clipboard(); let mut current_cursor_icon = CursorIcon::Default; + let mut is_focused = true; + #[cfg(feature = "persistence")] let mut last_auto_save = Instant::now(); @@ -234,6 +236,15 @@ pub fn run(mut app: Box) -> ! { 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 pixels_per_point = input_state .raw .pixels_per_point @@ -330,6 +341,10 @@ pub fn run(mut app: Box) -> ! { glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(), glutin::event::Event::WindowEvent { event, .. } => { + if let glutin::event::WindowEvent::Focused(new_focused) = event { + is_focused = new_focused; + } + input_to_egui( ctx.pixels_per_point(), event,