Merge pull request #11841 from DuBearte/fix/11788-note-delete-key

fix(ui): Delete with a note selected asks to delete the note, not the layer (#11788)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-07 12:09:36 +01:00
commit 99f70bfe9b
4 files changed
+227 -6

No files matched your search

+20 -1
View File
@@ -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<bool> {
let mut outcome: Option<bool> = 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<Value, String>
fn confirm_command_more(app: &mut PhotocraftApp, d: Dialog) -> Result<Value, String> {
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) => {
+4
View File
@@ -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)
}
+96 -5
View File
@@ -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<String, Value>) -> bool {
fields.contains_key(DELETE_NOTE_MARK)
}
pub fn delete_note_body(ui: &mut egui::Ui, fields: &mut Map<String, Value>) {
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<String, Value>) -> Result<Value, String> {
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;
@@ -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<u64> {
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);
}