SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Keep picked points when a type-first dimension turns horizontal or vertical (#3168)
SS-ENd21e2b1afd78
diff --git a/crates/ui-egui/src/dim_view.rs b/crates/ui-egui/src/dim_view.rsindex 2b0421c430..1fa26a3157 100644--- a/crates/ui-egui/src/dim_view.rs+++ b/crates/ui-egui/src/dim_view.rs@@ -172,16 +172,38 @@ pub fn pin(app: &SolveApp, at: Vec2) {EDIT.with(|edit| {let mut edit = edit.borrow_mut();let Some(e) = edit.as_mut() else { return };- let Some(live) = e.live.take() else { return };- if let Some(draft) = e.draft.as_mut() {- draft.at = Some(at);- reevaluate(app, draft, &e.expr);- }+ let Some(live) = e.live else { return };+ move_text(app, e, at);+ e.live = None;// An untouched value stays selected, so typing still replaces it.e.fresh = if live.typed { FRESH - 1 } else { FRESH };});}+/// Move a draft's text to `at`. Where the text goes can change what the dimension measures+/// (two points or a slanted line: across from them the distance between them, above or below+/// the horizontal one, beside them the vertical one), so a value nothing was typed into yet+/// becomes what the new dimension measures; carrying the old one over would preview, and on+/// Enter apply, the aligned length as a horizontal or vertical span, moving the picked points.+fn move_text(app: &SolveApp, e: &mut Edit, at: Vec2) {+ let typed = e.live.is_none_or(|l| l.typed);+ let Some(draft) = e.draft.as_mut() else { return };+ if !typed+ && let Ok((param, expr, redone)) = dimension_draft(app, draft.entities.clone(), Some(at))+ && !redone.driven+ {+ if expr != e.expr {+ // Keep the new value selected, so typing still replaces it.+ e.fresh = FRESH;+ }+ (e.param, e.expr, *draft) = (param, expr, redone);+ return;+ }+ draft.at = Some(at);+ let expr = e.expr.clone();+ reevaluate(app, draft, &expr);+}+fn reevaluate(app: &SolveApp, draft: &mut DimensionDraft, expr: &str) {draft.checked_expr = expr.to_string();match draft.evaluate(app, expr) {@@ -435,33 +457,28 @@ pub fn show(app: &mut SolveApp, ui: &egui::Ui, painter: &egui::Painter, proj: &Pif edit.as_ref().and_then(|e| e.draft.as_ref()).is_some_and(|d| !d.valid(app)) {*edit = None;}- if let Some(e) = edit.as_mut()- && let Some(draft) = e.draft.as_mut()- {- let mut moved = false;- if let Some(live) = e.live.as_mut() {- // The text follows the pointer once it leaves the pick.- let pointer = ui.input(|i| i.pointer.hover_pos()).filter(|p| proj.rect.contains(*p));- if !live.moved && pointer.is_some_and(|p| live.origin.is_none_or(|o| o.distance(p) > 4.0)) {- live.moved = true;- }- let hover = app.tool.as_ref().and_then(|t| t.hover.as_ref()).map(|h| h.0);- if live.moved- && let Some(at) = hover- && draft.at != Some(at)- {- draft.at = Some(at);- moved = true;- }+ let e = edit.as_mut()?;+ let current = e.draft.as_ref()?.at;+ if let Some(live) = e.live.as_mut() {+ // The text follows the pointer once it leaves the pick.+ let pointer = ui.input(|i| i.pointer.hover_pos()).filter(|p| proj.rect.contains(*p));+ if !live.moved && pointer.is_some_and(|p| live.origin.is_none_or(|o| o.distance(p) > 4.0)) {+ live.moved = true;}- if moved || e.expr != draft.checked_expr {- let expr = e.expr.clone();- reevaluate(app, draft, &expr);+ let hover = app.tool.as_ref().and_then(|t| t.hover.as_ref()).map(|h| h.0);+ if live.moved+ && let Some(at) = hover+ && current != Some(at)+ {+ move_text(app, e, at);}- Some((e.param.clone(), draft.clone()))- } else {- None}+ let draft = e.draft.as_mut()?;+ if e.expr != draft.checked_expr {+ let expr = e.expr.clone();+ reevaluate(app, draft, &expr);+ }+ Some((e.param.clone(), draft.clone()))});let placing = !editing();let draft = editor_draft.or_else(|| placing.then(|| placement_preview(app)).flatten());@@ -788,6 +805,19 @@ pub fn editing() -> bool {EDIT.with(|e| e.borrow().is_some())}+/// The open draft: its expression, the dimension it adds and the sketch it previews (tests).+#[cfg(test)]+pub fn draft_preview() -> Option<(String, solidcraft_engine::sketch::Constraint, solidcraft_engine::sketch::Sketch)> {+ EDIT.with(|edit| {+ let edit = edit.borrow();+ let e = edit.as_ref()?;+ let draft = e.draft.as_ref()?;+ let sk = draft.preview.sketch(draft.sketch)?.sketch.clone();+ let c = sk.constraints.iter().find(|c| c.param.as_deref() == Some(e.param.as_str()))?.clone();+ Some((e.expr.clone(), c, sk))+ })+}+/// Dimensions drawn last frame: (id, text centre on screen), for tests and the control channel.pub fn drawn() -> Vec<(String, Pos2)> {DRAWN.with(|d| d.borrow().iter().map(|x| (x.id.clone(), x.text.center())).collect())diff --git a/crates/ui-egui/src/dimension_create_ui_tests.rs b/crates/ui-egui/src/dimension_create_ui_tests.rsindex 8ddf532039..6d7717abde 100644--- a/crates/ui-egui/src/dimension_create_ui_tests.rs+++ b/crates/ui-egui/src/dimension_create_ui_tests.rs@@ -745,3 +745,73 @@ fn type_first_keeps_the_place_first_flow() {assert!((new[0].kind.value().unwrap() - 21.).abs() < 1e-6);crate::dim_view::cancel();}++/// Type-first, the text follows the pointer: moving it from across the picks (the distance+/// between them) to above or beside them switches to the horizontal or vertical distance and+/// back. Until something is typed the value follows the dimension, so the preview measures the+/// picked points where they are (it neither moves nor scales them) and Enter keeps them there.+#[test]+fn type_first_switching_between_aligned_horizontal_and_vertical_keeps_the_picked_points() {+ use solidcraft_engine::sketch::ConstraintKind as K;+ let end = [12., 16.];+ let mid = [6., 8.];+ // Across from the picks, above them, beside them.+ let (across, above, beside) = ([-5., 12.], [6., 25.], [-10., 1.]);+ type Kind = fn(&K) -> bool;+ let aligned: Kind = |k| matches!(k, K::Distance { .. } | K::Length { .. });+ let horizontal: Kind = |k| matches!(k, K::DistanceX { .. });+ let vertical: Kind = |k| matches!(k, K::DistanceY { .. });+ let picks: [Vec<[f64; 2]>; 2] = [vec![[0., 0.], end], vec![mid]];+ for picks in picks {+ for (commit_at, commit_kind, commit_value) in [(above, horizontal, 12.), (beside, vertical, 16.), (across, aligned, 20.)] {+ let mut h = sketch_with(&[("sketch.line", json!({"points":[[0,0],end],"infer":false}))], mid);+ let undo = h.app.session.undo.len();+ for p in &picks {+ h.step(&json!({"click":{"world":[p[0],p[1],0]}})).unwrap();+ }+ assert_live(&h);+ for (at, kind, value) in [+ (across, aligned, 20.),+ (above, horizontal, 12.),+ (across, aligned, 20.),+ (beside, vertical, 16.),+ (commit_at, commit_kind, commit_value),+ ] {+ h.step(&json!({"move":{"world":[at[0],at[1],0]}})).unwrap();+ h.settle();+ let (expr, c, sk) = crate::dim_view::draft_preview().expect("the draft is open");+ let ctx = format!("{picks:?} at {at:?}: {expr} {:?}", c.kind);+ assert!(kind(&c.kind), "{ctx}");+ assert!((c.kind.value().unwrap() - value).abs() < 1e-6, "{ctx}");+ assert_eq!(expr, format!("{value} mm"), "{ctx}");+ let a = sk.point(sk.resolve_point("l1.start").unwrap()).unwrap();+ let b = sk.point(sk.resolve_point("l1.end").unwrap()).unwrap();+ assert!(+ a.dist(solidcraft_engine::geom::Vec2::new(0., 0.)) < 1e-6 && b.dist(solidcraft_engine::geom::Vec2::new(end[0], end[1])) < 1e-6,+ "{ctx}: the preview moved the points to {a:?} {b:?}"+ );+ }+ h.step(&json!({"key":"Enter"})).unwrap();+ assert!(!crate::dim_view::editing());+ assert_eq!(h.app.session.undo.len(), undo + 1);+ let sk = solved(&h);+ let a = sk.point(sk.resolve_point("l1.start").unwrap()).unwrap();+ let b = sk.point(sk.resolve_point("l1.end").unwrap()).unwrap();+ assert!(+ a.dist(solidcraft_engine::geom::Vec2::new(0., 0.)) < 1e-6 && b.dist(solidcraft_engine::geom::Vec2::new(end[0], end[1])) < 1e-6,+ "{picks:?} at {commit_at:?}: {a:?} {b:?}"+ );+ let new = added(&h, 0);+ assert_eq!(new.len(), 1);+ assert!(commit_kind(&new[0].kind), "{picks:?} at {commit_at:?}: {:?}", new[0].kind);+ assert!((new[0].kind.value().unwrap() - commit_value).abs() < 1e-6, "{:?}", new[0].kind);+ // The committed dimension's text is where the pointer left it, and its extension lines+ // start at the picked points.+ h.settle();+ let shown = crate::dim_view::text_at(&new[0].id).expect("the new dimension is drawn");+ let [x, y] = h.at(&json!({"world":[commit_at[0],commit_at[1],0]})).unwrap();+ assert!(shown.distance(egui::pos2(x as f32, y as f32)) < 40.0, "{picks:?}: text at {shown:?}, cursor at {x},{y}");+ crate::dim_view::cancel();+ }+ }+}