diff --git a/crates/ui-egui/src/ribbon/home.rs b/crates/ui-egui/src/ribbon/home.rs index e232b7d63f..1e583f372f 100644 --- a/crates/ui-egui/src/ribbon/home.rs +++ b/crates/ui-egui/src/ribbon/home.rs @@ -1,4 +1,4 @@ -use craft::behavior::{IconId, keyed}; +use craft::behavior::{EditEvent, IconId, keyed}; use craft::menu::{CommandRef, RibbonGroup, RibbonRow}; use craft::theme::variant::Tone; use craft::theme::{Emphasis, Variant}; @@ -218,14 +218,30 @@ pub(super) fn home(row: &mut RibbonRow<'_>, app: &mut WordApp) { } // Enter or a pick from the list hands the keyboard back to the document, as Word does (#9233). A commit // by clicking another control leaves focus on that control. Up, Down and the wheel step the size with a - // `Set` too; the box keeps focus then, so repeated steps go on. + // `Set` too; the box keeps focus then, so repeated steps go on. Only a `Set` from a wheel over the box + // or Up/Down on the focused box counts as a step, so a stray wheel event in the same frame as Enter + // still returns focus to the document (#9248). let held = ui.ctx().memory(|m| m.focused()); - let stepped = ui.ctx().input(|i| { - i.events.iter().any(|e| { - matches!(e, egui::Event::MouseWheel { .. }) - || matches!(e, egui::Event::Key { key: egui::Key::ArrowUp | egui::Key::ArrowDown, pressed: true, .. }) + let is_set = out + .edit + .events + .iter() + .rev() + .find_map(|e| match e { + EditEvent::Set { .. } => Some(true), + EditEvent::Commit { .. } => Some(false), + _ => None, }) - }); + .unwrap_or(false); + let stepped = is_set + && ((out.edit.response.hovered() + && ui.ctx().input(|i| i.events.iter().any(|e| matches!(e, egui::Event::MouseWheel { .. })))) + || (craft::ui::runtime::focus::has(ui.ctx(), out.edit.response.id) + && ui.ctx().input(|i| { + i.events + .iter() + .any(|e| matches!(e, egui::Event::Key { key: egui::Key::ArrowUp | egui::Key::ArrowDown, pressed: true, .. })) + }))); if out.edit.events.committed().is_some() && !stepped && (held.is_none() || craft::ui::runtime::focus::has(ui.ctx(), out.edit.response.id)) diff --git a/crates/ui-egui/src/ribbon/tests.rs b/crates/ui-egui/src/ribbon/tests.rs index 0e08359882..0c429dd132 100644 --- a/crates/ui-egui/src/ribbon/tests.rs +++ b/crates/ui-egui/src/ribbon/tests.rs @@ -1688,3 +1688,33 @@ fn down_in_the_font_size_box_steps_the_size_again_and_keeps_focus() { let page = h.state().canvas.shown.canvas.expect("the page is shown"); assert!(!craft::ui::runtime::focus::has(&ctx, page), "the box keeps the keyboard while stepping"); } + +/// #9248: a stray MouseWheel event in the same frame as Enter must not keep focus in the Font Size box. +#[test] +fn enter_in_the_font_size_box_returns_focus_even_with_a_stray_wheel_event() { + use egui::accesskit::Role; + + let mut app = WordApp::new(Session::new(wordcraft_doc::Document::from_text("Sample\n\nafter")), crate::Services::default()); + app.run("select.all", json!({})).unwrap(); + let mut h = tab_harness(app, "Home"); + h.get_all_by_role(Role::TextInput).find(|n| n.value().is_some_and(|v| v.trim().parse::().is_ok())).expect("the font size box").focus(); + crate::test_harness::frames(&mut h, 2); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + crate::test_harness::frames(&mut h, 2); + h.event(egui::Event::Text("15".into())); + crate::test_harness::frames(&mut h, 2); + h.event(egui::Event::MouseWheel { + unit: egui::MouseWheelUnit::Line, + delta: egui::vec2(0.0, 1.0), + phase: egui::TouchPhase::Move, + modifiers: egui::Modifiers::NONE, + source: egui::MouseWheelSource::Trackpad, + }); + h.key_press(egui::Key::Enter); + crate::test_harness::frames(&mut h, 3); + let st = wordcraft_engine::cmd::format::cached_state(&h.state().session); + assert_eq!(st.get("size").and_then(Value::as_f64), Some(15.0)); + let ctx = h.ctx.clone(); + let page = h.state().canvas.shown.canvas.expect("the page is shown"); + assert!(craft::ui::runtime::focus::has(&ctx, page), "the page has the keyboard after Enter even with a stray wheel event"); +}