mirror of
https://github.com/storytold/gridcraft.git
synced 2026-12-01 10:14:21 -06:00
fix(ui): drain modified Escape/Enter in message_box so open dialogs stay open (#2189)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user