diff --git a/crates/engine/src/cmd/drawing_scene.rs b/crates/engine/src/cmd/drawing_scene.rs index 74c22f5c8..cd93af310 100644 --- a/crates/engine/src/cmd/drawing_scene.rs +++ b/crates/engine/src/cmd/drawing_scene.rs @@ -7,7 +7,8 @@ //! page's picking uses ([`crate::drawing_scene::hit`]). //! //! `drawing.status` (#4894 WE2) says where the drawing open stands: each view as the provider last -//! drew it, and each dimension as the model has it now, in one read. `drawing.update` (#4892) lets a +//! drew it, and each dimension as the model has it now and whether its sheet shows that yet, in one +//! read. `drawing.update` (#4892) lets a //! deferred drawing draw from the model as it is now. use serde_json::Value; @@ -40,7 +41,7 @@ pub static COMMANDS: &[CommandSpec] = &[CommandSpec::new(STATUS, "Drawing Status .doc_only() .contract(&STATUS_CONTRACT) .output(output_schema_for::) - .params("→ {views: [{view, sheet, status: current|out_of_date|placeholder|failed, reason?}], dimensions: [{dimension, status: ok|overridden|ambiguous|suppressed|lost|unmeasurable, why?}], waiting: how many views are out of date, held?: true when a deferred drawing's wait for drawing.update}. current: drawn, from the model and with the settings the view has now. Balloons aren't listed: drawing.balloon.inspect reads each one. Read-only: nothing is drawn, and each body a dimension names is read once for the call."), + .params("→ {views: [{view, sheet, status: current|out_of_date|placeholder|failed, reason?}], dimensions: [{dimension, status: ok|overridden|ambiguous|suppressed|lost|unmeasurable, why?, out_of_date?: true when its sheet shows it as it last was}], waiting: how many views are out of date, held?: true when a deferred drawing's views or dimensions out of date wait for drawing.update}. current: drawn, from the model and with the settings the view has now. A dimension's status is its reading from the model now; out_of_date says the sheet doesn't show that yet: its view is out of date, or it was read from another model or record than it has now (as drawing.scene's out_of_date lists it). Balloons aren't listed: drawing.balloon.inspect reads each one. Read-only: nothing is drawn, and each body a dimension names is read once for the call."), CommandSpec::new(UPDATE, "Update Drawing", crate::checked!("drawing.update", update)) .icon("update") .in_drawing() @@ -77,7 +78,9 @@ pub static COMMANDS: &[CommandSpec] = &[CommandSpec::new(STATUS, "Drawing Status fn status(s: &mut Session, p: &Value) -> Result { let _: DrawingStatusParams = crate::schema::read(STATUS, p)?; let d = s.drawing(STATUS)?.clone(); - let views: Vec = crate::drawing_scene::standing(s, &d) + let standing = crate::drawing_scene::standing(s, &d); + let views: Vec = standing + .views .into_iter() .map(|v| ViewStatusResult { view: ids::encode(v.view), sheet: ids::encode(v.sheet), status: v.status, reason: v.reason }) .collect(); @@ -88,11 +91,11 @@ fn status(s: &mut Session, p: &Value) -> Result { .zip(read) .map(|(id, r)| { let (status, why, _) = super::drawing_dimension::status_words(r.status); - DimensionStatusResult { dimension: ids::encode(*id), status, why } + DimensionStatusResult { dimension: ids::encode(*id), status, why, out_of_date: standing.dimensions.contains(id) } }) .collect(); let waiting = views.iter().filter(|v| v.status == "out_of_date").count(); - let held = waiting > 0 && crate::drawing_scene::held(s, &d); + let held = (waiting > 0 || !standing.dimensions.is_empty()) && crate::drawing_scene::held(s, &d); let reply = DrawingStatusResult { views, dimensions, waiting, held, param_warnings: Vec::new() }; serde_json::to_value(reply).map_err(|e| EngineError::Other(e.to_string())) } @@ -101,7 +104,7 @@ fn update(s: &mut Session, p: &Value) -> Result { let _: DrawingUpdateParams = crate::schema::read(UPDATE, p)?; let d = s.drawing(UPDATE)?.clone(); crate::drawing_scene::update(s); - let waiting = crate::drawing_scene::standing(s, &d).iter().filter(|v| v.status == "out_of_date").count(); + let waiting = crate::drawing_scene::standing(s, &d).views.iter().filter(|v| v.status == "out_of_date").count(); serde_json::to_value(DrawingUpdateResult { waiting, param_warnings: Vec::new() }).map_err(|e| EngineError::Other(e.to_string())) } diff --git a/crates/engine/src/drawing_scene.rs b/crates/engine/src/drawing_scene.rs index 526886d92..fcf945f3a 100644 --- a/crates/engine/src/drawing_scene.rs +++ b/crates/engine/src/drawing_scene.rs @@ -322,10 +322,9 @@ pub fn as_shown(s: &mut Session, d: &Drawing, sheet: u64) -> Result extract(s, sheet)?; settle(s, d); let shown = extract(s, sheet)?; - // A held view's dimensions are out of date with it: listed after the views, never read again here - // against its old lines. `stale_dimensions` holds those of the views up to date. - let held = dimensions_on(d, shown.stale.iter().copied()).into_iter().map(|record| record.id); - let out_of_date = shown.stale.iter().copied().chain(held).chain(shown.stale_dimensions).collect(); + // A held view's dimensions are listed after the views, never read again here against its old lines. + let dimensions = out_of_date_dimensions(d, &shown.stale, shown.stale_dimensions); + let out_of_date = shown.stale.iter().copied().chain(dimensions).collect(); let (scene, objects) = (Arc::unwrap_or_clone(shown.scene), shown.objects.to_vec()); Ok(SheetScene { scene, views: shown.views, objects, dimensions: shown.dimensions, out_of_date }) } @@ -499,6 +498,14 @@ fn draw_dimensions( wanted.iter().enumerate().map(|(at, record)| one(at, record)).collect() } +/// The dimensions of `d` a sheet shows as they last were, by id: each of a view out of date +/// (`stale_views`), as its view is, then `stale_dimensions`, those of the views up to date that were +/// read from another record, model, drawing of their view or lettering ([`Cache::stale_dimensions`]). +fn out_of_date_dimensions(d: &Drawing, stale_views: &[u64], stale_dimensions: Vec) -> Vec { + let held = dimensions_on(d, stale_views.iter().copied()).into_iter().map(|record| record.id); + held.chain(stale_dimensions).collect() +} + /// The dimensions of `views` of `d`, by view and id. fn dimensions_on(d: &Drawing, views: impl Iterator) -> Vec> { let views: BTreeSet = views.collect(); @@ -997,16 +1004,26 @@ pub struct ViewStanding { pub reason: Option, } +/// Where the drawing open stands with the provider ([`standing`]). +#[derive(Clone, Debug, PartialEq)] +pub struct Standing { + /// Each view, in id order. + pub views: Vec, + /// The dimensions its sheets show as they last were ([`out_of_date_dimensions`]). + pub dimensions: Vec, +} + /// Each view of `d`, the drawing open, in id order, as the provider last drew it, whichever sheet it -/// is on. It reads the cache, caught up first ([`catch_up`]): nothing is drawn. A view that drew but -/// didn't fit its sheet's scene is as the last assembly left it, while that is still its sheet's -/// scene ([`Cache::assembled`]). -pub fn standing(s: &mut Session, d: &Drawing) -> Vec { +/// is on, and the dimensions shown as they last were. It reads the cache, caught up first +/// ([`catch_up`]): nothing is drawn. A view that drew but didn't fit its sheet's scene is as the last +/// assembly left it, while that is still its sheet's scene ([`Cache::assembled`]). +pub fn standing(s: &mut Session, d: &Drawing) -> Standing { forget_closed(s); let state = s.world_state().ok(); if let Some(state) = &state { catch_up(s, d, d.views.iter().map(|v| v.id), state); } + let lettering = Lettering::of(s, d); let cache = &s.drawing_scenes; let assembled = cache.assembled(d).unwrap_or_default(); let in_assembly = |id: u64| assembled.binary_search_by_key(&id, |v| v.view).ok().and_then(|at| assembled.get(at)); @@ -1021,7 +1038,15 @@ pub fn standing(s: &mut Session, d: &Drawing) -> Vec { }; ViewStanding { view: view.id, sheet: view.sheet, status, reason } }; - d.views.iter().map(one).collect() + let views: Vec = d.views.iter().map(one).collect(); + let stale: Vec = views.iter().filter(|v| v.status == "out_of_date").map(|v| v.view).collect(); + // Every sheet's views as `extract` takes one sheet's (`views_on`), but each on its own, so one + // that can't be placed leaves out only its own dimensions. + let on: Vec<(u64, ResolvedView, Vec2)> = + d.views.iter().filter_map(|v| Some((v.id, d.resolve_view(v.id).ok()?, d.view_centre(v.id).ok()?))).collect(); + let stale_dimensions = state.as_ref().map_or_else(Vec::new, |state| cache.stale_dimensions(d, &on, state, &lettering)); + let dimensions = out_of_date_dimensions(d, &stale, stale_dimensions.iter().map(|record| record.id).collect()); + Standing { views, dimensions } } /// Sheet `sheet` of the drawing open, as the provider last drew it: at once, as nothing is drawn diff --git a/crates/engine/src/drawing_scene_tests.rs b/crates/engine/src/drawing_scene_tests.rs index edf049033..aed661862 100644 --- a/crates/engine/src/drawing_scene_tests.rs +++ b/crates/engine/src/drawing_scene_tests.rs @@ -1573,14 +1573,15 @@ fn overfill(s: &mut Session, view: u64) -> Drawn { std::mem::replace(&mut entry.drawn, Drawn { group: Some(group), status: Status::Drawn, stats: None, boundary: None, frame: None, owners: None }) } -/// Each dimension's word is the one inspect uses: read, then lost when its body goes, with why. A -/// parameter status takes nothing, and needs a drawing open. +/// Each dimension's word is the one inspect uses: read, then lost when its body goes, with why. Its +/// view not drawn yet, it isn't either: it is out of date. A parameter status takes nothing, and +/// needs a drawing open. #[test] fn status_says_where_each_dimension_stands() { let (mut s, _, [front, _]) = front_and_top(); let added = run(&mut s, "drawing.dimension.add", json!({"view": ids::encode(front), "kind": {"length": {"edge": [50, 0, 20]}}})); let reply = status_of(&mut s).0; - assert_eq!(reply["dimensions"], json!([{"dimension": added["record"]["id"], "status": "ok"}]), "{reply}"); + assert_eq!(reply["dimensions"], json!([{"dimension": added["record"]["id"], "status": "ok", "out_of_date": true}]), "{reply}"); let drawing = ids::encode(s.open_drawing.unwrap()); run(&mut s, "drawing.close", json!({})); assert!(s.execute("drawing.status", &json!({})).is_err(), "no drawing open"); @@ -1594,6 +1595,68 @@ fn status_says_where_each_dimension_stands() { assert!(s.execute("drawing.status", &json!({"sheet": "x"})).is_err()); } +/// The dimensions a `drawing.status` reply says are out of date, by id, in its order. +fn out_of_date_dimensions(reply: &Value) -> Vec { + let dimensions = reply["dimensions"].as_array().unwrap().iter(); + dimensions.filter(|d| d["out_of_date"] == json!(true)).map(|d| d["dimension"].clone()).collect() +} + +/// A deferred drawing, its front view's bottom edge dimensioned, updated and drawn, then closed while +/// `change` runs on the model and opened again: the session, its sheet and the dimension. +fn held_after(change: impl FnOnce(&mut Session)) -> (Session, u64, u64) { + let (mut s, sheet, [front, _]) = front_and_top(); + let id = front_edge(&mut s, front); + let drawing = json!({"drawing": ids::encode(s.open_drawing.unwrap())}); + run(&mut s, "drawing.set", json!({"updates": "deferred"})); + run(&mut s, "drawing.update", json!({})); + settled(&mut s, sheet); + run(&mut s, "drawing.close", json!({})); + change(&mut s); + run(&mut s, "drawing.open", drawing); + (s, sheet, id) +} + +/// Updated and drawn, a drawing has nothing out of date, and doesn't hold. +fn nothing_waits_once_updated(s: &mut Session, sheet: u64) { + run(s, "drawing.update", json!({})); + settled(s, sheet); + let (reply, words) = status_of(s); + assert!(all_are(&words, "current") && out_of_date_dimensions(&reply).is_empty() && reply.get("held").is_none(), "{reply}"); +} + +/// A deferred drawing held by a model change lists, beside its views out of date, their dimension: +/// the sheet shows its last reading too, as `drawing.export` lists it. +#[test] +fn status_lists_a_held_views_dimensions_with_it() { + let (mut s, sheet, id) = held_after(|s| { + let block = s.doc.features[0].id; + run(s, "timeline.edit", json!({"feature": block.to_string(), "set": {"length": 120}})); + }); + let (reply, words) = status_of(&mut s); + assert!(all_are(&words, "out_of_date"), "{reply}"); + assert_eq!((&reply["held"], &reply["waiting"]), (&json!(true), &json!(2)), "{reply}"); + assert_eq!(out_of_date_dimensions(&reply), [json!(ids::encode(id))], "{reply}"); + assert_eq!(out_of_date_dimensions(&reply), listed(&mut s, sheet)[2..], "as the export lists it, after the views"); + nothing_waits_once_updated(&mut s, sheet); +} + +/// A change that shows the same (a sketch: a new world state, no new body) leaves a held drawing's +/// views current, but not their dimension, read from the model before it: listed out of date, with +/// the drawing held though no view waits, as `drawing.export` lists it. +#[test] +fn status_lists_dimensions_out_of_date_whose_views_are_current() { + let (mut s, sheet, id) = held_after(|s| { + run(s, "sketch.create", json!({"plane": "XY", "name": "Note"})); + run(s, "sketch.finish", json!({})); + }); + let (reply, words) = status_of(&mut s); + assert!(all_are(&words, "current"), "{reply}"); + assert_eq!((&reply["held"], &reply["waiting"]), (&json!(true), &json!(0)), "{reply}"); + assert_eq!(out_of_date_dimensions(&reply), [json!(ids::encode(id))], "{reply}"); + assert_eq!(out_of_date_dimensions(&reply), listed(&mut s, sheet), "as the export lists it"); + nothing_waits_once_updated(&mut s, sheet); +} + /// A dimension of the block's top front edge on view `view`, 10 from it; its id. fn top_edge(s: &mut Session, view: u64) -> u64 { let added = run(s, "drawing.dimension.add", json!({"view": ids::encode(view), "kind": {"linear": {"a": [0, 0, 20], "b": [100, 0, 20]}}})); diff --git a/crates/engine/src/schema/drawing_scene.rs b/crates/engine/src/schema/drawing_scene.rs index 90ef66cc3..847727b78 100644 --- a/crates/engine/src/schema/drawing_scene.rs +++ b/crates/engine/src/schema/drawing_scene.rs @@ -207,6 +207,10 @@ pub struct DimensionStatusResult { pub status: &'static str, #[serde(skip_serializing_if = "Option::is_none")] pub why: Option, + /// Its sheet shows it as it last was: its view is out of date, or it was read from another model + /// or record than it has now. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub out_of_date: bool, } /// What `drawing.status` returns. @@ -218,7 +222,7 @@ pub struct DrawingStatusResult { pub dimensions: Vec, /// How many views are out of date. pub waiting: usize, - /// A deferred drawing: they wait for `drawing.update`. + /// A deferred drawing: its views and dimensions out of date wait for `drawing.update`. #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub held: bool, #[serde(default, skip_serializing_if = "Vec::is_empty")]