Merge pull request #9546 from dotCooCoo/fix/perspective-plane-opening-document

Keep Perspective Plane Options in the opening drawing
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 07:33:30 +07:00
commit 23c05774b7
1 file changed
+78 -7
@@ -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<Value, String> {
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<Value, String> {
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(&params(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(&params(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,15 +111,75 @@ 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![]);
}
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);
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);
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();