Skip to content

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

Line tool: the readout measures the line as drawn

moppeeer638824413211
diff --git a/crates/tools/src/frame.rs b/crates/tools/src/frame.rs
index de2d410a2..86f621c61 100644
--- a/crates/tools/src/frame.rs
+++ b/crates/tools/src/frame.rs
@@ -405,6 +405,9 @@ impl Tool for FrameTool {
snap_free_line(cx, sr, sa, b, &mut self.guides)
};
}
+ // The readout measures the line as drawn (Shift and snapping included), in
+ // spread space.
+ self.last = Some((spread_json(sr), Rect::from_points(sa, b)));
out.push(Action::Preview("line.create".into(), json!({"spread": spread_json(sr), "a": [sa.x, sa.y], "b": [b.x, b.y]})));
} else {
let end = cx.layout.to_spread(sr, ev.pos);
@@ -458,10 +461,13 @@ impl Tool for FrameTool {
}
fn overlays(&self, cx: &ToolContext) -> Vec<Overlay> {
- if let (true, Some(a)) = (self.active, self.start) {
- // The frame being drawn (Shift, Alt and snapping included), in the document's units
- // to two decimals, on two lines: InDesign's readout ("W: 3.13 cm", "H: 3.13 cm").
- let r = self.last.as_ref().map(|(_, r)| *r).unwrap_or_else(|| Rect::from_points(a, self.cur));
+ // The frame or line being drawn (Shift, Alt and snapping included, in spread space), in the
+ // document's units to two decimals, on two lines: InDesign's readout ("W: 3.13 cm",
+ // "H: 3.13 cm"). Nothing before the first drag step.
+ if self.active
+ && self.start.is_some()
+ && let Some((_, r)) = self.last.as_ref()
+ {
let units = &cx.doc.settings;
let w = designcraft_geom::units::format_measure_prec(r.width().abs(), units.horizontal_units, 2);
let h = designcraft_geom::units::format_measure_prec(r.height().abs(), units.vertical_units, 2);
diff --git a/crates/tools/src/tests.rs b/crates/tools/src/tests.rs
index d39fb6433..42543848e 100644
--- a/crates/tools/src/tests.rs
+++ b/crates/tools/src/tests.rs
@@ -93,6 +93,32 @@ fn frame_tool_readout_shows_the_size_in_the_document_units() {
assert_eq!(texts, vec!["W: 6.08 cm\nH: 6.08 cm".to_string()]);
}
+/// The Line tool's readout measures the line as drawn: Shift makes it level, so it has no
+/// height, wherever the pointer is.
+#[test]
+fn line_tool_readout_measures_the_line_as_drawn() {
+ let mut d = Document::new(&NewDocument::default());
+ d.settings.horizontal_units = Unit::Centimeters;
+ d.settings.vertical_units = Unit::Centimeters;
+ let (s, c, l) = (Selection::default(), Cache::new(), CanvasLayout::new(&d, false));
+ let cx = ctx(&d, &s, &c, &l);
+ let mut t = create("line");
+ t.pointer(&cx, &PointerEvent::new(PointerKind::Down, 10.0, 10.0));
+ let mut drag = PointerEvent::new(PointerKind::Drag, 182.35, 15.0);
+ drag.mods.shift = true;
+ t.pointer(&cx, &drag);
+ let texts: Vec<String> = t
+ .overlays(&cx)
+ .into_iter()
+ .filter_map(|o| match o {
+ Overlay::Measure { text, .. } => Some(text),
+ _ => None,
+ })
+ .collect();
+ assert_eq!(texts.len(), 1, "{texts:?}");
+ assert_eq!(texts[0], "W: 6.08 cm\nH: 0 cm", "level, as drawn, not 5 pt down to the pointer");
+}
+
#[test]
fn selection_click_and_marquee() {
let mut d = Document::new(&NewDocument::default());