Skip to content

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

Save As offers the open file's own name, not the name stored in the design (#8329) (#8354)

austin62jake65784cae2f8c
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index c6db3fe6cd..10cb19c73b 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -643,6 +643,19 @@ impl Session {
self.saved_visibility = self.visibility.clone();
}
+ /// The name Save As offers for the design's file, without its extension: the name of the
+ /// file it was opened from or last saved to, unless the design has been renamed since, and
+ /// the design's own name otherwise. The name stored in a file need not be the file's: a
+ /// design named `Plate` saved as `Plate-a.sc` and copied to `Plate-b.sc` is `Plate` in both,
+ /// and offering `Plate.sc` for each invites replacing one file with the other (#8329).
+ pub fn save_as_name(&self) -> String {
+ let stem = self.path.as_deref().and_then(|p| std::path::Path::new(p).file_stem()).map(|s| s.to_string_lossy().trim().to_string());
+ match stem {
+ Some(stem) if !stem.is_empty() && self.doc.name == self.saved.name => stem,
+ _ => self.doc.name.clone(),
+ }
+ }
+
pub fn render_backend(&self) -> render::BackendDescriptor {
let mut backend = render::builtin_backend();
if let Some(viewport) = &self.render_viewport {
diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs
index 7231d3a0de..c3cbd71506 100644
--- a/crates/ui-egui/src/toolbar.rs
+++ b/crates/ui-egui/src/toolbar.rs
@@ -793,12 +793,13 @@ pub(crate) fn save_as(app: &mut SolveApp) {
/// Save As offering a copy's name ("Bracket copy"), so the file it is a copy of is kept.
pub(crate) fn save_copy(app: &mut SolveApp) {
- let name = crate::i18n::format("document.copy_name", &[("name", &app.session.doc.name)]);
+ let name = crate::i18n::format("document.copy_name", &[("name", &app.session.save_as_name())]);
save_as_named(app, &name, solidcraft_engine::io::DESIGN_EXT);
}
+/// Save As offers the open file's own name, not the name stored in the design (#8329).
fn save_as_with_extension(app: &mut SolveApp, extension: &str) {
- let name = app.session.doc.name.clone();
+ let name = app.session.save_as_name();
save_as_named(app, &name, extension);
}
@@ -2507,6 +2508,43 @@ mod header_report_tests {
assert!(h.dir.join(&name).is_file() && old.is_file(), "Save As leaves the .solidcraft file as it was");
}
+ /// #8329: Save As (and Save a Copy) offer the name of the file the design came from, not the
+ /// name stored in the design, which a copy of the file still carries; a design renamed since
+ /// it was opened is offered its new name.
+ #[test]
+ fn save_as_offers_the_open_files_own_name() {
+ let mut h = crate::scenario::Harness::new();
+ let offered = std::rc::Rc::new(std::cell::RefCell::new(Vec::<String>::new()));
+ let (seen, dir) = (offered.clone(), h.dir.clone());
+ h.app.services.pick_save = Some(Box::new(move |name, _| {
+ seen.borrow_mut().push(name.to_string());
+ Box::pin(std::future::ready(Some(dir.join(name).to_string_lossy().into_owned())))
+ }));
+ let ask = |h: &mut crate::scenario::Harness, how: fn(&mut SolveApp)| {
+ how(&mut h.app);
+ h.frames(3);
+ h.settle();
+ offered.borrow().last().cloned().unwrap_or_default()
+ };
+ h.app.run("solid.box", json!({"length":2,"width":3,"height":4})).unwrap();
+ h.app.session.doc_mut().name = "Sample Plate".into();
+ assert_eq!(ask(&mut h, save_as), "Sample Plate.sc", "a new design is offered its own name");
+ // The same design under another file name, as copies of a file are (the report's
+ // "Sample Plate-bug.solidcraft"): it is still named "Sample Plate" inside.
+ let copy = h.dir.join("Sample Plate-bug.solidcraft");
+ std::fs::copy(h.dir.join("Sample Plate.sc"), &copy).unwrap();
+ h.app.run("doc.open", json!({"path": copy.to_string_lossy()})).unwrap();
+ assert_eq!(h.app.session.doc.name, "Sample Plate");
+ assert_eq!(ask(&mut h, save_as), "Sample Plate-bug.sc");
+ // Saved under that name, the file is the one Save As offers next time.
+ assert_eq!(h.app.session.path.as_deref(), Some(h.dir.join("Sample Plate-bug.sc").to_string_lossy().as_ref()));
+ assert_eq!(ask(&mut h, save_copy), "Sample Plate-bug copy.sc");
+ h.app.run("doc.open", json!({"path": copy.to_string_lossy()})).unwrap();
+ // Renamed since it was opened: the new name.
+ h.app.run("component.rename", json!({"component": 0, "name": "Plate Mk2"})).unwrap();
+ assert_eq!(ask(&mut h, save_as), "Plate Mk2.sc");
+ }
+
#[test]
fn native_menu_actions_are_disabled_behind_an_open_issue_report() {
let mut h = crate::scenario::Harness::new();
diff --git a/docs/book/src/guide/files.md b/docs/book/src/guide/files.md
index b8e6ab573b..d91e6a08bd 100644
--- a/docs/book/src/guide/files.md
+++ b/docs/book/src/guide/files.md
@@ -114,7 +114,9 @@ from a newer version are refused with a message.
**Save** writes a container by default; files named `.json` keep JSON on subsequent saves.
**Save As** offers a container or plain JSON on
-desktop; **Save As JSON** is also available in the File menu on desktop and web. Choose
+desktop; **Save As JSON** is also available in the File menu on desktop and web. Both start
+from the name of the open file, or from the design's new name when it was renamed since it
+was opened or saved; a new design starts from its own name. Choose
the `.json` extension for plain JSON. **Export Design as JSON** writes an independent,
self-contained copy without changing the current file. Scripts can explicitly pass
`"format": "json"` to `file.save` or `file.save_as` even with a `.sc` name.