mirror of
https://github.com/storytold/AniCraft.git
synced 2026-11-28 17:19:31 +01:00
fix(shell): settle an import's package and Save suggestion when it opens
Review follow-ups on the import name (ADR 0009 note):
* Session::imported is an `Import` {source, package, save_as}, fixed at
import time: removing an XFL package's DOMDocument.xml afterwards no
longer moves the Save suggestion inside the folder.
* Duplicate-tab lookup compares packages (`import_package`), so an XFL
folder and the .xfl in it are one document and share a tab.
* In a browser an import isn't matched by path: an upload is known only by
its name, and two different files may share one.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
de0424171d
commit
ea281bd334
7 files changed
+95
-33
No files matched your search
@@ -183,18 +183,43 @@ fn new_sample(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({"frames": s.frame_count()}))
|
||||
}
|
||||
|
||||
/// Where Save first suggests keeping a document imported from `source` (ADR 0009): an
|
||||
/// `.anicraft` file of the same name beside the Animate file, so `walk.fla` suggests
|
||||
/// `walk.anicraft` in its folder. An XFL document is a folder: whether the folder or the `.xfl`
|
||||
/// file in it was opened, the suggestion goes beside the folder, never inside it.
|
||||
pub fn import_save_path(source: &std::path::Path) -> PathBuf {
|
||||
let xfl_file = source.extension().is_some_and(|e| e.eq_ignore_ascii_case("xfl"))
|
||||
&& source.parent().is_some_and(|d| crate::vfs::is_file(&d.join("DOMDocument.xml")));
|
||||
let package = if xfl_file { source.parent().unwrap_or(source) } else { source };
|
||||
// A folder's whole name ("walk.v2" stays "walk.v2"); a file's without its extension.
|
||||
let name = if crate::vfs::is_dir(package) { package.file_name() } else { package.file_stem() };
|
||||
let name = name.map_or_else(|| "animation".to_string(), |n| n.to_string_lossy().into_owned());
|
||||
package.with_file_name(format!("{name}.anicraft"))
|
||||
/// Where an untitled document came from (ADR 0009), settled when it was imported, so a later
|
||||
/// change on disk (a package's files moved or removed) never changes what it is called or where
|
||||
/// Save suggests keeping it.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Import {
|
||||
/// What was opened: the `.fla`, the `.xfl` or the XFL folder. Its name names the document.
|
||||
pub source: PathBuf,
|
||||
/// The Animate document itself: the XFL folder, whether it or the `.xfl` in it was opened,
|
||||
/// or the `.fla`. Two imports of the same package are the same document.
|
||||
pub package: PathBuf,
|
||||
/// Where Save first suggests keeping it: an `.anicraft` of the same name beside the
|
||||
/// package, so `walk.fla` suggests `walk.anicraft` in its folder, and an XFL folder's goes
|
||||
/// beside the folder, never inside it.
|
||||
pub save_as: PathBuf,
|
||||
}
|
||||
|
||||
impl Import {
|
||||
/// The import of `source`, as the disk has it now.
|
||||
pub fn of(source: &std::path::Path) -> Import {
|
||||
let package = import_package(source);
|
||||
// A folder's whole name ("walk.v2" stays "walk.v2"); a file's without its extension.
|
||||
let name = if crate::vfs::is_dir(&package) { package.file_name() } else { package.file_stem() };
|
||||
let name = name.map_or_else(|| "animation".to_string(), |n| n.to_string_lossy().into_owned());
|
||||
let save_as = package.with_file_name(format!("{name}.anicraft"));
|
||||
Import { source: source.to_path_buf(), package, save_as }
|
||||
}
|
||||
}
|
||||
|
||||
/// The Animate document `path` opens: the XFL folder for the `.xfl` file in one, else `path`
|
||||
/// itself (a `.fla`, an XFL folder, or any other file).
|
||||
pub fn import_package(path: &std::path::Path) -> PathBuf {
|
||||
let xfl_file = path.extension().is_some_and(|e| e.eq_ignore_ascii_case("xfl"))
|
||||
&& path.parent().is_some_and(|d| crate::vfs::is_file(&d.join("DOMDocument.xml")));
|
||||
match path.parent() {
|
||||
Some(folder) if xfl_file => folder.to_path_buf(),
|
||||
_ => path.to_path_buf(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `path` is an Animate document: a `.fla` archive, an `.xfl` file or an XFL folder.
|
||||
@@ -230,7 +255,7 @@ pub(crate) fn open(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// as it was in Animate (ADR 0042).
|
||||
let merged = crate::merge::merge_document(&mut doc);
|
||||
s.replace_document(doc, None);
|
||||
s.imported = Some(path.clone());
|
||||
s.imported = Some(Import::of(&path));
|
||||
// Open Recent reopens the Animate file (ADR 0059).
|
||||
s.recent.add(&path);
|
||||
// `imported` and `save_as` come with the session's info.
|
||||
|
||||
@@ -100,7 +100,7 @@ pub use anicraft_render as render;
|
||||
pub use anicraft_svg as svg;
|
||||
/// Whether a path is an Animate document (`.fla`, `.xfl`, or an XFL folder) that `file.open`
|
||||
/// imports.
|
||||
pub use cmds_file::{import_save_path, is_animate};
|
||||
pub use cmds_file::{Import, import_package, is_animate};
|
||||
pub use cmds_modify::{Item as SelectedItem, transform_origins};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
@@ -411,7 +411,7 @@ pub struct Session {
|
||||
/// The Animate file (`.fla`, `.xfl` or XFL folder) this untitled document was imported from
|
||||
/// (ADR 0009). It names the document until it is saved, but is never written to: `path`
|
||||
/// stays `None`, so Save asks for a new `.anicraft` name.
|
||||
pub imported: Option<PathBuf>,
|
||||
pub imported: Option<cmds_file::Import>,
|
||||
pub style: ToolStyle,
|
||||
/// Whether the rulers, grid and guides show and snap (ADR 0076): the editor's, not the
|
||||
/// document's.
|
||||
@@ -1330,8 +1330,8 @@ impl Session {
|
||||
"dirty": self.is_dirty(),
|
||||
// The Animate file an untitled document came from, and the name Save suggests for
|
||||
// it (ADR 0009).
|
||||
"imported": self.imported.as_ref().map(|p| p.display().to_string()),
|
||||
"save_as": self.imported.as_deref().map(|p| cmds_file::import_save_path(p).display().to_string()),
|
||||
"imported": self.imported.as_ref().map(|i| i.source.display().to_string()),
|
||||
"save_as": self.imported.as_ref().map(|i| i.save_as.display().to_string()),
|
||||
"history": {"undo": self.history.undo_len(), "redo": self.history.redo_len(), "bytes": self.history.bytes(), "evicted": self.history.evicted},
|
||||
"state": self.state,
|
||||
})
|
||||
|
||||
@@ -388,12 +388,13 @@ fn an_import_keeps_its_animate_name_and_save_suggests_one_beside_it() {
|
||||
for (opened, suggested) in [(folder.clone(), beside_folder.clone()), (folder.join("primitives.xfl"), beside_folder), (fla.clone(), dir.join("Walk Cycle.v2.anicraft"))] {
|
||||
let mut s = Session::default();
|
||||
let info = s.execute("file.open", &json!({"path": opened.display().to_string()})).unwrap_or_else(|e| panic!("{}: {e}", opened.display()));
|
||||
assert_eq!(s.imported.as_deref(), Some(opened.as_path()), "the document is called by what was opened");
|
||||
assert_eq!(s.imported.as_ref().map(|i| i.source.as_path()), Some(opened.as_path()), "the document is called by what was opened");
|
||||
assert!(s.path.is_none(), "{}: still untitled, so Save never writes over the Animate file", opened.display());
|
||||
assert!(s.execute("file.save", &json!({})).is_err(), "Save asks for a name");
|
||||
assert_eq!(info["imported"], json!(opened.display().to_string()));
|
||||
assert_eq!(info["save_as"], json!(suggested.display().to_string()), "{}", opened.display());
|
||||
assert_eq!(anicraft_engine::import_save_path(&opened), suggested);
|
||||
assert_eq!(anicraft_engine::Import::of(&opened).save_as, suggested);
|
||||
assert_eq!(anicraft_engine::import_package(&opened), if opened == fla { fla.clone() } else { folder.clone() }, "the folder and its .xfl are one document");
|
||||
// A new document is no longer the import.
|
||||
let mut t = Session::default();
|
||||
t.execute("file.open", &json!({"path": opened.display().to_string()})).unwrap();
|
||||
@@ -405,7 +406,27 @@ fn an_import_keeps_its_animate_name_and_save_suggests_one_beside_it() {
|
||||
s.execute("file.open", &json!({"path": fla.display().to_string()})).unwrap();
|
||||
let saved = dir.join("Walk Cycle.v2.anicraft");
|
||||
s.execute("file.save", &json!({"path": saved.display().to_string(), "confirm": true})).unwrap();
|
||||
assert_eq!((s.path.as_deref(), s.imported.as_deref()), (Some(saved.as_path()), None));
|
||||
assert_eq!((s.path.as_deref(), s.imported.as_ref()), (Some(saved.as_path()), None));
|
||||
assert!(s.info()["save_as"].is_null());
|
||||
assert!(fla.is_file(), "the Animate file is untouched");
|
||||
}
|
||||
|
||||
/// An import's name and Save suggestion are settled when it opens (ADR 0009): an XFL package
|
||||
/// whose files change on disk afterwards still suggests saving beside its folder, not inside it.
|
||||
#[test]
|
||||
fn an_imports_save_suggestion_outlives_changes_to_its_package_on_disk() {
|
||||
let root = Path::new(env!("CARGO_TARGET_TMPDIR")).join("xfl-corpus-import-settled");
|
||||
let folder = root.join("Walk.v2");
|
||||
std::fs::create_dir_all(&folder).unwrap();
|
||||
for f in ["DOMDocument.xml", "primitives.xfl"] {
|
||||
std::fs::copy(corpus().join("primitives").join(f), folder.join(f)).unwrap();
|
||||
}
|
||||
let xfl = folder.join("primitives.xfl");
|
||||
let mut s = Session::default();
|
||||
s.execute("file.open", &json!({"path": xfl.display().to_string()})).unwrap();
|
||||
std::fs::remove_file(folder.join("DOMDocument.xml")).unwrap();
|
||||
let info = s.info();
|
||||
assert_eq!(info["save_as"], json!(root.join("Walk.v2.anicraft").display().to_string()), "still beside the folder");
|
||||
assert_eq!(info["imported"], json!(xfl.display().to_string()));
|
||||
std::fs::remove_dir_all(&root).ok();
|
||||
}
|
||||
@@ -32,12 +32,14 @@ elements) and `LIBRARY/*.xml` (symbols), with shape geometry encoded as edge str
|
||||
was opened (`walk.fla`, `walk.xfl` or the XFL folder `walk`) on its tab and in the window
|
||||
title, marked as imported (a dim "Imported" after the tab's name, "(imported)" in the
|
||||
title, the source's path in the tab's tooltip), so artists see which file they are working
|
||||
on. `Session::imported` holds that source; `path` stays `None`, so Save is still Save As.
|
||||
It first suggests `walk.anicraft` beside the original, and beside an XFL folder rather than
|
||||
inside it (`import_save_path`; `file.open` and the session's info report it as
|
||||
`imported` and `save_as`). Saving clears the mark: the document is the `.anicraft` file
|
||||
from then on. Opening the same Animate file again shows its tab instead of importing it a
|
||||
second time. The same holds for XFL folders, `.fla` archives and older binary `.fla` files,
|
||||
on. `Session::imported` (an `Import`) holds that source; `path` stays `None`, so Save is
|
||||
still Save As. It first suggests `walk.anicraft` beside the original, and beside an XFL
|
||||
folder rather than inside it (`file.open` and the session's info report it as `imported`
|
||||
and `save_as`). The package and the suggestion are settled when the file is imported, so
|
||||
later changes on disk don't move them. Saving clears the mark: the document is the
|
||||
`.anicraft` file from then on. Opening the same Animate document again (the XFL folder or
|
||||
the `.xfl` in it) shows its tab instead of importing it a second time; not in a browser,
|
||||
where an upload is known only by its name. The same holds for XFL folders, `.fla` archives and older binary `.fla` files,
|
||||
since they all enter through `file.open`.
|
||||
5. **Report, don't guess.** Anything not yet mapped (bitmaps, text, sound, masks, motion
|
||||
tweens with property curves, shape tweens, filters, extra scenes) is counted and shown in
|
||||
|
||||
+1
-1
@@ -3999,7 +3999,7 @@ impl App {
|
||||
pub(crate) fn save_suggestion(&self) -> String {
|
||||
match (&self.s.path, &self.s.imported) {
|
||||
(Some(p), _) => p.display().to_string(),
|
||||
(None, Some(from)) => anicraft_engine::import_save_path(from).display().to_string(),
|
||||
(None, Some(import)) => import.save_as.display().to_string(),
|
||||
(None, None) => "animation.anicraft".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
+13
-4
@@ -206,7 +206,7 @@ impl Tabs {
|
||||
/// A document's name on its tab: the file's, the Animate file's it was imported from (ADR 0009),
|
||||
/// or "Untitled" (numbered from the second on).
|
||||
fn name_of(untitled: u32, s: &Session) -> String {
|
||||
match s.path.as_deref().or(s.imported.as_deref()).and_then(Path::file_name) {
|
||||
match s.path.as_deref().or(s.imported.as_ref().map(|i| i.source.as_path())).and_then(Path::file_name) {
|
||||
Some(n) => n.to_string_lossy().into_owned(),
|
||||
None if untitled <= 1 => tr!("Untitled").to_string(),
|
||||
None => tr!("Untitled {n}", n = untitled),
|
||||
@@ -223,7 +223,7 @@ fn session(app: &App, i: usize) -> Option<&Session> {
|
||||
pub fn info(app: &App, i: usize) -> Option<TabInfo> {
|
||||
let slot = app.tabs.slots.get(i)?;
|
||||
let s = session(app, i)?;
|
||||
let imported = if s.path.is_none() { s.imported.clone() } else { None };
|
||||
let imported = if s.path.is_none() { s.imported.as_ref().map(|i| i.source.clone()) } else { None };
|
||||
Some(TabInfo { id: slot.id, name: name_of(slot.untitled, s), dirty: s.is_dirty(), path: s.path.clone(), imported, active: i == app.tabs.active })
|
||||
}
|
||||
|
||||
@@ -420,8 +420,17 @@ fn already_open(app: &App, id: &str, params: &Value) -> Option<usize> {
|
||||
},
|
||||
_ => return None,
|
||||
};
|
||||
// An unsaved import of the same Animate file counts too: its tab has that file's name.
|
||||
(0..app.tabs.len()).find(|i| session(app, *i).and_then(|s| s.path.as_deref().or(s.imported.as_deref())).is_some_and(|p| same_file(p, &path)))
|
||||
// An unsaved import of the same Animate document counts too, whether its XFL folder or the
|
||||
// .xfl in it was opened. Not in a browser: an upload is known only by its name there, and
|
||||
// two different files may share one.
|
||||
let package = anicraft_engine::import_package(&path);
|
||||
(0..app.tabs.len()).find(|i| {
|
||||
session(app, *i).is_some_and(|s| match (&s.path, &s.imported) {
|
||||
(Some(p), _) => same_file(p, &path),
|
||||
(None, Some(import)) => !platform::WEB && same_file(&import.package, &package),
|
||||
(None, None) => false,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// The shown document is the one the app started with, untouched: what is opened takes its
|
||||
|
||||
+7
-2
@@ -105,15 +105,20 @@ fn an_import_is_named_after_its_animate_file_until_it_is_saved() {
|
||||
app.request("file.new", json!({}));
|
||||
assert_eq!(names(&app), ["primitives", "Untitled"]);
|
||||
assert_eq!(window_title(&app), "AniCraft — Untitled");
|
||||
// Opening the same Animate file again shows its tab instead of importing it twice.
|
||||
// Opening the same Animate file again shows its tab instead of importing it twice, and so
|
||||
// does the .xfl in its folder: they are one document.
|
||||
app.request("file.open", json!({"path": folder.display().to_string()}));
|
||||
assert_eq!((app.tabs.len(), app.tabs.active()), (2, 0));
|
||||
app.request("file.new", json!({}));
|
||||
app.request("file.open", json!({"path": folder.join("primitives.xfl").display().to_string()}));
|
||||
assert_eq!((app.tabs.len(), app.tabs.active()), (3, 0), "the .xfl shows the folder's tab");
|
||||
assert_eq!(names(&app), ["primitives", "Untitled", "Untitled 2"]);
|
||||
// Saved, it is the .anicraft document, no longer marked.
|
||||
let dir = std::env::temp_dir().join(format!("anicraft-tabs-import-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let saved = dir.join("primitives.anicraft");
|
||||
app.run("file.save", json!({"path": saved.display().to_string(), "confirm": true}));
|
||||
assert_eq!(names(&app), ["primitives.anicraft", "Untitled"]);
|
||||
assert_eq!(names(&app), ["primitives.anicraft", "Untitled", "Untitled 2"]);
|
||||
assert!(infos(&app)[0].imported.is_none());
|
||||
assert_eq!(window_title(&app), "AniCraft — primitives.anicraft");
|
||||
assert_eq!(app.save_suggestion(), saved.display().to_string());
|
||||
|
||||
Reference in new issue
Block a user