diff --git a/crates/ui-egui/src/keymap.rs b/crates/ui-egui/src/keymap.rs index 5acce9b4d..7e04fe72d 100644 --- a/crates/ui-egui/src/keymap.rs +++ b/crates/ui-egui/src/keymap.rs @@ -41,6 +41,8 @@ pub enum Shortcut { Zoom(i32), /// Ctrl+Alt+Shift+P: edit the active cell as Python code (Python mode). PythonMode, + /// Excel for Mac Control+Q: Quick Analysis (Windows Ctrl+Q is in `ribbon::shortcut`). + QuickAnalysis, } fn run(id: &'static str) -> Shortcut { @@ -139,6 +141,8 @@ fn lookup_control_shortcuts(ctx: LookupContext) -> Option { // Windows Ctrl+; is in `ribbon::shortcut`; Excel for Mac uses Control+; and Command+;. Semicolon if mac && ctrl => Shortcut::Date, Semicolon if mac && cmd && !s => Shortcut::Time, + // Control+Q only: Command+Q is the native Quit item. + Q if mac && ctrl && !m.command && !s => Shortcut::QuickAnalysis, _ => return None, }) } @@ -349,6 +353,7 @@ pub(crate) fn dispatch(app: &mut SheetApp, ctx: &egui::Context, key: Key, physic } Shortcut::Edit => app.begin_edit(None, false), Shortcut::PythonMode => app.toggle_python_mode(), + Shortcut::QuickAnalysis => crate::ribbon::quick_analysis(app), Shortcut::ContextMenu => app.grid.context_menu = Some(geo.cell_rect(sh, active).center()), Shortcut::DropDown => { if sh.validations.iter().any(|d| d.in_cell_dropdown && d.ranges.iter().any(|r| r.contains(active))) { diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 47f1eabd7..4e8ba9dd2 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -37,6 +37,7 @@ use home::home; use insert::{draw, insert}; use page_layout::page_layout; use review::review; +pub(crate) use shortcuts::quick_analysis; pub use shortcuts::shortcut; pub use title_bar::{MAC_CONTENT_LEFT, MAC_TITLE_BAR, TITLE_LEFT_PAD, title_bar}; use title_bar::{TITLE_BAR_H, TITLE_CONTROL_H}; diff --git a/crates/ui-egui/src/ribbon/shortcuts.rs b/crates/ui-egui/src/ribbon/shortcuts.rs index 2eb118cd1..53d99318b 100644 --- a/crates/ui-egui/src/ribbon/shortcuts.rs +++ b/crates/ui-egui/src/ribbon/shortcuts.rs @@ -1,5 +1,15 @@ use super::{Key, Modifiers, SheetApp, json}; +/// Ctrl+Q (Control+Q in Excel for Mac): Quick Analysis. A single cell has nothing to analyse, +/// which is not worth an alert, but several areas deserve to be told why nothing opened. +pub(crate) fn quick_analysis(app: &mut SheetApp) { + if let Err(e) = crate::quick_analysis::toggle(app, None) + && app.session.active().is_some_and(|d| d.selection.ranges.len() > 1) + { + app.alert(e); + } +} + /// Command-key shortcuts when the grid has focus. pub fn shortcut(app: &mut SheetApp, key: Key, m: Modifiers) { let shift = m.shift; @@ -43,14 +53,9 @@ pub fn shortcut(app: &mut SheetApp, key: Key, m: Modifiers) { Key::T => Some(("insert.table", json!({}))), Key::E => Some(("edit.flashFill", json!({}))), // Plain Ctrl+Q only: Ctrl+Shift+Q and Ctrl+Alt+Q (AltGr+Q) are not Quick Analysis. - Key::Q if !shift && !m.alt => { - // Quick Analysis; a single cell has nothing to analyse, which is not worth an alert, - // but several areas deserve to be told why nothing opened. - if let Err(e) = crate::quick_analysis::toggle(app, None) - && app.session.active().is_some_and(|d| d.selection.ranges.len() > 1) - { - app.alert(e); - } + // Command+Q on a Mac is the native Quit item; Control+Q there goes through the keymap. + Key::Q if !shift && !m.alt && !m.mac_cmd => { + quick_analysis(app); None } Key::Num1 => { diff --git a/crates/ui-egui/tests/it/keyboard_shortcuts.rs b/crates/ui-egui/tests/it/keyboard_shortcuts.rs index 56dadde3a..ff0510fb2 100644 --- a/crates/ui-egui/tests/it/keyboard_shortcuts.rs +++ b/crates/ui-egui/tests/it/keyboard_shortcuts.rs @@ -315,3 +315,28 @@ fn only_plain_ctrl_q_toggles_quick_analysis() { key(&mut h, Key::Q, CTRL); assert!(h.state().grid.quick_analysis.open.is_some(), "Ctrl+Q"); } + +/// Excel for Mac: Control+Q (ctrl without command) opens Quick Analysis, Command+Q is left to the +/// native Quit item, and a single cell still opens nothing without an alert. +#[test] +fn mac_control_q_toggles_quick_analysis_and_command_q_does_not() { + const MAC_CONTROL: Modifiers = Modifiers { alt: false, ctrl: true, shift: false, mac_cmd: false, command: false }; + const MAC_COMMAND: Modifiers = Modifiers { alt: false, ctrl: false, shift: false, mac_cmd: true, command: true }; + assert_eq!(lookup(Key::Q, Some(Key::Q), MAC_CONTROL, true), Some(Shortcut::QuickAnalysis)); + assert_eq!(lookup(Key::Q, Some(Key::Q), MAC_COMMAND, true), None); + assert_eq!(lookup(Key::Q, Some(Key::Q), MAC_CONTROL, false), None, "Windows Ctrl+Q goes through ribbon::shortcut"); + let mac = |sel: &str| crate::common::os_harness(book(sel), egui::os::OperatingSystem::Mac); + let mut h = mac("A1:B3"); + key(&mut h, Key::Q, MAC_COMMAND); + assert!(h.state().grid.quick_analysis.open.is_none(), "Command+Q is Quit"); + key(&mut h, Key::Q, Modifiers { shift: true, ..MAC_CONTROL }); + assert!(h.state().grid.quick_analysis.open.is_none(), "Control+Shift+Q"); + key(&mut h, Key::Q, MAC_CONTROL); + assert!(h.state().grid.quick_analysis.open.is_some(), "Control+Q opens"); + key(&mut h, Key::Q, MAC_CONTROL); + assert!(h.state().grid.quick_analysis.open.is_none(), "Control+Q again closes"); + let mut h = mac("B2"); + key(&mut h, Key::Q, MAC_CONTROL); + assert!(h.state().grid.quick_analysis.open.is_none()); + assert!(h.state().message.is_none() && h.state().toast.is_none(), "no alert for a single cell"); +} diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 121c45d5a..8c747c152 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -145,10 +145,10 @@ otherwise. Number-format and border keys match the physical key (Ctrl+Shift+4, w | Ctrl+Alt+Shift+P | Cmd+Option+Shift+P | Python mode on or off | **added** | | F3 | not checked | Paste Name dialog, only when the workbook has names: OK puts the name at the caret of the formula being edited (or starts `=Name`), Paste List writes each name and what it refers to from the active cell (`formulas.pasteName`) | **added** | | F1, F4 (repeat), F6 / Shift+F6, F8 / Shift+F8, F11, End mode | same | help, Repeat, pane cycling, extend/add mode, chart sheet | missing (no such feature yet) | -| Ctrl+Q | Ctrl+Q | Quick Analysis | **added** (#791) | +| Ctrl+Q | Control+Q (Cmd+Q stays Quit) | Quick Analysis | **added** (#791) | | Ctrl+A after a function name, Ctrl+Shift+U, Ctrl+Alt+P | same | Function Arguments, formula bar height, tooltips | missing | | F1, F3, F4 (repeat), F6 / Shift+F6, F8 / Shift+F8, F11, End mode | same | help, Paste Name, Repeat, pane cycling, extend/add mode, chart sheet | missing (no such feature yet) | -| Ctrl+Q | Ctrl+Q | Quick Analysis | **added** (#791) | +| Ctrl+Q | Control+Q (Cmd+Q stays Quit) | Quick Analysis | **added** (#791) | | Ctrl+A after a function name, Ctrl+Alt+P | same | Function Arguments, tooltips | missing | | Ctrl+Shift+A after a function name | Control+Shift+A | argument names: `=SUMIF(` becomes `=SUMIF(range,criteria,sum_range)`, the `(` added when missing; one Undo takes them back | **added** | | Ctrl+Shift+U | Control+Shift+U | expand or collapse the formula bar (`view.expandFormulaBar`), on the grid and while editing; on Windows and Linux it no longer underlines | **added** |