diff --git a/crates/ui-contract/src/contract/text.rs b/crates/ui-contract/src/contract/text.rs index 82ab276938..bd9a9157e4 100644 --- a/crates/ui-contract/src/contract/text.rs +++ b/crates/ui-contract/src/contract/text.rs @@ -413,6 +413,8 @@ struct PreparedText { window_blur: bool, /// The part's edit menu is open and holds its editing focus. menu_open: bool, + /// Focus moved from the text part to another part of its component in this pass. + left_for_part: bool, } /// Run the text adapter and adjust focus before the component's typed resolver handles its value. @@ -534,7 +536,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, window_blur, menu_open: held } + PreparedText { widget, out, read_only, list_open, caret_visible, commit_now, was_focused, window_blur, menu_open: held, left_for_part } } /// Places the caret of the text part's widget `widget` as focus arrives on the part: where the codec puts it, or over @@ -589,7 +591,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, window_blur, menu_open } = text; + let PreparedText { widget, out, read_only, list_open, caret_visible, commit_now, was_focused, window_blur, menu_open, left_for_part } = text; if part.options.complete.is_some() { drive_completions(cx, &mut state); } @@ -643,7 +645,7 @@ pub(crate) fn run( let left_by_tab = if was_focused && !out.focused { crate::runtime::focus::tabbed_off(&ctx, widget) } else { None }; // 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; + let text_lost_focus = was_focused && !menu_open && (!out.focused || left_for_part); 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/alt_text.rs b/crates/ui-widgets/tests/it/text/alt_text.rs index 431aad6998..cb703056ba 100644 --- a/crates/ui-widgets/tests/it/text/alt_text.rs +++ b/crates/ui-widgets/tests/it/text/alt_text.rs @@ -57,3 +57,24 @@ fn marking_decorative_disables_the_description_and_keeps_its_text() { assert_eq!(value.borrow().text, Mixed::Same("Kept".to_owned())); assert!(box_disabled(&fixture)); } + +/// Focus that moves from the description box to the check, another part of the same component, is the box losing its +/// key focus: the field reports it once, in the pass it happens, and not while the focus stays in the box. +#[trial] +fn focus_moving_to_the_check_reports_the_box_losing_it_once() { + let value = Rc::new(RefCell::new(AltText::new(""))); + let lost = Rc::new(std::cell::Cell::new(0_usize)); + let (shown, seen) = (value.clone(), lost.clone()); + let mut fixture = Fixture::new(move |ui| { + let out = AltTextField::new(PROMPT, Bind::bound(&mut *shown.borrow_mut())).show(ui); + seen.set(seen.get() + usize::from(out.text_lost_focus)); + }); + fixture.settle(); + let description = fixture.by_role(Role::MULTILINE_TEXT_INPUT).remove(0); + fixture.click(&description); + fixture.idle(3); + assert_eq!(lost.get(), 0, "the box keeps its focus while it is typed in"); + fixture.key(Key::Tab, Modifiers::NONE); + fixture.idle(3); + assert_eq!(lost.get(), 1, "Tab to the check reports the loss once"); +}