Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

PR-1392: the place-cursor preview follows place.drop's 2 pt drag threshold

moppeeer53b8b0414132
diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs
index 973262937..3fb243216 100644
--- a/crates/engine/src/cmd/file.rs
+++ b/crates/engine/src/cmd/file.rs
@@ -882,7 +882,7 @@ fn place_drop(s: &mut Session, p: &Value) -> Result<Value> {
}
// Drag: fit proportionally; click: actual size at the point.
let (r, k) = match rect {
- Some(r) if r.width() > 2.0 && r.height() > 2.0 => {
+ Some(r) if r.width() > designcraft_tools::PLACE_DRAG_MIN && r.height() > designcraft_tools::PLACE_DRAG_MIN => {
let k = (r.width() / nw).min(r.height() / nh);
(Rect::new(r.x0, r.y0, r.x0 + nw * k, r.y0 + nh * k), k)
}
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index ad8c5b107..5d0795196 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -501,6 +501,22 @@ Y: 200.00%")),
s.pointer(&PointerEvent::new(PointerKind::Up, 220.0, 100.0), v).unwrap();
let it = s.doc().unwrap().doc.spreads[0].items.last().unwrap().clone();
assert_eq!(it.bounds(), designcraft_geom::Rect::new(220.0, 260.0, 300.0, 300.0), "the frame previewed");
+ // 80 × 1 is past the drag tolerance, but its 2 × 1 proportional frame is too small for
+ // `place.drop`, which places at natural size: the preview shows that frame and 100 %.
+ s.execute("place.load", &json!({"base64": cmd::base64_encode(&png), "name": "a.png"})).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Down, 100.0, 400.0), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Drag, 180.0, 401.0), v).unwrap();
+ let natural = designcraft_geom::Rect::new(100.0, 400.0, 140.0, 420.0);
+ let o = s.overlays(v);
+ assert!(o.iter().any(|o| matches!(o, Overlay::Marquee(r) if *r == natural)), "{o:?}");
+ assert!(
+ o.iter().any(|o| matches!(o, Overlay::Measure { text, .. } if text == "X: 100.00%
+Y: 100.00%")),
+ "{o:?}"
+ );
+ s.pointer(&PointerEvent::new(PointerKind::Up, 180.0, 401.0), v).unwrap();
+ let it = s.doc().unwrap().doc.spreads[0].items.last().unwrap().clone();
+ assert_eq!(it.bounds(), natural, "the frame previewed");
}
#[test]
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index 3491401b1..5f05d01ba 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -16,7 +16,7 @@ mod frame;
mod gradient;
pub mod layout;
mod nav;
-pub use nav::{LOCK_OFFSET_PX, PIN_OFFSET_PX};
+pub use nav::{LOCK_OFFSET_PX, PIN_OFFSET_PX, PLACE_DRAG_MIN};
mod pen;
mod pencil;
pub mod select;
diff --git a/crates/tools/src/nav.rs b/crates/tools/src/nav.rs
index db15effad..4a582c385 100644
--- a/crates/tools/src/nav.rs
+++ b/crates/tools/src/nav.rs
@@ -158,7 +158,7 @@ impl Tool for PlaceGun {
return vec![crate::Overlay::Marquee(designcraft_geom::Rect::from_points(a, self.cur))];
};
// As InDesign: the frame the graphic will fill, and its scale beside the pointer.
- let (r, k) = proportional(a, self.cur, size);
+ let (r, k) = drawn_frame(a, self.cur, size);
let pct = format!("{:.2}%", k * 100.0);
vec![
crate::Overlay::Marquee(r),
@@ -184,6 +184,22 @@ Y: {pct}"
}
}
+/// A dragged frame `place.drop` takes only when it is wider and taller than this, in points;
+/// a thinner one places the graphic at its natural size instead.
+pub const PLACE_DRAG_MIN: f64 = 2.0;
+
+/// The frame `place.drop` makes for a place-cursor drag from `a` to `b`: the proportional frame
+/// (see [`proportional`]), or the graphic at natural size from `a` when that frame is at most
+/// [`PLACE_DRAG_MIN`] in either direction. With its scale.
+fn drawn_frame(a: Point, b: Point, size: (f64, f64)) -> (designcraft_geom::Rect, f64) {
+ let (r, k) = proportional(a, b, size);
+ if r.width() > PLACE_DRAG_MIN && r.height() > PLACE_DRAG_MIN {
+ (r, k)
+ } else {
+ (designcraft_geom::Rect::new(a.x, a.y, a.x + size.0, a.y + size.1), 1.0)
+ }
+}
+
/// The frame a place-cursor drag from `a` to `b` draws for a graphic of natural `size`: the
/// graphic's proportions, from `a` towards `b`, as large as fits the drag. With its scale.
fn proportional(a: Point, b: Point, size: (f64, f64)) -> (designcraft_geom::Rect, f64) {