mirror of
https://github.com/storytold/AniCraft.git
synced 2026-11-25 12:00:50 +01:00
feat(shell): an imported Animate file keeps its name on its tab and in the title
A .fla, .xfl or XFL folder used to open as "Untitled" (ADR 0009, decision 4), which artists found confusing. Untitled is about saving, not naming: * Session::imported holds the Animate file a document was imported from; path stays None, so Save is still Save As and never writes over it. * The tab shows the source's name with a dim "Imported" caption, the window title "<name> (imported)", the tooltip where it came from; screen readers hear "imported". Saving clears the mark. * Save / Save As suggest <name>.anicraft beside the original (beside an XFL folder, never inside it): engine import_save_path, reported by file.open and the session's info as `imported` and `save_as`. * Native save dialogs open in the suggested file's folder with only its name in the field (platform::split_suggestion), also for Save As of a saved document. * Opening the same Animate file again shows its tab; an import doesn't take an "Untitled N" number. Docs: ADR 0009 note, the guide, changelog. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bcfea81647
commit
355303a169
12 files changed
+245
-30
No files matched your search
@@ -2,6 +2,14 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
* **An imported Animate file keeps its name** (ADR 0009 note): a `.fla`, `.xfl` or XFL folder
|
||||
opens with its own name on its tab and in the window title instead of "Untitled", marked as
|
||||
imported (a dim "Imported" on the tab, "(imported)" in the title, where it came from in the
|
||||
tooltip). Saving still never writes over it: Save asks for a new name, suggesting an
|
||||
`.anicraft` of the same name beside the original, and from then on the tab shows that file.
|
||||
Opening the same Animate file again shows its tab. Save dialogs now open in the suggested
|
||||
file's folder.
|
||||
|
||||
* **Themes of your own** (#340, ADR 0117, M8): a theme is a small JSON file that names colours
|
||||
for the interface's roles, kept in `themes` in the settings folder (`data/themes` in a
|
||||
portable copy).
|
||||
|
||||
@@ -174,6 +174,20 @@ 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"))
|
||||
}
|
||||
|
||||
/// Whether `path` is an Animate document: a `.fla` archive, an `.xfl` file or an XFL folder.
|
||||
pub fn is_animate(path: &std::path::Path) -> bool {
|
||||
let ext = path.extension().and_then(|e| e.to_str()).map(str::to_ascii_lowercase);
|
||||
@@ -185,8 +199,9 @@ pub(crate) fn open(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let unsaved = opt_bool(p, "unsaved")?.unwrap_or(false);
|
||||
let untitled = opt_bool(p, "untitled")?.unwrap_or(false);
|
||||
if is_animate(&path) {
|
||||
// Imported, not adopted: the document is untitled, so Save asks for a new .anicraft
|
||||
// name and never writes over the Animate file.
|
||||
// Imported, not adopted: the document has no file name, so Save asks for a new
|
||||
// .anicraft name and never writes over the Animate file. It is still called by the
|
||||
// Animate file's name, and Save suggests one beside it (ADR 0009).
|
||||
let fla = path.extension().is_some_and(|e| e.eq_ignore_ascii_case("fla")) && crate::vfs::is_file(&path);
|
||||
let imported = if fla {
|
||||
// Through `vfs`, so an archive the browser handed over opens too (ADR 0036).
|
||||
@@ -204,10 +219,11 @@ 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());
|
||||
// Open Recent reopens the Animate file (ADR 0059).
|
||||
s.recent.add(&path);
|
||||
// `imported` and `save_as` come with the session's info.
|
||||
let mut info = s.info();
|
||||
info["imported"] = json!(path.display().to_string());
|
||||
info["report"] = json!(report.summary());
|
||||
info["skipped"] = json!(report.skipped);
|
||||
info["kept"] = json!(report.kept);
|
||||
@@ -235,6 +251,8 @@ fn save(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
format::save(&s.doc, &path)?;
|
||||
s.path = Some(path.clone());
|
||||
// Saved: the document is the .anicraft file now, not the import.
|
||||
s.imported = None;
|
||||
s.mark_saved();
|
||||
s.recent.add(&path);
|
||||
Ok(json!({"saved": path.display().to_string()}))
|
||||
|
||||
@@ -93,7 +93,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::is_animate;
|
||||
pub use cmds_file::{import_save_path, is_animate};
|
||||
pub use cmds_modify::{Item as SelectedItem, transform_origins};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
@@ -395,6 +395,10 @@ pub struct Session {
|
||||
pub state: StateId,
|
||||
saved_state: Option<StateId>,
|
||||
pub path: Option<PathBuf>,
|
||||
/// 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 style: ToolStyle,
|
||||
/// Whether the rulers, grid and guides show and snap (ADR 0076): the editor's, not the
|
||||
/// document's.
|
||||
@@ -558,6 +562,7 @@ impl Session {
|
||||
state,
|
||||
saved_state: Some(state),
|
||||
path: None,
|
||||
imported: None,
|
||||
style: ToolStyle::default(),
|
||||
aids: ViewAids::default(),
|
||||
text_style: cmds_text::TextStyle::default(),
|
||||
@@ -1303,6 +1308,10 @@ impl Session {
|
||||
"sounds": sounds,
|
||||
"revision": self.revision,
|
||||
"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()),
|
||||
"history": {"undo": self.history.undo_len(), "redo": self.history.redo_len(), "bytes": self.history.bytes(), "evicted": self.history.evicted},
|
||||
"state": self.state,
|
||||
})
|
||||
|
||||
@@ -373,3 +373,39 @@ fn file_open_names_the_sounds_and_videos_that_did_not_come_across() {
|
||||
let got = s.execute("file.open", &json!({"path": corpus().join("video").display().to_string(), "confirm": true})).unwrap();
|
||||
assert_eq!(got["named"]["videos AniCraft can't play"], json!(["intro.flv: FLV, On2 VP6, 4.5 s", "gone.mp4: its file is missing, 1.0 s"]));
|
||||
}
|
||||
|
||||
/// ADR 0009: an import is called by the Animate file it came from, while Save still asks for a
|
||||
/// new name, first suggesting an `.anicraft` of the same name beside the original, whether the
|
||||
/// XFL folder, the `.xfl` in it or a `.fla` was opened. Saving makes it the `.anicraft` file.
|
||||
#[test]
|
||||
fn an_import_keeps_its_animate_name_and_save_suggests_one_beside_it() {
|
||||
let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("xfl-corpus-import-name");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let folder = corpus().join("primitives");
|
||||
let fla = dir.join("Walk Cycle.v2.FLA");
|
||||
std::fs::write(&fla, pack::pack(&pack::package(&folder), &pack::VARIANTS[0])).unwrap();
|
||||
let beside_folder = corpus().join("primitives.anicraft");
|
||||
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!(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);
|
||||
// A new document is no longer the import.
|
||||
let mut t = Session::default();
|
||||
t.execute("file.open", &json!({"path": opened.display().to_string()})).unwrap();
|
||||
t.execute("file.new", &json!({"confirm": true})).unwrap();
|
||||
assert!(t.imported.is_none() && t.info()["imported"].is_null());
|
||||
}
|
||||
// Saved, it is the .anicraft document: the import's name and suggestion are gone.
|
||||
let mut s = Session::default();
|
||||
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!(s.info()["save_as"].is_null());
|
||||
assert!(fla.is_file(), "the Animate file is untouched");
|
||||
}
|
||||
@@ -27,6 +27,18 @@ elements) and `LIBRARY/*.xml` (symbols), with shape geometry encoded as edge str
|
||||
palette, the command line and agents all import the same way, and it inherits the
|
||||
unsaved-work confirmation. **An imported document is untitled**: Save asks for a new
|
||||
`.anicraft` name and can never write over the Animate file.
|
||||
|
||||
*Note (2026-11):* untitled is about saving, not naming. An import keeps the name of what
|
||||
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,
|
||||
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
|
||||
the status bar after opening ("Imported. Not yet supported: 2 bitmaps, 1 text field.").
|
||||
|
||||
@@ -183,7 +183,7 @@ ActionScript 1.0 and 2.0, and scripts placed on instances. See [Interactivity](i
|
||||
### Files, Test Movie and publishing
|
||||
|
||||
* AniCraft saves `.anicraft` documents, an open, documented format. It **opens** `.fla`, `.xfl`
|
||||
and XFL folders, as new, untitled documents, but can't save back to them. See
|
||||
and XFL folders, as new documents named after the file, but can't save back to them. See
|
||||
[Opening Animate files](opening-animate-files.md).
|
||||
* There is **no SWF** export. For the web, **Export HTML Canvas** writes one page that plays the
|
||||
movie, buttons and scripts in any browser. Video (with your own ffmpeg), GIF, animated PNG and
|
||||
|
||||
@@ -12,8 +12,8 @@ structure and from observation. On a Mac, read **⌘** wherever this chapter say
|
||||
|
||||
1. Choose **File › Open…** (**Ctrl+O**, **⌘O** on macOS).
|
||||
2. Pick a `.fla` file, or the `.xfl` file inside an XFL folder.
|
||||
3. AniCraft opens it as a new, untitled document, and the status bar says what came across and
|
||||
what didn't.
|
||||
3. AniCraft opens it as a new document named after the file, and the status bar says what came
|
||||
across and what didn't.
|
||||
|
||||
Other ways in:
|
||||
|
||||
@@ -31,9 +31,12 @@ folder can't be opened there.
|
||||
|
||||
## Saving: your Animate file is never touched
|
||||
|
||||
An opened Animate file comes in **untitled**. When you save, AniCraft asks for a new name and
|
||||
writes an `.anicraft` document, so the original `.fla` or XFL is never overwritten. The status
|
||||
bar reminds you: "Save to keep it as an AniCraft document."
|
||||
An opened Animate file keeps its name on its tab and in the window title, marked
|
||||
**Imported**, but it has no AniCraft file yet. When you save, AniCraft asks for a new name,
|
||||
suggesting one beside the original (`walk.fla` suggests `walk.anicraft` in the same folder),
|
||||
and writes an `.anicraft` document, so the original `.fla` or XFL is never overwritten. The
|
||||
status bar reminds you: "Save to keep it as an AniCraft document." Once saved, the tab shows
|
||||
the `.anicraft` name. Hover over an imported tab to see which file it came from.
|
||||
|
||||
AniCraft doesn't save back to `.fla` or XFL.
|
||||
|
||||
|
||||
@@ -222,8 +222,9 @@ a report after opening. Motion tweens from Animate files are not imported yet. S
|
||||
|
||||
### Can it save back to `.fla`?
|
||||
|
||||
No. AniCraft saves its own `.anicraft` format, and an opened Animate file comes in as a new,
|
||||
untitled document, so saving never overwrites the original.
|
||||
No. AniCraft saves its own `.anicraft` format. An opened Animate file keeps its name, marked
|
||||
as imported, but saving always asks for a new `.anicraft` name, so it never overwrites the
|
||||
original.
|
||||
|
||||
### Can it export SWF?
|
||||
|
||||
|
||||
+13
-4
@@ -3660,8 +3660,8 @@ impl App {
|
||||
}
|
||||
let prompt = match id {
|
||||
"file.open" => Some((tr_noop!("Open document"), "path", "animation.anicraft".to_string())),
|
||||
"file.save" if self.s.path.is_none() => Some((tr_noop!("Save document as"), "path", "animation.anicraft".to_string())),
|
||||
"file.save_as" => Some((tr_noop!("Save document as"), "path", self.s.path.as_ref().map_or_else(|| "animation.anicraft".to_string(), |p| p.display().to_string()))),
|
||||
"file.save" if self.s.path.is_none() => Some((tr_noop!("Save document as"), "path", self.save_suggestion())),
|
||||
"file.save_as" => Some((tr_noop!("Save document as"), "path", self.save_suggestion())),
|
||||
"layer.rename" => Some((tr_noop!("Rename layer"), "name", self.s.active_layer().map_or_else(String::new, |l| l.name.clone()))),
|
||||
"scene.rename" => Some((tr_noop!("Rename scene"), "name", self.s.doc.scenes.get(self.s.scene).map_or_else(String::new, |s| s.name.clone()))),
|
||||
"file.export_png" => Some((tr_noop!("Export frame as PNG"), "path", format!("frame_{:04}.png", self.s.frame + 1))),
|
||||
@@ -3732,6 +3732,16 @@ impl App {
|
||||
self.request(id, json!({}));
|
||||
}
|
||||
|
||||
/// The name Save As offers: the document's own file, or for an unsaved import an
|
||||
/// `.anicraft` of the same name beside the Animate file (ADR 0009), else a placeholder.
|
||||
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, None) => "animation.anicraft".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The stage point at the centre of the visible canvas.
|
||||
/// Pastes the system clipboard's picture, when it is newer than what AniCraft copied.
|
||||
/// AniCraft's own Copy leaves a note as text on the system clipboard, so while that note is
|
||||
@@ -4168,8 +4178,7 @@ impl App {
|
||||
}
|
||||
|
||||
fn update_title(&mut self, ctx: &egui::Context) {
|
||||
let name = crate::tabs::active_name(self);
|
||||
let title = format!("AniCraft — {name}{}", if self.s.is_dirty() { " •" } else { "" }); // i18n-exempt: the product name and the file's
|
||||
let title = crate::tabs::window_title(self);
|
||||
if title != self.title {
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Title(title.clone()));
|
||||
self.title = title;
|
||||
|
||||
+43
-2
@@ -999,6 +999,18 @@ pub fn reads_file(cmd: &str) -> bool {
|
||||
matches!(cmd, "file.open" | "plugin.install" | "file.import" | "file.import_to_library" | "brush.preset_import" | "filter.preset_import" | "swatch.import" | "macro.run" | "macro.import")
|
||||
}
|
||||
|
||||
/// A save dialog's suggestion split into the folder it opens in and the name in its field: a
|
||||
/// full path ("/art/walk.anicraft", the file the document has or, for an import, beside the
|
||||
/// Animate file) opens in its folder when that exists; a bare name opens wherever the platform
|
||||
/// last did.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub(crate) fn split_suggestion(default_name: &str) -> (Option<PathBuf>, String) {
|
||||
let path = std::path::Path::new(default_name);
|
||||
let dir = path.parent().filter(|d| !d.as_os_str().is_empty() && d.is_dir()).map(std::path::Path::to_path_buf);
|
||||
let name = path.file_name().map_or_else(|| default_name.to_string(), |n| n.to_string_lossy().into_owned());
|
||||
(dir, name)
|
||||
}
|
||||
|
||||
/// Shows the platform's own dialog (Windows common dialog, macOS panel, Linux XDG portal), with
|
||||
/// `title` already in the interface language. `None` when cancelled.
|
||||
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "macos")))]
|
||||
@@ -1008,7 +1020,15 @@ pub fn file_dialog(cmd: &str, title: &str, default_name: &str) -> Option<PathBuf
|
||||
match cmd {
|
||||
c if reads_file(c) => d.add_filter(label, exts).pick_file(),
|
||||
"file.export_png_sequence" => d.pick_folder(),
|
||||
_ => d.add_filter(label, exts).set_file_name(default_name).save_file(),
|
||||
_ => {
|
||||
let (dir, name) = split_suggestion(default_name);
|
||||
let d = d.add_filter(label, exts).set_file_name(name);
|
||||
match dir {
|
||||
Some(dir) => d.set_directory(dir),
|
||||
None => d,
|
||||
}
|
||||
.save_file()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1049,7 +1069,15 @@ pub fn start_file_dialog(cmd: &str, title: &str, default_name: &str, window: &ef
|
||||
let answer: Answer = match cmd {
|
||||
c if reads_file(c) => Box::pin(d.add_filter(label, exts).pick_file()),
|
||||
"file.export_png_sequence" => Box::pin(d.pick_folder()),
|
||||
_ => Box::pin(d.add_filter(label, exts).set_file_name(default_name).save_file()),
|
||||
_ => {
|
||||
let (dir, name) = split_suggestion(default_name);
|
||||
let d = d.add_filter(label, exts).set_file_name(name);
|
||||
Box::pin(match dir {
|
||||
Some(dir) => d.set_directory(dir),
|
||||
None => d,
|
||||
}
|
||||
.save_file())
|
||||
}
|
||||
};
|
||||
let _ = sheet_tx.send(answer);
|
||||
}
|
||||
@@ -1242,6 +1270,19 @@ mod tests {
|
||||
assert_eq!(menu_inset(), 0.0);
|
||||
}
|
||||
|
||||
/// A save dialog opens in the suggested file's folder with only its name in the field
|
||||
/// (ADR 0009: an import's `.anicraft` goes beside the Animate file).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn a_save_suggestion_opens_in_its_folder_with_its_name() {
|
||||
let dir = std::env::temp_dir();
|
||||
let full = dir.join("walk.anicraft");
|
||||
assert_eq!(split_suggestion(&full.display().to_string()), (Some(dir.clone()), "walk.anicraft".to_string()));
|
||||
assert_eq!(split_suggestion("animation.anicraft"), (None, "animation.anicraft".to_string()), "a bare name: the platform picks the folder");
|
||||
let gone = dir.join("anicraft-no-such-folder-here").join("walk.anicraft");
|
||||
assert_eq!(split_suggestion(&gone.display().to_string()).0, None, "a folder that isn't there is not offered");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_desktop_runs_every_command() {
|
||||
assert!(anicraft_engine::commands().iter().all(|c| unavailable(c.id).is_none()));
|
||||
|
||||
+54
-12
@@ -119,6 +119,9 @@ pub struct TabInfo {
|
||||
pub name: String,
|
||||
pub dirty: bool,
|
||||
pub path: Option<PathBuf>,
|
||||
/// The Animate file an unsaved import came from (ADR 0009): the tab is named after it and
|
||||
/// marked "Imported".
|
||||
pub imported: Option<PathBuf>,
|
||||
pub active: bool,
|
||||
}
|
||||
|
||||
@@ -195,9 +198,10 @@ impl Tabs {
|
||||
}
|
||||
}
|
||||
|
||||
/// A document's name on its tab: the file's, or "Untitled" (numbered from the second on).
|
||||
fn name_of(untitled: u32, path: Option<&Path>) -> String {
|
||||
match path.and_then(Path::file_name) {
|
||||
/// 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) {
|
||||
Some(n) => n.to_string_lossy().into_owned(),
|
||||
None if untitled <= 1 => tr!("Untitled").to_string(),
|
||||
None => tr!("Untitled {n}", n = untitled),
|
||||
@@ -214,7 +218,8 @@ 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)?;
|
||||
Some(TabInfo { id: slot.id, name: name_of(slot.untitled, s.path.as_deref()), dirty: s.is_dirty(), path: s.path.clone(), active: i == app.tabs.active })
|
||||
let imported = if s.path.is_none() { s.imported.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 })
|
||||
}
|
||||
|
||||
/// Every tab, in the bar's order.
|
||||
@@ -227,6 +232,17 @@ pub fn active_name(app: &App) -> String {
|
||||
info(app, app.tabs.active).map_or_else(|| tr!("Untitled").to_string(), |t| t.name)
|
||||
}
|
||||
|
||||
/// The window's title for the shown document: its name, marked when it is an unsaved import
|
||||
/// (ADR 0009), and a dot while it has unsaved changes.
|
||||
pub fn window_title(app: &App) -> String {
|
||||
let name = match info(app, app.tabs.active) {
|
||||
Some(t) if t.imported.is_some() => tr!("{name} (imported)", name = t.name),
|
||||
Some(t) => t.name,
|
||||
None => tr!("Untitled").to_string(),
|
||||
};
|
||||
format!("AniCraft — {name}{}", if app.s.is_dirty() { " •" } else { "" }) // i18n-exempt: the product name and the document's
|
||||
}
|
||||
|
||||
/// Whether any open document has unsaved changes.
|
||||
pub fn any_dirty(app: &App) -> bool {
|
||||
(0..app.tabs.len()).any(|i| session(app, i).is_some_and(Session::is_dirty))
|
||||
@@ -395,7 +411,8 @@ fn already_open(app: &App, id: &str, params: &Value) -> Option<usize> {
|
||||
},
|
||||
_ => return None,
|
||||
};
|
||||
(0..app.tabs.len()).find(|i| session(app, *i).and_then(|s| s.path.as_deref()).is_some_and(|p| same_file(p, &path)))
|
||||
// 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)))
|
||||
}
|
||||
|
||||
/// The shown document is the one the app started with, untouched: what is opened takes its
|
||||
@@ -436,7 +453,7 @@ pub fn open(app: &mut App, id: &str, params: Value) -> Option<Value> {
|
||||
let id_new = app.tabs.next_id;
|
||||
app.tabs.next_id += 1;
|
||||
// The lowest number no open document without a file has.
|
||||
let taken: Vec<u32> = (0..app.tabs.len()).filter(|i| session(app, *i).is_some_and(|s| s.path.is_none())).filter_map(|i| app.tabs.slots.get(i).map(|s| s.untitled)).collect();
|
||||
let taken: Vec<u32> = (0..app.tabs.len()).filter(|i| session(app, *i).is_some_and(|s| s.path.is_none() && s.imported.is_none())).filter_map(|i| app.tabs.slots.get(i).map(|s| s.untitled)).collect();
|
||||
let untitled = (1..=MAX_TABS as u32 + 1).find(|n| !taken.contains(n)).unwrap_or(1);
|
||||
let parked = Parked { s: Session::new(Document::default()), view: DocView::fresh(app), saver: app.recovery.for_new_document() };
|
||||
app.tabs.slots.push(Slot { id: id_new, untitled, placeholder: false, parked: Some(Box::new(parked)) });
|
||||
@@ -674,9 +691,23 @@ fn elided(p: &egui::Painter, name: &str, width: f32) -> std::sync::Arc<egui::Gal
|
||||
|
||||
/// What a screen reader says for a tab.
|
||||
fn spoken(t: &TabInfo) -> String {
|
||||
if t.dirty { tr!("{name}, unsaved changes", name = t.name) } else { t.name.clone() }
|
||||
match (t.imported.is_some(), t.dirty) {
|
||||
(true, true) => tr!("{name}, imported, unsaved changes", name = t.name),
|
||||
(true, false) => tr!("{name}, imported", name = t.name),
|
||||
(false, true) => tr!("{name}, unsaved changes", name = t.name),
|
||||
(false, false) => t.name.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The dim "Imported" after an unsaved import's name (ADR 0009). It never shortens: the name
|
||||
/// gives way first.
|
||||
fn imported_tag(p: &egui::Painter) -> std::sync::Arc<egui::Galley> {
|
||||
p.layout_no_wrap(tr!("Imported").to_string(), font(Type::Caption), T.text_dim)
|
||||
}
|
||||
|
||||
/// The gap between a name and its "Imported".
|
||||
const TAG_GAP: f32 = 6.0;
|
||||
|
||||
/// The document tabs, above the edit bar: one per open document, with a dot while it has
|
||||
/// unsaved changes and a button to close it. Click shows a document, middle click closes it,
|
||||
/// a drag moves the tab, right-click has Close, Close Others, Close All and where the file is.
|
||||
@@ -685,7 +716,11 @@ pub fn bar(app: &mut App, ui: &mut egui::Ui) {
|
||||
let full = ui.max_rect();
|
||||
let mut acts: Vec<Act> = Vec::new();
|
||||
// How wide each tab is: its name, within limits; all a little narrower when they don't fit.
|
||||
let natural: Vec<f32> = tabs.iter().map(|t| (elided(ui.painter(), &t.name, TAB_MAX).rect.width() + 2.0 * PAD + CLOSE_W + DOT_W).clamp(TAB_MIN, TAB_MAX)).collect();
|
||||
let tag_w = imported_tag(ui.painter()).rect.width() + TAG_GAP;
|
||||
let natural: Vec<f32> = tabs
|
||||
.iter()
|
||||
.map(|t| (elided(ui.painter(), &t.name, TAB_MAX).rect.width() + if t.imported.is_some() { tag_w } else { 0.0 } + 2.0 * PAD + CLOSE_W + DOT_W).clamp(TAB_MIN, TAB_MAX))
|
||||
.collect();
|
||||
let wanted: f32 = natural.iter().sum();
|
||||
let shrink = if wanted > full.width() && wanted > 0.0 { (full.width() / wanted).max(TAB_MIN / TAB_MAX) } else { 1.0 };
|
||||
let widths: Vec<f32> = natural.iter().map(|w| (w * shrink).max(TAB_MIN.min(*w))).collect();
|
||||
@@ -746,9 +781,15 @@ pub fn bar(app: &mut App, ui: &mut egui::Ui) {
|
||||
p.circle_filled(pos2(left + 3.5, r.center().y), 3.5, if t.active { T.accent } else { T.text_dim });
|
||||
left += DOT_W;
|
||||
}
|
||||
let galley = elided(p, &t.name, close_r.left() - 4.0 - left);
|
||||
let tag = t.imported.is_some().then(|| imported_tag(p));
|
||||
let room = close_r.left() - 4.0 - left - tag.as_ref().map_or(0.0, |g| g.rect.width() + TAG_GAP);
|
||||
let galley = elided(p, &t.name, room);
|
||||
let cut = galley.elided;
|
||||
let after = left + galley.rect.width() + TAG_GAP;
|
||||
p.galley_with_override_text_color(pos2(left, r.center().y - galley.rect.height() * 0.5), galley, if t.active { T.text } else { T.text_dim });
|
||||
if let Some(g) = tag {
|
||||
p.galley(pos2(after, r.center().y - g.rect.height() * 0.5), g, T.text_dim);
|
||||
}
|
||||
// The close button: an ×, brighter under the pointer.
|
||||
if close.hovered() {
|
||||
p.rect_filled(close_r, T.rc(), T.hover);
|
||||
@@ -770,9 +811,10 @@ pub fn bar(app: &mut App, ui: &mut egui::Ui) {
|
||||
app.tabs.drag = Some(t.id);
|
||||
acts.push(Act::Show(t.id));
|
||||
}
|
||||
let place = match &t.path {
|
||||
Some(path) => path.display().to_string(),
|
||||
None => tr!("Not saved yet").to_string(),
|
||||
let place = match (&t.path, &t.imported) {
|
||||
(Some(path), _) => path.display().to_string(),
|
||||
(None, Some(from)) => tr!("Imported from {path}. Saving makes a new AniCraft document; the original is never changed.", path = from.display().to_string()),
|
||||
(None, None) => tr!("Not saved yet").to_string(),
|
||||
};
|
||||
let tip = if cut { format!("{}\n{place}", t.name) } else { place };
|
||||
close.on_hover_text(tr!("Close"));
|
||||
|
||||
@@ -84,6 +84,42 @@ fn the_first_document_opened_takes_the_samples_place_and_the_next_gets_a_tab() {
|
||||
assert_eq!(app.tabs.len(), 2);
|
||||
}
|
||||
|
||||
/// ADR 0009: an imported Animate file's tab and the window are named after it and marked as
|
||||
/// imported, while it stays untitled: Save As offers an `.anicraft` beside the original, opening
|
||||
/// the same file again shows its tab, and once saved it is the `.anicraft` document.
|
||||
#[test]
|
||||
fn an_import_is_named_after_its_animate_file_until_it_is_saved() {
|
||||
let (ctx, mut app) = app();
|
||||
let corpus = Path::new(env!("CARGO_MANIFEST_DIR")).join("crates/xfl/tests/corpus");
|
||||
let folder = corpus.join("primitives");
|
||||
app.request("file.open", json!({"path": folder.display().to_string()}));
|
||||
settle(&ctx, &mut app);
|
||||
assert!(app.s.path.is_none(), "still untitled: Save never writes over the Animate file");
|
||||
assert_eq!(names(&app), ["primitives"]);
|
||||
let t = &infos(&app)[0];
|
||||
assert_eq!(t.imported.as_deref(), Some(folder.as_path()));
|
||||
assert_eq!(spoken(t), "primitives, imported");
|
||||
assert_eq!(window_title(&app), "AniCraft — primitives (imported)");
|
||||
assert_eq!(app.save_suggestion(), corpus.join("primitives.anicraft").display().to_string(), "beside the XFL folder");
|
||||
// A new document is "Untitled": the import has a name of its own.
|
||||
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.
|
||||
app.request("file.open", json!({"path": folder.display().to_string()}));
|
||||
assert_eq!((app.tabs.len(), app.tabs.active()), (2, 0));
|
||||
// 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!(infos(&app)[0].imported.is_none());
|
||||
assert_eq!(window_title(&app), "AniCraft — primitives.anicraft");
|
||||
assert_eq!(app.save_suggestion(), saved.display().to_string());
|
||||
std::fs::remove_dir_all(&dir).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn switching_keeps_each_documents_undo_selection_playhead_and_view() {
|
||||
let (ctx, mut app) = app();
|
||||
|
||||
Reference in new issue
Block a user