Skip to content

A headless frame that stops for a timer delivers an older on_layout report after a newer one #3290

Backlog

Description

@Orkunnnn

What happens

In a headless runtime, when a timer falls due inside the step of Runtime::frame(dt), the element's on_layout handler hears the rect of the previous layout after the rect of the newest one. The app is left holding a rect the element no longer has, and nothing corrects it until the element's layout changes again. The frame also commits twice.

Reproduction

On dev at 308baad, appended to crates/rune/src/runtime/layout_read.rs:

#[cfg(test)]
mod stale_layout_report {
    use crate::{Cx, El, Env, Runtime, Tx, el};
    struct S { reports: Vec<f32>, deadline: Option<f64> }
    fn view(_: &S, _: &mut Cx<S>) -> El {
        el().key("row").w_full().h(40.0).on_layout(|s: &mut S, r| s.reports.push(r[2]))
    }
    // Keeps one timer armed, as an app with a debounce does.
    fn poll(s: &mut S, now: f64, tx: &mut Tx) -> bool {
        let deadline = match s.deadline { Some(d) if d > now => d, _ => now + 0.055 };
        s.deadline = Some(deadline);
        tx.wake_after(std::time::Duration::from_secs_f64(deadline - now));
        false
    }
    #[test]
    fn the_last_layout_report_is_the_rect_the_element_has() {
        let dt = 0.01675; // a frame a little longer than 1/60 s
        let mut rt = Runtime::new(S { reports: vec![], deadline: None }, view, (1600.0, 100.0), Env::new(true, 1.0));
        rt.poll = Some(poll);
        rt.frame(0.0);
        rt.frame(dt);
        for width in [1590.0, 1580.0, 1570.0] {
            rt.set_viewport((width, 100.0), 1.0);
            rt.frame(dt);
        }
        for _ in 0..120 { rt.frame(dt); }
        let laid_out = rt.elements().into_iter().find(|e| e.key.as_deref() == Some("row")).unwrap().rect[2];
        assert_eq!(laid_out, 1570.0);
        assert_eq!(rt.state.reports.last().copied(), Some(laid_out), "reports: {:?}", rt.state.reports);
    }
}

cargo test -p rune --lib stale_layout_report fails:

assertion `left == right` failed: reports: [1600.0, 1590.0, 1570.0, 1580.0]
  left: Some(1580.0)
 right: Some(1570.0)

The element is 1570 wide; the last report says 1580, 120 frames after the last resize. With dt of exactly 1/60 s, or without the timer, the reports arrive in order.

Why

This is the headless path only (wall_start.is_none(): tests, benches, scripts); a window driven by the wall clock does not take it.

frame_split (crates/rune/src/runtime/frame.rs): when timer_step(target) finds a deadline inside the step, it takes the queued actions out of self.frame.queue (all but hover changes), runs a frame_step per deadline, and then puts the held actions back with self.frame.queue.extend(input). The held actions include the Action::Layout report the previous frame's commit queued. The timer's frame_step commits at the new viewport size and queues a newer Action::Layout for the same element, so the held, older report ends up behind it and runs last. report_rects has already recorded the newer rect as the one last sent, so no further report follows.

Expected

The last on_layout report an element hears is the rect it has.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions