diff --git a/crates/ui-egui/src/app/file_dialogs.rs b/crates/ui-egui/src/app/file_dialogs.rs index e67320c83..9c1e9074f 100644 --- a/crates/ui-egui/src/app/file_dialogs.rs +++ b/crates/ui-egui/src/app/file_dialogs.rs @@ -84,6 +84,10 @@ pub type Dialogs = craft_dialogs::Dialogs; #[cfg(not(target_arch = "wasm32"))] const DIALOG_UNAVAILABLE: &str = "The file dialog couldn't be shown"; +/// A Save panel answered with a path that isn't Unicode text. +#[cfg(not(target_arch = "wasm32"))] +const SAVE_PATH_NOT_UNICODE: &str = "Not saved: the name of the file or of a folder it is in isn’t Unicode text"; + /// The browser build has no dialog for this purpose. #[cfg(target_arch = "wasm32")] const NO_PICKER: &str = "needs a file picker"; @@ -297,8 +301,20 @@ impl DesignApp { self.continue_in(Held { doc, dialog, then, path: first }); } } - (Step::Save { ext, then }, Answer::Paths(paths)) => { - let Some(picked) = paths.into_iter().next() else { continue }; + // A Save panel answers `Saved`; `Paths` is what a dialog answered by an override may still give. + (Step::Save { ext, then }, answer @ (Answer::Saved { .. } | Answer::Paths(_))) => { + let picked = match answer { + Answer::Saved { path, .. } => Some(path), + Answer::Paths(paths) => paths.into_iter().next(), + _ => None, + }; + let Some(picked) = picked else { continue }; + // The engine takes its paths as text. A path that isn't Unicode would be changed on the way, to a + // file the dialog never asked about: nothing is written. + if picked.to_str().is_none() { + self.status(crate::i18n::tr(&self.ui.language, SAVE_PATH_NOT_UNICODE)); + continue; + } let path = picked_save_path(picked.clone(), ext.as_deref()); if path != picked && path.exists() { self.ask_replace(key, dialog, path, then); @@ -505,7 +521,7 @@ fn save_dialog_start(suggested: &str) -> (Option, String) { #[cfg(test)] pub(crate) mod testing { - use craft_dialogs::{Answer, MessageResult, Request}; + use craft_dialogs::{Answer, Kind, MessageResult, Request}; use super::DesignApp; @@ -524,10 +540,12 @@ pub(crate) mod testing { } } - /// Every file dialog answers `path` (`None`: Cancel); every message box answers Yes. + /// Every file dialog answers `path` (`None`: Cancel) as its panel does, a Save with `Saved`; every message box + /// answers Yes. pub(crate) fn answer_with(path: Option) -> impl FnMut(&Request) -> Answer + 'static { move |request| match (request, &path) { (Request::Message(_), _) => Answer::Message(MessageResult::Yes), + (Request::File(dialog), Some(path)) if dialog.kind == Kind::Save => Answer::Saved { path: path.into(), filter: None }, (Request::File(_), Some(path)) => Answer::Paths(vec![path.into()]), (Request::File(_), None) => Answer::Cancelled, } @@ -650,6 +668,43 @@ mod tests { assert!(!app.file_dialogs_pending()); } + /// File › Save As without a path writes the document where the Save panel says, on the frame its answer arrives. + #[test] + fn save_as_without_a_path_saves_where_the_save_panel_answers() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("Saved.designcraft"); + let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default()); + app.answer_dialogs(answer_with(Some(path.to_string_lossy().to_string()))); + app.run("file.new", serde_json::json!({})).unwrap(); + app.run("app.saveDialog", serde_json::json!({})).unwrap(); + assert!(!path.exists(), "nothing is written before the panel has answered"); + app.settle_dialogs(); + assert!(path.exists(), "the document is written to the chosen file"); + assert_eq!(app.session.active().and_then(|d| d.path.clone()).map(PathBuf::from), Some(path)); + } + + /// A Save panel's path that isn't Unicode text can't reach the engine unchanged: nothing is written, the + /// document keeps no file, and the status line says why. + #[cfg(unix)] + #[test] + fn a_save_panel_path_that_is_not_unicode_writes_nothing() { + use std::os::unix::ffi::OsStrExt; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join(std::ffi::OsStr::from_bytes(b"Sav\xffed.designcraft")); + let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default()); + app.answer_dialogs(move |request| match request { + craft_dialogs::Request::File(_) => craft_dialogs::Answer::Saved { path: path.clone(), filter: None }, + craft_dialogs::Request::Message(_) => craft_dialogs::Answer::Message(craft_dialogs::MessageResult::Yes), + }); + app.run("file.new", serde_json::json!({})).unwrap(); + app.run("app.saveDialog", serde_json::json!({})).unwrap(); + app.settle_dialogs(); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 0, "no file under another name"); + assert_eq!(app.session.active().and_then(|d| d.path.clone()), None); + assert!(!app.ui.status.is_empty(), "the status line says the save did not happen"); + assert!(!app.file_dialogs_pending()); + } + /// File › Open without a path asks and returns at once; the document opens on a later frame, /// when the dialog's answer arrives. The frame never waits for the dialog. #[test]