M7: Save as to another file names the tab after that file

Save as kept the document's name unless it was Untitled or the old
file's own name (#935). A file opened from an import (.fig, .penpot,
.sketch) carries its source's name and has no file yet, so after Save as
to Other.figcraft its tab still showed the source's name; a file renamed
here did the same. The tab and the file on disk then disagree, and the
file can't be found by the name on its tab.

Saving to another file now always names the document after it, as a
desktop app's document window does. Saving to the same file keeps a name
given here, so a name a file name can't hold (with a colon, say) stays
until the next Save as.

Keeping a name chosen here across Save as (the #935 rule) was the
alternative; it is what left imported files with names no file has.
This commit is contained in:
Denis Gomes
2026-11-24 02:57:37 -05:00
parent c01d391df7
commit fe25e194d1
4 changed files with 30 additions and 11 deletions
+2 -3
View File
@@ -173,7 +173,8 @@ fn wants_data(p: &Value) -> bool {
/// Save the file of the active tab (the main file for a branch tab, with its branches), or export
/// the active document as an experimental `.fig` copy without changing its native save state.
/// Without a path the file comes back as base64 `data` for the caller to deliver (#969).
/// Without a path the file comes back as base64 `data` for the caller to deliver (#969). Saved
/// to another file, the document takes that file's name, as its tab does.
fn save_to(s: &mut Session, path: Option<String>) -> Result<Value> {
if let Some(path) = path.as_deref().filter(|p| ext(p) == "fig") {
return export_fig(s, &json!({"path": path}));
@@ -185,9 +186,7 @@ fn save_to(s: &mut Session, path: Option<String>) -> Result<Value> {
refuse_dropping_images(ds, path)?;
}
let mut snapshot = FileSnapshot::of(ds);
let follows_path = ds.path.as_deref().map_or(ds.doc.name == "Untitled", |old| ds.doc.name == file_stem(old));
if let Some(path) = path.as_deref()
&& follows_path
&& ds.path.as_deref() != Some(path)
{
Arc::make_mut(&mut snapshot.doc).name = file_stem(path);
+13 -3
View File
@@ -94,7 +94,7 @@ fn assert_save_state_unchanged(s: &Session, before: &DocState, recent: &[String]
}
#[test]
fn first_native_save_uses_chosen_filename_for_an_untitled_document() {
fn saving_to_another_file_names_the_document_after_it() {
let mut files = SaveFiles::new();
let path = files.path("Redis.figcraft");
let copy = files.path("Redis copy.figcraft");
@@ -111,9 +111,19 @@ fn first_native_save_uses_chosen_filename_for_an_untitled_document() {
s.execute("file.saveAs", &json!({"path": copy.to_string_lossy()})).unwrap();
assert_eq!(s.ds().unwrap().doc.name, "Redis copy");
s.execute("file.rename", &json!({"name": "Chosen title"})).unwrap();
s.execute("file.rename", &json!({"name": "Chosen: title"})).unwrap();
s.execute("file.save", &json!({})).unwrap();
assert_eq!(s.ds().unwrap().doc.name, "Chosen: title", "saving to the same file keeps a name given here");
s.execute("file.saveAs", &json!({"path": manual_copy.to_string_lossy()})).unwrap();
assert_eq!(s.ds().unwrap().doc.name, "Chosen title");
assert_eq!(s.ds().unwrap().doc.name, "Other", "a name given here too");
assert_eq!(figcraft_format::load_bytes(&std::fs::read(&manual_copy).unwrap()).unwrap().0.name, "Other");
// A file without one on disk yet: imported, here with its source's name.
let imported = files.path("Imported.figcraft");
let mut i = Session::new();
i.execute("file.rename", &json!({"name": "ШИМ"})).unwrap();
i.execute("file.saveAs", &json!({"path": imported.to_string_lossy()})).unwrap();
assert_eq!(i.ds().unwrap().doc.name, "Imported");
}
#[test]
+13 -4
View File
@@ -73,7 +73,12 @@ fn toast(app: &FigApp) -> &str {
}
fn expected_picker() -> Vec<(String, String, Vec<String>)> {
vec![("Save as".into(), "Prototype.figcraft".into(), vec!["figcraft".into(), "fig".into()])]
picker_named("Prototype")
}
/// The Save as picker suggesting `name` with the native extension.
fn picker_named(name: &str) -> Vec<(String, String, Vec<String>)> {
vec![("Save as".into(), format!("{name}.figcraft"), vec!["figcraft".into(), "fig".into()])]
}
#[test]
@@ -124,7 +129,8 @@ fn native_paths_and_an_extensionless_default_save_native_files_and_show_the_writ
let mut reopened = Session::empty();
reopened.execute("file.open", &json!({"path": path})).unwrap();
assert_eq!(reopened.doc().unwrap().nodes().count(), app.session.doc().unwrap().nodes().count());
assert_eq!(reopened.doc().unwrap().name, "Prototype");
let stem = std::path::Path::new(written).file_stem().unwrap().to_str().unwrap();
assert_eq!(reopened.doc().unwrap().name, stem, "named after the file it was saved as");
}
}
@@ -261,16 +267,19 @@ fn saving_legacy_fig_paths_keeps_the_success_toast_and_original_source_bytes() {
let legacy_bytes = std::fs::read(&legacy).unwrap();
app.save();
app.poll_dialog(&egui::Context::default());
assert_eq!(*picks.lock().unwrap(), expected_picker());
assert_eq!(*picks.lock().unwrap(), picker_named("original"), "named after the file it was saved as first");
assert_eq!(std::fs::read(&legacy).unwrap(), legacy_bytes);
assert!(target.is_file());
assert!(app.ui.last_error.is_none());
assert_eq!(app.session.doc().unwrap(), &before);
if destination == "corrected.figcraft" {
let mut renamed = before.clone();
renamed.name = "corrected".into();
assert_eq!(app.session.doc().unwrap(), &renamed, "saved to another file: named after it");
assert_eq!(toast(&app), "Saved corrected.figcraft");
assert_eq!(app.session.ds().unwrap().path.as_deref(), target.to_str());
assert!(!app.session.ds().unwrap().is_dirty());
} else {
assert_eq!(app.session.doc().unwrap(), &before, "an export leaves the document alone");
assert_eq!(toast(&app), "Exported copy.fig");
assert_eq!(app.session.ds().unwrap().path.as_deref(), Some(legacy_path.as_str()));
assert!(app.session.ds().unwrap().is_dirty());
File diff suppressed because one or more lines are too long