Skip to content

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

Guides: a dragged ruler guide snaps to guides, margins, columns and objects (#1542)

aionggc3382b422c4b
diff --git a/crates/tools/src/frame.rs b/crates/tools/src/frame.rs
index 4b52e2ee5..ff65e7ce7 100644
--- a/crates/tools/src/frame.rs
+++ b/crates/tools/src/frame.rs
@@ -76,6 +76,7 @@ pub(crate) fn snapped_drag_rect(cx: &ToolContext, sr: SpreadRef, sa: Point, end:
x_edges,
y_edges,
exclude: &[],
+ exclude_guides: &[],
copying: false,
lengths: [length_on(x_edges, r.width()), length_on(y_edges, r.height())],
angle: None,
@@ -160,6 +161,7 @@ fn line_hit(cx: &ToolContext, spread: SpreadRef, point: Point, len: Option<f64>)
x_edges: [true, true, true],
y_edges: [true, true, true],
exclude: &[],
+ exclude_guides: &[],
copying: false,
lengths: [len, None],
angle: None,
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index b5153e183..f0fd68019 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -191,6 +191,9 @@ pub enum Gesture {
Create,
Rotate,
Point,
+ /// A ruler guide being dragged: one line on one axis, with no size or spacing to match and
+ /// no pull from the baseline grid.
+ Guide,
}
pub struct SnapRequest<'a> {
@@ -202,6 +205,8 @@ pub struct SnapRequest<'a> {
/// Top, center, bottom.
pub y_edges: [bool; 3],
pub exclude: &'a [ItemId],
+ /// Ruler guides that are not targets: the ones being dragged.
+ pub exclude_guides: &'a [designcraft_doc::GuideSel],
pub copying: bool,
/// Proposed width and height for the dimensions pass. None when that length is not being set.
pub lengths: [Option<f64>; 2],
diff --git a/crates/tools/src/select.rs b/crates/tools/src/select.rs
index b3d3780f2..866003102 100644
--- a/crates/tools/src/select.rs
+++ b/crates/tools/src/select.rs
@@ -929,6 +929,7 @@ impl Tool for SelectionTool {
x_edges,
y_edges,
exclude: &exclude,
+ exclude_guides: &[],
copying: ev.mods.alt && !content,
lengths: [None, None],
angle: None,
@@ -1002,6 +1003,7 @@ impl Tool for SelectionTool {
x_edges,
y_edges,
exclude: &exclude,
+ exclude_guides: &[],
copying: false,
lengths: [axis_length(x_edges, proposed.width(), pad_x), axis_length(y_edges, proposed.height(), pad_y)],
angle: None,
@@ -1054,6 +1056,7 @@ impl Tool for SelectionTool {
x_edges: [false, false, false],
y_edges: [false, false, false],
exclude: &exclude,
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: Some(resulting),
@@ -1092,6 +1095,7 @@ impl Tool for SelectionTool {
x_edges: [true, true, true],
y_edges: [true, true, true],
exclude: &exclude,
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: None,
diff --git a/crates/tools/src/snap.rs b/crates/tools/src/snap.rs
index 0cbc9aadd..11da46178 100644
--- a/crates/tools/src/snap.rs
+++ b/crates/tools/src/snap.rs
@@ -31,6 +31,7 @@ pub fn snap_point(cx: &ToolContext, sr: SpreadRef, p: Point) -> (Point, Vec<Over
x_edges: [true, true, true],
y_edges: [true, true, true],
exclude: &[],
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: None,
@@ -395,9 +396,10 @@ fn grid_origin(cx: &ToolContext, req: &SnapRequest<'_>, axis: Axis) -> Option<f6
/// Lines are `page.y0 + start + n * increment` for n >= 0 while the line is inside the page
/// (the bottom edge is not a line, matching the canvas). An increment of 0 or less skips the
/// pass. `relative_to` and `view_threshold` are not read, and hiding the baseline grid does not
-/// turn this off. Only a page the rect crosses on x contributes.
+/// turn this off. Only a page the rect crosses on x contributes. A dragged ruler guide
+/// ([`Gesture::Guide`]) doesn't snap to it.
fn pass_baseline(cx: &ToolContext, req: &SnapRequest<'_>, axis: Axis, tol: f64) -> Option<AxisHit> {
- if !matches!(axis, Axis::Y) || !cx.snap.snap_to_guides {
+ if !matches!(axis, Axis::Y) || !cx.snap.snap_to_guides || matches!(req.gesture, Gesture::Guide) {
return None;
}
let baseline = &cx.doc.settings.baseline_grid;
@@ -1000,7 +1002,8 @@ fn push_parent_lengths(out: &mut Vec<f64>, cx: &ToolContext, req: &SnapRequest<'
///
/// A page guide attracts when the moving rect overlaps that page on the other axis. A spread
/// guide uses the spread bounds. Locked guides are included, and so are the guides a page shows
-/// from its parent page. A guide is a target only when `Guide::visible_in` accepts it.
+/// from its parent page. A guide is a target only when `Guide::visible_in` accepts it, and not
+/// when the request excludes it (the guides being dragged).
fn pass_guides(cx: &ToolContext, req: &SnapRequest<'_>, axis: Axis, tol: f64) -> Option<AxisHit> {
if !cx.snap.snap_to_guides || !cx.snap.show_guides {
return None;
@@ -1010,10 +1013,15 @@ fn pass_guides(cx: &ToolContext, req: &SnapRequest<'_>, axis: Axis, tol: f64) ->
let flags = axis_flags(req, axis);
let (moving_lo, moving_hi) = perp_ends(req.rect, axis);
let mut best: Option<Best> = None;
- for page in &sp.pages {
+ for (pi, page) in sp.pages.iter().enumerate() {
let page_bounds = page.bounds();
- // The page's own guides, then those it shows from its parent page.
- for guide in page.guides.iter().cloned().chain(cx.doc.parent_guides(page)) {
+ // The page's own guides, less the excluded ones, then those it shows from its parent page.
+ let own = page
+ .guides
+ .iter()
+ .enumerate()
+ .filter(|&(gi, _)| !req.exclude_guides.contains(&designcraft_doc::GuideSel { spread: req.spread, page: pi, index: gi }));
+ for guide in own.map(|(_, g)| g.clone()).chain(cx.doc.parent_guides(page)) {
if !guide.visible_in(cx.doc) || !guide.position.is_finite() || guide.orientation != axis_orientation(axis) {
continue;
}
@@ -1427,6 +1435,7 @@ mod tests {
x_edges: [false, false, false],
y_edges: [true, true, true],
exclude: &[],
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: None,
@@ -1443,6 +1452,7 @@ mod tests {
x_edges: [true, true, true],
y_edges: [false, false, false],
exclude,
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: None,
@@ -1475,6 +1485,7 @@ mod tests {
x_edges: [false, false, false],
y_edges: [true, true, true],
exclude: &[moving],
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: None,
@@ -1504,6 +1515,7 @@ mod tests {
x_edges: [true, true, true],
y_edges: [false, false, false],
exclude: &[moving],
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: None,
@@ -1515,6 +1527,28 @@ mod tests {
assert!(hit.guides.is_empty());
}
+ /// A dragged guide is not its own target: excluded, the guide at 104 doesn't pull a line at 102.
+ /// Nor does the baseline grid pull it.
+ #[test]
+ fn excluded_guide_is_not_a_target() {
+ let far = Rect::new(400.0, 400.0, 420.0, 420.0);
+ let (doc, _, _) = doc_with_items(far, far, 104.0);
+ let cache = Cache::new();
+ let layout = CanvasLayout::new(&doc, false);
+ let sel = Selection::default();
+ let cx = ctx_on(&doc, &sel, &cache, &layout);
+ let line = Rect::new(102.0, 100.0, 102.0, 300.0);
+ let dragged = [designcraft_doc::GuideSel { spread: SpreadRef::Doc(0), page: 0, index: 0 }];
+ let hit = snap(&cx, move_req(line, &[]));
+ assert!((hit.delta.x - 2.0).abs() < 1e-6, "{:?}", hit.delta);
+ let hit = snap(&cx, SnapRequest { exclude_guides: &dragged, ..move_req(line, &[]) });
+ assert_eq!(hit.delta.x, 0.0);
+ // A dragged guide passes the baseline grid's lines (36 + 12n), which a moved object meets.
+ let flat = Rect::new(100.0, 86.0, 300.0, 86.0);
+ assert!((snap(&cx, y_req(flat)).delta.y - -2.0).abs() < 1e-6);
+ assert_eq!(snap(&cx, SnapRequest { gesture: Gesture::Guide, ..y_req(flat) }).delta.y, 0.0);
+ }
+
#[test]
fn parent_centered_stroke_attracts_at_the_outer_edge() {
let mut doc = Document::new(&NewDocument::default());
@@ -1542,6 +1576,7 @@ mod tests {
x_edges: [true, false, false],
y_edges: [false, false, false],
exclude: &[],
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: None,
@@ -1569,6 +1604,7 @@ mod tests {
x_edges: [true, true, true],
y_edges: [false, false, false],
exclude: &[moving],
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: None,
@@ -1599,6 +1635,7 @@ mod tests {
x_edges: [false, false, true],
y_edges: [false, false, false],
exclude: &[moving],
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: None,
@@ -1630,6 +1667,7 @@ mod tests {
x_edges: [true, false, false],
y_edges: [false, false, false],
exclude: &[id],
+ exclude_guides: &[],
copying: true,
lengths: [None, None],
angle: None,
@@ -1666,6 +1704,7 @@ mod tests {
x_edges: [true, false, false],
y_edges: [false, false, false],
exclude: &[original],
+ exclude_guides: &[],
copying: true,
lengths: [None, None],
angle: None,
@@ -1793,6 +1832,7 @@ mod tests {
x_edges: [false, false, true],
y_edges: [false, false, false],
exclude: &[],
+ exclude_guides: &[],
copying: false,
lengths: [Some(197.0), None],
angle: None,
@@ -1836,6 +1876,7 @@ mod tests {
x_edges: [false, false, true],
y_edges: [false, false, false],
exclude: &[],
+ exclude_guides: &[],
copying: false,
lengths: [Some(82.0), None],
angle: None,
@@ -1871,6 +1912,7 @@ mod tests {
x_edges: [true, true, true],
y_edges: [false, false, false],
exclude: &[moving],
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: None,
@@ -2009,6 +2051,7 @@ mod tests {
x_edges: [true, true, true],
y_edges: [false, false, false],
exclude: &[],
+ exclude_guides: &[],
copying: false,
lengths: [Some(38.0), None],
angle: None,
@@ -2041,6 +2084,7 @@ mod tests {
x_edges: [false, false, false],
y_edges: [false, false, false],
exclude: &[],
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: Some(28.0),
@@ -2085,6 +2129,7 @@ mod tests {
x_edges: [false, false, false],
y_edges: [false, false, false],
exclude: &[],
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: Some(28.0),
@@ -2157,6 +2202,7 @@ mod tests {
x_edges: [false, false, false],
y_edges: [false, false, false],
exclude: &[],
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: Some(angle),
diff --git a/crates/tools/src/xform.rs b/crates/tools/src/xform.rs
index 258bcd62b..561ad0c86 100644
--- a/crates/tools/src/xform.rs
+++ b/crates/tools/src/xform.rs
@@ -100,6 +100,7 @@ impl Tool for XformTool {
x_edges: [false, false, false],
y_edges: [false, false, false],
exclude: &exclude,
+ exclude_guides: &[],
copying: false,
lengths: [None, None],
angle: Some(resulting),
@@ -151,6 +152,7 @@ impl Tool for XformTool {
x_edges,
y_edges,
exclude: &exclude,
+ exclude_guides: &[],
copying: false,
lengths: [length_on(x_edges, w, pad_x), length_on(y_edges, h, pad_y)],
angle: None,
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 322f75d80..f8ddc9487 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -539,10 +539,18 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
&& let Some(p) = g.at
{
let col = Color32::from_rgb(74, 227, 255);
+ let spot = guide_spot(app, &xf, &g);
+ let line = spot.as_ref().map_or(p, |s| s.screen);
let (a, b) = match g.orientation {
- designcraft_doc::Orientation::Horizontal => (pos2(rect.min.x, p.y), pos2(rect.max.x, p.y)),
- designcraft_doc::Orientation::Vertical => (pos2(p.x, rect.min.y), pos2(p.x, rect.max.y)),
+ designcraft_doc::Orientation::Horizontal => (pos2(rect.min.x, line.y), pos2(rect.max.x, line.y)),
+ designcraft_doc::Orientation::Vertical => (pos2(line.x, rect.min.y), pos2(line.x, rect.max.y)),
};
+ // What caught it, as for a moved object.
+ for mark in spot.iter().flat_map(|s| &s.marks) {
+ if let Overlay::Guide { a, b } = mark {
+ painter.line_segment([xf.to_screen(*a), xf.to_screen(*b)], Stroke::new(1.0, t.smart_guide));
+ }
+ }
// A new guide from the rulers with the Page tool is a liquid guide: dashed.
if g.from.is_none() && app.session.tool_id() == "page" {
dashed(&painter, a, b, Stroke::new(1.0, col), 6.0, 3.0);
@@ -2881,6 +2889,9 @@ struct GuideDrag {
from: Option<(designcraft_doc::SpreadRef, usize, usize)>,
/// Pointer position (screen).
at: Option<Pos2>,
+ /// Modifiers held at the pointer's last move: Shift snaps to the ruler's ticks, Command
+ /// suspends snapping.
+ mods: egui::Modifiers,
}
const GUIDE_PRESS: &str = "canvas_guide_press";
@@ -2921,10 +2932,12 @@ fn guide_measure(app: &DesignApp, xf: &Xf, g: &GuideDrag) -> Option<(&'static st
let p = g.at?;
let st = app.session.active()?;
let layout = CanvasLayout::new(&st.doc, st.editing_parents);
- let c = xf.to_canvas(p);
- // Measured on the spread under the pointer, which the guide is dropped on (`guide_drag`),
- // as the rulers measure that spread: not the one in the middle of the view.
- let slot = layout.spread_at(c).and_then(|(r, _)| layout.slot(r)).or_else(|| current_slot(app, &layout).and_then(|i| layout.slots.get(i)))?;
+ // Measured where the guide is dropped (`guide_drag`): its snapped spot, on the spread under
+ // the pointer, as the rulers measure that spread, not the one in the middle of the view.
+ let (c, slot) = match guide_spot(app, xf, g) {
+ Some(spot) => (layout.to_canvas(spot.spread, spot.sp), layout.slot(spot.spread)?),
+ None => (xf.to_canvas(p), current_slot(app, &layout).and_then(|i| layout.slots.get(i))?),
+ };
// An existing guide moves only on its own spread (`guide_drag`): over another, dropping it
// leaves it where it was, so there is no place to show.
if g.from.is_some_and(|(r, _, _)| r != slot.spread) {
@@ -2943,6 +2956,119 @@ fn guide_measure(app: &DesignApp, xf: &Xf, g: &GuideDrag) -> Option<(&'static st
value.is_finite().then_some((label, value, unit))
}
+/// Where a dragged ruler guide is (#1221).
+struct GuideSpot {
+ /// The spread under the pointer.
+ spread: SpreadRef,
+ /// The pointer in that spread's coordinates.
+ pointer: Point,
+ /// [`Self::pointer`] with the guide's axis snapped.
+ sp: Point,
+ /// [`Self::sp`] on screen, where the preview line goes.
+ screen: Pos2,
+ /// Alignment marks (canvas coordinates) when an object or page edge or center caught it.
+ marks: Vec<Overlay>,
+}
+
+/// Where the guide of drag `g` is: its axis (x for a vertical guide, y for a horizontal one)
+/// snapped as a moved object's is, to ruler guides, margins, columns, bleed, the document grid and
+/// smart-guide targets (not the baseline grid, which would catch a horizontal guide almost
+/// anywhere). Shift snaps to the ruler's ticks instead and Command suspends snapping. The guides
+/// being dragged are not targets. An existing guide over another spread, where it
+/// can't be dropped, stays at the pointer. The preview, the readout and the drop all read it.
+/// `None` with no spread under the pointer.
+fn guide_spot(app: &DesignApp, xf: &Xf, g: &GuideDrag) -> Option<GuideSpot> {
+ let p = g.at?;
+ let st = app.session.active()?;
+ let layout = CanvasLayout::new(&st.doc, st.editing_parents);
+ let (r, pointer) = layout.spread_at(xf.to_canvas(p))?;
+ let spot = |sp: Point, marks: Vec<Overlay>| GuideSpot { spread: r, pointer, sp, screen: xf.to_screen(layout.to_canvas(r, sp)), marks };
+ if g.mods.command || g.from.is_some_and(|(from, _, _)| from != r) {
+ return Some(GuideSpot { spread: r, pointer, sp: pointer, screen: p, marks: Vec::new() });
+ }
+ let vertical = matches!(g.orientation, designcraft_doc::Orientation::Vertical);
+ let place = |v: f64| if vertical { Point::new(v, pointer.y) } else { Point::new(pointer.x, v) };
+ if g.mods.shift {
+ let slot = layout.slot(r)?;
+ let at = ruler_tick_of(&st.doc, slot, g.orientation, xf.zoom, pointer).map_or(pointer, place);
+ return Some(spot(at, Vec::new()));
+ }
+ let spread = st.doc.spread(r)?;
+ // Not targets: the dragged guide, and the others of a selected group, which move with it.
+ let exclude: Vec<GuideSel> = match g.from {
+ Some((sr, pi, gi)) if drags_group(app, GuideSel { spread: sr, page: pi, index: gi }) => st.selection.guides.to_vec(),
+ Some((sr, pi, gi)) => vec![GuideSel { spread: sr, page: pi, index: gi }],
+ None => Vec::new(),
+ };
+ // How far the guide reaches across its axis: its page, or the whole spread.
+ let page = match g.from {
+ Some((_, pi, gi)) => spread.pages.get(pi).filter(|pg| pg.guides.get(gi).is_some_and(|guide| !guide.spread)),
+ None => spread.pages.iter().find(|pg| pg.bounds().contains(pointer)),
+ };
+ let reach = page.map_or_else(|| spread.bounds(), |pg| pg.bounds());
+ let (at, rect, x_edges, y_edges) = if vertical {
+ (pointer.x, DRect::new(pointer.x, reach.y0, pointer.x, reach.y1), [true; 3], [false; 3])
+ } else {
+ (pointer.y, DRect::new(reach.x0, pointer.y, reach.x1, pointer.y), [false; 3], [true; 3])
+ };
+ let cx = designcraft_tools::ToolContext {
+ doc: &st.doc,
+ selection: &st.selection,
+ cache: &app.session.cache,
+ layout: &layout,
+ zoom: xf.zoom.max(1e-6),
+ layer: st.active_layer,
+ snap: app.ui.snap_view(),
+ unit: app.ui.units,
+ stroke_in_front: false,
+ };
+ let s = designcraft_tools::snap::snap(
+ &cx,
+ designcraft_tools::SnapRequest {
+ spread: r,
+ gesture: designcraft_tools::Gesture::Guide,
+ rect,
+ x_edges,
+ y_edges,
+ exclude: &[],
+ exclude_guides: &exclude,
+ copying: false,
+ lengths: [None, None],
+ angle: None,
+ radius: 0.0,
+ pointer,
+ },
+ );
+ let snapped = at + if vertical { s.delta.x } else { s.delta.y };
+ Some(if snapped.is_finite() { spot(place(snapped), s.guides) } else { spot(pointer, Vec::new()) })
+}
+
+/// The nearest ruler tick to spread point `sp` along a guide of `orientation`'s axis, in spread
+/// coordinates: the horizontal ruler's (the piece under the point) where that axis runs across the
+/// screen, else the vertical ruler's. `None` when the ruler has no ticks there.
+fn ruler_tick_of(doc: &Document, slot: &SpreadSlot, orientation: designcraft_doc::Orientation, zoom: f64, sp: Point) -> Option<f64> {
+ let vertical = matches!(orientation, designcraft_doc::Orientation::Vertical);
+ // A quarter-turned spread runs its x axis down the screen.
+ let across = vertical == slot.rotation.is_multiple_of(2);
+ let c = slot.to_canvas(sp);
+ let pieces = ruler_pieces_of(doc, slot, doc.zero_point());
+ let step = |unit: designcraft_geom::Unit| {
+ let (major, sub) = unit.ruler_ticks(zoom);
+ let step = major / f64::from(sub.max(1));
+ (step.is_finite() && step > 0.0).then_some(step)
+ };
+ let tick = |v: f64, o: f64, step: f64| o + ((v - o) / step).round() * step;
+ let c = if across {
+ let &(_, _, o) = pieces.iter().find(|(from, to, _)| *from <= c.x && c.x < *to).or(pieces.first())?;
+ Point::new(tick(c.x, o.x, step(doc.settings.horizontal_units)?), c.y)
+ } else {
+ let o = pieces.first()?.2;
+ Point::new(c.x, tick(c.y, o.y, step(doc.settings.vertical_units)?))
+ };
+ let s = slot.to_spread(c);
+ Some(if vertical { s.x } else { s.y }).filter(|v| v.is_finite())
+}
+
/// The guide under a screen point (within 3 px), as (spread, page, index, orientation).
fn guide_at(app: &DesignApp, xf: &Xf, p: Pos2) -> Option<(designcraft_doc::SpreadRef, usize, usize, designcraft_doc::Orientation)> {
let st = app.session.active()?;
@@ -2994,7 +3120,7 @@ fn guide_drag(app: &mut DesignApp, ui: &mut egui::Ui, resp: &egui::Response, rec
None
};
if let Some(orientation) = orientation {
- drag = Some(GuideDrag { orientation, from: None, at: Some(o) });
+ drag = Some(GuideDrag { orientation, from: None, at: Some(o), mods: egui::Modifiers::NONE });
}
} else if rect.contains(o)
&& matches!(app.session.tool_id(), "selection" | "directSelection")
@@ -3007,13 +3133,14 @@ fn guide_drag(app: &mut DesignApp, ui: &mut egui::Ui, resp: &egui::Response, rec
let _ = app.run("guide.select", json!({"spread": r, "page": pi, "index": gi}));
}
ui.data_mut(|d| d.insert_temp(egui::Id::new(GUIDE_PRESS), GuidePress { origin: o, shift }));
- drag = Some(GuideDrag { orientation, from: Some((r, pi, gi)), at: Some(o) });
+ drag = Some(GuideDrag { orientation, from: Some((r, pi, gi)), at: Some(o), mods: egui::Modifiers::NONE });
}
}
let Some(mut g) = drag else { return false };
if let Some(p) = latest {
g.at = Some(p);
}
+ g.mods = ui.input(|i| i.modifiers);
if !released {
ui.ctx().set_cursor_icon(match g.orientation {
designcraft_doc::Orientation::Horizontal => egui::CursorIcon::ResizeVertical,
@@ -3041,12 +3168,11 @@ fn guide_drag(app: &mut DesignApp, ui: &mut egui::Ui, resp: &egui::Response, rec
// Dragging one of several selected guides moves (or deletes) them all.
let group = g.from.is_some_and(|(spread, page, index)| drags_group(app, GuideSel { spread, page, index }));
let on_canvas = rect.contains(p);
- let target = app.session.active().and_then(|st| {
- let layout = CanvasLayout::new(&st.doc, st.editing_parents);
- let (r, sp) = layout.spread_at(xf.to_canvas(p))?;
- let spread = st.doc.spread(r)?;
- let on_page = spread.pages.iter().position(|pg| pg.bounds().contains(sp));
- Some((r, sp, on_page))
+ // Placed where the preview showed it (snapped); the page under the pointer owns a new guide.
+ let target = guide_spot(app, xf, &g).and_then(|spot| {
+ let spread = app.session.active()?.doc.spread(spot.spread)?;
+ let on_page = spread.pages.iter().position(|pg| pg.bounds().contains(spot.pointer));
+ Some((spot.spread, spot.sp, on_page))
});
let pos_of = |sp: Point| match g.orientation {
designcraft_doc::Orientation::Horizontal => sp.y,
@@ -3065,7 +3191,7 @@ fn guide_drag(app: &mut DesignApp, ui: &mut egui::Ui, resp: &egui::Response, rec
let _ = app.run("guide.delete", json!({"spread": r, "page": pi, "index": gi}));
}
(Some((r, _, _)), true, Some((tr, _, _))) if tr == r && group => {
- if let Some((dx, dy)) = press.and_then(|press| guide_drag_delta(app, xf, &g, press.origin, p)) {
+ if let Some((dx, dy)) = press.and_then(|press| guide_drag_delta(app, xf, &g, press.origin)) {
let _ = app.run("guide.moveBy", json!({"dx": dx, "dy": dy}));
}
}
@@ -3093,18 +3219,19 @@ fn drags_group(app: &DesignApp, from: GuideSel) -> bool {
app.session.active().is_some_and(|st| st.selection.guides.len() > 1 && st.selection.guides.contains(&from))
}
-/// How far a drag of the existing guide `g.from`, pressed at `origin` and now at `p` (screen),
-/// moves guides, in spread coordinates: the dragged guide goes to the pointer, the other selected
-/// guides as far along their own axis as the pointer moved. `None` while the pointer is not over
-/// the guide's own spread, where a drop leaves it in place.
-fn guide_drag_delta(app: &DesignApp, xf: &Xf, g: &GuideDrag, origin: Pos2, p: Pos2) -> Option<(f64, f64)> {
+/// How far a drag of the existing guide `g.from`, pressed at `origin` (screen), moves guides, in
+/// spread coordinates: the dragged guide goes to its [`guide_spot`], the other selected guides as
+/// far along their own axis as it moved. `None` while the pointer is not over the guide's own
+/// spread, where a drop leaves it in place.
+fn guide_drag_delta(app: &DesignApp, xf: &Xf, g: &GuideDrag, origin: Pos2) -> Option<(f64, f64)> {
let (r, pi, gi) = g.from?;
- let st = app.session.active()?;
- let layout = CanvasLayout::new(&st.doc, st.editing_parents);
- let (tr, sp) = layout.spread_at(xf.to_canvas(p))?;
- if tr != r {
+ let spot = guide_spot(app, xf, g)?;
+ if spot.spread != r {
return None;
}
+ let sp = spot.sp;
+ let st = app.session.active()?;
+ let layout = CanvasLayout::new(&st.doc, st.editing_parents);
let start = layout.slot(r)?.to_spread(xf.to_canvas(origin));
let at = st.doc.spread(r)?.pages.get(pi)?.guides.get(gi)?.position;
let (dx, dy) = match g.orientation {
@@ -3130,7 +3257,7 @@ fn moving_guides(app: &DesignApp, xf: &Xf, rect: Rect, g: &GuideDrag, press: Gui
if !rect.contains(p) || (p - press.origin).length() <= GUIDE_CLICK_SLOP {
return None;
}
- let (dx, dy) = guide_drag_delta(app, xf, g, press.origin, p)?;
+ let (dx, dy) = guide_drag_delta(app, xf, g, press.origin)?;
let from = GuideSel { spread, page, index };
let guides = if drags_group(app, from) { app.session.active()?.selection.guides.iter().copied().collect() } else { [from].into_iter().collect() };
Some(MovingGuides { guides, dx, dy })
@@ -4231,7 +4358,7 @@ mod tests {
for (orientation, label, want) in
[(designcraft_doc::Orientation::Vertical, "X", at.x - 100.0), (designcraft_doc::Orientation::Horizontal, "Y", at.y - 50.0)]
{
- let g = GuideDrag { orientation, from: None, at: Some(dest) };
+ let g = GuideDrag { orientation, from: None, at: Some(dest), mods: egui::Modifiers::NONE };
let (l, v, _) = guide_measure(h.state(), &xf, &g).unwrap();
assert_eq!(l, label);
assert!((v - want).abs() < 1e-6, "{label}: {v} vs {want}");
@@ -4270,7 +4397,12 @@ mod tests {
let inside = Point::new(last.bounds.center().x, last.bounds.y0 + (last.bounds.y1 - last.bounds.y0) * 0.3);
let (r, sp) = layout.spread_at(inside).unwrap();
assert_eq!(r, last.spread);
- let g = GuideDrag { orientation: designcraft_doc::Orientation::Horizontal, from: None, at: Some(xf.to_screen(inside)) };
+ let g = GuideDrag {
+ orientation: designcraft_doc::Orientation::Horizontal,
+ from: None,
+ at: Some(xf.to_screen(inside)),
+ mods: egui::Modifiers::NONE,
+ };
let (label, v, _) = guide_measure(h.state(), &xf, &g).unwrap();
assert_eq!(label, "Y");
// The guide would be stored at `sp.y` on that spread, which its ruler reads from the zero point.
@@ -4279,7 +4411,12 @@ mod tests {
// An existing guide of another spread can't be dropped there (it only moves on its own
// spread): no readout over this one, a readout over its own.
let first = layout.slots[current].spread;
- let moving = GuideDrag { orientation: designcraft_doc::Orientation::Horizontal, from: Some((first, 0, 0)), at: Some(xf.to_screen(inside)) };
+ let moving = GuideDrag {
+ orientation: designcraft_doc::Orientation::Horizontal,
+ from: Some((first, 0, 0)),
+ at: Some(xf.to_screen(inside)),
+ mods: egui::Modifiers::NONE,
+ };
assert!(guide_measure(h.state(), &xf, &moving).is_none(), "an existing guide over another spread");
let own = layout.slots[current].bounds;
let over_own = Point::new(own.center().x, own.y0 + (own.y1 - own.y0) * 0.3);
diff --git a/crates/ui-egui/src/guide_selection_tests.rs b/crates/ui-egui/src/guide_selection_tests.rs
index 23ad14c91..6c41d0122 100644
--- a/crates/ui-egui/src/guide_selection_tests.rs
+++ b/crates/ui-egui/src/guide_selection_tests.rs
@@ -243,7 +243,8 @@ fn dragging_one_of_several_selected_guides_previews_them_all() {
let mut h = with_guides();
h.state_mut().run("guide.select", json!({"guides": [{"page": 0, "index": 0}, {"page": 0, "index": 2}]})).unwrap();
let (a, b) = (screen_of(&h, 200.0, 600.0), screen_of(&h, 250.0, 620.0));
- let g = GuideDrag { orientation: designcraft_doc::Orientation::Vertical, from: Some((SpreadRef::Doc(0), 0, 0)), at: Some(b) };
+ let g =
+ GuideDrag { orientation: designcraft_doc::Orientation::Vertical, from: Some((SpreadRef::Doc(0), 0, 0)), at: Some(b), mods: Modifiers::NONE };
let press = GuidePress { origin: a, shift: false };
let app = h.state();
let xf = Xf::new(app.canvas_rect.unwrap(), app.view().unwrap());
@@ -253,6 +254,79 @@ fn dragging_one_of_several_selected_guides_previews_them_all() {
moved.sort_unstable();
assert_eq!(moved, [0, 2]);
// The drop moves them by the same amounts.
- let (dx, dy) = guide_drag_delta(app, &xf, &g, a, b).unwrap();
+ let (dx, dy) = guide_drag_delta(app, &xf, &g, a).unwrap();
assert_eq!((dx, dy), (m.dx, m.dy));
}
+
+/// Spread points per screen pixel at the harness's view.
+fn pt_per_px(h: &Harness<'_, DesignApp>) -> f64 {
+ let app = h.state();
+ 1.0 / Xf::new(app.canvas_rect.unwrap(), app.view().unwrap()).zoom
+}
+
+/// A letter page with two columns: the left column's right side.
+fn with_columns() -> (Harness<'static, DesignApp>, f64) {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"preset": "Letter", "columns": 2, "gutter": 18, "margins": 54})).unwrap();
+ app.run("guide.add", json!({"orientation": "vertical", "position": 200.0})).unwrap();
+ let side = app.session.doc().unwrap().doc.spreads[0].pages[0].column_rects()[0].x1;
+ (harness(app), side)
+}
+
+/// A dragged guide snaps to a column guide as a moved object does (#1221).
+#[test]
+fn a_dragged_guide_snaps_to_a_column_guide() {
+ let (mut h, side) = with_columns();
+ let near = side + 1.5 * pt_per_px(&h);
+ let (a, b) = (screen_of(&h, 200.0, 600.0), screen_of(&h, near, 600.0));
+ drag(&mut h, a, b);
+ assert_eq!(positions(&h), [side]);
+}
+
+/// Snap to Guides and Smart Guides off: the guide goes where the pointer is.
+#[test]
+fn with_snapping_off_a_dragged_guide_stays_at_the_pointer() {
+ let (mut h, side) = with_columns();
+ h.state_mut().ui.snap_to_guides = false;
+ h.state_mut().ui.smart_guides = false;
+ let near = side + 1.5 * pt_per_px(&h);
+ let (a, b) = (screen_of(&h, 200.0, 600.0), screen_of(&h, near, 600.0));
+ drag(&mut h, a, b);
+ let p = positions(&h);
+ assert!((p[0] - near).abs() < 0.1 && p[0] != side, "{p:?} vs {near}");
+}
+
+/// A frame's edge catches a dragged guide, and the preview shows the alignment mark.
+#[test]
+fn a_dragged_guide_snaps_to_a_frame_edge_with_a_smart_guide() {
+ let mut h = with_guides();
+ h.state_mut().run("frame.create", json!({"rect": [400, 100, 500, 200]})).unwrap();
+ h.state_mut().run("edit.deselectAll", json!({})).unwrap();
+ let near = 400.0 + 1.5 * pt_per_px(&h);
+ let (a, b) = (screen_of(&h, 200.0, 600.0), screen_of(&h, near, 600.0));
+ let g =
+ GuideDrag { orientation: designcraft_doc::Orientation::Vertical, from: Some((SpreadRef::Doc(0), 0, 0)), at: Some(b), mods: Modifiers::NONE };
+ {
+ let app = h.state();
+ let xf = Xf::new(app.canvas_rect.unwrap(), app.view().unwrap());
+ let spot = guide_spot(app, &xf, &g).unwrap();
+ assert_eq!(spot.sp.x, 400.0);
+ assert!(spot.marks.iter().any(|m| matches!(m, Overlay::Guide { .. })), "{:?}", spot.marks);
+ // Command suspends snapping.
+ let spot = guide_spot(app, &xf, &GuideDrag { mods: Modifiers::COMMAND, ..g.clone() }).unwrap();
+ assert!((spot.sp.x - near).abs() < 0.1 && spot.marks.is_empty());
+ }
+ drag(&mut h, a, b);
+ assert_eq!(positions(&h), [400.0, 300.0, 400.0]);
+}
+
+/// A new guide dragged out of a ruler snaps too: here to the horizontal guide at y 400.
+#[test]
+fn a_guide_dragged_out_of_a_ruler_snaps() {
+ let mut h = with_guides();
+ let rect = h.state().canvas_rect.unwrap();
+ let ruler = pos2(rect.min.x + 200.0, rect.min.y - RULER / 2.0);
+ let dest = screen_of(&h, 250.0, 400.0 + 1.5 * pt_per_px(&h));
+ drag(&mut h, ruler, dest);
+ assert_eq!(positions(&h), [200.0, 300.0, 400.0, 400.0]);
+}
diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md
index 92b86f0f1..f61acacba 100644
--- a/docs/target-app-parity.md
+++ b/docs/target-app-parity.md
@@ -283,7 +283,7 @@ It is evidence of breadth: each item exists. It is not a claim of InDesign-level
- Index: page references (topic from the selection or up to 4 levels, sort keys; current page, to end of story, next n paragraphs, suppressed, See / See also), Generate / Update Index (section headings, nested or run-in, page ranges merged, Index Title / Section Head / Level 1–4 styles); IDML export/import as InDesign topics, page references and topic cross-references (verified in InDesign 2026); `index.*` commands.
- Text clipboard keeps formatting (character/paragraph formats, footnotes, cross-references, index markers, tables) and falls back to plain text when the system clipboard changed; Paste without Formatting (⇧⌘V); Change Case (UPPERCASE, lowercase, Title Case, Sentence case — formatting kept); Type › Insert Special Character / White Space / Break Character submenus.
- Anchored objects: items flow in the text inline (on the baseline, with Y offset; lines grow to fit) or above the line (left/center/right, space before/after); paste copied items into text to anchor them; Anchored Object Options and Release (back onto the page where shown); rendered, exported to PDF and to/from IDML (InDesign anchored object settings; verified in InDesign 2026); copy/paste and threading carry them; `anchored.*` commands.
-- Ruler guides: drag out of the rulers (page guides, or spread guides on the pasteboard), drag to move, drop on a ruler to delete; selected like objects with the Selection tools (click, Shift-click, a marquee that touches no object, Edit › Select All Guides ⌥⌘G), drawn highlighted, moved together by dragging or the arrow keys, deleted with Delete, and placed exactly with the X or Y field of the Control and Properties panels; Layout › Create Guides (rows/columns with gutters, fit to margins or page); Delete All Guides on Spread; `guide.*` commands.
+- Ruler guides: drag out of the rulers (page guides, or spread guides on the pasteboard), drag to move, drop on a ruler to delete; a dragged guide snaps along its axis to other guides, margins, columns, bleed, page and object edges and centers and the document grid (Shift: the ruler's ticks; Cmd suspends snapping); selected like objects with the Selection tools (click, Shift-click, a marquee that touches no object, Edit › Select All Guides ⌥⌘G), drawn highlighted, moved together by dragging or the arrow keys, deleted with Delete, and placed exactly with the X or Y field of the Control and Properties panels; Layout › Create Guides (rows/columns with gutters, fit to margins or page); Delete All Guides on Spread; `guide.*` commands.
- Links panel: every placed graphic with status (OK / Modified / Missing / Embedded, checked against the file on disk), page and effective PPI (low resolution flagged); Relink…, Go To, Update, Embed; `links.*` commands.
- Linked text files: `file.place {link: true}` or Preferences › File Handling › Create Links When Placing Text Files keeps a placed .docx/.rtf/.txt story linked to its file (path, size and hash, and the style map and conflict option used). The Links panel shows it OK / Modified / Missing; Update imports the file again into the same frames and thread with the saved style map, replacing edits made in the layout (the Links panel and menu ask first; the `links.update` command updates and returns a warning; undoable); Relink, Go To and Unlink. Saved in the native format; IDML exports the text unlinked.
- Missing fonts: highlighted pink on screen (never in output), listed by Preflight; Type › Find/Replace Font (fonts in the document, missing first; replace in text, cells, footnotes and styles); `font.list` / `font.replace`. Type › Reload Fonts (`font.reload`) scans the font folders again and reads again the installed font files edited or removed since they were read, then recomposes the open documents: a font installed or edited while DesignCraft runs needs no restart (#328, #547). InDesign notices such changes by itself.