1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 05:10:03 -04:00

Add Context::request_repaint_after (#1694)

This commit is contained in:
Red Artist
2022-06-22 16:49:13 +05:30
committed by GitHub
parent 1a89cb35e1
commit 935913b1ec
13 changed files with 180 additions and 43 deletions

View File

@@ -120,7 +120,7 @@ pub fn run_glow(
let egui::FullOutput {
platform_output,
needs_repaint,
repaint_after,
textures_delta,
shapes,
} = integration.update(app.as_mut(), window);
@@ -148,9 +148,18 @@ pub fn run_glow(
*control_flow = if integration.should_quit() {
winit::event_loop::ControlFlow::Exit
} else if needs_repaint {
} else if repaint_after.is_zero() {
window.request_redraw();
winit::event_loop::ControlFlow::Poll
} else if let Some(repaint_after_instant) =
std::time::Instant::now().checked_add(repaint_after)
{
// if repaint_after is something huge and can't be added to Instant,
// we will use `ControlFlow::Wait` instead.
// technically, this might lead to some weird corner cases where the user *WANTS*
// winit to use `WaitUntil(MAX_INSTANT)` explicitly. they can roll their own
// egui backend impl i guess.
winit::event_loop::ControlFlow::WaitUntil(repaint_after_instant)
} else {
winit::event_loop::ControlFlow::Wait
};
@@ -167,7 +176,6 @@ pub fn run_glow(
std::thread::sleep(std::time::Duration::from_millis(10));
}
};
match event {
// Platform-dependent event handlers to workaround a winit bug
// See: https://github.com/rust-windowing/winit/issues/987
@@ -209,7 +217,12 @@ pub fn run_glow(
painter.destroy();
}
winit::event::Event::UserEvent(RequestRepaintEvent) => window.request_redraw(),
_ => (),
winit::event::Event::NewEvents(winit::event::StartCause::ResumeTimeReached {
..
}) => {
window.request_redraw();
}
_ => {}
}
});
}
@@ -298,7 +311,7 @@ pub fn run_wgpu(
let egui::FullOutput {
platform_output,
needs_repaint,
repaint_after,
textures_delta,
shapes,
} = integration.update(app.as_mut(), window);
@@ -319,9 +332,18 @@ pub fn run_wgpu(
*control_flow = if integration.should_quit() {
winit::event_loop::ControlFlow::Exit
} else if needs_repaint {
} else if repaint_after.is_zero() {
window.request_redraw();
winit::event_loop::ControlFlow::Poll
} else if let Some(repaint_after_instant) =
std::time::Instant::now().checked_add(repaint_after)
{
// if repaint_after is something huge and can't be added to Instant,
// we will use `ControlFlow::Wait` instead.
// technically, this might lead to some weird corner cases where the user *WANTS*
// winit to use `WaitUntil(MAX_INSTANT)` explicitly. they can roll their own
// egui backend impl i guess.
winit::event_loop::ControlFlow::WaitUntil(repaint_after_instant)
} else {
winit::event_loop::ControlFlow::Wait
};
@@ -395,6 +417,11 @@ pub fn run_wgpu(
painter.destroy();
}
winit::event::Event::UserEvent(RequestRepaintEvent) => window.request_redraw(),
winit::event::Event::NewEvents(winit::event::StartCause::ResumeTimeReached {
..
}) => {
window.request_redraw();
}
_ => (),
}
});

View File

@@ -251,10 +251,10 @@ impl AppRunner {
Ok(())
}
/// Returns `true` if egui requests a repaint.
/// Returns how long to wait until the next repaint.
///
/// Call [`Self::paint`] later to paint
pub fn logic(&mut self) -> Result<(bool, Vec<egui::ClippedPrimitive>), JsValue> {
pub fn logic(&mut self) -> Result<(std::time::Duration, Vec<egui::ClippedPrimitive>), JsValue> {
let frame_start = now_sec();
resize_canvas_to_screen_size(self.canvas_id(), self.app.max_size_points());
@@ -266,7 +266,7 @@ impl AppRunner {
});
let egui::FullOutput {
platform_output,
needs_repaint,
repaint_after,
textures_delta,
shapes,
} = full_output;
@@ -288,7 +288,7 @@ impl AppRunner {
}
self.frame.info.cpu_usage = Some((now_sec() - frame_start) as f32);
Ok((needs_repaint, clipped_primitives))
Ok((repaint_after, clipped_primitives))
}
pub fn clear_color_buffer(&self) {

View File

@@ -9,11 +9,12 @@ pub fn paint_and_schedule(
let mut runner_lock = runner_ref.lock();
if runner_lock.needs_repaint.fetch_and_clear() {
runner_lock.clear_color_buffer();
let (needs_repaint, clipped_primitives) = runner_lock.logic()?;
let (repaint_after, clipped_primitives) = runner_lock.logic()?;
runner_lock.paint(&clipped_primitives)?;
if needs_repaint {
if repaint_after.is_zero() {
runner_lock.needs_repaint.set_true();
}
// TODO: schedule a repaint after `repaint_after` when it is not zero
runner_lock.auto_save();
}