From 2240950eb650ad5016e5abebb10ce226dded5e7f Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 17:39:33 +0100 Subject: [PATCH] Quick Analysis: Esc closes the popup only; bind it to the sheet's identity Esc used to clear the pending copy and Format Painter before the popup saw it. The popup also remembers the sheet by its identity rather than its position, so moving sheets cannot hand it to another sheet. Co-Authored-By: Claude Sonnet 5.5 --- crates/ui-egui/src/grid/keyboard.rs | 14 ++++++--- crates/ui-egui/src/quick_analysis.rs | 10 +++---- .../ui-egui/tests/it/quick_analysis_button.rs | 29 +++++++++++++++++++ 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/grid/keyboard.rs b/crates/ui-egui/src/grid/keyboard.rs index a852d04c3..3fce6bedb 100644 --- a/crates/ui-egui/src/grid/keyboard.rs +++ b/crates/ui-egui/src/grid/keyboard.rs @@ -133,10 +133,7 @@ pub(super) fn keyboard(app: &mut SheetApp, ctx: &egui::Context, resp: &egui::Res } } Key::Escape if app.selected_object.is_some() => app.selected_object = None, - Key::Escape => { - app.session.clipboard = None; - app.session.format_painter = None; - } + Key::Escape => cancel_pending(app), Key::Space if !cmd && !shift && is_checkbox(sh, app) => app.run_or_alert("cell.toggleCheckbox", json!({})), Key::F8 if m.alt => { app.open_dialog("macros", json!({})); @@ -187,3 +184,12 @@ fn form_control_event(app: &mut SheetApp, sh: &Sheet, ev: &egui::Event) -> bool _ => false, } } + +/// Esc with nothing else to dismiss drops the pending copy and the Format Painter. An open Quick +/// Analysis popup owns the key: it closes and the pending copy stays. +fn cancel_pending(app: &mut SheetApp) { + if app.grid.quick_analysis.open.take().is_none() { + app.session.clipboard = None; + app.session.format_painter = None; + } +} diff --git a/crates/ui-egui/src/quick_analysis.rs b/crates/ui-egui/src/quick_analysis.rs index ea257ccd8..f7db7b569 100644 --- a/crates/ui-egui/src/quick_analysis.rs +++ b/crates/ui-egui/src/quick_analysis.rs @@ -44,8 +44,8 @@ impl Tab { #[derive(Default)] pub struct State { - /// The sheet (by index) and selection the popup was opened for; it closes when either changes. - pub open: Option<(usize, RangeRef)>, + /// The sheet (by its identity, not its position) and selection the popup was opened for; it closes when either changes. + pub open: Option<(u64, RangeRef)>, pub tab: Tab, /// Totals by row (results to the right) instead of by column (results below). pub by_rows: bool, @@ -145,7 +145,7 @@ pub fn toggle(app: &mut SheetApp, on: Option) -> Result { return Err("Quick Analysis works on one block of cells; select a single contiguous range.".into()); } let sel = d.selection.current(); - let sheet = d.wb.active_sheet(); + let sheet = d.wb.active().map_or(0, |s| s.uid.get()); let want = on.unwrap_or(app.grid.quick_analysis.open.is_none()); if !want { app.grid.quick_analysis.open = None; @@ -188,7 +188,7 @@ pub fn button_rect(geo: &Geo, sh: &Sheet, sel: RangeRef) -> (Rect, bool) { pub fn show(app: &mut SheetApp, ui: &mut Ui, geo: &Geo, sh: &Sheet) { let Some(d) = app.session.active() else { return }; let sel = d.selection.current(); - let sheet = d.wb.active_sheet(); + let sheet = d.wb.active().map_or(0, |s| s.uid.get()); let hidden = is_hidden(app, geo, sh); // The popup belongs to one selection on one sheet; a new one (or editing) closes it. if hidden || app.grid.quick_analysis.open.is_some_and(|(opened_on, r)| opened_on != sheet || r != sel) { @@ -269,7 +269,7 @@ pub fn show(app: &mut SheetApp, ui: &mut Ui, geo: &Geo, sh: &Sheet) { Action::Run(id, params) => app.run_or_alert(id, params), Action::Dialog(name, params) => app.open_dialog(name, params), } - } else if outside_click || ctx.input(|i| i.key_pressed(egui::Key::Escape)) { + } else if outside_click || ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape)) { app.grid.quick_analysis.open = None; } } diff --git a/crates/ui-egui/tests/it/quick_analysis_button.rs b/crates/ui-egui/tests/it/quick_analysis_button.rs index 3213c8cec..9a5e1f2b1 100644 --- a/crates/ui-egui/tests/it/quick_analysis_button.rs +++ b/crates/ui-egui/tests/it/quick_analysis_button.rs @@ -105,3 +105,32 @@ fn switching_sheets_closes_the_popup_even_for_the_same_selection() { frame(&mut app); assert!(app.grid.quick_analysis.open.is_none(), "another sheet with the same selection closes it"); } + +/// Esc dismisses the popup and nothing else: the pending copy (and an armed Format Painter) stay. +#[test] +fn escape_closes_the_popup_without_cancelling_the_pending_copy() { + let mut session = crate::support::blank_session(); + session.execute("range.setValues", json!({"range": "A1", "values": [[1, 2, 3], [4, 5, 6]]})).unwrap(); + session.execute("selection.set", json!({"range": "A1:C2"})).unwrap(); + session.execute("edit.copy", json!({})).unwrap(); + let mut h = crate::support::harness(session, 4); + assert!(h.state().session.clipboard.is_some()); + toggle(h.state_mut(), Some(true)).unwrap(); + h.run_steps(2); + assert!(h.state().grid.quick_analysis.open.is_some()); + let esc = |h: &mut crate::common::Harness| { + h.input_mut().events.push(egui::Event::Key { + key: egui::Key::Escape, + physical_key: None, + pressed: true, + repeat: false, + modifiers: egui::Modifiers::NONE, + }); + h.run_steps(2); + }; + esc(&mut h); + assert!(h.state().grid.quick_analysis.open.is_none(), "Esc closes the popup"); + assert!(h.state().session.clipboard.is_some(), "the copy stays pending"); + esc(&mut h); + assert!(h.state().session.clipboard.is_none(), "a second Esc, with no popup, cancels the copy as before"); +}