diff --git a/crates/ui-egui/src/file_dialog.rs b/crates/ui-egui/src/file_dialog.rs index cfec9c0f05..aabff48324 100644 --- a/crates/ui-egui/src/file_dialog.rs +++ b/crates/ui-egui/src/file_dialog.rs @@ -278,13 +278,15 @@ impl PhotocraftApp { }) } - /// File › Open: opens every chosen file, reporting each failure (see [`Self::open_paths`]). + /// File › Open: opens every chosen file in the order the dialog returns them, reporting each + /// failure (see [`Self::open_paths_in_order`]). pub fn open_dialog_file(&mut self) -> Result { let initial_dir = last_used_dir(&self.ui.recent_files); self.ask_file(FileDialogRequest::Open { multiple: true, initial_dir, extensions: None }, |app, answer| { match answer { FileDialogAnswer::Paths(paths) => { - app.open_paths(&paths); + // The order the dialog returns, not name order; the last is active. + app.open_paths_in_order(&paths); } FileDialogAnswer::Contents(name, bytes) => app.open_contents(vec![(name, bytes)]), FileDialogAnswer::ContentsMany(files) => app.open_contents(files), diff --git a/crates/ui-egui/src/file_open.rs b/crates/ui-egui/src/file_open.rs index a4f89c416b..488bfa3a91 100644 --- a/crates/ui-egui/src/file_open.rs +++ b/crates/ui-egui/src/file_open.rs @@ -303,11 +303,18 @@ impl PhotocraftApp { /// open in name order ([`natural_cmp`]: `File_2` before `File_10`), and their tabs keep that /// order however long each takes to open in the background (#3248). Returns how many opened. pub fn open_paths(&mut self, paths: &[String]) -> usize { + let mut paths: Vec = paths.iter().map(|p| absolute(p)).collect(); + paths.sort_by(|a, b| natural_cmp(a, b)); + self.open_paths_in_order(&paths) + } + + /// [`Self::open_paths`] without the name sort: the files open in the order given and the last + /// one is the active document. File › Open uses it, so files picked together open as picked. + pub fn open_paths_in_order(&mut self, paths: &[String]) -> usize { // The command line's files open before the first frame, which otherwise applies the // interface language: their status lines and notices would come out in English. crate::i18n::set_current(crate::i18n::Lang::from_pref(&self.session.prefs().interface.language)); - let mut paths: Vec = paths.iter().map(|p| absolute(p)).collect(); - paths.sort_by(|a, b| natural_cmp(a, b)); + let paths: Vec = paths.iter().map(|p| absolute(p)).collect(); let batch = crate::jobs_ui::begin_batch(self); let mut opened = 0; for (seq, abs) in paths.iter().enumerate() { @@ -700,5 +707,7 @@ fn same_file(p: &str, path: &str, canon: Option<&std::path::Path>) -> bool { p == path || canon.is_some_and(|c| std::path::Path::new(p) == c || canonical(p).is_some_and(|q| q == c)) } +#[cfg(test)] +mod dialog_order_tests; #[cfg(test)] mod tests; diff --git a/crates/ui-egui/src/file_open/dialog_order_tests.rs b/crates/ui-egui/src/file_open/dialog_order_tests.rs new file mode 100644 index 0000000000..e4462b2052 --- /dev/null +++ b/crates/ui-egui/src/file_open/dialog_order_tests.rs @@ -0,0 +1,83 @@ +//! #14056: files picked together in File > Open open in the order picked, the last one active; +//! the command line and drops keep name order. + +use crate::file_dialog::{self, FileDialogAnswer}; +use crate::{PhotocraftApp, Services}; +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::Document; +use photocraft_geom::Size; + +fn app_with(answers: Vec>) -> PhotocraftApp { + let services = Services { + import: Some(Box::new(|name: &str, _bytes: &[u8], _opts: &photocraft_io::ImportOptions| { + Ok((Document::new(name, Size::new(4, 4), ColorMode::Rgb, SampleType::U8), Vec::new())) + })), + file_dialog: Some(file_dialog::fake(answers).0), + ..Default::default() + }; + PhotocraftApp::new(photocraft_engine::Session::new(), services) +} + +fn files(tag: &str, names: &[&str]) -> (std::path::PathBuf, Vec) { + let dir = std::env::temp_dir().join(format!("photocraft-dialog-order-{tag}-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let paths = names + .iter() + .map(|name| { + let path = dir.join(name); + std::fs::write(&path, b"x").unwrap(); + path.to_string_lossy().into_owned() + }) + .collect(); + (dir, paths) +} + +fn doc_names(app: &PhotocraftApp) -> Vec { + app.session.documents().iter().map(|d| d.doc.name.clone()).collect() +} + +fn active_name(app: &PhotocraftApp) -> Option { + app.session.active().map(|s| s.doc.name.clone()) +} + +fn pick(app: &mut PhotocraftApp) { + app.open_dialog_file().unwrap(); + app.poll_file_dialog(&egui::Context::default(), None); +} + +#[test] +fn dialog_picks_open_in_the_order_picked_and_the_last_is_active() { + let (dir, paths) = files("order", &["flat.png", "layers.psd", "flat.jpg"]); + let mut app = app_with(vec![Some(FileDialogAnswer::Paths(paths))]); + pick(&mut app); + assert_eq!(doc_names(&app), ["flat.png", "layers.psd", "flat.jpg"]); + assert_eq!(active_name(&app).as_deref(), Some("flat.jpg")); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn a_picked_file_that_is_already_open_is_activated_not_opened_twice() { + let (dir, paths) = files("already", &["flat.png", "layers.psd", "flat.jpg"]); + let mut app = app_with(vec![Some(FileDialogAnswer::Paths(vec![paths[1].clone(), paths[0].clone()]))]); + assert_eq!(app.open_paths_in_order(&[paths[2].clone()]), 1); + pick(&mut app); + assert_eq!(doc_names(&app), ["flat.jpg", "layers.psd", "flat.png"], "each file once"); + assert_eq!(active_name(&app).as_deref(), Some("flat.png")); + // The last pick being the already open file activates it. + let mut app = app_with(vec![Some(FileDialogAnswer::Paths(vec![paths[0].clone(), paths[2].clone()]))]); + assert_eq!(app.open_paths_in_order(&[paths[2].clone()]), 1); + pick(&mut app); + assert_eq!(doc_names(&app), ["flat.jpg", "flat.png"]); + assert_eq!(active_name(&app).as_deref(), Some("flat.jpg")); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn the_command_line_and_drop_path_still_opens_in_name_order() { + let (dir, paths) = files("sorted", &["flat.png", "layers.psd", "flat.jpg"]); + let mut app = app_with(Vec::new()); + assert_eq!(app.open_paths(&paths), 3); + assert_eq!(doc_names(&app), ["flat.jpg", "flat.png", "layers.psd"]); + assert_eq!(active_name(&app).as_deref(), Some("layers.psd")); + let _ = std::fs::remove_dir_all(&dir); +}