diff --git a/crates/ui-egui/src/formula_bar.rs b/crates/ui-egui/src/formula_bar.rs index 6cca1103c..a693ee936 100644 --- a/crates/ui-egui/src/formula_bar.rs +++ b/crates/ui-egui/src/formula_bar.rs @@ -112,6 +112,10 @@ fn name_box(app: &mut SheetApp, ui: &mut egui::Ui, t: &Tokens) { } if resp.lost_focus() { if ui.input(|i| i.key_pressed(Key::Enter)) { + // The Enter that commits the Name Box is used up here. Left in the frame's input, the + // grid (drawn after the formula bar, and focused again now the box let go) would read + // it as its own Enter and move the active cell one row down from the jump target (#247). + ui.input_mut(|i| i.events.retain(|e| !matches!(e, egui::Event::Key { key: Key::Enter, .. }))); let target = text.trim().to_string(); if !target.is_empty() && target != shown { // A new name for the selection, or a place to go. diff --git a/crates/ui-egui/tests/interaction.rs b/crates/ui-egui/tests/interaction.rs index 84f20a675..b0ab7ae8c 100644 --- a/crates/ui-egui/tests/interaction.rs +++ b/crates/ui-egui/tests/interaction.rs @@ -125,6 +125,26 @@ fn space_toggles_the_active_checkbox_without_clicking_the_hovered_cell() { assert!(h.state().editor.is_none()); } +#[test] +fn name_box_enter_goes_to_the_typed_cell_not_the_one_below() { + // #247: typing A1 in the Name Box and pressing Enter selected A2; the grid replayed the Enter. + let mut h = harness(blank()); + h.state_mut().session.run("selection.set", json!({"cell": "C5"})).unwrap(); + h.run_steps(2); + h.ctx.memory_mut(|m| m.request_focus(egui::Id::new("gridcraft.name_box"))); + h.run_steps(2); + h.input_mut().events.push(Event::Key { key: Key::A, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::COMMAND }); + h.input_mut().events.push(Event::Text("A1".into())); + h.run_steps(2); + key(&mut h, Key::Enter, Modifiers::NONE); + h.run_steps(2); + assert_eq!(h.state().session.active().unwrap().selection.active, CellRef::new(0, 0)); + assert!(h.state().name_box.is_none()); + // The grid has the focus back: a plain Enter there still moves down. + key(&mut h, Key::Enter, Modifiers::NONE); + assert_eq!(h.state().session.active().unwrap().selection.active, CellRef::new(1, 0)); +} + #[test] fn typing_enters_values_and_moves_down() { let mut h = harness(blank());