SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch: a selected text moves when dragged from anywhere inside its frame (#8835) (#8851)
austin62jake420785b514e7
diff --git a/crates/ui-egui/src/sketch_drag_ui_tests.rs b/crates/ui-egui/src/sketch_drag_ui_tests.rsindex 742fa5d940..a641ad0be3 100644--- a/crates/ui-egui/src/sketch_drag_ui_tests.rs+++ b/crates/ui-egui/src/sketch_drag_ui_tests.rs@@ -54,6 +54,69 @@ fn text_move_drag_is_one_editable_unit_and_escape_restores_the_gesture() {}}+/// A drag from inside the selected text's frame moves the text, also from the space between its+/// letters, where only a press on a letter did before: one undo step, no selection box (#8835).+#[test]+fn a_selected_text_moves_when_dragged_from_between_its_letters() {+ let (mut h, gap) = two_letters(true);+ let undo = h.app.session.undo.len();+ let a = screen(&mut h, gap.x, gap.y);+ let b = screen(&mut h, gap.x + 3.0, gap.y + 2.0);+ let mut path = vec![a];+ path.extend(steps(a, b, 6));+ press_and_move(&mut h, &path);+ assert!(h.app.viewport.text_drag.is_some() && h.app.viewport.boxsel.is_none());+ release(&mut h, b);+ assert!(text_at(&h).dist(Vec2::new(3.0, 2.0)) < 0.02, "{:?}", text_at(&h));+ assert_eq!(h.app.session.undo.len(), undo + 1);+}++/// Empty space outside a selected text's frame, and between the letters of a text that isn't+/// selected, still starts a selection box, and the text stays where it is.+#[test]+fn elsewhere_a_drag_from_empty_space_draws_a_selection_box() {+ for selected in [true, false] {+ let (mut h, gap) = two_letters(selected);+ let from = if selected { gap + Vec2::new(12.0, 0.0) } else { gap };+ let a = screen(&mut h, from.x, from.y);+ let b = screen(&mut h, from.x + 3.0, from.y + 2.0);+ let mut path = vec![a];+ path.extend(steps(a, b, 6));+ press_and_move(&mut h, &path);+ assert!(h.app.viewport.boxsel.is_some() && h.app.viewport.text_drag.is_none(), "selected: {selected}");+ release(&mut h, b);+ assert_eq!(text_at(&h), Vec2::ZERO, "selected: {selected}");+ }+}++/// "II" (10 mm) seen from the top, selected or not, and the middle of its outlines: the space+/// between the two letters.+fn two_letters(selected: bool) -> (Harness, Vec2) {+ let mut h = Harness::new();+ h.app.home.open = false;+ run(&mut h, "sketch.create", json!({"plane":"XY"}));+ let text = h.app.run("sketch.text", json!({"text":"II","at":[0,0],"height":10})).expect("the text");+ h.call("ui.view", json!({"view":"top"}));+ h.app.cam.distance = 35.0;+ if selected {+ h.app.session.selection = vec![Sel::SketchCurve { id: text["curves"][0].as_str().expect("a letter").into(), sketch: None }];+ }+ h.frames(3);+ let sk = h.app.session.doc.sketch(1).expect("the sketch");+ let outline: Vec<Vec2> = sk.link_curves(text["link"].as_str().expect("its link")).into_iter().flat_map(|ci| sk.polyline(ci)).collect();+ let first = outline.first().copied().expect("outlines");+ let (lo, hi) =+ outline.iter().fold((first, first), |(lo, hi), p| (Vec2::new(lo.x.min(p.x), lo.y.min(p.y)), Vec2::new(hi.x.max(p.x), hi.y.max(p.y))));+ (h, (lo + hi) * 0.5)+}++/// Where the text of sketch 1 is placed.+fn text_at(h: &Harness) -> Vec2 {+ let sk = h.app.session.doc.sketch(1).expect("the sketch");+ let solidcraft_engine::sketch::LinkSource::Text { at, .. } = &sk.links[0].source else { panic!("a text") };+ *at+}+fn run(h: &mut Harness, id: &str, p: Value) {if let Err(e) = h.app.run(id, p.clone()) {panic!("{id} {p}: {e}");diff --git a/crates/ui-egui/src/sketch_text_handles.rs b/crates/ui-egui/src/sketch_text_handles.rsindex b340c5364f..8015fe76ea 100644--- a/crates/ui-egui/src/sketch_text_handles.rs+++ b/crates/ui-egui/src/sketch_text_handles.rs@@ -3,17 +3,17 @@//! that turns the text about its centre (Shift: steps of 15°). Each frame of a drag runs//! `sketch.edit_text` live; the whole gesture is one undo step and Esc cancels it (the drag//! shares [`crate::viewport::Viewport::text_drag`] with moving a text, so it ends and cancels-//! the same way).+//! the same way). A drag from anywhere else inside the frame moves the text ([`grab_inside`]).use std::cell::RefCell;use egui::{Align2, CursorIcon, FontId, Painter, Pos2, Rect, Stroke, StrokeKind, vec2};use serde_json::json;use solidcraft_engine::geom::Vec2;-use solidcraft_engine::sketch::{LinkKind, LinkSource};+use solidcraft_engine::sketch::{CurveKind, LinkKind, LinkSource};use crate::SolveApp;use crate::theme::Tokens;-use crate::viewport::Proj;+use crate::viewport::{Hit, Proj};/// Pixels from a handle's centre that still grab it.const GRAB_PX: f32 = 8.0;@@ -176,6 +176,24 @@ pub(crate) fn over(app: &SolveApp, proj: &Proj, pos: Pos2) -> bool {hit(app, proj, pos).is_some()}+/// The selected text's curve, as a drag from `pos` grabs it, when `pos` is inside the text's+/// frame: a drag from anywhere in the frame moves the text, not only one from a letter (#8835).+pub(crate) fn grab_inside(app: &SolveApp, proj: &Proj, pos: Pos2) -> Option<Hit> {+ let sketch = app.session.active_sketch?;+ let f = frame(app, &target(app)?)?;+ let at = on_plane(app, proj, pos)?;+ let q = f.to_local(at);+ if !((f.min.x..=f.max.x).contains(&q.x) && (f.min.y..=f.max.y).contains(&q.y)) {+ return None;+ }+ let id = crate::sketch_text::selected_entity(app)?;+ let state = app.session.world_state();+ let ss = state.sketch(sketch)?;+ let curve = ss.sketch.curves.iter().find(|c| c.id == id)?;+ let straight = matches!(curve.kind, CurveKind::Line { .. });+ Some(Hit::SketchCurve { sketch, id, straight, point: ss.plane.to_world(at) })+}+/// Where the pointer is on the active sketch's plane.fn on_plane(app: &SolveApp, proj: &Proj, pos: Pos2) -> Option<Vec2> {let state = app.session.world_state();diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rsindex 6461c100c5..e2cf7f589f 100644--- a/crates/ui-egui/src/viewport.rs+++ b/crates/ui-egui/src/viewport.rs@@ -4569,6 +4569,10 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {&& !crate::sketch_text_handles::dragging(app){let active = app.session.active_sketch;+ // Off the sketch's own geometry, a drag from inside the selected text's frame moves+ // the text, also from the space between its letters (#8835).+ let own = matches!(&hit, Some(Hit::SketchPoint { sketch, .. } | Hit::SketchMidpoint { sketch, .. } | Hit::SketchCurve { sketch, .. }) if Some(*sketch) == active);+ let hit = if own { hit } else { crate::sketch_text_handles::grab_inside(app, &proj, a).or(hit) };match hit {Some(Hit::SketchPoint { sketch, id, .. }) if Some(sketch) == active => {// A point dragged with the rest of a selection slides with it, from where it