From e6f5de7e42307d2936a234cae0d4e478bdebece4 Mon Sep 17 00:00:00 2001 From: AnhNgoo <110978888+AnhNgoo@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:54:39 +0700 Subject: [PATCH] fix(ui): drain modified Escape/Enter in message_box so open dialogs stay open (#2189) --- crates/ui-egui/src/dialogs/show.rs | 1 + crates/ui-egui/src/widgets.rs | 11 +++++++--- crates/ui-egui/tests/it/dialog_focus.rs | 28 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/dialogs/show.rs b/crates/ui-egui/src/dialogs/show.rs index b4c478322..7a7d83d93 100644 --- a/crates/ui-egui/src/dialogs/show.rs +++ b/crates/ui-egui/src/dialogs/show.rs @@ -522,6 +522,7 @@ pub(super) fn open_confirmed_link(app: &mut SheetApp, d: &Dialog) -> Result<(), r } +/// Draws the standard OK and Cancel button row, consuming unmodified Enter on confirmation (#2189). pub(super) fn ok_cancel(ui: &mut egui::Ui, confirm: &mut bool, open: &mut bool) { ui.add_space(8.0); // A row as tall as the buttons: a right-to-left layout straight in the window takes all the diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 9d1ed3586..a1f7d2ca0 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -598,9 +598,14 @@ pub fn message_box(app: &mut SheetApp, ctx: &egui::Context) { }); ui.add_space(8.0); ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - // Dismissal keys belong to this message, not the dialog underneath. - let (enter, escape) = - ui.input_mut(|i| (i.consume_key(egui::Modifiers::NONE, egui::Key::Enter), i.consume_key(egui::Modifiers::NONE, egui::Key::Escape))); + // Dismissal keys belong to this message, not the dialog underneath; modified Enter/Escape + // events (such as Ctrl+Escape) are also drained so they cannot close the open dialog. + let (enter, escape) = ui.input_mut(|i| { + let enter = i.consume_key(egui::Modifiers::NONE, egui::Key::Enter); + let escape = i.consume_key(egui::Modifiers::NONE, egui::Key::Escape); + i.events.retain(|e| !matches!(e, egui::Event::Key { key: egui::Key::Enter | egui::Key::Escape, pressed: true, .. })); + (enter, escape) + }); if ui.button(" OK ").clicked() || enter || escape { close = true; } diff --git a/crates/ui-egui/tests/it/dialog_focus.rs b/crates/ui-egui/tests/it/dialog_focus.rs index ccbed7746..91f8616f5 100644 --- a/crates/ui-egui/tests/it/dialog_focus.rs +++ b/crates/ui-egui/tests/it/dialog_focus.rs @@ -377,3 +377,31 @@ fn existing_message_takes_enter_and_escape_before_open_dialog() { assert!(h.state().message.is_none()); assert_eq!(h.state().session.active().unwrap().wb.active().unwrap().name, "LaterName"); } + +/// #2189: Ctrl+Escape while an error message is open must not close the dialog behind it or lose the draft. +#[test] +fn ctrl_escape_does_not_close_dialog_behind_error_message() { + let mut h = harness(); + h.state_mut().open_dialog("renameSheet", json!({})); + h.run_steps(4); + + h.state_mut().dialog.as_mut().unwrap().values.insert("name".into(), json!("a/b")); + h.key_press(Key::Enter); + h.run_steps(3); + + assert!(h.state().message.is_some()); + assert!(h.state().dialog.is_some()); + + h.key_press_modifiers(egui::Modifiers::CTRL, Key::Escape); + h.run_steps(3); + + assert!(h.state().message.is_some(), "Ctrl+Escape does not dismiss the error alert"); + assert!(h.state().dialog.is_some(), "Ctrl+Escape must not close the dialog behind the alert"); + assert_eq!(h.state().dialog.as_ref().unwrap().values.get("name"), Some(&json!("a/b")), "rename draft remains intact"); + + // Plain Escape still dismisses only the message. + h.key_press(Key::Escape); + h.run_steps(3); + assert!(h.state().message.is_none()); + assert!(h.state().dialog.is_some()); +}