Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Alerts: Enter is OK, but not the Enter that raised the alert (#4872) (#4880)

F4LCON3d54e8cd68e1
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 7ae2730bd..7b747f42f 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -338,7 +338,14 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| {
ui.spacing_mut().item_spacing.x = 10.0;
if matches!(d.kind, DialogKind::About | DialogKind::Error) {
- if dialog_buttons(ui, &[DialogButton::new(ButtonRole::Default, tl!("OK"), 84.0)]).is_some() {
+ // ↩ is OK too, as on every other dialog (#4872), from the frame after the
+ // alert appeared: the ↩ that raised it (committing a locked layer's Free
+ // Transform, say) must not close it again at once.
+ let seen = id.with("alert-seen");
+ let shown_before = ui.ctx().data(|m| m.get_temp::<bool>(seen).unwrap_or(false));
+ ui.ctx().data_mut(|m| m.insert_temp(seen, true));
+ let enter = interactive && shown_before && ui.input(|i| i.key_pressed(egui::Key::Enter));
+ if dialog_buttons(ui, &[DialogButton::new(ButtonRole::Default, tl!("OK"), 84.0)]).is_some() || enter {
outcome = Some(false);
}
} else {
@@ -795,6 +802,32 @@ mod tests {
assert_eq!(harness.state().ui.dialogs.len(), 1, "dragging must not close the dialog");
}
+ /// #4872: an alert (Error, About) closes with ↩ as with OK, but not on the ↩ that raised it.
+ #[test]
+ fn enter_closes_an_alert_but_not_the_enter_that_opened_it() {
+ let app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(900.0, 600.0)).build_ui_state(|ui, app| show(app, ui.ctx()), app);
+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
+ h.run_steps(2);
+ let alert = |h: &mut egui_kittest::Harness<'static, PhotocraftApp>| {
+ h.state_mut().ui.open_dialog(DialogKind::Error, json!({"message": crate::move_lock::MESSAGE}).as_object().cloned().unwrap_or_default())
+ };
+ // Raised in the frame its own ↩ arrives: that ↩ leaves it open.
+ alert(&mut h);
+ h.key_press(egui::Key::Enter);
+ h.run_steps(2);
+ assert_eq!(h.state().ui.dialogs.len(), 1, "the ↩ that raised the alert doesn't close it");
+ h.key_press(egui::Key::Enter);
+ h.run_steps(2);
+ assert!(h.state().ui.dialogs.is_empty(), "↩ is OK");
+ // A new alert starts over, and Esc still closes it.
+ alert(&mut h);
+ h.run_steps(2);
+ h.key_press(egui::Key::Escape);
+ h.run_steps(2);
+ assert!(h.state().ui.dialogs.is_empty(), "Esc too");
+ }
+
fn dialog_harness(size: egui::Vec2, command: &str) -> egui_kittest::Harness<'static, PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();