Skip to content

Commit

Permalink
Check the repaint delay via viewport output instead of callback
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Jan 6, 2025
1 parent 51facca commit e498284
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions crates/egui_kittest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod snapshot;
#[cfg(feature = "snapshot")]
pub use snapshot::*;
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;
use std::time::Duration;

mod app_kind;
Expand All @@ -29,7 +28,6 @@ use crate::event::EventState;
pub use builder::*;
pub use renderer::*;

use egui::mutex::Mutex;
use egui::{Modifiers, Pos2, Rect, RepaintCause, Vec2, ViewportId};
use kittest::{Node, Queryable};

Expand Down Expand Up @@ -303,18 +301,14 @@ impl<'a, State> Harness<'a, State> {
/// - [`Harness::step`].
/// - [`Harness::run_steps`].
pub fn try_run(&mut self) -> Result<u64, ExceededMaxStepsError> {
let repaint = Arc::new(Mutex::new(true));
let repaint_clone = repaint.clone();
self.ctx.set_request_repaint_callback(move |info| {
// We only care about immediate repaints
if info.delay == Duration::ZERO {
*repaint_clone.lock() = true;
}
});
let mut steps = 0;
while std::mem::replace(&mut *repaint.lock(), false) {
loop {
steps += 1;
self.step();
// We only care about immediate repaints
if self.root_viewport_output().repaint_delay != Duration::ZERO {
break;
}
if steps > self.max_steps {
return Err(ExceededMaxStepsError {
max_steps: self.max_steps,
Expand Down Expand Up @@ -411,6 +405,14 @@ impl<'a, State> Harness<'a, State> {
pub fn render(&mut self) -> Result<image::RgbaImage, String> {
self.renderer.render(&self.ctx, &self.output)
}

/// Get the root viewport output
fn root_viewport_output(&self) -> &egui::ViewportOutput {
self.output
.viewport_output
.get(&ViewportId::ROOT)
.expect("Missing root viewport")
}
}

/// Utilities for stateless harnesses.
Expand Down

0 comments on commit e498284

Please sign in to comment.