diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index ad747af5ce..5b6932f497 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -164,6 +164,8 @@ pub(crate) struct State { pub(super) seen: FastMap, /// A scope moved focus to its initial stop in the running pass. pub(super) entered: bool, + /// A component asked for the focus in the running pass (`request`), as an editor that appears under a press does. + pub(super) asked: bool, pub(crate) visible: bool, pub(crate) record: FocusedRecord, pub(super) menu_bar: Option, @@ -693,13 +695,15 @@ pub(crate) fn register_doc(ctx: &Context, key: DocKey, id: Id) { /// Moves focus to `target`, leaves modality unchanged and asks the enclosing scroll owner to reveal it. Called /// while a pass runs, it moves focus in that pass, so the rest of the pass reads the new focus; called between -/// passes, it moves focus at the next pass's begin hook. The one public path to programmatic focus. `false` in a measuring +/// passes, it moves focus at the next pass's begin hook; a pointer press of the pass moves it no further at the pass's +/// end. The one public path to programmatic focus. `false` in a measuring /// run, which moves nothing: a caller that keeps a pending request keeps it until a run takes it. pub fn request(ctx: &Context, target: FocusTarget) -> bool { if !crate::runtime::measure::effect(ctx, "focus::request") { return false; } if crate::runtime::frame::in_pass(ctx) { + with(ctx, |state| state.asked = true); request_now(ctx, target); return true; } diff --git a/crates/ui-contract/src/runtime/focus/nav.rs b/crates/ui-contract/src/runtime/focus/nav.rs index c38e1c7e92..8492de42a5 100644 --- a/crates/ui-contract/src/runtime/focus/nav.rs +++ b/crates/ui-contract/src/runtime/focus/nav.rs @@ -125,6 +125,7 @@ impl State { self.open.clear(); self.body_from.clear(); self.entered = false; + self.asked = false; } /// The registrations of last pass, which Tab and the arrows move over. @@ -712,8 +713,9 @@ pub(super) fn end(state: &mut State, focused: Option, on_app_widget: bool, f // The moves below belong to the next frame's passes. state.frame = frame + 1; let mut acts = state.apply_press(focused, on_app_widget); - // A layer that appeared in this pass took focus already: the press that opened it does not take it back. - if state.entered { + // A layer that appeared in this pass took focus already, and a component that asked for it in this pass has it: the + // press of the pass does not take it back. + if state.entered || state.asked { acts.clear(); } if acts.is_empty() { diff --git a/crates/ui-contract/tests/it/runtime/focus.rs b/crates/ui-contract/tests/it/runtime/focus.rs index 0fbfdb4b58..f6e80e4d0e 100644 --- a/crates/ui-contract/tests/it/runtime/focus.rs +++ b/crates/ui-contract/tests/it/runtime/focus.rs @@ -45,3 +45,32 @@ fn a_removed_stop_hands_its_focus_to_no_neighbour_and_the_body_stays() { assert_eq!(craft_ui::testing::focus::egui_focused(&frames.ctx), None, "no neighbour inherits the focus"); assert!((top.get() - start).abs() < 0.5, "the body keeps its offset: content top {} from {start}", top.get()); } + +/// A component that asks for the focus in the pass of a press on another component keeps it: the press's own focus +/// move at the pass's end does not take it back, as a text editor that appears under a click on the canvas and asks +/// for the keys keeps them. Guards against the end hook's press move overriding every request of the pass but a +/// layer's that appeared in it. +#[trial] +fn a_request_in_a_press_pass_keeps_the_focus_from_the_press() { + use craft_behavior::{Input, Sense}; + use craft_ui::runtime::focus::KeyUse; + use craft_ui::testing::focus::{Stop, egui_focused, named_id, press_shape, register_stop, widget}; + let (canvas, editor) = (named_id("press_canvas"), named_id("press_editor")); + let at = Cell::new(Rect::NOTHING); + let mut show = |ui: &mut Ui| { + let ctx = ui.ctx().clone(); + let rect = widget(ui, canvas); + register_stop(&ctx, Stop::new(canvas, KeyUse::CANVAS)); + press_shape(ui, rect, Sense::CLICK | Sense::TAKE_FOCUS, canvas); + at.set(rect); + widget(ui, editor); + register_stop(&ctx, Stop::new(editor, KeyUse::of(Input::Text))); + if ctx.input(|input| input.pointer.primary_pressed()) { + request(&ctx, FocusTarget::Id(editor)); + } + }; + let mut frames = Frames::new(); + frames.idle(2, &mut show); + craft_ui::testing::pointer::click(&mut frames, at.get().center(), &mut show); + assert_eq!(egui_focused(&frames.ctx), Some(editor), "the request of the press's pass holds"); +}