LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Merge upstream/main into upstream/edit-in-errors
Andrew Frahna8814731c7b1
diff --cc apps/lightcraft/src/editors.rsindex c34b3e258,cf6d66079..c8fe298b3--- a/apps/lightcraft/src/editors.rs+++ b/apps/lightcraft/src/editors.rs@@@ -53,22 -61,39 +53,55 @@@ pub fn open_with(path: &str, app: &strc.spawn().map(|_| ()).map_err(|e| e.to_string())}+/// macOS: run `open` and wait for it (it returns as soon as LaunchServices has handed the file+/// over), so an application it can't find or start is reported instead of silently doing nothing.+fn run_open(args: &[&str]) -> Result<(), String> {+ let out = Command::new("open").args(args).output().map_err(|e| e.to_string())?;+ open_result(out.status.success(), &String::from_utf8_lossy(&out.stderr))+}++/// What `open` said, as a result: its own message when it failed.+fn open_result(ok: bool, stderr: &str) -> Result<(), String> {+ if ok {+ return Ok(());+ }+ let msg = stderr.lines().map(str::trim).find(|l| !l.is_empty()).unwrap_or("the application couldn't be started");+ Err(msg.to_string())+}++ /// Open every file in `paths` with one start of `app`, as a file manager hands over a selection:+ /// an editor that keeps a single window (DxO PhotoLab) loses the files that arrive while it is+ /// still starting when it is started once per file. The system default opens them one by one+ /// (Explorer and `xdg-open` take one file).+ pub fn open_all_with(paths: &[String], app: &str) -> Result<(), String> {+ match all_command(launcher(app)?, paths) {+ Some(mut c) => c.spawn().map(|_| ()).map_err(|e| e.to_string()),+ None => paths.iter().try_for_each(|p| open_with(p, app)),+ }+ }++ /// The one command that opens all of `paths` in `launcher` (none for the system default).+ fn all_command(launcher: Launcher, paths: &[String]) -> Option<Command> {+ match launcher {+ Launcher::Default => None,+ Launcher::MacApp(app) => {+ let mut c = Command::new("open");+ c.args(["-a", &app]).args(paths);+ Some(c)+ }+ Launcher::Program(p) => {+ let mut c = Command::new(p);+ c.args(paths);+ Some(c)+ }+ Launcher::Flatpak => {+ let mut c = Command::new("flatpak");+ c.args(["run", "--file-forwarding", PHOTOCRAFT_ID, "@@"]).args(paths).arg("@@");+ Some(c)+ }+ }+ }+/// Windows: open `path` in its default application through Explorer, given the path as its one/// argument. Not `cmd /C start`: cmd.exe would read `&`, `^`, `%` or `|` in a file name as its own/// syntax (running what follows an `&` as a command).@@@ -232,11 -226,39 +265,30 @@@ mod testsassert_eq!(args, [r"C:\Edits\a & b ^c %PATH% | d.psd"]);}+ /// Open Original with several photos: one start of the editor with every file, which a+ /// single-window editor (DxO PhotoLab) needs to get them all.+ #[test]+ fn several_files_go_to_one_start_of_the_editor() {+ let paths = [r"D:\Pictures\a.dng".to_string(), r"D:\Pictures\b c.dng".to_string()];+ let args = |c: &Command| c.get_args().map(|a| a.to_string_lossy().into_owned()).collect::<Vec<_>>();+ let c = all_command(Launcher::Program(PathBuf::from(r"C:\DxO\DxO.PhotoLab.exe")), &paths).unwrap();+ assert_eq!(c.get_program(), r"C:\DxO\DxO.PhotoLab.exe");+ assert_eq!(args(&c), paths);+ let c = all_command(Launcher::MacApp("DxO PhotoLab 8".into()), &paths).unwrap();+ assert_eq!(+ (c.get_program().to_string_lossy().into_owned(), args(&c)),+ ("open".into(), vec!["-a".into(), "DxO PhotoLab 8".into(), paths[0].clone(), paths[1].clone()])+ );+ let c = all_command(Launcher::Flatpak, &paths).unwrap();+ assert_eq!(args(&c), ["run", "--file-forwarding", PHOTOCRAFT_ID, "@@", &paths[0], &paths[1], "@@"]);+ assert!(all_command(Launcher::Default, &paths).is_none(), "the system default opens them one by one");+ }+#[test]- fn windows_lookup_prefers_the_recorded_install_folder() {- let env = |k: &str| match k {- "ProgramFiles" => Some(r"C:\Program Files".to_string()),- "LOCALAPPDATA" => Some(r"C:\Users\Simon\AppData\Local".to_string()),- _ => None,- };- let c = windows_candidates(Some(r"D:\Apps\PhotoCraft\".into()), Some(r#""C:\Program Files\PhotoCraft\photocraft.exe""#.into()), env);- let c: Vec<String> = c.iter().map(|p| p.to_string_lossy().replace('/', "\\")).collect();- assert!(c[0].starts_with(r"D:\Apps\PhotoCraft") && c[0].ends_with("photocraft.exe"), "{c:?}");- assert_eq!(c[1], r"C:\Program Files\PhotoCraft\photocraft.exe", "App Paths, quotes dropped");- assert!(c[2].starts_with(r"C:\Program Files") && c[2].ends_with("photocraft.exe"), "{c:?}");- assert!(c[3].contains("Programs") && c[3].ends_with("photocraft.exe"), "{c:?}");- assert_eq!(c.len(), 4);+ fn a_failed_open_is_an_error() {+ assert!(open_result(true, "").is_ok());+ assert!(open_result(false, "Unable to find application\n").is_err());+ assert!(open_result(false, "").is_err(), "even when it says nothing");}#[test]