SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Measure: drag the labels away from the part (#8510) (#9395)
PaddleStroke3877ef08b9b7
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 8a7c928596..bb336aafc0 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -287,6 +287,7 @@ pub(crate) fn owns_pointer(app: &SolveApp, proj: &Proj, p: Pos2) -> bool {&& app.tool.is_none()&& crate::browser::occurrence_triad_model(app).is_some_and(|t| crate::gizmo::contains_pointer(proj, &t, p)))|| crate::place_gizmo::contains_pointer(app, p)+ || app.dialog.as_ref().is_some_and(|d| matches!(&d.kind, Kind::Measure { labels, .. } if labels.contains(p)))}/// Draw the manipulator and the value box (called by the viewport after the model is drawn).@@ -308,10 +309,16 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, projapp.dialog = Some(d);return;}- if let Kind::Measure { result: Some(r), show, .. } = &d.kind {- measure_line(app, painter, proj, r, *show);- app.dialog = Some(d);- return;+ if let Kind::Measure { result, show, labels, .. } = &mut d.kind {+ labels.drawn = Default::default();+ if !ui.input(|i| i.pointer.primary_down()) {+ labels.grab = None;+ }+ if let Some(r) = result {+ measure_line(app, ui, painter, proj, r, *show, labels);+ app.dialog = Some(d);+ return;+ }}// Move: the triad at the pivot; dragging it sets the distances and angles. Its value box// sits beside it and reads the move's main value, so a drag shows its number and typing@@ -1300,9 +1307,85 @@ fn radius_spline_handles(app: &SolveApp, ui: &mut egui::Ui, painter: &egui::Paintrue}+/// The Measure labels in the view the user dragged away from the part (#8510): the distance's,+/// then X, Y and Z's. They last while the measurement does; another one starts them over.+#[derive(Clone, Copy, Debug, Default, PartialEq)]+pub struct MeasureLabels {+ /// How far each label was dragged from its own place, in points.+ offset: [egui::Vec2; 4],+ /// Where each was drawn in the last frame, so a press on one drags it instead of picking+ /// under it or navigating.+ drawn: [Option<egui::Rect>; 4],+ /// The dragged label's offset when the button went down (while it is held).+ grab: Option<egui::Vec2>,+}++impl MeasureLabels {+ /// Whether `p` is on one of the labels drawn in the last frame.+ pub(crate) fn contains(&self, p: Pos2) -> bool {+ self.drawn.iter().flatten().any(|r| r.contains(p))+ }++ /// How far label `i` was dragged (zero: in its own place).+ pub(crate) fn offset(&self, i: usize) -> egui::Vec2 {+ self.offset.get(i).copied().unwrap_or(egui::Vec2::ZERO)+ }+}++/// Label `i`, drawn at `rect`, follows a drag of the pointer; a double-click puts it back.+fn drag_label(ui: &mut egui::Ui, labels: &mut MeasureLabels, i: usize, rect: egui::Rect) {+ if let Some(drawn) = labels.drawn.get_mut(i) {+ *drawn = Some(rect);+ }+ let r = ui.interact(rect, egui::Id::new(("sc_measure_label", i)), egui::Sense::click_and_drag());+ if r.dragged() {+ ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);+ } else if r.hovered() {+ ui.ctx().set_cursor_icon(egui::CursorIcon::Grab);+ }+ let Some(offset) = labels.offset.get_mut(i) else { return };+ if r.double_clicked() {+ *offset = egui::Vec2::ZERO;+ } else if r.dragged()+ && let (Some(from), Some(to)) = (ui.input(|i| i.pointer.press_origin()), r.interact_pointer_pos())+ {+ // Where it was at the press, plus how far the pointer went since: the label stays under+ // the pointer where it was grabbed.+ let start = *labels.grab.get_or_insert(*offset);+ let moved = start + (to - from);+ // Far enough to leave any view, finite whatever the pointer reports.+ if moved.is_finite() {+ *offset = moved.clamp(egui::Vec2::splat(-10_000.0), egui::Vec2::splat(10_000.0));+ }+ }+}++/// `rect` moved by a dragged label's `offset`, kept inside the view so it can be dragged back.+fn moved_label(proj: &Proj, rect: egui::Rect, offset: egui::Vec2) -> egui::Rect {+ if offset == egui::Vec2::ZERO {+ return rect;+ }+ let bounds = proj.rect.shrink(4.0);+ let moved = rect.translate(offset);+ let min = egui::pos2(+ moved.left().clamp(bounds.left(), (bounds.right() - rect.width()).max(bounds.left())),+ moved.top().clamp(bounds.top(), (bounds.bottom() - rect.height()).max(bounds.top())),+ );+ egui::Rect::from_min_size(min, rect.size())+}+/// The measured distance: a line between the ends of the shown measure (the centres for-/// centre to centre, the closest or farthest points) with its length.-fn measure_line(app: &SolveApp, painter: &egui::Painter, proj: &Proj, r: &serde_json::Value, show: Option<crate::dialogs::Shown>) {+/// centre to centre, the closest or farthest points) with its length. Its labels can be dragged+/// away from the part; a moved one keeps a leader to what it measures.+fn measure_line(+ app: &SolveApp,+ ui: &mut egui::Ui,+ painter: &egui::Painter,+ proj: &Proj,+ r: &serde_json::Value,+ show: Option<crate::dialogs::Shown>,+ labels: &mut MeasureLabels,+) {let t = Tokens::get();let Some((_, d, a, b)) = crate::dialogs::shown_distance(r, show) else { return };if !a.is_finite() || !b.is_finite() || !d.is_finite() {@@ -1317,15 +1400,20 @@ fn measure_line(app: &SolveApp, painter: &egui::Painter, proj: &Proj, r: &serde_painter.circle(s, 3.5, t.manipulator, Stroke::new(1.0, Color32::WHITE));}pick_numbers(painter, sa, sb);- let mid = sa + (sb - sa) * 0.5 + vec2(10.0, -14.0);+ let mid = sa + (sb - sa) * 0.5;let galley = painter.layout_no_wrap(crate::prefs::show_mm(app, d, 1), egui::FontId::proportional(crate::theme::Tokens::get().font_label), t.text);- let rect = egui::Rect::from_min_size(mid, galley.size() + vec2(10.0, 6.0));+ let own = egui::Rect::from_min_size(mid + vec2(10.0, -14.0), galley.size() + vec2(10.0, 6.0));+ let rect = moved_label(proj, own, labels.offset(0));crate::scenario::publish_target("measure:label", rect);if app.preferences.measure_axis_distances {- measure_components(app, painter, proj, a, b, rect);+ measure_components(app, ui, painter, proj, a, b, rect, labels);+ }+ if rect != own {+ painter.line_segment([mid, rect.clamp(mid)], Stroke::new(1.0, t.manipulator));}painter.rect(rect, t.radius_sm.saturating_sub(1), t.overlay, t.control_border_stroke(t.border), egui::StrokeKind::Inside);painter.galley(rect.min + vec2(5.0, 3.0), galley, t.text);+ drag_label(ui, labels, 0, rect);}/// "1" and "2" beside the measured line's ends (#6020), as the panel lists the picks: each just@@ -1347,13 +1435,24 @@ fn pick_numbers(painter: &egui::Painter, sa: Pos2, sb: Pos2) {}/// A world-axis-aligned chain between the same two measured points. Zero-length and-/// view-aligned legs still get a label; labels move apart when their projections coincide.-fn measure_components(app: &SolveApp, painter: &egui::Painter, proj: &Proj, a: Vec3, b: Vec3, total: egui::Rect) {+/// view-aligned legs still get a label; labels move apart when their projections coincide, and+/// the user can drag each away (`labels` 1 to 3).+#[allow(clippy::too_many_arguments)]+fn measure_components(+ app: &SolveApp,+ ui: &mut egui::Ui,+ painter: &egui::Painter,+ proj: &Proj,+ a: Vec3,+ b: Vec3,+ total: egui::Rect,+ labels: &mut MeasureLabels,+) {let t = Tokens::get();let x = Vec3::new(b.x, a.y, a.z);let y = Vec3::new(b.x, b.y, a.z);let mut occupied = vec![total];- for (axis, start, end, color) in [("x", a, x, t.axis_x), ("y", x, y, t.axis_y), ("z", y, b, t.axis_z)] {+ for (i, (axis, start, end, color)) in [("x", a, x, t.axis_x), ("y", x, y, t.axis_y), ("z", y, b, t.axis_z)].into_iter().enumerate() {let color = color.lerp_to_gamma(t.text, 0.25);let distance = start.dist(end);if !distance.is_finite() {@@ -1401,12 +1500,14 @@ fn measure_components(app: &SolveApp, painter: &egui::Painter, proj: &Proj, a: V}}}+ let rect = moved_label(proj, rect, labels.offset(i + 1));painter.line_segment([at, rect.center()], Stroke::new(0.8, color));painter.rect(rect, t.radius_sm.saturating_sub(1), t.overlay, Stroke::new(1.0, color), egui::StrokeKind::Inside);painter.galley(rect.min + vec2(5.0, 3.0), galley, t.text);let handle = format!("measure:label:{axis}");crate::scenario::publish_target(&handle, rect);occupied.push(rect);+ drag_label(ui, labels, i + 1, rect);}}diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex 9bc2cdb49f..2068807412 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -302,6 +302,8 @@ crate::dialog_descriptor::dialog_descriptors! {/// The measure chosen in the panel (centre, min or max); `None` follows the result's/// own default (centre where a round item gives one).show: Option<Shown>,+ /// Where the labels in the view were dragged to (#8510).+ labels: crate::canvas::MeasureLabels,},/// Move bodies by a distance along X, Y and Z./// Move/Copy (see `dialogs_move`): the move type, X/Y/Z distances (Point to Position: the@@ -929,7 +931,7 @@ impl Dialog {vec![SelInput::new("Plane", PLANES | PLANAR_FACES, false)],),"inspect.measure" => Dialog::new(- Kind::Measure { result: None, of: Vec::new(), show: None },+ Kind::Measure { result: None, of: Vec::new(), show: None, labels: Default::default() },vec![SelInput::new("Items",FACES@@ -2916,9 +2918,11 @@ pub(crate) fn fill_cells(st: &solidcraft_engine::doc::ModelState, tools: &[Strin/// Measure again when the picked items changed.fn update_measure(app: &SolveApp, d: &mut Dialog) {let items: Vec<Sel> = sels(d, 0).to_vec();- if let Kind::Measure { result, of, .. } = &mut d.kind+ if let Kind::Measure { result, of, labels, .. } = &mut d.kind&& *of != items{+ // Another measurement: its labels start in their own places.+ *labels = Default::default();*result = if items.is_empty() { None } else { Some(app.session.measure_items(&items)) };*of = items;}diff --git a/crates/ui-egui/src/measurement_ui_tests.rs b/crates/ui-egui/src/measurement_ui_tests.rsindex d2a958c765..a5cc549858 100644--- a/crates/ui-egui/src/measurement_ui_tests.rs+++ b/crates/ui-egui/src/measurement_ui_tests.rs@@ -210,6 +210,65 @@ fn the_view_numbers_the_two_picks() {}}+/// The labels in the view can be dragged away from the part (#8510): each follows the pointer,+/// keeps a leader to what it measures, picks nothing and starts no box selection on the way, and+/// goes back to its own place on a double-click or with the next measurement.+#[test]+fn the_measure_labels_drag_away_from_the_part_and_back() {+ let mut h = vertex_measurement([0., 0., 0.], [-3., 4., -12.]);+ h.call("ui.view", json!({"view": "iso"}));+ painted_text(&mut h);+ let own = component_rects();+ let measurement = result(&h).clone();+ let picks = h.call("ui.selection", json!({}))["result"].clone();+ let drag = |h: &mut Harness, from: egui::Pos2, by: egui::Vec2| {+ h.call("ui.drag", json!({"x0": from.x, "y0": from.y, "x1": from.x + by.x, "y1": from.y + by.y, "steps": 6}));+ painted_text(h);+ };+ // The distance, then the Z part, each dragged by the grabbed point away from the part.+ for (i, by) in [(0, egui::vec2(180.0, -130.0)), (3, egui::vec2(-160.0, 90.0))] {+ let from = component_rects()[i].left_top() + egui::vec2(6.0, 4.0);+ let before = component_rects();+ drag(&mut h, from, by);+ let after = component_rects();+ let moved = after[i].min - before[i].min;+ assert!((moved - by).length() < 1.0, "label {i} follows the pointer: moved {moved:?}, dragged {by:?}");+ if i == 3 {+ assert_eq!(after[..3], before[..3], "only the dragged label moves");+ }+ assert_eq!(*result(&h), measurement, "dragging a label measures nothing new");+ assert_eq!(h.call("ui.selection", json!({}))["result"], picks, "dragging a label picks nothing");+ assert!(h.app.viewport.boxsel.is_none(), "dragging a label starts no box selection");+ }+ let dragged = component_rects();+ assert!(!dragged[0].intersects(own[0]), "the distance left its place over the part: {:?} vs {:?}", dragged[0], own[0]);+ // The view turns: a dragged label stays away from the line by the same amount.+ h.call("ui.view", json!({"view": "front"}));+ painted_text(&mut h);+ let turned = component_rects();+ h.call("ui.view", json!({"view": "iso"}));+ painted_text(&mut h);+ assert_eq!(component_rects()[0], dragged[0], "back in the same view, back in the same place");+ assert!(turned.iter().all(|r| r.is_finite()));+ // A double-click puts the distance back.+ let at = dragged[0].center();+ h.call("ui.click", json!({"x": at.x, "y": at.y, "double": true}));+ painted_text(&mut h);+ assert_eq!(component_rects()[0], own[0], "a double-click puts the label back");+ // Another measurement puts the Z part back too.+ let offsets = |h: &Harness| match &h.app.dialog.as_ref().unwrap().kind {+ Kind::Measure { labels, .. } => (0..4).map(|i| labels.offset(i)).collect::<Vec<_>>(),+ _ => panic!("expected the Measure dialog"),+ };+ assert_ne!(offsets(&h)[3], egui::Vec2::ZERO);+ if let Some(input) = h.app.dialog.as_mut().and_then(|d| d.inputs.first_mut()) {+ input.items = [[0., 0., 0.], [-3., 4., 0.]].into_iter().map(|p| Sel::Vertex { body: "Box".into(), point: p.into() }).collect();+ }+ painted_text(&mut h);+ assert!((result(&h)["distance_mm"].as_f64().unwrap() - 5.).abs() < 1e-8, "measured the new picks");+ assert!(offsets(&h).iter().all(|o| *o == egui::Vec2::ZERO), "{:?}", offsets(&h));+}+#[test]fn the_distance_row_has_a_copy_button_that_copies_the_shown_value() {let mut h = vertex_measurement([0., 0., 0.], [3., 4., 0.]);diff --git a/docs/book/src/guide/sketching.md b/docs/book/src/guide/sketching.mdindex fb4095ac24..396a46bb10 100644--- a/docs/book/src/guide/sketching.md+++ b/docs/book/src/guide/sketching.md@@ -422,6 +422,9 @@ Measure shows the currently selected distance (centre, minimum or maximum) and sdistances on the canvas. The component dimensions follow the world axes and remain visibleuntil the Measure dialog closes. To hide these extra lines and labels, turn off**Preferences > Graphics > Show X/Y/Z distances**. The choice is saved between runs.+Drag the distance or an X, Y or Z label to move it away from the part; a moved label keeps a+line to what it measures. Double-click a label to put it back. Measuring other items starts the+labels in their own places again.### Reviewing mirror, pattern, and offset resultsdiff --git a/docs/control-protocol.md b/docs/control-protocol.mdindex b06405fcd4..c425896f1b 100644--- a/docs/control-protocol.md+++ b/docs/control-protocol.md@@ -355,6 +355,9 @@ to hide them while keeping the total distance and the dialog's ΔX/ΔY/ΔZ valueoverlays remain visible until Measure closes.Overlay handles `measure:label:x`, `measure:label:y` and `measure:label:z` identify the extralabels; `prefs:measure_axis_distances` identifies the checkbox.+A `ui.drag` from `measure:label` or one of the axis labels moves that label (screen offset, kept+while the measured items stay the same; a double-click puts it back). It picks nothing and starts+no box selection.## UI