mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-05 22:59:18 +00:00
fix(ui-egui): narrow Font Size step detection to combo Set events (#9248)
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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::<f64>().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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user