Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LR-BEHAV-DIALOGS: a Rename Folder prompt confirmed unchanged keeps a name with spaces at its ends, and ui.dialog.confirm shows why a prompt was refused

Nathaniel Ledfordd59726393f22
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index f53ee0f31..edf583b5f 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -414,6 +414,10 @@ pub fn handle(app: &mut LightcraftApp, ctx: &egui::Context, req: &ControlRequest
"ui.dialog.confirm" => match app.ui.dialog.take() {
Some(d) => {
let r = crate::panels::dialogs::confirm_dialog(app, &d);
+ // a refused one-line prompt says why on screen, as with its button
+ if let (Err(e), crate::state::Dialog::TextPrompt { .. }) = (&r, &d) {
+ app.toast_error(ctx, e.clone());
+ }
// the import review, the rule editor and the one-line prompts stay open on an error, as with their buttons
if (r.is_err()
&& matches!(
diff --git a/crates/ui-egui/src/dialogs/confirm.rs b/crates/ui-egui/src/dialogs/confirm.rs
index 8891c1b55..fdb90055d 100644
--- a/crates/ui-egui/src/dialogs/confirm.rs
+++ b/crates/ui-egui/src/dialogs/confirm.rs
@@ -56,10 +56,13 @@ pub fn confirm_dialog(app: &mut LightcraftApp, dlg: &Dialog) -> Result<serde_jso
Dialog::RenameAlbum { id, name } => app.run("album.rename", json!({"id": id, "name": name})),
Dialog::TextPrompt { value, command, params, key, .. } => {
// Rename Folder opens with the folder's own name typed: confirming it unchanged renames nothing, as it always has
- // (the engine would refuse it as a name that "already exists")
+ // (the engine would refuse it as a name that "already exists"). A name with spaces at its ends is the same
+ // folder when typed back as it is, so it is compared as typed as well as trimmed.
let kept = command == "folder.rename"
&& params["path"].as_str().is_some_and(|path| {
- std::path::Path::new(path.trim_end_matches(['/', '\\'])).file_name().is_some_and(|f| f.to_string_lossy() == value.trim())
+ std::path::Path::new(path.trim_end_matches(['/', '\\']))
+ .file_name()
+ .is_some_and(|f| f.to_string_lossy() == value.as_str() || f.to_string_lossy() == value.trim())
});
if kept {
return Ok(serde_json::Value::Null);
diff --git a/crates/ui-egui/src/tests/text_prompt.rs b/crates/ui-egui/src/tests/text_prompt.rs
index c089b2430..87328dedc 100644
--- a/crates/ui-egui/src/tests/text_prompt.rs
+++ b/crates/ui-egui/src/tests/text_prompt.rs
@@ -108,6 +108,20 @@ fn the_control_channel_keeps_a_refused_prompt_open_too() {
Some(Dialog::TextPrompt { value, .. }) => assert_eq!(value, "Taken", "the typed text is kept"),
other => panic!("the prompt stays open: {other:?}"),
}
+ assert!(toast(&h).contains("already exists"), "the reason is on screen too, as with the button: {:?}", toast(&h));
+ clean(&dir);
+}
+
+#[test]
+fn a_rename_that_keeps_a_name_with_spaces_at_its_ends_changes_nothing() {
+ let dir = shoot("rename-padded");
+ std::fs::create_dir_all(dir.join(" Pad ")).unwrap();
+ let mut h = app();
+ // Rename Folder opens with the folder's own name typed, spaces and all
+ confirm(&mut h, prompt("folder.rename", " Pad ", json!({"path": dir.join(" Pad ").to_string_lossy()}), "name"));
+ assert_eq!(h.app.ui.dialog, None, "the prompt closes");
+ assert!(toast(&h).is_empty(), "no error is shown: {:?}", toast(&h));
+ assert!(dir.join(" Pad ").is_dir() && !dir.join("Pad").exists(), "the folder keeps its name");
clean(&dir);
}