From 86c5f7a8d3875e846056cfddd85b07665b341387 Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 25 Nov 2026 02:31:27 +0000 Subject: [PATCH] Editor part: its focus widget is an egui focus candidate only while its component takes focus, so egui's arrows never reach a disabled editor; trial ids through the crate's id helpers --- crates/ui-contract/src/contract/editor.rs | 3 ++- crates/ui-contract/src/runtime/egui/editor.rs | 8 ++++--- .../src/testing/host_key_trials.rs | 10 ++++++--- crates/ui-contract/tests/it/text/editor.rs | 22 ++++++++++++++++--- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/crates/ui-contract/src/contract/editor.rs b/crates/ui-contract/src/contract/editor.rs index 922f2ced2..d20a202b6 100644 --- a/crates/ui-contract/src/contract/editor.rs +++ b/crates/ui-contract/src/contract/editor.rs @@ -72,7 +72,8 @@ pub(crate) fn run(cx: &mut Cx<'_, C>, ui: &mut Ui, step: &mut supe } let (ctx, id) = (cx.core.ctx.clone(), cx.core.id); let widget = ImeSurface::of(id, decl.key).widget(); - let focused = egui_editor::show(ui, widget, decl.rect, egui_focus::event_filter(keys.keep, false)); + let focusable = super::steps::takes_focus(cx.core.resolved); + let focused = egui_editor::show(ui, widget, decl.rect, egui_focus::event_filter(keys.keep, false), focusable); // The focus widget is the component's, enabled or not: it is never one of the app's own widgets (`take_app_widgets`). crate::runtime::pointer::record_text_widget(&ctx, widget, id); let read_only = cx.core.resolved.read_only || !cx.core.resolved.enabled; diff --git a/crates/ui-contract/src/runtime/egui/editor.rs b/crates/ui-contract/src/runtime/egui/editor.rs index e1824adda..63e618eed 100644 --- a/crates/ui-contract/src/runtime/egui/editor.rs +++ b/crates/ui-contract/src/runtime/egui/editor.rs @@ -4,9 +4,11 @@ use egui::{EventFilter, Id, Rect, Ui}; /// Registers the editor's focus widget over `rect` and applies `filter` while it holds focus; returns whether it holds -/// focus. -pub(crate) fn show(ui: &mut Ui, id: Id, rect: Rect, filter: EventFilter) -> bool { - let response = ui.interact(rect, id, egui::Sense::focusable_noninteractive()); +/// focus. The widget is one of egui's focus candidates only while `focusable` (its component takes focus), so neither +/// Tab nor egui's arrows reach the editor of a disabled component. +pub(crate) fn show(ui: &mut Ui, id: Id, rect: Rect, filter: EventFilter, focusable: bool) -> bool { + let sense = if focusable { egui::Sense::focusable_noninteractive() } else { egui::Sense::hover() }; + let response = ui.interact(rect, id, sense); let focused = response.has_focus(); if focused { ui.memory_mut(|memory| memory.set_focus_lock_filter(id, filter)); diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index 30f7d0b42..2ef494050 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -714,9 +714,13 @@ fn each_focus_candidate_of_a_pass_is_classified_once() { let lookups = |n: usize| { let mut show = move |ui: &mut Ui| { for at in 0..n { - fixture::host_button(ui, Id::new(("app", at)), Rect::from_min_size(Pos2::new(10.0, 30.0 * at as f32), egui::vec2(60.0, 20.0))); - widget(ui, Id::new(("framework", at))); - register_stop(ui.ctx(), Stop::new(Id::new(("framework", at)), KeyUse::of(Input::Pointer))); + fixture::host_button( + ui, + crate::runtime::egui::memory::unique(("app", at)), + Rect::from_min_size(Pos2::new(10.0, 30.0 * at as f32), egui::vec2(60.0, 20.0)), + ); + widget(ui, crate::runtime::egui::memory::unique(("framework", at))); + register_stop(ui.ctx(), Stop::new(crate::runtime::egui::memory::unique(("framework", at)), KeyUse::of(Input::Pointer))); } }; let mut frames = Frames::new(); diff --git a/crates/ui-contract/tests/it/text/editor.rs b/crates/ui-contract/tests/it/text/editor.rs index ecc63f134..fed1efee1 100644 --- a/crates/ui-contract/tests/it/text/editor.rs +++ b/crates/ui-contract/tests/it/text/editor.rs @@ -1030,16 +1030,21 @@ fn an_alt_chord_bound_only_out_of_scope_leaves_its_text_typed() { assert!(fired.is_empty(), "nothing ran: {fired:?}"); } -/// A disabled editor registers no stop, and its focus widget is still the component's own: it never becomes a Tab stop -/// of the app's own widgets. With every control in Tab order, Tab and Shift+Tab land on the button beside it each time. +/// A disabled editor registers no stop, and its focus widget is still the component's own and no egui focus candidate: +/// it never becomes a Tab stop of the app's own widgets, and egui's arrows from the app's own focused widget never +/// reach it. With every control in Tab order, Tab and Shift+Tab land on the button beside it each time. #[trial] fn tab_never_focuses_a_disabled_editor_s_widget() { let log = RefCell::new(Vec::new()); let seen = RefCell::new(Seen::default()); let (editor, button) = (Cell::new(Id::NULL), Cell::new(Id::NULL)); + // The app's own egui button, level with the editor on its right: the editor lies straight to its left. + let app = craft_ui::testing::focus::named_id("app button"); + let app_at = Rect::from_min_size(Pos2::new(AT.right() + 40.0, AT.center().y - 10.0), egui::vec2(60.0, 20.0)); let mut show = |ui: &mut egui::Ui| { editor.set(Probe::new(&log, true).label("Source").enabled(false).id_salt("code").show_at(ui, AT).id); button.set(Button::new(&seen).id_salt("next").show_at(ui, NEXT).id); + ui.interact(app_at, app, egui::Sense::click()); }; let mut frames = Frames::new(); frames.root({ @@ -1053,6 +1058,17 @@ fn tab_never_focuses_a_disabled_editor_s_widget() { frames.frame(vec![key_press(Key::Tab, mods), key_release(Key::Tab, mods)], &mut show); frames.frame(Vec::new(), &mut show); let focused = craft_ui::testing::focus::egui_focused(&frames.ctx); - assert_eq!(focused, Some(button.get()), "Tab (shift {shift}) lands on the button, never on the disabled editor {:?}", editor.get()); + assert!( + focused == Some(button.get()) || focused == Some(app), + "Tab (shift {shift}) lands on a control, never on the disabled editor {:?}: {focused:?}", + editor.get() + ); } + craft_ui::runtime::focus::request(&frames.ctx, craft_ui::runtime::focus::FocusTarget::Id(app)); + frames.idle(2, &mut show); + assert_eq!(craft_ui::testing::focus::egui_focused(&frames.ctx), Some(app), "the app's button has the focus"); + frames.frame(vec![key_press(Key::ArrowLeft, Modifiers::NONE), key_release(Key::ArrowLeft, Modifiers::NONE)], &mut show); + frames.frame(Vec::new(), &mut show); + let focused = craft_ui::testing::focus::egui_focused(&frames.ctx); + assert!(focused == Some(app) || focused == Some(button.get()), "the arrow never reaches the disabled editor {:?}: {focused:?}", editor.get()); }