Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Line tool: press and drag draws one line (#7247) (#7252)

Lars Jansena4274f9b59a1
diff --git a/assets/locales/pending/line-drag-one.json b/assets/locales/pending/line-drag-one.json
new file mode 100644
index 0000000000..f6fedb5a48
--- /dev/null
+++ b/assets/locales/pending/line-drag-one.json
@@ -0,0 +1,38 @@
+{
+ "en": {
+ "prefs.line_drag_draws_one": "Press and drag draws one line",
+ "prefs.line_drag_draws_one_note": "In the Line tool, pressing, dragging and letting go draws one line from the press to the release, and it ends there. Clicks still draw a chain. Off: a drag places a point where it is let go, as a click does."
+ },
+ "ja": {
+ "prefs.line_drag_draws_one": "押してドラッグすると線を 1 本描く",
+ "prefs.line_drag_draws_one_note": "線ツールで押してドラッグし、離すと、押した位置から離した位置まで線を 1 本描いてそこで終わります。クリックでは従来どおり連続した線を描きます。オフ: ドラッグはクリックと同じく、離した位置に点を置きます。"
+ },
+ "zh": {
+ "prefs.line_drag_draws_one": "按下并拖动绘制一条直线",
+ "prefs.line_drag_draws_one_note": "在直线工具中,按下、拖动再松开,会从按下处到松开处绘制一条直线并在此结束。单击仍会绘制连续的直线。关闭时:拖动与单击一样,在松开处放置一个点。"
+ },
+ "de": {
+ "prefs.line_drag_draws_one": "Drücken und Ziehen zeichnet eine Linie",
+ "prefs.line_drag_draws_one_note": "Im Linienwerkzeug zeichnet Drücken, Ziehen und Loslassen eine Linie vom Drückpunkt bis zum Loslassen, und dort endet sie. Klicks zeichnen weiterhin eine Kette. Aus: Ein Ziehen setzt wie ein Klick einen Punkt dort, wo losgelassen wird."
+ },
+ "ko": {
+ "prefs.line_drag_draws_one": "누른 채 드래그하면 선 하나 그리기",
+ "prefs.line_drag_draws_one_note": "선 도구에서 누르고 드래그한 뒤 놓으면 누른 곳에서 놓은 곳까지 선 하나를 그리고 거기서 끝납니다. 클릭하면 이전처럼 이어지는 선을 그립니다. 끄면: 드래그는 클릭처럼 놓은 곳에 점을 둡니다."
+ },
+ "nl": {
+ "prefs.line_drag_draws_one": "Indrukken en slepen tekent één lijn",
+ "prefs.line_drag_draws_one_note": "In het lijngereedschap tekent indrukken, slepen en loslaten één lijn van het indrukpunt tot het loslaatpunt, en daar eindigt de lijn. Klikken tekent nog steeds een keten. Uit: slepen zet, net als een klik, een punt waar losgelaten wordt."
+ },
+ "ar": {
+ "prefs.line_drag_draws_one": "الضغط والسحب يرسم خطًا واحدًا",
+ "prefs.line_drag_draws_one_note": "في أداة الخط، الضغط ثم السحب ثم الإفلات يرسم خطًا واحدًا من نقطة الضغط إلى نقطة الإفلات وينتهي هناك. لا تزال النقرات ترسم سلسلة. عند الإيقاف: يضع السحب نقطة حيث يُفلت، كما تفعل النقرة."
+ },
+ "he": {
+ "prefs.line_drag_draws_one": "לחיצה וגרירה משרטטות קו אחד",
+ "prefs.line_drag_draws_one_note": "בכלי הקו, לחיצה, גרירה ושחרור משרטטים קו אחד מנקודת הלחיצה עד נקודת השחרור, והוא מסתיים שם. לחיצות עדיין משרטטות שרשרת. כבוי: גרירה מציבה נקודה היכן שמשחררים, כמו לחיצה."
+ },
+ "vi": {
+ "prefs.line_drag_draws_one": "Nhấn và kéo vẽ một đường",
+ "prefs.line_drag_draws_one_note": "Trong công cụ Đường, nhấn, kéo rồi thả sẽ vẽ một đường từ điểm nhấn đến điểm thả, và kết thúc ở đó. Nhấp vẫn vẽ một chuỗi. Tắt: kéo đặt một điểm tại nơi thả, như một lần nhấp."
+ }
+}
diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs
index 0a8461d193..022bcb8590 100644
--- a/crates/ui-egui/src/prefs.rs
+++ b/crates/ui-egui/src/prefs.rs
@@ -1253,6 +1253,10 @@ preference_settings! {
/// A centre-point arc runs the way the cursor goes round its centre from the start (#6645).
/// Off: it always runs counter-clockwise from the start, as it did before.
center_arc_follows_cursor: bool = true, PAGE_SKETCH;
+ /// In the Line tool, a press, a drag and the release draw one line from the press to the
+ /// release and end there (#7247); clicks still draw a chain. Off (the default): a drag is a
+ /// click that moved on the way, placing a point where it is let go (#2448).
+ line_drag_draws_one: bool = false, PAGE_SKETCH => check_id("prefs.line_drag_draws_one");
/// When true: Shift+click adds without removing; Primary toggles.
/// When false (default): Shift and Primary both toggle.
selection_shift_add_only: bool = false, PAGE_SKETCH;
diff --git a/crates/ui-egui/src/prefs/sketch.rs b/crates/ui-egui/src/prefs/sketch.rs
index 87facca5e4..b72466c2c8 100644
--- a/crates/ui-egui/src/prefs/sketch.rs
+++ b/crates/ui-egui/src/prefs/sketch.rs
@@ -30,6 +30,8 @@ pub(super) fn show(app: &mut SolveApp, ui: &mut egui::Ui) {
.on_hover_text(crate::i18n::tr_label(
"A center point arc runs clockwise or counter-clockwise the way the cursor goes round its center. Off: always counter-clockwise from the start.",
));
+ let line_drag = setting::line_drag_draws_one(app, ui).on_hover_text(app.tr("prefs.line_drag_draws_one_note"));
+ crate::scenario::publish_target("prefs:line_drag_draws_one", line_drag.rect);
ui_kit::settings_block_hint(ui, app.tr("prefs.hold_stop_snapping"), app.tr("prefs.snap_drawing_note"), t.space_24 * 8.0 + t.space_8, |ui| {
let combo = &mut app.preferences.suppress_sketch_snap;
ui_kit::settings_control_width(ui, t.space_24 * 7.0 + t.space_12, |ui| {
diff --git a/crates/ui-egui/src/sketch_moving_click_tests.rs b/crates/ui-egui/src/sketch_moving_click_tests.rs
index effeafe5fa..221a8b0613 100644
--- a/crates/ui-egui/src/sketch_moving_click_tests.rs
+++ b/crates/ui-egui/src/sketch_moving_click_tests.rs
@@ -88,3 +88,99 @@ fn a_short_drag_starts_a_rectangle_circle_or_polygon_by_clicks() {
assert!(on_shape, "{command}: the second click wasn't where it was let go");
}
}
+
+/// Press at sketch point `from`, drag to `to` and let go.
+fn drag(h: &mut Harness, from: [f64; 2], to: [f64; 2]) {
+ let a = h.call("ui.at", json!({"sketch":from}))["result"].clone();
+ let b = h.call("ui.at", json!({"sketch":to}))["result"].clone();
+ h.call("ui.drag", json!({"x0":a[0],"y0":a[1],"x1":b[0],"y1":b[1],"steps":8}));
+ h.settle();
+}
+
+/// The lines of the sketch being edited, end to end.
+fn lines(h: &Harness) -> Vec<[solidcraft_engine::geom::Vec2; 2]> {
+ let sid = h.app.session.active_sketch.unwrap();
+ let sk = h.app.session.model.state().sketch(sid).unwrap().sketch.clone();
+ (0..sk.curves.len())
+ .filter_map(|i| match sk.shape(i) {
+ Some(solidcraft_engine::sketch::Shape::Line { a, b }) => Some([a, b]),
+ _ => None,
+ })
+ .collect()
+}
+
+/// #7247: with Preferences › Sketch › Press and drag draws one line, a press, a drag and the
+/// release draw one line from the press to the release and end there; the next drag starts a
+/// new line, snapping to the first one's end as a click does, and a drag that ends close to
+/// horizontal lines up as a click does. Clicks still chain, and a short drag is still a click.
+#[test]
+fn a_line_drag_draws_one_line_with_the_preference() {
+ let mut h = harness("sketch.line");
+ h.call("ui.prefs", json!({"line_drag_draws_one": true}));
+ drag(&mut h, [-20.0, 0.0], [20.0, 10.0]);
+ let drawn = lines(&h);
+ assert_eq!(drawn.len(), 1, "one line: {:?}", h.app.status);
+ let near = |p: solidcraft_engine::geom::Vec2, q: [f64; 2]| (p - solidcraft_engine::geom::Vec2::new(q[0], q[1])).len() < 0.5;
+ assert!(near(drawn[0][0], [-20.0, 0.0]) && near(drawn[0][1], [20.0, 10.0]), "from the press to the release: {drawn:?}");
+ let tool = h.app.tool.as_ref().expect("the Line tool stays on");
+ assert_eq!((tool.cmd, tool.pts.len()), ("sketch.line", 0), "no chain goes on from the line's end");
+ // The next drag starts on the first line's end (a little off it) and snaps there.
+ drag(&mut h, [20.3, 10.2], [20.0, 30.0]);
+ let drawn = lines(&h);
+ assert_eq!(drawn.len(), 2);
+ assert!((drawn[1][0] - drawn[0][1]).len() < 1e-9, "the press snapped to the end: {drawn:?}");
+ let sid = h.app.session.active_sketch.unwrap();
+ let sk = h.app.session.model.state().sketch(sid).unwrap().sketch.clone();
+ assert!(
+ sk.constraints.iter().any(|c| matches!(c.kind, solidcraft_engine::sketch::ConstraintKind::Vertical { .. })),
+ "the nearly vertical drag lined up as a click does: {:?}",
+ sk.constraints
+ );
+ assert_eq!(h.app.tool.as_ref().map(|t| t.pts.len()), Some(0));
+ // A short drag is still a click: the chain starts where it is let go, and clicks go on.
+ moving_click(&mut h, [-20.0, -20.0], 12.0);
+ assert_eq!(lines(&h).len(), 2, "a short drag drew nothing");
+ assert_eq!(h.app.tool.as_ref().map(|t| t.pts.len()), Some(1));
+ for at in [[0.0, -25.0], [20.0, -20.0]] {
+ let p = h.call("ui.at", json!({"sketch":at}))["result"].clone();
+ h.call("ui.click", json!({"x":p[0],"y":p[1]}));
+ h.settle();
+ }
+ assert_eq!(lines(&h).len(), 4, "clicks chain as before");
+ assert_eq!(h.app.tool.as_ref().map(|t| t.pts.len()), Some(1), "the chain is still open");
+}
+
+/// Off, as by default, a drag in the Line tool is a click that moved on the way (#2448): the
+/// chain's first point goes where it is let go, and nothing is drawn yet.
+#[test]
+fn without_the_preference_a_line_drag_is_a_click_where_it_is_let_go() {
+ let mut h = harness("sketch.line");
+ assert!(!h.app.preferences.line_drag_draws_one, "off by default");
+ drag(&mut h, [-20.0, 0.0], [20.0, 10.0]);
+ assert!(lines(&h).is_empty());
+ let tool = h.app.tool.as_ref().unwrap();
+ assert_eq!(tool.pts.len(), 1);
+ assert!((tool.pts[0].0 - solidcraft_engine::geom::Vec2::new(20.0, 10.0)).len() < 0.5, "{:?}", tool.pts);
+}
+
+/// The preference saves, and the Sketch page's checkbox, Restore Defaults and Cancel treat it as
+/// the page's own.
+#[test]
+fn the_line_drag_preference_saves_and_follows_the_sketch_page() {
+ use crate::prefs::control;
+ let mut h = Harness::new();
+ h.call("ui.prefs", json!({"open": "Sketch"}));
+ h.frames(4);
+ crate::prefs::reveal_preference(&mut h, "prefs:line_drag_draws_one");
+ h.step(&json!({"click":{"handle":"prefs:line_drag_draws_one"}})).unwrap();
+ h.frames(2);
+ assert!(h.app.preferences.line_drag_draws_one, "the checkbox turns it on");
+ control(&mut h.app, &json!({"button": "apply"})).unwrap();
+ let mut other = crate::SolveApp::new(solidcraft_engine::Session::default(), Default::default());
+ other.load_prefs(&h.app.prefs());
+ assert!(other.preferences.line_drag_draws_one, "saved");
+ control(&mut h.app, &json!({"button": "defaults"})).unwrap();
+ assert!(!h.app.preferences.line_drag_draws_one);
+ control(&mut h.app, &json!({"button": "cancel"})).unwrap();
+ assert!(h.app.preferences.line_drag_draws_one);
+}
diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs
index f5903604d1..4cfa1f1d0f 100644
--- a/crates/ui-egui/src/viewport.rs
+++ b/crates/ui-egui/src/viewport.rs
@@ -55,10 +55,11 @@ pub struct ViewportState {
pub(crate) transform_drag: Option<crate::sketch_transform_ui::Drag>,
/// The arrow turning the view that is still down (`navigation::step_key_hold`).
pub(crate) key_hold: Option<crate::navigation::KeyHold>,
- /// A two-point drawing tool's first point was placed by a press (here) that became a drag:
- /// the release places the second (#1630), unless it lands within [`DRAW_DRAG_MIN`] of the
- /// press. Such a drag was a click that moved on the way (#2448): the release places the
- /// first point instead and the shape goes on by clicks, rather than a sliver of a shape.
+ /// A two-point drawing tool's (by preference also the Line tool's, #7247) first point was
+ /// placed by a press (here) that became a drag: the release places the second (#1630), unless
+ /// it lands within [`DRAW_DRAG_MIN`] of the press. Such a drag was a click that moved on the
+ /// way (#2448): the release places the first point instead and the shape goes on by clicks,
+ /// rather than a sliver of a shape.
pub(crate) draw_drag: Option<Pos2>,
/// A tool's click moved on the way (#2448): egui takes a press that moves past its
/// click distance, or is held past its click time, as a drag, so its release is the click.
@@ -4325,6 +4326,13 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {
}
crate::tools::on_hover(app, &proj, p, mods.shift);
crate::tools::on_click(app, &proj, p, mods.shift);
+ // A line dragged out is that one line: no chain goes on from its end (#7247).
+ if p.distance(from) >= DRAW_DRAG_MIN
+ && let Some(t) = app.tool.as_mut().filter(|t| t.cmd == "sketch.line")
+ {
+ t.pts.clear();
+ crate::sketch_dims::reset(t);
+ }
}
} else if let Some(p) = at {
crate::tools::on_hover(app, &proj, p, mods.shift);
@@ -4370,8 +4378,13 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {
}
}
// Rectangles, circles and polygons also draw by press, drag and release (#1630): the press
- // places the first point as a click there would, the release the second.
- let two_point_start = app.tool.as_ref().is_some_and(|t| matches!(t.kind, crate::tools::Kind::Draw(2)) && t.pts.is_empty());
+ // places the first point as a click there would, the release the second. So does a line
+ // with Preferences › Sketch › "Press and drag draws one line" (#7247).
+ let line_drag = app.preferences.line_drag_draws_one;
+ let two_point_start = app
+ .tool
+ .as_ref()
+ .is_some_and(|t| t.pts.is_empty() && (matches!(t.kind, crate::tools::Kind::Draw(2)) || (line_drag && t.cmd == "sketch.line")));
if two_point_start
&& !held
&& primary_nav.is_none()
diff --git a/crates/ui-egui/tests/scenarios/sketch_line_drag_one_line.json b/crates/ui-egui/tests/scenarios/sketch_line_drag_one_line.json
new file mode 100644
index 0000000000..1cd2c90105
--- /dev/null
+++ b/crates/ui-egui/tests/scenarios/sketch_line_drag_one_line.json
@@ -0,0 +1,16 @@
+[
+ {"note": "#7247: with Preferences › Sketch › Press and drag draws one line, a press, a drag and the release in the Line tool draw one line from the press to the release and end there; the next drag starts a separate line on the first one's end."},
+ {"call": "ui.prefs", "params": {"line_drag_draws_one": true}},
+ {"call": "engine.execute", "params": {"command": "sketch.create", "params": {"plane": "XY"}}},
+ {"call": "ui.view", "params": {"view": "top"}},
+ {"call": "ui.camera", "params": {"target": [0, 10, 0], "distance": 120}},
+ {"start": "sketch.line"},
+ {"drag": [{"sketch": [-20, 0]}, {"sketch": [20, 10]}], "steps": 8},
+ {"frames": 3},
+ {"expect": {"sketch_curves": {"own": 1}}},
+ {"expect": {"tool": "sketch.line"}},
+ {"drag": [{"sketch": [20, 10]}, {"sketch": [20, 30]}], "steps": 8},
+ {"frames": 3},
+ {"expect": {"sketch_curves": {"own": 2}}},
+ {"expect": {"errors": 0}}
+]
diff --git a/docs/book/src/guide/sketching.md b/docs/book/src/guide/sketching.md
index 4cc0b7b1c7..a0d0474b99 100644
--- a/docs/book/src/guide/sketching.md
+++ b/docs/book/src/guide/sketching.md
@@ -51,6 +51,12 @@ fillet, chamfer, offset (O), mirror, patterns, move/copy and scale. With trim on
piece to remove it, or press and drag a path across several pieces: the ones it crosses light up
and all go when you let go, in one undo step (`sketch.trim.path`).
+Line draws a chain: each click adds a segment from the last point, and a right-click or Esc
+ends the chain. To draw one separate line with a press, a drag and the release instead, turn on
+**Preferences › Sketch › Press and drag draws one line**: the line runs from where you press to
+where you let go, snapping and lining up as clicks do, and ends there; clicks still draw a
+chain, and a drag shorter than about 24 points is still a click.
+
Tools with several variants (line and midpoint line; two-point, three-point, centre and rounded
rectangle; the circle, arc, slot, polygon and spline variants) share one toolbar button with a
drop-down. The button and the family's shortcut start the variant you picked last, so a
diff --git a/docs/control-protocol.md b/docs/control-protocol.md
index 313a76aa44..1db7cd15aa 100644
--- a/docs/control-protocol.md
+++ b/docs/control-protocol.md
@@ -361,6 +361,7 @@ One setting per line, sorted by name. A change that adds a setting adds its own
- `joint_rotation_fine` (default 0.1°): fine Assembly Move rotation step, in 0.001…180°.
- `language` (`en`, `ar`, `ja`, `de`, `fr`, `he`, `ko`, `ms`, `ru`, `uk`, `zh`; see [localization](localization.md))
- `length_decimals`
+- `line_drag_draws_one` (default false: in the Line tool, a press, a drag and the release draw one line from the press to the release and end there, snapping as clicks do; clicks still chain, and a drag under 24 points is a click; off, a drag places a point where it is let go, #7247)
- `logo_accent` (bool, default false: the application bar's logo in shades of the theme's accent instead of the theme's logo colours)
- `look_at_anim`
- `look_at_speed`