From 21a2665c92a4bd30a637b673bd13a8c841c59bad Mon Sep 17 00:00:00 2001 From: DuBearte <130190089+DuBearte@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:35:13 -0500 Subject: [PATCH] fix(ui): Delete with a note selected asks to delete the note, not the layer (#11788) With the Note tool and a note selected, Delete and Backspace went the usual way and deleted the active layer, leaving the note. They now ask "Delete this note?" (Yes / No, "Don't show again"); Yes deletes the note as one "Delete Note" step and selects nothing, No changes nothing. The Notes panel's trash asks the same, and "Don't show again" skips the question for both. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/dialogs.rs | 21 +++- crates/ui-egui/src/input/shortcuts.rs | 4 + crates/ui-egui/src/tools/analysis_ui.rs | 101 ++++++++++++++++- .../src/tools/note_delete_key_tests.rs | 107 ++++++++++++++++++ 4 files changed, 227 insertions(+), 6 deletions(-) create mode 100644 crates/ui-egui/src/tools/note_delete_key_tests.rs diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 0cfeb7fdc1..157c38281d 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -277,6 +277,19 @@ pub(crate) fn close_box(ui: &mut egui::Ui, rect: egui::Rect) -> bool { /// it. Returns `Some(true)` / `Some(false)` when the body itself answered OK / Cancel. /// The body of a command dialog: the first handler whose marker the fields carry. The prompts and /// small editors come first; the rest are in `command_body_more`. +/// The delete confirmations: a layer from the Layers panel's trash, or a note (#11788). +fn delete_prompt(fields: &Fields) -> bool { + crate::delete_layer_prompt::owns(fields) || crate::analysis_ui::owns_delete_note(fields) +} + +fn delete_prompt_body(ui: &mut egui::Ui, fields: &mut Fields) { + if crate::analysis_ui::owns_delete_note(fields) { + crate::analysis_ui::delete_note_body(ui, fields); + } else { + crate::delete_layer_prompt::body(ui, fields); + } +} + fn command_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, d: &Dialog, fields: &mut Fields, id: egui::Id, interactive: bool) -> Option { let mut outcome: Option = None; match d.kind { @@ -292,7 +305,7 @@ fn command_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, d: &Dialog, fields: _ if crate::channel_options_ui::owns(fields) => crate::channel_options_ui::body(ui, fields), _ if crate::channel_options_ui::owns_new_spot(fields) => crate::channel_options_ui::new_spot_body(ui, fields), _ if crate::shape_dialog::owns(fields) => crate::shape_dialog::body(app, ui, fields), - _ if crate::delete_layer_prompt::owns(fields) => crate::delete_layer_prompt::body(ui, fields), + _ if delete_prompt(fields) => delete_prompt_body(ui, fields), _ if crate::disk_change_prompt::owns(fields) => crate::disk_change_prompt::body(ui, fields), _ if crate::replace_prompt::owns(fields) => crate::replace_prompt::body(ui, fields), _ if crate::preset_group_prompt::owns(fields) => crate::preset_group_prompt::body(ui, fields), @@ -464,6 +477,8 @@ fn button_row( tl!("Reset") } else if d.kind == DialogKind::NewDocument { tl!("Close") + } else if crate::analysis_ui::owns_delete_note(fields) { + tl!("No") } else { tl!("Cancel") }; @@ -533,6 +548,8 @@ fn ok_label(d: &Dialog) -> &'static str { tl!("Continue") } else if crate::delete_layer_prompt::owns(&d.fields) { tl!("Delete") + } else if crate::analysis_ui::owns_delete_note(&d.fields) { + tl!("Yes") } else if crate::disk_change_prompt::owns(&d.fields) || crate::replace_prompt::owns(&d.fields) { tl!("Replace") } else if d.fields.contains_key("__export") { @@ -864,6 +881,7 @@ const ROUTES: &[fn(&Fields) -> bool] = &[ merge_channels_ui::owns, crate::shape_dialog::owns, crate::delete_layer_prompt::owns, + crate::analysis_ui::owns_delete_note, crate::disk_change_prompt::owns, crate::actions::owns_stop_message, crate::actions::owns_insert_stop, @@ -1059,6 +1077,7 @@ fn confirm_command(app: &mut PhotocraftApp, d: Dialog) -> Result fn confirm_command_more(app: &mut PhotocraftApp, d: Dialog) -> Result { match d.kind { _ if merge_channels_ui::owns(&d.fields) => merge_channels_ui::confirm(app, &d.fields), + _ if crate::analysis_ui::owns_delete_note(&d.fields) => crate::analysis_ui::confirm_delete_note(app, &d.fields), _ if crate::file_ui::owns(&d.fields) => crate::file_ui::confirm(app, &d.fields), _ if crate::new_guide_ui::owns(&d.fields) => crate::new_guide_ui::confirm(app, &d.fields), _ if crate::color_picker_ui::owns(&d.fields) => { diff --git a/crates/ui-egui/src/input/shortcuts.rs b/crates/ui-egui/src/input/shortcuts.rs index 0abd096530..ab1c6a1614 100644 --- a/crates/ui-egui/src/input/shortcuts.rs +++ b/crates/ui-egui/src/input/shortcuts.rs @@ -745,6 +745,10 @@ fn handle_object_tool_keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool if crate::slice_ui::keys(app, ctx) { return true; } + // A note selected with the Note tool: ⌫/Delete asks to delete the note, not the layer (#11788). + if crate::analysis_ui::note_keys(app, ctx) { + return true; + } // A selected smart filter row: ⌫/Delete removes the filter, not its smart object (#4751). crate::smart_ui::keys(app, ctx) } diff --git a/crates/ui-egui/src/tools/analysis_ui.rs b/crates/ui-egui/src/tools/analysis_ui.rs index 6cad2b2158..2175ea708d 100644 --- a/crates/ui-egui/src/tools/analysis_ui.rs +++ b/crates/ui-egui/src/tools/analysis_ui.rs @@ -821,11 +821,8 @@ fn notes_panel(app: &mut PhotocraftApp, ctx: &egui::Context) { if let Some(p) = edit { let _ = app.run("notes.set", p); } - if delete - && let Some(i) = sel - && app.run("notes.delete", json!({"index": i})).is_ok() - { - app.ui.analysis.note_selected = (n > 1).then(|| i.min(n - 2)); + if delete && let Some(i) = sel { + ask_delete_note(app, i); } if close { app.ui.analysis.notes = false; @@ -988,3 +985,97 @@ fn dialog(app: &mut PhotocraftApp, ctx: &egui::Context) { #[cfg(test)] pub(crate) mod tests; + +/// Marks the "Delete this note?" alert (#11788). +const DELETE_NOTE_MARK: &str = "__deleteNote"; +/// Its "Don't show again", kept with the other dialog preferences. +const DELETE_NOTE_PREF: &str = "notes.delete.confirmation"; + +/// Delete note `index` of the active document, as the Note tool's ⌫/Delete and the Notes panel's +/// trash do: after the alert "Delete this note?" ([Yes] [No], "Don't show again"), or at once once +/// that is ticked. Nothing is selected afterwards. +pub fn ask_delete_note(app: &mut PhotocraftApp, index: usize) { + let skip = app.session.prefs().dialogs.get(DELETE_NOTE_PREF).and_then(|v| v.get("skip")).and_then(Value::as_bool) == Some(true); + let Some(doc) = app.session.active().map(|s| s.doc.id.0) else { return }; + if skip { + delete_note(app, index); + return; + } + if app.ui.dialogs.iter().any(|d| owns_delete_note(&d.fields)) { + return; + } + let mut f = Map::new(); + f.insert(DELETE_NOTE_MARK.into(), json!(true)); + f.insert("__command".into(), json!("notes.delete")); + f.insert("__label".into(), json!(tl!("Delete Note"))); + f.insert("document".into(), json!(doc)); + f.insert("index".into(), json!(index)); + f.insert("skip".into(), json!(false)); + app.ui.open_dialog(crate::state::DialogKind::Command, f); +} + +fn delete_note(app: &mut PhotocraftApp, index: usize) { + match app.run("notes.delete", json!({"index": index})) { + Ok(_) => app.ui.analysis.note_selected = None, + Err(e) => { + app.ui.status = e; + app.ui.status_error = true; + } + } +} + +pub fn owns_delete_note(fields: &Map) -> bool { + fields.contains_key(DELETE_NOTE_MARK) +} + +pub fn delete_note_body(ui: &mut egui::Ui, fields: &mut Map) { + ui.label(tl!("Delete this note?")); + ui.add_space(8.0); + let mut skip = fields.get("skip").and_then(Value::as_bool).unwrap_or(false); + ui.checkbox(&mut skip, tl!("Don't show again")); + fields.insert("skip".into(), json!(skip)); +} + +/// Yes: the note goes (one "Delete Note" step), and "Don't show again" is remembered. +pub fn confirm_delete_note(app: &mut PhotocraftApp, fields: &Map) -> Result { + let index = fields.get("index").and_then(Value::as_u64).and_then(|i| usize::try_from(i).ok()).ok_or(tl!("No note to delete"))?; + let doc = app.session.active().map(|s| (s.doc.id.0, s.doc.notes.len())); + if doc.map(|d| d.0) != fields.get("document").and_then(Value::as_u64) || doc.is_none_or(|d| index >= d.1) { + return Err(tl!("The document or its notes changed. Please delete the note again.").into()); + } + let result = app.run("notes.delete", json!({"index": index}))?; + app.ui.analysis.note_selected = None; + if fields.get("skip").and_then(Value::as_bool) == Some(true) { + app.session.edit_prefs(|prefs| { + prefs.dialogs.insert(DELETE_NOTE_PREF.into(), json!({"skip": true})); + }); + if let Err(error) = crate::prefs_ui::save_preferences(app) { + app.ui.status = crate::i18n::fmt(tl!("Note deleted, but the preference could not be saved: {error}"), &[("error", &error)]); + app.ui.status_error = true; + } + } + Ok(result) +} + +/// The Note tool with a note selected: ⌫/Delete asks to delete the note (#11788), never the +/// layer (Delete Layer) or the pixels (Edit › Clear). Text fields keep the keys. +pub fn note_keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool { + use crate::shortcut_dispatch::Focus; + let Some(index) = app.ui.analysis.note_selected else { return false }; + if app.ui.tool != Tool::Note || app.ui.text_edit.is_some() || matches!(Focus::of(ctx), Focus::Text | Focus::DeletingWidget) { + return false; + } + if app.session.active().is_none_or(|s| index >= s.doc.notes.len()) { + return false; + } + let mods = ctx.input(|i| i.modifiers); + if !mods.is_none() || !ctx.input_mut(|i| i.consume_key(mods, egui::Key::Delete) || i.consume_key(mods, egui::Key::Backspace)) { + return false; + } + ask_delete_note(app, index); + true +} + +#[cfg(test)] +#[path = "note_delete_key_tests.rs"] +mod note_delete_key_tests; diff --git a/crates/ui-egui/src/tools/note_delete_key_tests.rs b/crates/ui-egui/src/tools/note_delete_key_tests.rs new file mode 100644 index 0000000000..9a1630e4e7 --- /dev/null +++ b/crates/ui-egui/src/tools/note_delete_key_tests.rs @@ -0,0 +1,107 @@ +//! #11788: with the Note tool, ⌫/Delete on a selected note asks "Delete this note?" and deletes +//! the note, never the layer; the Notes panel's trash asks the same. + +use super::*; + +/// A document with a second layer and one note, the Note tool on and the note selected. +fn app() -> PhotocraftApp { + let mut s = photocraft_engine::Session::new(); + s.execute("file.new", json!({"width": 64, "height": 48})).unwrap(); + s.execute("layer.new.layer", json!({"name": "Ink"})).unwrap(); + s.execute("notes.add", json!({"x": 10, "y": 12, "text": "fix this"})).unwrap(); + let mut app = PhotocraftApp::new(s, crate::Services::default()); + app.ui.tool = Tool::Note; + app.ui.analysis.note_selected = Some(0); + app +} + +/// A key through the app's shortcut handling, as the canvas gets it. +fn press(app: &mut PhotocraftApp, key: egui::Key) { + let ctx = egui::Context::default(); + let m = egui::Modifiers::NONE; + let raw = egui::RawInput { + events: vec![egui::Event::ModifiersChanged(m), egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: m }], + ..Default::default() + }; + ctx.begin_pass(raw); + crate::shortcuts::handle(app, &ctx); + ctx.end_pass().textures_delta.clear(); +} + +fn layers(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().doc.layers.len() +} + +fn notes(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().doc.notes.len() +} + +fn alert(app: &PhotocraftApp) -> Option { + app.ui.dialogs.iter().find(|d| owns_delete_note(&d.fields)).map(|d| d.id) +} + +#[test] +fn delete_with_a_note_selected_asks_and_deletes_the_note_not_the_layer() { + for key in [egui::Key::Delete, egui::Key::Backspace] { + let mut app = app(); + let steps = app.session.active().unwrap().history.past_len(); + press(&mut app, key); + let id = alert(&app).unwrap_or_else(|| panic!("{key:?}: the alert")); + assert_eq!((layers(&app), notes(&app)), (2, 1), "{key:?}: nothing changes before the answer"); + crate::dialogs::confirm(&mut app, id).unwrap(); + assert_eq!((layers(&app), notes(&app)), (2, 0), "{key:?}: the note goes, the layers stay"); + let st = app.session.active().unwrap(); + assert_eq!(st.history.past_len(), steps + 1); + assert_eq!(st.history.undo_label(), Some("Delete Note")); + assert_eq!(app.ui.analysis.note_selected, None, "nothing selected after"); + } +} + +#[test] +fn no_keeps_the_document_and_history() { + let mut app = app(); + let steps = app.session.active().unwrap().history.past_len(); + press(&mut app, egui::Key::Delete); + let id = alert(&app).expect("the alert"); + crate::dialogs::cancel(&mut app, id).unwrap(); + assert_eq!((layers(&app), notes(&app)), (2, 1)); + assert_eq!(app.session.active().unwrap().history.past_len(), steps); +} + +#[test] +fn dont_show_again_skips_the_alert_next_time() { + let mut app = app(); + app.run("notes.add", json!({"x": 30, "y": 30})).unwrap(); + press(&mut app, egui::Key::Delete); + let id = alert(&app).expect("the alert"); + app.ui.dialog_mut(id).unwrap().fields.insert("skip".into(), json!(true)); + crate::dialogs::confirm(&mut app, id).unwrap(); + assert_eq!(notes(&app), 1); + // The panel's trash goes the same way: no alert, the note is deleted at once. + ask_delete_note(&mut app, 0); + assert!(alert(&app).is_none()); + assert_eq!((layers(&app), notes(&app)), (2, 0)); +} + +#[test] +fn the_notes_panel_trash_asks_too() { + let mut app = app(); + ask_delete_note(&mut app, 0); + assert!(alert(&app).is_some()); + assert_eq!(notes(&app), 1, "nothing goes before Yes"); +} + +#[test] +fn without_a_selected_note_delete_keeps_its_usual_route() { + let mut app = app(); + app.ui.analysis.note_selected = None; + press(&mut app, egui::Key::Delete); + assert!(alert(&app).is_none()); + assert_eq!(notes(&app), 1, "the note stays"); + // Another tool with a note still selected: the note isn't asked about. + let mut other = self::app(); + other.ui.tool = Tool::Move; + press(&mut other, egui::Key::Delete); + assert!(alert(&other).is_none()); + assert_eq!(notes(&other), 1); +}