Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Integration: #1080 Enter/Esc

aiongg99b2b6a08809
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 6b8a5a32d..c9e8683ac 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -8371,11 +8371,17 @@ fn color_picker(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, label));
resp.clicked()
};
- if pill(ui, 0, &tr("OK"), true) || ui.input(|i| i.key_pressed(egui::Key::Enter)) {
- d.fields.insert("_close".into(), json!("ok"));
- }
- if pill(ui, 1, &tr("Cancel"), false) || ui.input(|i| i.key_pressed(egui::Key::Escape)) {
- d.fields.insert("_close".into(), json!("cancel"));
+ // The picker draws its own buttons, so the generic footer's Enter/Esc handling is skipped.
+ // The keys are consumed here so nothing else reacts to them as well, and a click on a pill
+ // wins over a key pressed in the same frame, so the dialog closes once.
+ let ok = pill(ui, 0, &tr("OK"), true);
+ let cancel = pill(ui, 1, &tr("Cancel"), false);
+ let enter = ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter));
+ let esc = ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape));
+ if ok || cancel {
+ d.fields.insert("_close".into(), json!(if cancel { "cancel" } else { "ok" }));
+ } else if enter || esc {
+ d.fields.insert("_close".into(), json!(if enter { "ok" } else { "cancel" }));
}
// The C/M/Y/K the fields show: the percentages typed or given, else the colour converted.
let cmyk = nums::<4>(d, "cmyk").map(|v| v.map(|x| x as f32)).unwrap_or_else(|| {
diff --git a/crates/ui-egui/src/dialogs/tests.rs b/crates/ui-egui/src/dialogs/tests.rs
index fb4c6ea44..284d6bbc6 100644
--- a/crates/ui-egui/src/dialogs/tests.rs
+++ b/crates/ui-egui/src/dialogs/tests.rs
@@ -576,6 +576,26 @@ fn fill(app: &DesignApp, id: u64) -> designcraft_color::Color {
d.resolve_color(&d.item(designcraft_doc::ItemId(id)).unwrap().fill.swatch, 1.0).unwrap()
}
+fn key_press(key: egui::Key) -> egui::Event {
+ egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE }
+}
+
+#[test]
+fn the_color_picker_enter_confirms_and_escape_cancels_even_in_a_field() {
+ for (key, applied) in [(egui::Key::Enter, true), (egui::Key::Escape, false)] {
+ let (mut app, ctx, screen, id) = picker(json!({"target": "fill", "hex": "#ffffff"}));
+ let labels = dialog_frame(&mut app, &ctx, screen, vec![]);
+ click_dialog(&mut app, &ctx, screen, visible_label(&labels, screen, "ffffff").center());
+ dialog_frame(&mut app, &ctx, screen, vec![select_all_key()]);
+ type_slowly(&mut app, &ctx, screen, "#f59019");
+ dialog_frame(&mut app, &ctx, screen, vec![key_press(key)]);
+ dialog_frame(&mut app, &ctx, screen, vec![]);
+ assert!(app.ui.dialog.is_none(), "{key:?} closes the picker");
+ let hex = fill(&app, id).to_hex();
+ assert_eq!(hex == "#f59019", applied, "{key:?}: {hex}");
+ }
+}
+
#[test]
fn the_hex_field_keeps_typed_text_and_commits_a_valid_code() {
let (mut app, ctx, screen, _) = picker(json!({"target": "fill", "hex": "#ffffff"}));