From 931c67c49cf8c70d3bbd16fe1f74a1cddf6f0508 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 20:48:07 -0800 Subject: [PATCH] Preserve exact-size Place drag boxes --- crates/engine/src/tests/place.rs | 5 +++- crates/engine/src/tooling.rs | 4 ++- crates/tools/src/place.rs | 46 ++++++++++++++++++++++++++----- crates/ui-egui/src/place.rs | 11 ++++++-- crates/ui-egui/src/tests/place.rs | 20 ++++++++++++++ 5 files changed, 74 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/tests/place.rs b/crates/engine/src/tests/place.rs index e62505697d..94da32aa01 100644 --- a/crates/engine/src/tests/place.rs +++ b/crates/engine/src/tests/place.rs @@ -1225,11 +1225,14 @@ fn a_click_of_the_place_cursor_asks_the_frontend_to_place_the_file_when_asked_to let [UiRequest::Place(p)] = &reqs[..] else { panic!("{reqs:?}") }; assert_eq!(p["rect"], json!([10.0, 10.0, 40.0, 20.0])); assert_eq!(p[vectorcraft_tools::place::SOURCE_INDEX], 0); + assert_eq!(p[vectorcraft_tools::place::CLICK], true); assert!(top_children(&s).is_empty(), "the frontend places it"); assert_eq!(s.tool_options()["names"], json!(["b.png"]), "the cursor goes on to the next file"); // The frontend consumes the private source identity before placing the file. let mut p = p.clone(); - p.as_object_mut().unwrap().remove(vectorcraft_tools::place::SOURCE_INDEX); + for key in [vectorcraft_tools::place::SOURCE_INDEX, vectorcraft_tools::place::CLICK] { + p.as_object_mut().unwrap().remove(key); + } s.execute("file.place", &p).unwrap(); // Left out: the second each document was made in. let doc = |s: &Session| { diff --git a/crates/engine/src/tooling.rs b/crates/engine/src/tooling.rs index 19910fc5c1..53a7830248 100644 --- a/crates/engine/src/tooling.rs +++ b/crates/engine/src/tooling.rs @@ -1889,7 +1889,9 @@ impl Session { // The place cursor's file, read within the roots of whoever loaded the cursor. Action::Exec(cmd, mut p) if cmd == "file.place" => { if let Some(params) = p.as_object_mut() { - params.remove(vectorcraft_tools::place::SOURCE_INDEX); + for key in [vectorcraft_tools::place::SOURCE_INDEX, vectorcraft_tools::place::CLICK] { + params.remove(key); + } } let roots = self.place_queue_roots.clone(); if let Some(w) = crate::file_access::confine(roots.as_ref(), || self.execute(&cmd, &p))?.get("warning").and_then(Value::as_str) { diff --git a/crates/tools/src/place.rs b/crates/tools/src/place.rs index 43f9a962b5..84f6ee1bad 100644 --- a/crates/tools/src/place.rs +++ b/crates/tools/src/place.rs @@ -13,6 +13,9 @@ use crate::{Action, Cursor, Mods, Overlay, PointerEvent, PointerKind, Tool, Tool /// consumes it before opening import options; the engine removes it before direct placement. pub const SOURCE_INDEX: &str = "__placeSourceIndex"; +/// Whether a cursor placement was a click under the pointer tolerance, rather than a drag. +pub const CLICK: &str = "__placeCursorClick"; + /// One loaded file. #[derive(Clone, Debug, PartialEq)] struct Entry { @@ -100,9 +103,11 @@ impl Tool for PlaceTool { let (Some(r), Some(e)) = (self.target(a, ev.pos, cx.tol(3.0)), self.entry()) else { return vec![] }; let mut p = e.params.clone(); p[SOURCE_INDEX] = json!(e.source_index); + let clicked = (ev.pos.x - a.x).abs().max((ev.pos.y - a.y).abs()) < cx.tol(3.0); + p[CLICK] = json!(clicked); p["rect"] = json!([r.x0, r.y0, r.width(), r.height()]); // A Shift-click (not a drag) asks for the file's import options before placing it. - if ev.mods.shift && (ev.pos.x - a.x).abs().max((ev.pos.y - a.y).abs()) < cx.tol(3.0) { + if ev.mods.shift && clicked { p["showImportOptions"] = json!(true); } let mut acts = vec![Action::Exec("file.place".into(), p)]; @@ -289,7 +294,10 @@ mod tests { let acts = t.pointer(&c, &ev(PointerKind::Up, 11.0, 10.0)); assert_eq!( acts, - vec![Action::Exec("file.place".into(), json!({"path": "f0.png", "__placeSourceIndex": 0, "rect": [10.0, 10.0, 40.0, 20.0]}))] + vec![Action::Exec( + "file.place".into(), + json!({"path": "f0.png", "__placeSourceIndex": 0, "__placeCursorClick": true, "rect": [10.0, 10.0, 40.0, 20.0]}) + )] ); // A drag up and to the left grows the box from the press point, aspect kept. t.pointer(&c, &ev(PointerKind::Down, 100.0, 100.0)); @@ -297,7 +305,13 @@ mod tests { let c_marquee_only = ToolContext { measurement_labels: false, ..c }; assert_eq!(t.overlays(&c_marquee_only), vec![Overlay::Marquee(Rect::new(20.0, 60.0, 100.0, 100.0))]); let acts = t.pointer(&c, &ev(PointerKind::Up, 20.0, 90.0)); - assert_eq!(acts[0], Action::Exec("file.place".into(), json!({"path": "f1.png", "__placeSourceIndex": 1, "rect": [20.0, 60.0, 80.0, 40.0]}))); + assert_eq!( + acts[0], + Action::Exec( + "file.place".into(), + json!({"path": "f1.png", "__placeSourceIndex": 1, "__placeCursorClick": false, "rect": [20.0, 60.0, 80.0, 40.0]}) + ) + ); assert_eq!(t.options()["count"], 1); // Placing the last file returns to the previous tool. t.pointer(&c, &ev(PointerKind::Down, 0.0, 0.0)); @@ -315,18 +329,30 @@ mod tests { let mut t = loaded(3); t.pointer(&c, &ev(PointerKind::Down, 10.0, 10.0).with_mods(shift)); let acts = t.pointer(&c, &ev(PointerKind::Up, 11.0, 10.0).with_mods(shift)); - let asked = json!({"path": "f0.png", "__placeSourceIndex": 0, "rect": [10.0, 10.0, 40.0, 20.0], "showImportOptions": true}); + let asked = json!({"path": "f0.png", "__placeSourceIndex": 0, "__placeCursorClick": true, "rect": [10.0, 10.0, 40.0, 20.0], "showImportOptions": true}); assert_eq!(acts, vec![Action::Exec("file.place".into(), asked)]); assert_eq!(t.options()["names"], json!(["f1.png", "f2.png"]), "the cursor goes on to the next file"); // A Shift-drag places at the dragged size, as a drag does. t.pointer(&c, &ev(PointerKind::Down, 100.0, 100.0).with_mods(shift)); t.pointer(&c, &ev(PointerKind::Drag, 20.0, 90.0).with_mods(shift)); let acts = t.pointer(&c, &ev(PointerKind::Up, 20.0, 90.0).with_mods(shift)); - assert_eq!(acts[0], Action::Exec("file.place".into(), json!({"path": "f1.png", "__placeSourceIndex": 1, "rect": [20.0, 60.0, 80.0, 40.0]}))); + assert_eq!( + acts[0], + Action::Exec( + "file.place".into(), + json!({"path": "f1.png", "__placeSourceIndex": 1, "__placeCursorClick": false, "rect": [20.0, 60.0, 80.0, 40.0]}) + ) + ); // Shift released before the button: a plain click. t.pointer(&c, &ev(PointerKind::Down, 0.0, 0.0).with_mods(shift)); let acts = t.pointer(&c, &ev(PointerKind::Up, 0.0, 0.0)); - assert_eq!(acts[0], Action::Exec("file.place".into(), json!({"path": "f2.png", "__placeSourceIndex": 2, "rect": [0.0, 0.0, 40.0, 20.0]}))); + assert_eq!( + acts[0], + Action::Exec( + "file.place".into(), + json!({"path": "f2.png", "__placeSourceIndex": 2, "__placeCursorClick": true, "rect": [0.0, 0.0, 40.0, 20.0]}) + ) + ); assert_eq!(acts.last(), Some(&Action::SwitchTool("pen".into()))); // An empty cursor places nothing on a Shift-click. t.pointer(&c, &ev(PointerKind::Down, 0.0, 0.0).with_mods(shift)); @@ -385,7 +411,13 @@ mod tests { assert_eq!((t.options()["current"].clone(), t.options()["sourceIndex"].clone()), (json!(1), json!(2))); t.pointer(&c, &ev(PointerKind::Down, 0.0, 0.0)); let acts = t.pointer(&c, &ev(PointerKind::Up, 0.0, 0.0)); - assert_eq!(acts, vec![Action::Exec("file.place".into(), json!({"path": "f2.png", "__placeSourceIndex": 2, "rect": [0.0, 0.0, 40.0, 20.0]}))]); + assert_eq!( + acts, + vec![Action::Exec( + "file.place".into(), + json!({"path": "f2.png", "__placeSourceIndex": 2, "__placeCursorClick": true, "rect": [0.0, 0.0, 40.0, 20.0]}) + )] + ); assert_eq!(t.options()["sourceIndex"], 0); assert!(t.check_option("sourceIndex", &json!(0)).is_err(), "the source identity is read only"); t.key(&c, ToolKey::Escape, Mods::default()); diff --git a/crates/ui-egui/src/place.rs b/crates/ui-egui/src/place.rs index f1cbd3e1be..c8844a79fc 100644 --- a/crates/ui-egui/src/place.rs +++ b/crates/ui-egui/src/place.rs @@ -669,7 +669,9 @@ pub(crate) fn place_from_cursor(app: &mut VectorcraftApp, p: &Value) { let ask = app.place.queue_asks || locked || shift_click; let mut p = if ask { clicked_at_full_size(app, p) } else { p.clone() }; if let Some(params) = p.as_object_mut() { - params.remove(vectorcraft_tools::place::SOURCE_INDEX); + for key in [vectorcraft_tools::place::SOURCE_INDEX, vectorcraft_tools::place::CLICK] { + params.remove(key); + } } vectorcraft_engine::file_access::confine(roots.as_ref(), || { if ask { @@ -746,10 +748,13 @@ fn source_index(p: &Value) -> Option { /// The params of a cursor click (`rect` at the file's queued size) on a PDF or DXF file, whose /// import options may change its size (another page, another scale): placed as its options place /// it at the click (`at`, the center of the box the click gave), as File › Place of that one file -/// places it, instead of fitted into the box of the first page or the default scale. A drag keeps -/// its `rect`; one to exactly the queued size gives the same `rect` as a click, and is taken as one. +/// places it, instead of fitted into the box of the first page or the default scale. The cursor +/// marks clicks explicitly: a deliberate drag keeps its `rect`, even at exactly the queued size. fn clicked_at_full_size(app: &VectorcraftApp, p: &Value) -> Value { let mut p = p.clone(); + if p.get(vectorcraft_tools::place::CLICK) != Some(&Value::Bool(true)) { + return p; + } let Some(name) = p.get("path").or_else(|| p.get("name")).and_then(Value::as_str).map(fileio::file_name) else { return p }; if !matches!(fileio::extension(&name).as_str(), "pdf" | "ai" | "ait" | "dxf") { return p; diff --git a/crates/ui-egui/src/tests/place.rs b/crates/ui-egui/src/tests/place.rs index b2afb81c21..ed1c1a3ae5 100644 --- a/crates/ui-egui/src/tests/place.rs +++ b/crates/ui-egui/src/tests/place.rs @@ -1346,6 +1346,26 @@ fn browser_place_same_named_pdfs_keep_each_click_at_full_size() { } } +/// A deliberate drag keeps its box even when it equals that source's queued dimensions. +#[test] +fn browser_place_same_named_pdfs_keep_an_exact_size_drag_box() { + let mut app = app(); + let ctx = window(&app); + let picker = scripted(&mut app); + let first = pdf(&two_pages(), None); + let second = pdf(&[PdfPage::new(200.0, 100.0, "0 g 0 0 20 10 re f"), PdfPage::new(400.0, 100.0, "0 g 0 0 40 10 re f")], None); + app.run("file.place", json!({})).unwrap(); + picker.answer_contents(&[("same.pdf", &first), ("same.pdf", &second)]); + poll(&mut app, &ctx); + answer(&mut app, &[("link", json!(false)), ("showImportOptions", json!(true))]); + cursor_gesture(&mut app, vectorcraft_tools::Mods::default(), (110.0, 110.0)); + assert_eq!(dialog_kind(&app).as_deref(), Some(PDF_IMPORT)); + answer(&mut app, &[("page", json!(2))]); + let st = app.session.active().unwrap(); + let bounds = st.doc.node(st.selection.objects[0]).unwrap().geometric_bounds().unwrap(); + assert_eq!((bounds.width().round(), bounds.center()), (100.0, vectorcraft_geom::Point::new(60.0, 60.0))); +} + /// A locked PDF must not make a same-named unlocked PDF discard its deliberate drag size. #[test] fn browser_place_same_named_pdfs_keep_password_checks_separate() {