Skip to content

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

Leaving a sketch keeps the camera where it is (#4909)

bherbruck1814adb06a1a
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index b35585a01d..91f9f69f94 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -2443,7 +2443,6 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
plane["fixed"] = json!(true);
match app.run("sketch.create", json!({"plane":plane})) {
Ok(_) => {
- app.pre_sketch_cam = Some(app.cam);
look_at_sketch(app);
cancel = true;
then_start = d.then_start;
@@ -2778,9 +2777,7 @@ pub fn start_sketch(app: &mut SolveApp, sel: &Sel) -> Result<(), String> {
{
params["flip"] = json!(flip);
}
- let before = app.cam;
app.run("sketch.create", params).map_err(|error| crate::friendly_error("sketch.create", &error))?;
- app.pre_sketch_cam = Some(before);
look_at_sketch(app);
// On a face: look at the picked point, not the plane's origin (a corner of the face),
// which left the face filling the view off-centre like a big square.
diff --git a/crates/ui-egui/src/documents.rs b/crates/ui-egui/src/documents.rs
index da23a8f92f..8d7ef4fbe1 100644
--- a/crates/ui-egui/src/documents.rs
+++ b/crates/ui-egui/src/documents.rs
@@ -23,7 +23,6 @@ pub struct DocSlot {
pub(crate) autosave: Option<solidcraft_engine::recovery::Autosaver>,
pub(crate) big_operation: bool,
cam: Camera,
- pre_sketch_cam: Option<Camera>,
pre_sketch_marker: Option<Option<usize>>,
view: DocView,
occurrence_move: Option<crate::browser::OccMove>,
@@ -45,7 +44,6 @@ impl DocSlot {
autosave: None,
big_operation: false,
cam: Camera::default(),
- pre_sketch_cam: None,
pre_sketch_marker: None,
view: DocView::default(),
occurrence_move: None,
@@ -128,7 +126,6 @@ fn swap_in(app: &mut SolveApp, slot: DocSlot) -> DocSlot {
autosave: app.autosave.as_mut().and_then(|a| std::mem::replace(&mut a.active, slot.autosave)),
big_operation: std::mem::replace(&mut app.big_operation, slot.big_operation),
cam: std::mem::replace(&mut app.cam, slot.cam),
- pre_sketch_cam: std::mem::replace(&mut app.pre_sketch_cam, slot.pre_sketch_cam),
pre_sketch_marker: std::mem::replace(&mut app.pre_sketch_marker, slot.pre_sketch_marker),
view: take_view(app),
occurrence_move: std::mem::replace(&mut app.tree.occurrence_move, slot.occurrence_move),
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 67b0d712d8..40dbc43c94 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -367,8 +367,6 @@ pub struct SolveApp {
pub cam_anim: Option<CameraAnim>,
/// Seconds since start, as of this frame.
pub now: f64,
- /// The view before the current sketch started (Finish Sketch returns to it).
- pub pre_sketch_cam: Option<Camera>,
/// The history marker before Edit Sketch rolled the timeline to the sketch; Finish Sketch
/// restores it.
pub pre_sketch_marker: Option<Option<usize>>,
@@ -535,7 +533,6 @@ impl SolveApp {
cam: Camera::default(),
cam_anim: None,
now: 0.0,
- pre_sketch_cam: None,
pre_sketch_marker: None,
services,
file_picker: None,
@@ -1111,7 +1108,7 @@ impl SolveApp {
/// Edit a sketch: roll the timeline to just after it (back, so later features don't hide
/// it, or forward from before it, where it isn't built yet: #2716), turn the camera to face
- /// it, and remember both so Finish Sketch puts them back. Editing the sketch that is already open does nothing; editing another one
+ /// it, and remember the marker so Finish Sketch puts it back. Editing the sketch that is already open does nothing; editing another one
/// finishes the current sketch first, so edit sessions never nest.
pub fn edit_sketch(&mut self, id: u64) {
if let solidcraft_engine::doc::FaceAttachmentStatus::Lost { message, .. } =
@@ -1132,7 +1129,6 @@ impl SolveApp {
let Some(idx) = self.session.doc.feature_index(id) else { return };
let marker = self.session.doc.marker;
let rolled = marker != Some(idx + 1) && self.run("timeline.roll_to", json!({ "position": idx + 1 })).is_ok();
- let before = self.cam;
if self.run("sketch.edit", json!({ "sketch": id })).is_err() {
if rolled {
self.restore_marker(marker);
@@ -1140,12 +1136,11 @@ impl SolveApp {
return;
}
self.pre_sketch_marker = rolled.then_some(marker);
- self.pre_sketch_cam = Some(before);
dialogs::look_at_sketch(self);
}
- /// Finish the active sketch: stop editing, restore the history marker Edit Sketch moved and
- /// return to the view from before the sketch.
+ /// Finish the active sketch: stop editing and restore the history marker Edit Sketch moved.
+ /// The camera stays where it is, however the sketch is left.
pub fn finish_sketch(&mut self) {
sketch_text_move::cancel_drag(self);
if sketch_text::editing(self) || self.dialog.as_ref().is_some_and(|d| matches!(d.kind, dialogs::Kind::TextMove(_))) {
@@ -1157,9 +1152,6 @@ impl SolveApp {
if let Some(marker) = self.pre_sketch_marker.take() {
self.restore_marker(marker);
}
- if let Some(c) = self.pre_sketch_cam.take() {
- self.animate_to(c);
- }
}
fn restore_marker(&mut self, marker: Option<usize>) {
diff --git a/crates/ui-egui/src/sketch_edit_tests.rs b/crates/ui-egui/src/sketch_edit_tests.rs
index 47ba3a5c00..38f1c635aa 100644
--- a/crates/ui-egui/src/sketch_edit_tests.rs
+++ b/crates/ui-egui/src/sketch_edit_tests.rs
@@ -82,3 +82,83 @@ fn toolbar_finish_and_other_feature_edit_leave_the_sketch() {
app.edit_feature(extrude);
assert_eq!(app.session.active_sketch, None);
}
+
+/// Leaving a sketch any way leaves the camera exactly where it is; entering still faces the plane.
+mod exit_keeps_view {
+ use crate::scenario::Harness;
+ use serde_json::json;
+ use solidcraft_engine::render::Camera;
+
+ /// Start a sketch on XY from the iso view, then orbit and pan into a 3D view of the user's own.
+ fn sketching_in_3d() -> (Harness, Camera) {
+ let mut h = Harness::new();
+ h.call("ui.view", json!({"view": "iso"}));
+ h.frames(3);
+ h.step(&json!({"start": "sketch.create"})).unwrap();
+ h.step(&json!({"click": {"plane": "XY"}})).unwrap();
+ h.settle();
+ crate::scenario::check(&mut h, &json!({"sketching": true, "camera": {"yaw_deg": 0, "pitch_deg": 90}})).unwrap();
+ h.app.cam.orbit(60.0, -40.0);
+ h.app.cam.pan(25.0, 15.0, 800.0);
+ h.app.cam_anim = None;
+ let cam = h.app.cam;
+ (h, cam)
+ }
+
+ #[test]
+ fn finish_keeps_the_camera() {
+ let (mut h, cam) = sketching_in_3d();
+ h.app.run("sketch.line", json!({"points": [[0, 0], [30, 10]]})).unwrap();
+ h.app.finish_sketch();
+ h.settle();
+ assert_eq!(h.app.session.active_sketch, None);
+ assert_eq!(h.app.cam, cam);
+ }
+
+ #[test]
+ fn double_escape_keeps_the_camera_with_or_without_geometry() {
+ for line in [true, false] {
+ let (mut h, cam) = sketching_in_3d();
+ if line {
+ h.app.run("sketch.line", json!({"points": [[0, 0], [30, 10]]})).unwrap();
+ }
+ h.step(&json!({"key": "Escape"})).unwrap();
+ h.step(&json!({"key": "Escape"})).unwrap();
+ h.settle();
+ assert_eq!(h.app.session.active_sketch, None, "line: {line}");
+ assert_eq!(h.app.session.doc.features.len(), usize::from(line), "a blank sketch is discarded, one with a line kept");
+ assert_eq!(h.app.cam, cam, "line: {line}");
+ }
+ }
+
+ #[test]
+ fn cancelling_the_new_sketch_dialog_keeps_the_camera() {
+ let mut h = Harness::new();
+ h.call("ui.view", json!({"view": "iso"}));
+ h.frames(3);
+ h.step(&json!({"start": "sketch.create"})).unwrap();
+ h.app.cam.orbit(60.0, -40.0);
+ h.app.cam_anim = None;
+ let cam = h.app.cam;
+ crate::dialogs::cancel(&mut h.app);
+ h.step(&json!({"key": "Escape"})).unwrap();
+ h.settle();
+ assert_eq!(h.app.session.active_sketch, None);
+ assert_eq!(h.app.cam, cam);
+ }
+
+ #[test]
+ fn editing_a_sketch_faces_it_and_finishing_leaves_the_view() {
+ let mut app = crate::test_support::sample_app();
+ let id = super::first_sketch(&app);
+ let before = app.cam;
+ app.edit_sketch(id);
+ assert!(app.cam_anim.is_some_and(|a| a.to != before), "entering turns the view to face the sketch");
+ app.cam_anim = None;
+ app.cam.orbit(30.0, 20.0);
+ let cam = app.cam;
+ app.finish_sketch();
+ assert_eq!(app.cam, cam);
+ assert!(app.cam_anim.is_none(), "no animation back");
+ }
+}
diff --git a/crates/ui-egui/src/sketch_palette.rs b/crates/ui-egui/src/sketch_palette.rs
index 95f2415492..96842b0e3b 100644
--- a/crates/ui-egui/src/sketch_palette.rs
+++ b/crates/ui-egui/src/sketch_palette.rs
@@ -1012,7 +1012,7 @@ mod tests {
}
/// A double Esc on a sketch left blank takes it away (no empty sketch in the browser or the
- /// timeline, one undo step) and puts the view back; one line is enough to keep it.
+ /// timeline, one undo step) and leaves the view alone; one line is enough to keep it.
#[test]
fn a_quick_double_escape_discards_a_blank_sketch_and_keeps_one_with_a_line() {
let mut h = crate::scenario::Harness::new();
@@ -1026,7 +1026,7 @@ mod tests {
h.step(&json!({"key":"Escape"})).unwrap();
h.step(&json!({"key":"Escape"})).unwrap();
h.settle();
- crate::scenario::check(&mut h, &json!({"sketching": false, "camera": {"yaw_deg": -45, "pitch_deg": 35}})).unwrap();
+ crate::scenario::check(&mut h, &json!({"sketching": false, "camera": {"yaw_deg": 0, "pitch_deg": 90}})).unwrap();
assert_eq!(h.app.ui.tab, "MODEL");
assert!(h.app.session.doc.features.is_empty(), "the blank sketch is gone");
assert!(crate::scenario::handles("timeline:").iter().all(|(n, _)| !n.contains("Sketch")));
diff --git a/crates/ui-egui/src/workspace.rs b/crates/ui-egui/src/workspace.rs
index eab60311b9..52ddc0e915 100644
--- a/crates/ui-egui/src/workspace.rs
+++ b/crates/ui-egui/src/workspace.rs
@@ -857,11 +857,10 @@ mod tests {
/// Every way into a sketch (New Sketch on the ribbon then a plane, a double-click in the
/// timeline or the browser, Edit Sketch in the browser's menu) opens the SKETCH tab and looks
- /// at the sketch; Finish Sketch (palette or ribbon) lands on MODEL with the earlier view back
+ /// at the sketch; Finish Sketch (palette or ribbon) lands on MODEL with the view left where it is
/// and the sketch still listed, as before the ribbon redesign.
#[test]
fn entering_and_finishing_a_sketch_keeps_the_tab_view_and_history() {
- let iso = json!({"camera": {"yaw_deg": -45, "pitch_deg": 35}});
let top = json!({"camera": {"yaw_deg": 0, "pitch_deg": 90}});
let expect = |h: &mut crate::scenario::Harness, e: &serde_json::Value, what: &str| {
h.frames(3);
@@ -877,7 +876,7 @@ mod tests {
h.app.run("sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [40, 30]})).unwrap();
h.step(&json!({"click": {"handle": "sketch_palette:finish"}})).unwrap();
expect(&mut h, &json!({"sketching": false, "errors": 0}), "Finish");
- expect(&mut h, &iso, "Finish puts the view back");
+ expect(&mut h, &top, "Finish leaves the view where it is");
assert_eq!(h.app.ui.tab, "MODEL");
assert!(crate::scenario::handle_at("timeline:Sketch1").is_some() && crate::scenario::handle_at("row:Sketch1").is_some());
@@ -899,7 +898,7 @@ mod tests {
assert_eq!(h.app.ui.tab, "SKETCH", "{how}");
h.step(&json!({"click": {"handle": "button:sketch.finish"}})).unwrap();
expect(&mut h, &json!({"sketching": false}), how);
- expect(&mut h, &iso, how);
+ expect(&mut h, &top, how);
assert_eq!(h.app.ui.tab, "MODEL", "{how}");
}
}