diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index 1e9045c55..4f2d00f8e 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -67,10 +67,10 @@ impl Lazy { /// - its place, room, clip or scale differs from its last run, or its environment does: the theme, density, language /// or the window's active state; /// - its [`content`](Self::content) version differs from its last run; - /// - the app asked for a repaint outside every settling child (`frame::request_repaint`), or a given value was - /// edited: the framework cannot see which children show that app data, so every child runs then; - /// - for a child with no content version: the frame or the one before had input other than pointer motion, which - /// may have changed app data it shows; + /// - the app asked for a repaint outside every settling child (`frame::request_repaint`): the framework cannot see + /// which children show that app data, so every child runs then; + /// - for a child with no content version: the frame or the one before had input other than pointer motion or an edit + /// of a given value, either of which may have changed app data it shows; /// - its last run showed an overlay or painted on another layer. /// /// A bound value is known by where it lives, so a settled reader needs a binding that stays in place between @@ -83,8 +83,8 @@ impl Lazy { /// The version of the app data a settling child shows that the framework does not track (a document's revision, /// a job's progress): the child runs when it differs from the version of its last run. A child with a version - /// does not run for input that reaches other parts of the window: what that input changes in the data it shows - /// moves the version, or reaches it as a command or app repaint request. + /// does not run for input that reaches other parts of the window or for an edit of a given value elsewhere: what + /// either changes in the data it shows moves the version, or reaches it as a command or app repaint request. pub fn content(mut self, version: u64) -> Lazy { self.content = Some(version); self diff --git a/crates/ui-contract/src/contract/steps/apply.rs b/crates/ui-contract/src/contract/steps/apply.rs index 7b12affc4..393bc139f 100644 --- a/crates/ui-contract/src/contract/steps/apply.rs +++ b/crates/ui-contract/src/contract/steps/apply.rs @@ -160,7 +160,7 @@ pub(crate) fn rerun_after_edit( ) { if edit.given { let ctx = &p.core_ref().ctx; - crate::runtime::lazy::unsettle(ctx); + crate::runtime::lazy::note_given_edit(ctx); ctx.request_repaint(); return; } diff --git a/crates/ui-contract/src/runtime/lazy.rs b/crates/ui-contract/src/runtime/lazy.rs index b6fd3756c..b147bd677 100644 --- a/crates/ui-contract/src/runtime/lazy.rs +++ b/crates/ui-contract/src/runtime/lazy.rs @@ -165,12 +165,13 @@ struct Settled { /// What changed in the frame for every settling child. #[derive(Default)] struct Changes { - /// Every settling child runs through this frame: an app's repaint request outside every settling child or an edit - /// of a given value unsettles every child for that frame and the next. + /// Every settling child runs through this frame: an app's repaint request outside every settling child unsettles + /// every child for that frame and the next. unsettled_through: u64, /// Every settling child with no content version runs through this frame: a frame with input other than pointer - /// motion unsettles them for that frame and the next. - input_through: u64, + /// motion, or with an edit of a given value, unsettles them for that frame and the next. Either may change app data + /// the framework does not track; a child with a content version sees that change as a new version. + unversioned_through: u64, /// The pass whose input was looked at. scanned: u64, /// The values changed in a frame, and the frame. @@ -184,7 +185,7 @@ impl Changes { if self.scanned != pass_nr { self.scanned = pass_nr; if input { - self.input_through = self.input_through.max(frame + 1); + self.unversioned_through = self.unversioned_through.max(frame + 1); } } } @@ -298,9 +299,10 @@ pub(crate) fn replay(ctx: &Context, id: Id) { /// - no value it read (a bound value, an inspection record, a command's state) changed this frame (`note_edit`); /// - its content version is the one of its last run, and its environment (theme, density, scale, language, window /// active) the one it ran in; -/// - no frame unsettled every child (`unsettle`): an app's repaint request outside every settling child or an edit of a -/// given value, this frame or the one before; -/// - for a child with no content version: no input other than pointer motion this frame or the one before; +/// - no frame unsettled every child (`unsettle`): an app's repaint request outside every settling child, this frame or +/// the one before; +/// - for a child with no content version: no input other than pointer motion and no edit of a given value +/// (`note_given_edit`) this frame or the one before; /// - the run did nothing a replay cannot repeat (`runtime::replay`). pub(crate) fn settled(ctx: &Context, owner: Option, id: Id, sizing: bool, place: Option, content: Option) -> Option { // Keyed calls feed their 8-byte content version into the key. @@ -320,7 +322,7 @@ pub(crate) fn settled(ctx: &Context, owner: Option, id: Id, sizing: bool, pl let changes = &store.changes; // Usually empty: only the values written this frame. let edited = (changes.edited.0 == frame).then(|| changes.edited.1.clone()); - let unsettled = changes.unsettled_through >= frame || (content.is_none() && changes.input_through >= frame); + let unsettled = changes.unsettled_through >= frame || (content.is_none() && changes.unversioned_through >= frame); let entry = store.touch(owner, id, frame); let replays = match entry.replays { Some((decided_in, replays)) if decided_in == pass_nr => replays, @@ -420,9 +422,9 @@ pub(crate) fn forget(ctx: &Context, id: Id) { /// again, and that size. It does when that run was in this pass, or in an earlier frame and since then none of the /// settled rules a measure reads has broken (`settled`): nothing in it moves or asked to be shown again, no value it /// read changed, no frame unsettled every child, and the frame and the one before had no input other than pointer -/// motion. A measure-only child has no place on screen, no stops and no widgets, so the pointer, the focus and what a -/// replay repeats do not count. Returns the values that run read with the size: the caller notes them as read again, -/// so an edit of one later in the pass reruns it. +/// motion and no edit of a given value. A measure-only child has no place on screen, no stops and no widgets, so the +/// pointer, the focus and what a replay repeats do not count. Returns the values that run read with the size: the +/// caller notes them as read again, so an edit of one later in the pass reruns it. pub(crate) fn measured(ctx: &Context, id: Id, place: Place) -> Option<(Vec2, Vec)> { let input = ctx.input(|input| { input.events.iter().any(|event| !matches!(event, egui::Event::PointerMoved(_) | egui::Event::MouseMoved(_) | egui::Event::PointerGone)) @@ -432,7 +434,7 @@ pub(crate) fn measured(ctx: &Context, id: Id, place: Place) -> Option<(Vec2, Vec store.changes.scan(pass_nr, frame, input); let changes = &store.changes; let edited = (changes.edited.0 == frame).then(|| changes.edited.1.clone()); - let unsettled = changes.unsettled_through >= frame || changes.input_through >= frame; + let unsettled = changes.unsettled_through >= frame || changes.unversioned_through >= frame; let run = store.touch(None, id, frame).measured.as_ref()?; let same_pass = run.pass == pass_nr; let quiet = !unsettled && run.frame < frame && run.facts.quiet(now, edited.as_ref()); @@ -467,6 +469,13 @@ pub(crate) fn unsettle(ctx: &Context) { with(ctx, |store, _, frame| store.changes.unsettled_through = store.changes.unsettled_through.max(frame + 1)); } +/// Unsettles the settling children with no content version for this frame and the next: an edit of a given value, +/// which the app applies to data the framework does not track. A child with a content version sees what the edit +/// changed in its data as a new version, as it does for input. +pub(crate) fn note_given_edit(ctx: &Context) { + with(ctx, |store, _, frame| store.changes.unversioned_through = store.changes.unversioned_through.max(frame + 1)); +} + /// Records the extent of a row of the collection `owner` that was built this pass. The collection places the rows it /// does not build by it (its item layout answers for rows it never built). pub(crate) fn remember(ctx: &Context, owner: Id, id: Id, size: Vec2) { diff --git a/crates/ui-widgets/tests/it/body/settle.rs b/crates/ui-widgets/tests/it/body/settle.rs index 7774f7a8c..4162770e4 100644 --- a/crates/ui-widgets/tests/it/body/settle.rs +++ b/crates/ui-widgets/tests/it/body/settle.rs @@ -36,6 +36,9 @@ struct Scene { versions: [Cell>; 2], repaints: Cell, app_repaints: Cell, + /// Whether the editor's binding is given: the scene applies each edit to the value itself and, as an app's content + /// version covers its data, moves the reader's version (when it has one). + given: Cell, /// Whether the scene shows only what is outside the settling children. bare: Cell, runs: [Cell; 2], @@ -53,6 +56,7 @@ impl Scene { versions: Default::default(), repaints: Cell::new(false), app_repaints: Cell::new(false), + given: Cell::new(false), bare: Cell::new(false), runs: Default::default(), buttons: [Cell::new((Id::NULL, Rect::NOTHING)), Cell::new((Id::NULL, Rect::NOTHING))], @@ -106,7 +110,18 @@ impl Scene { } fn editor(&self, ui: &mut Ui) { - Scrub::new(Bind::bound(&mut *self.value.borrow_mut())).id_salt("editor").show_at(ui, AT); + if !self.given.get() { + Scrub::new(Bind::bound(&mut *self.value.borrow_mut())).id_salt("editor").show_at(ui, AT); + return; + } + let shown = *self.value.borrow(); + let edit = Scrub::new(Bind::given(shown)).id_salt("editor").show_at(ui, AT); + if let Some(value) = edit.events.landed() { + *self.value.borrow_mut() = *value; + if let Some(version) = self.versions[0].get() { + self.versions[0].set(Some(version + 1)); + } + } } /// The runs of each child in one frame with `events`. @@ -232,6 +247,33 @@ fn an_edit_runs_the_settled_child_that_read_the_value() { } } +/// A drag of a control on a given binding, outside both children: the app applies each edit to its data and moves the +/// content version of the child that shows it. That child runs and shows the dragged value (in the same frame when the +/// editor comes first in the pass, in the next one otherwise); a child with an unchanged content version stays settled; +/// a child with no content version runs, since the edit may have changed app data the framework does not track. +#[trial] +fn a_given_edit_runs_the_versioned_child_whose_version_moved_and_the_unversioned_ones() { + for (editor_first, other_version) in [(true, Some(1)), (false, Some(1)), (true, None)] { + let scene = Scene::new(editor_first, false); + scene.given.set(true); + scene.versions[0].set(Some(1)); + scene.versions[1].set(other_version); + let frames = drag(&scene); + let case = format!("editor first: {editor_first}, other's version: {other_version:?}: {frames:?}"); + for (passes, [reader, other], shows) in &frames[1..] { + assert!(*reader > 0, "the child whose version moved runs ({case})"); + if editor_first { + assert!(*shows, "it shows the dragged value in the same frame ({case})"); + } + match other_version { + Some(_) => assert_eq!(*other, 0, "the child whose version did not move stays settled ({case})"), + None => assert!(*other > 0, "the child with no content version runs ({case})"), + } + assert_eq!(*passes, 1, "a given edit asks for no rerun ({case})"); + } + } +} + /// A settled child whose room changes runs in that frame. #[trial] fn a_settled_child_runs_when_its_room_changes() {