From 7e9a507451aff724d5c07e3e60e34e6fd353795a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 15:37:54 -0800 Subject: [PATCH 1/2] Reproduce Perspective Plane Options crossing drawings --- .../ui-egui/src/dialogs/perspective_plane.rs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/crates/ui-egui/src/dialogs/perspective_plane.rs b/crates/ui-egui/src/dialogs/perspective_plane.rs index 90d37a3f70..6f41009b4a 100644 --- a/crates/ui-egui/src/dialogs/perspective_plane.rs +++ b/crates/ui-egui/src/dialogs/perspective_plane.rs @@ -109,6 +109,62 @@ mod tests { crate::tests_synthetic::key_frame(app, &ctx, vec![]); } + fn right_plane_with_art(app: &mut VectorcraftApp) { + app.run("perspective.grid.preset", json!({"kind": 2})).unwrap(); + let id = app.run("shape.rectangle", json!({"x": 450, "y": 380, "width": 60, "height": 60})).unwrap()["id"].clone(); + app.run("perspective.attach", json!({"ids": [id], "plane": "right"})).unwrap(); + } + + #[test] + fn plane_options_keep_the_plane_and_copied_objects_in_the_opening_document() { + let mut app = app(); + right_plane_with_art(&mut app); + let opening = app.session.doc().unwrap().doc.clone(); + app.run("ui.perspectivePlane", json!({"plane": "right"})).unwrap(); + set(&app, "location", json!(40.0)); + set(&app, "objects", json!("copy")); + + app.run("file.new", json!({"width": 800, "height": 600})).unwrap(); + right_plane_with_art(&mut app); + let other = app.session.doc().unwrap().doc.clone(); + let history = app.session.doc().unwrap().history.undo.len(); + let (id, _) = the_dialog(&app); + ok(&mut app); + + assert_eq!(app.session.doc().unwrap().doc, other, "foreign OK leaves the other drawing and its art unchanged"); + assert_eq!(app.session.doc().unwrap().history.undo.len(), history); + assert!(matches!(host(&app, |h| h.held_state(id)), Some(craft::ui::dialog::HeldOk::Refused(_)))); + assert_eq!(the_dialog(&app).1["location"], 40.0); + assert_eq!(the_dialog(&app).1["objects"], "copy"); + + app.run("document.activate", json!({"index": 0})).unwrap(); + assert_eq!(app.session.doc().unwrap().doc, opening); + ok(&mut app); + let doc = &app.session.doc().unwrap().doc; + assert_eq!(PerspectiveGrid::current(doc).right_offset, 40.0); + assert_eq!(doc.layers[0].children().unwrap().len(), 2, "one copy in the opening drawing"); + } + + #[test] + fn replacement_plane_options_use_the_replacement_drawing_and_plane() { + let mut app = app(); + app.run("perspective.grid.preset", json!({"kind": 2})).unwrap(); + let opening = app.session.doc().unwrap().doc.clone(); + app.run("ui.perspectivePlane", json!({"plane": "right"})).unwrap(); + + app.run("file.new", json!({"width": 800, "height": 600})).unwrap(); + app.run("perspective.grid.preset", json!({"kind": 2})).unwrap(); + app.run("ui.perspectivePlane", json!({"plane": "ground"})).unwrap(); + set(&app, "location", json!(40.0)); + ok(&mut app); + + let grid = PerspectiveGrid::current(&app.session.doc().unwrap().doc); + assert_eq!(grid.offset(Plane::Ground), 40.0, "OK moves the replacement opening's plane"); + assert_eq!(grid.offset(Plane::Right), 0.0); + app.run("document.activate", json!({"index": 0})).unwrap(); + assert_eq!(app.session.doc().unwrap().doc, opening); + } + #[test] fn double_clicking_a_plane_widget_opens_its_options_and_ok_moves_it() { let mut app = app(); From 6fa62df29f823a6636a5df74c70c6f27a02c2c4c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 15:39:58 -0800 Subject: [PATCH 2/2] Scope Perspective Plane Options to its opening drawing and plane --- .../ui-egui/src/dialogs/perspective_plane.rs | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/dialogs/perspective_plane.rs b/crates/ui-egui/src/dialogs/perspective_plane.rs index 6f41009b4a..13ec8754e0 100644 --- a/crates/ui-egui/src/dialogs/perspective_plane.rs +++ b/crates/ui-egui/src/dialogs/perspective_plane.rs @@ -19,6 +19,8 @@ use crate::canvas::general_lengths; /// The dialog kind of the plane options. pub const KIND: &str = "perspectivePlane"; +const OPENING_DOCUMENT: &str = "__openingDocument"; + /// What becomes of the objects on the plane: (`objects` value, label). const OBJECTS: [(&str, &str); 3] = [("none", "Do Not Move"), ("move", "Move All Objects"), ("copy", "Copy All Objects")]; @@ -47,8 +49,9 @@ fn build<'a>(title: &str, lengths: LengthUnits, _fields: &'a mut DialogFields) - /// Open the options of `plane` (`{plane}`; default the active plane), filled in with its location. pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { - let doc = &app.session.active().ok_or("no document")?.doc; - let g = PerspectiveGrid::current(doc); + let state = app.session.active().ok_or("no document")?; + let document = state.uid; + let g = PerspectiveGrid::current(&state.doc); let plane = match p.get("plane").and_then(Value::as_str) { Some(s) => Plane::parse(s).ok_or("plane must be left, right or ground")?, None => g.plane, @@ -60,12 +63,20 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { let lengths = general_lengths(app); let ctx = app.ui_ctx.clone().ok_or("no window")?; let Value::Object(map) = json!({"plane": plane.id(), "location": offset, "objects": "none"}) else { return Err("no window".into()) }; + let mut fields = form::fields_of(¶ms(lengths), &map); + // The held answer carries the drawing and plane from the same opening. + fields.set(OPENING_DOCUMENT, document.to_string()); let heading = title(plane); - let dialog = HostedParamDialog::new(&ctx, KIND, form::fields_of(¶ms(lengths), &map), move |f| build(&heading, lengths, f)); + let dialog = HostedParamDialog::new(&ctx, KIND, fields, move |f| build(&heading, lengths, f)); // A refusal keeps the dialog open with the reason under its rows. - let ok = move |app: &mut VectorcraftApp, r: &ParamResult| { + let ok = |app: &mut VectorcraftApp, r: &ParamResult| { + let document = r.fields.text(OPENING_DOCUMENT).and_then(|uid| uid.parse().ok()); + if !app.session.active().is_some_and(|state| Some(state.uid) == document) { + return Err(tl!("Perspective Plane Options can only change the document where it was opened.").to_owned()); + } + let plane = r.fields.text("plane").ok_or_else(|| tl!("The perspective plane is missing").to_owned())?; let objects = r.fields.text("objects").unwrap_or("none"); - let p = json!({"plane": plane.id(), "offset": r.fields.number("location").unwrap_or(0.0), "objects": objects}); + let p = json!({"plane": plane, "offset": r.fields.number("location").unwrap_or(0.0), "objects": objects}); app.run("perspective.plane.move", p).map(|_| ()) }; open_held(app, dialog, ok).ok_or("no window")?; @@ -100,12 +111,16 @@ mod tests { host(app, |h| h.set_json_field(&ctx, id, field, value)).unwrap(); } - /// OK on the open dialog, then the frame's pump runs the held OK. - fn ok(app: &mut VectorcraftApp) { + fn pump_ok(app: &mut VectorcraftApp) { let (ctx, (id, _)) = (app.ui_ctx.clone().unwrap(), the_dialog(app)); // A held OK reports `false` and files its answer for the app; the dialog closes at its next pass. assert!(host(app, |h| h.confirm_by_id(&ctx, id) || h.held_state(id) == Some(craft::ui::dialog::HeldOk::Waiting)), "OK is enabled"); crate::dialogs::host::pump(app, &ctx); + } + + fn ok(app: &mut VectorcraftApp) { + pump_ok(app); + let ctx = app.ui_ctx.clone().unwrap(); crate::tests_synthetic::key_frame(app, &ctx, vec![]); } @@ -129,7 +144,7 @@ mod tests { let other = app.session.doc().unwrap().doc.clone(); let history = app.session.doc().unwrap().history.undo.len(); let (id, _) = the_dialog(&app); - ok(&mut app); + pump_ok(&mut app); assert_eq!(app.session.doc().unwrap().doc, other, "foreign OK leaves the other drawing and its art unchanged"); assert_eq!(app.session.doc().unwrap().history.undo.len(), history);