From b58d43ffbca300b4b1d9f84d3869cb474d4c8397 Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Mon, 23 Nov 2026 20:18:14 +0100 Subject: [PATCH] Text fields: an open edit menu is no focus loss The adapter keeps the part's focused state through its edit menu, which holds the editing focus, so was_focused stayed true while key focus was with the menu and text_lost_focus was reported on every pass the menu was open. It is now reported only when the menu is not holding the focus, that is when key focus has really left. The new trial opens the edit menu on a field and checks that no pass reports a loss. --- crates/ui-contract/src/contract/text.rs | 10 +++++++--- crates/ui-widgets/tests/it/text/edit_run.rs | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/ui-contract/src/contract/text.rs b/crates/ui-contract/src/contract/text.rs index eca93efa7f..35c810f9bc 100644 --- a/crates/ui-contract/src/contract/text.rs +++ b/crates/ui-contract/src/contract/text.rs @@ -409,6 +409,8 @@ struct PreparedText { caret_visible: bool, commit_now: bool, was_focused: bool, + /// The part's edit menu is open and holds its editing focus. + menu_open: bool, } /// Run the text adapter and adjust focus before the component's typed resolver handles its value. @@ -520,7 +522,7 @@ fn prepare(core: &mut CxCore<'_>, ui: &mut Ui, decl: &TextPart, state: &mut Text state.select_on_click = false; } } - PreparedText { widget, out, read_only, list_open, caret_visible, commit_now, was_focused } + PreparedText { widget, out, read_only, list_open, caret_visible, commit_now, was_focused, menu_open: held } } /// Places the caret of the text part's widget `widget` as focus arrives on the part: where the codec puts it, or over @@ -574,7 +576,7 @@ pub(crate) fn run( } (decl.values.resync)(&mut state, shown_copy(cx).as_ref()); let text = prepare(cx.core, ui, part, &mut state, AdapterSettings { keys, rtl, ink, underline }, step.commit_now); - let PreparedText { widget, out, read_only, list_open, caret_visible, commit_now, was_focused } = text; + let PreparedText { widget, out, read_only, list_open, caret_visible, commit_now, was_focused, menu_open } = text; if part.options.complete.is_some() { drive_completions(cx, &mut state); } @@ -627,7 +629,9 @@ pub(crate) fn run( outcome.clicked = out.clicked; // The focus service moved focus off the widget with Tab before this pass's widgets ran. let left_by_tab = if was_focused && !out.focused { crate::runtime::focus::tabbed_off(&ctx, widget) } else { None }; - let text_lost_focus = was_focused && !out.focused; + // The edit menu holds the part's editing focus while it is open (`was_focused` stays true through it): key focus + // that is with the menu is not lost until the menu is gone and focus is not back. + let text_lost_focus = was_focused && !out.focused && !menu_open; cx.core.texts = Some((part.key, Box::new(RecordedText { outcome, submit_mods: resolved.submit_mods, left_by_tab, tab: out.tab, caret, text_lost_focus }))); apply_events(cx, resolved.events); diff --git a/crates/ui-widgets/tests/it/text/edit_run.rs b/crates/ui-widgets/tests/it/text/edit_run.rs index 618e67bbb0..9cc3d0ec3f 100644 --- a/crates/ui-widgets/tests/it/text/edit_run.rs +++ b/crates/ui-widgets/tests/it/text/edit_run.rs @@ -238,3 +238,19 @@ fn a_field_removed_with_its_edit_menu_open_starts_afresh() { rig.escape(); assert_eq!(rig.text(), "Porto", "Escape gives back the text the new run began with"); } + +/// The edit menu holds the field's editing focus while it is open: that is no focus loss, so the field reports none in +/// the passes the menu is open. +#[trial] +fn an_open_edit_menu_is_no_focus_loss() { + let mut rig = rig("Lisbon", false); + rig.retype("Rome"); + rig.fixture.advance(1.0); + let at = rig.at(); + rig.fixture.frame(vec![pointer::secondary(at, true)]); + rig.fixture.frame(vec![pointer::secondary(at, false)]); + rig.fixture.settle(); + assert!(rig.fixture.by_label("Cut").is_some(), "the edit menu is open"); + rig.fixture.idle(4); + assert_eq!(rig.lost_focus(), 0, "the passes with the menu open report no focus loss"); +}