diff --git a/crates/ui-egui/src/quick_analysis.rs b/crates/ui-egui/src/quick_analysis.rs index f7db7b5697..66f5fb2e89 100644 --- a/crates/ui-egui/src/quick_analysis.rs +++ b/crates/ui-egui/src/quick_analysis.rs @@ -49,6 +49,8 @@ pub struct State { pub tab: Tab, /// Totals by row (results to the right) instead of by column (results below). pub by_rows: bool, + /// The popup just opened: its first tile takes the keyboard focus so arrows, Enter and Space work. + focus_first_tile: bool, } /// What a tile does. @@ -153,6 +155,7 @@ pub fn toggle(app: &mut SheetApp, on: Option) -> Result { return Err("Select at least two cells to use Quick Analysis.".into()); } else { app.grid.quick_analysis.open = Some((sheet, sel)); + app.grid.quick_analysis.focus_first_tile = true; } Ok(json!({"open": app.grid.quick_analysis.open.is_some()})) } @@ -208,8 +211,11 @@ pub fn show(app: &mut SheetApp, ui: &mut Ui, geo: &Geo, sh: &Sheet) { // Right after a fill-handle drag the Auto Fill Options button sits at the same corner; it wins // until that fill is no longer current (Ctrl+Q still opens the popup). let fill_tag = app.grid.fill_tag.is_some(); + // Whether the launcher or a popup control has the keyboard, so the grid reads no keys this frame. + let mut keyboard_in_popup = false; if corner_visible && (open || (!busy && !fill_tag)) { let mut clicked = false; + let mut focused = false; egui::Area::new(Id::unique("quick_analysis_button")).fixed_pos(button.min).order(Order::Foreground).show(&ctx, |ui| { let (r, resp) = ui.allocate_exact_size(button.size(), Sense::click()); let fill = if resp.is_pointer_button_down_on() || open { @@ -222,8 +228,13 @@ pub fn show(app: &mut SheetApp, ui: &mut Ui, geo: &Geo, sh: &Sheet) { ui.painter().rect_filled(r, 4.0, fill); ui.painter().rect_stroke(r, 4.0, Stroke::new(1.0, t.separator), StrokeKind::Inside); icons::paint(ui.painter(), Rect::from_center_size(r.center(), vec2(16.0, 16.0)), Icon::QuickAnalysis, t.accent); - clicked = resp.on_hover_text(lang.tr("Quick Analysis (Ctrl+Q)")).clicked(); + paint_focus_ring(ui, &t, r, &resp); + let name = lang.tr("Quick Analysis (Ctrl+Q)"); + resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, open, name)); + focused = resp.has_focus(); + clicked = resp.on_hover_text(name).clicked(); }); + keyboard_in_popup = focused; if clicked { let _ = toggle(app, None); } @@ -231,12 +242,16 @@ pub fn show(app: &mut SheetApp, ui: &mut Ui, geo: &Geo, sh: &Sheet) { let Some((_, opened_for)) = app.grid.quick_analysis.open else { return }; let pos = pos2(button.left(), button.bottom() + 4.0); let mut picked: Option = None; + // The frame that opened the popup (Ctrl+Q is still in the input) must not close it again. + let just_opened = app.grid.quick_analysis.focus_first_tile; let resp = egui::Area::new(Id::unique("quick_analysis_popup")).fixed_pos(pos).order(Order::Foreground).constrain(true).show(&ctx, |ui| { egui::Frame::popup(ui.style()).show(ui, |ui| { let state = &mut app.grid.quick_analysis; ui.horizontal(|ui| { for tab in Tab::ALL { - if ui.selectable_label(state.tab == tab, lang.tr(tab.label())).clicked() { + let tab_button = ui.selectable_label(state.tab == tab, lang.tr(tab.label())); + keyboard_in_popup |= tab_button.has_focus(); + if tab_button.clicked() { state.tab = tab; } } @@ -244,16 +259,17 @@ pub fn show(app: &mut SheetApp, ui: &mut Ui, geo: &Geo, sh: &Sheet) { ui.separator(); if state.tab == Tab::Totals { ui.horizontal(|ui| { - ui.selectable_value(&mut state.by_rows, false, lang.tr("By Column")); - ui.selectable_value(&mut state.by_rows, true, lang.tr("By Row")); + keyboard_in_popup |= ui.selectable_value(&mut state.by_rows, false, lang.tr("By Column")).has_focus(); + keyboard_in_popup |= ui.selectable_value(&mut state.by_rows, true, lang.tr("By Row")).has_focus(); }); } let items = tiles(state.tab, &opened_for.a1(), &sheet_name, state.by_rows); + let mut take_focus = std::mem::take(&mut state.focus_first_tile); for row in items.chunks(6) { ui.horizontal(|ui| { ui.spacing_mut().item_spacing.x = 2.0; for item in row { - if paint_tile(ui, &t, item, lang.tr(item.label)) { + if paint_tile(ui, &t, item, lang.tr(item.label), std::mem::take(&mut take_focus), &mut keyboard_in_popup) { picked = Some(item.action.clone()); } } @@ -269,14 +285,43 @@ 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_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape)) { + } else if outside_click + || ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape)) + || (keyboard_in_popup && !just_opened && toggle_key_pressed(&ctx)) + { app.grid.quick_analysis.open = None; } } -/// One option of the popup: a small picture over a wrapped label. True when clicked. -fn paint_tile(ui: &mut Ui, t: &Tokens, item: &Tile, label: &str) -> bool { +/// Ctrl+Q (Control+Q on a Mac, where Command+Q stays Quit) while a tile has the keyboard: the grid +/// no longer reads keys then, so the popup closes itself on the key that opened it. +fn toggle_key_pressed(ctx: &egui::Context) -> bool { + ctx.input_mut(|i| { + let pressed = |e: &egui::Event| { + matches!(e, egui::Event::Key { key: egui::Key::Q, pressed: true, modifiers: m, .. } if m.ctrl && !m.shift && !m.alt && !m.mac_cmd) + }; + let Some(at) = i.events.iter().position(pressed) else { return false }; + + i.events.remove(at); + true + }) +} + +/// A visible ring around a control that has the keyboard focus. +fn paint_focus_ring(ui: &Ui, t: &Tokens, rect: Rect, resp: &egui::Response) { + if resp.has_focus() { + ui.painter().rect_stroke(rect.expand(1.0), 5.0, Stroke::new(2.0, t.sheet_focus), StrokeKind::Outside); + } +} + +/// One option of the popup: a small picture over a wrapped label, a button named by that label. +/// `take_focus` gives it the keyboard focus. True when clicked. +fn paint_tile(ui: &mut Ui, t: &Tokens, item: &Tile, label: &str, take_focus: bool, has_keyboard: &mut bool) -> bool { let (rect, resp) = ui.allocate_exact_size(TILE, Sense::click()); + if take_focus { + resp.request_focus(); + } + resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, label)); if resp.is_pointer_button_down_on() { ui.painter().rect_filled(rect, 4.0, t.pressed); } else if resp.hovered() { @@ -287,6 +332,8 @@ fn paint_tile(ui: &mut Ui, t: &Tokens, item: &Tile, label: &str) -> bool { let galley = ui.painter().layout(label.to_string(), theme::ui_font(11.0), t.text, TILE.x - 4.0); let at = pos2(rect.center().x - galley.size().x / 2.0, rect.top() + 34.0); ui.painter().with_clip_rect(rect.intersect(ui.clip_rect())).galley(at, galley, t.text); + paint_focus_ring(ui, t, rect, &resp); + *has_keyboard |= resp.has_focus(); resp.clicked() } diff --git a/crates/ui-egui/tests/it/quick_analysis_button.rs b/crates/ui-egui/tests/it/quick_analysis_button.rs index 9a5e1f2b14..e8ff38782f 100644 --- a/crates/ui-egui/tests/it/quick_analysis_button.rs +++ b/crates/ui-egui/tests/it/quick_analysis_button.rs @@ -134,3 +134,57 @@ fn escape_closes_the_popup_without_cancelling_the_pending_copy() { esc(&mut h); assert!(h.state().session.clipboard.is_none(), "a second Esc, with no popup, cancels the copy as before"); } + +/// The launcher and the tiles are named buttons. The popup hands the keyboard to its first tile, +/// an arrow moves the focus to the next one and Enter runs it. +#[test] +fn launcher_and_tiles_are_named_buttons_that_work_by_keyboard() { + use egui::accesskit::Role; + use egui_kittest::kittest::Queryable as _; + 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(); + let mut h = crate::support::harness(session, 4); + h.get_by_role_and_label(Role::Button, "Quick Analysis (Ctrl+Q)"); + + toggle(h.state_mut(), Some(true)).unwrap(); + h.run_steps(3); + assert!(h.get_by_role_and_label(Role::Button, "Data Bars").is_focused(), "the first tile takes the focus"); + h.get_by_role_and_label(Role::Button, "Clear Rules"); + + h.key_press(egui::Key::ArrowRight); + h.run_steps(2); + assert!(h.get_by_role_and_label(Role::Button, "Color Scale").is_focused(), "an arrow moves the focus to the next tile"); + h.key_press(egui::Key::Enter); + h.run_steps(2); + assert!(h.state().grid.quick_analysis.open.is_none(), "the chosen tile closes the popup"); + let sheet = h.state().session.active().unwrap().wb.active().unwrap(); + assert_eq!(sheet.cond_formats.len(), 1, "and its color scale is applied"); +} + +/// Whether a frame of the launcher and popup draws the focus ring colour as a stroke. +fn draws_focus_ring(app: &mut SheetApp, frames: usize) -> bool { + let sh = app.session.active().unwrap().wb.active().unwrap().clone(); + let g = Geo::new(&sh, Rect::from_min_max(pos2(0.0, 0.0), pos2(1200.0, 800.0)), Vec2::ZERO); + let ctx = egui::Context::default(); + // No fade-in, so the colours are the theme's own. + ctx.global_style_mut(|style| style.animation_time = 0.0); + let ring = gridcraft_ui_egui::theme::Tokens::get(&ctx).sheet_focus; + let mut seen = false; + for _ in 0..frames { + let out = ctx.run_ui(egui::RawInput::default(), |ui| gridcraft_ui_egui::quick_analysis::show(app, ui, &g, &sh)); + seen = out.shapes.iter().any(|s| matches!(&s.shape, egui::Shape::Rect(r) if r.stroke.color == ring)); + out.drop_without_applying_deltas(); + } + seen +} + +/// The focused tile shows a ring, so a keyboard user can see where they are. +#[test] +fn the_focused_tile_shows_a_visible_focus_ring() { + let mut app = app(); + app.run("selection.set", json!({"range": "B2:D5"})).unwrap(); + assert!(!draws_focus_ring(&mut app, 3), "no ring while the popup is closed"); + toggle(&mut app, Some(true)).unwrap(); + assert!(draws_focus_ring(&mut app, 3), "the first tile has the focus and a ring"); +}