diff --git a/crates/ui-contract/src/runtime/egui/fixture.rs b/crates/ui-contract/src/runtime/egui/fixture.rs index 6297dd938..42a16c1fb 100644 --- a/crates/ui-contract/src/runtime/egui/fixture.rs +++ b/crates/ui-contract/src/runtime/egui/fixture.rs @@ -347,6 +347,11 @@ pub(crate) fn host_focus(ctx: &Context, id: egui::Id) { ctx.memory_mut(|memory| memory.request_focus(id)); } +/// A host's own clickable egui widget `id` at `rect`, a focus stop for egui's own focus moves. +pub(crate) fn host_button(ui: &Ui, id: egui::Id, rect: Rect) { + ui.interact(rect, id, egui::Sense::click()); +} + /// A host's own egui drag area `id` at `rect`; returns whether egui reports it dragged and whether its drag stopped /// this pass. pub(crate) fn host_drag_area(ui: &Ui, id: egui::Id, rect: Rect) -> (bool, bool) { diff --git a/crates/ui-contract/src/runtime/egui/focus.rs b/crates/ui-contract/src/runtime/egui/focus.rs index ee880710a..c6e7872e7 100644 --- a/crates/ui-contract/src/runtime/egui/focus.rs +++ b/crates/ui-contract/src/runtime/egui/focus.rs @@ -54,8 +54,10 @@ pub(crate) fn surrender(ctx: &Context, id: Id) { ctx.memory_mut(|memory| memory.surrender_focus(id)); } -/// H7: egui's begin pass set a focus direction from an unmodified arrow; the runtime's focus service is the -/// only mover, so the direction is cleared before any component runs. +/// H7: egui's begin pass set a focus direction from an unmodified arrow or the Tab it kept. The focus service clears +/// it before any component runs unless egui focus is on an app's own widget (`FocusedRecord::host_focus`) and the +/// service moves no focus in the pass: egui would otherwise move focus off a framework stop that the same arrow +/// reaches as an intent. pub(crate) fn clear_directional_focus(ctx: &Context) { ctx.memory_mut(|memory| memory.move_focus(FocusDirection::None)); } diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 2786b9377..f0d0e2dda 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -76,6 +76,9 @@ pub(crate) struct FocusedRecord { /// drag is captured: an app's own focused widget or popup keeps the keys a focused component keeps (Tab, Escape, /// the clipboard events). Written by the end hook after its focus moves. pub(crate) foreign: bool, + /// egui focus is on a widget no framework component made, and no framework press or drag is captured: egui + /// moves focus among the app's own widgets with the arrows and the Tab it keeps. Written by the end hook. + pub(crate) host_focus: bool, /// egui holds a label text selection: a copy or cut event is egui's, which copies the selection. Written by the /// end hook. pub(crate) label_selection: bool, @@ -642,8 +645,16 @@ pub(crate) fn begin(ctx: &Context, tab_all_controls: bool) { tab_all_controls, frame: crate::runtime::egui::pass::frame_nr(ctx, viewport(ctx)), }; - egui_focus::clear_directional_focus(ctx); - let (acts, announcements) = with(ctx, |state| nav::begin(state, input)); + let (acts, announcements, host_focus) = with(ctx, |state| { + let host_focus = state.record.host_focus; + let (acts, announcements) = nav::begin(state, input); + (acts, announcements, host_focus) + }); + // egui's direction moves focus from the widget it holds: only from an app's own widget, and not in a pass whose + // focus the framework moves. + if !host_focus || !acts.is_empty() { + egui_focus::clear_directional_focus(ctx); + } apply(ctx, acts); for text in announcements { crate::runtime::frame::announce(ctx, text); @@ -661,11 +672,12 @@ pub(crate) fn end(ctx: &Context) { // Escape over a captured framework press or drag cancels it (`frame::begin_pass`), whatever egui has focused. let captured = crate::runtime::pointer::press_captured(ctx) || crate::runtime::drag::running(ctx); let unmade = with(ctx, |state| state.focus_now.filter(|_| state.stop_of_focus().is_none())); - let foreign = - !captured && (egui_focus::popup_open(ctx) || unmade.is_some_and(|id| !crate::runtime::pointer::made_by_component(ctx, viewport, id))); + let host_focus = !captured && unmade.is_some_and(|id| !crate::runtime::pointer::made_by_component(ctx, viewport, id)); + let foreign = host_focus || (!captured && egui_focus::popup_open(ctx)); let label_selection = egui_focus::label_selection(ctx); with(ctx, |state| { state.record.foreign = foreign; + state.record.host_focus = host_focus; state.record.label_selection = label_selection; }); } diff --git a/crates/ui-contract/src/runtime/focus/nav.rs b/crates/ui-contract/src/runtime/focus/nav.rs index bfa6b3e7a..962236b4e 100644 --- a/crates/ui-contract/src/runtime/focus/nav.rs +++ b/crates/ui-contract/src/runtime/focus/nav.rs @@ -585,6 +585,7 @@ pub(super) fn end(state: &mut State, focused: Option, frame: u64) -> Vec