From b961098d056b6364ef47a4dd24780a3e5d4d73a4 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 23 Nov 2026 21:09:06 -0500 Subject: [PATCH] A press on an app's own egui widget in a container body is not a press on the container's surface BodyHooks::surface_press counted a press as on the surface when no shape won it, and find_under gives no winner exactly where egui's click target is a raw egui widget. A dock group then moved focus to its tab stop on a click on an app's text field or canvas in its body. The press now leaves the surface alone when its click target is such a widget (pointer::foreign_target), for every container that senses Sense::SURFACE. --- .../ui-contract/src/contract/body/presses.rs | 11 ++--- crates/ui-panels/tests/it/dock_group.rs | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/crates/ui-contract/src/contract/body/presses.rs b/crates/ui-contract/src/contract/body/presses.rs index 98efd50590..4924066366 100644 --- a/crates/ui-contract/src/contract/body/presses.rs +++ b/crates/ui-contract/src/contract/body/presses.rs @@ -40,9 +40,9 @@ impl BodyCx<'_> { } /// Whether the primary press of this pass went down on the container's own surface, for a root that senses - /// `Sense::SURFACE`: inside its rect, on its layer, where none of its children or parts takes the press. The - /// container decides what it means (a dock group focuses its panel, as Photoshop does); it is no click and moves - /// no focus by itself. + /// `Sense::SURFACE`: inside its rect, on its layer, where none of its children or parts and no app's own egui widget + /// in its body takes the press. The container decides what it means (a dock group focuses its panel, as Photoshop + /// does); it is no click and moves no focus by itself. pub fn surface_pressed(&self) -> bool { self.memory.recall::(sub(self.id, "surface_press")).pressed } @@ -76,8 +76,8 @@ impl BodyCx<'_> { impl BodyHooks<'_> { /// Records for the container's hooks whether this pass's primary press went down on its own surface /// (`BodyCx::surface_pressed`): no shape of last pass under the press takes it, nor a component's text widget - /// egui gives it to (a focused field's), and the press lies in the - /// container's rect on its layer; and whether that press is the second of a double click + /// egui gives it to (a focused field's), nor an app's own egui widget (a raw text field or canvas in the body), and + /// the press lies in the container's rect on its layer; and whether that press is the second of a double click /// (`BodyCx::surface_double_pressed`), read from the same press's click count; and whether this pass's primary /// release ends a press that went down there (`BodyCx::surface_clicked`). The press origin waits in the press slot /// until the release; Escape and a blur clear it, and so does any pass with no button held. @@ -95,6 +95,7 @@ impl BodyHooks<'_> { inside(press.origin) && pointer::press_winner(&ctx, press.origin, pass.press_targets).is_none() && !pointer::text_takes_press(&ctx, pass.press_targets) + && !pass.press_targets.click.is_some_and(|widget| pointer::foreign_target(&ctx, widget)) }) } else { None diff --git a/crates/ui-panels/tests/it/dock_group.rs b/crates/ui-panels/tests/it/dock_group.rs index 5533928f06..7852225d7f 100644 --- a/crates/ui-panels/tests/it/dock_group.rs +++ b/crates/ui-panels/tests/it/dock_group.rs @@ -183,3 +183,45 @@ fn a_tab_mark_change_repaints_the_tab_row() { let cleared = width(&mut fixture); assert!((cleared - plain).abs() < 0.5, "the tab loses its dot again: {plain} then {cleared}"); } + +// A click on an app's own egui widget in a group's body (a raw text field) is the widget's, not a press on the +// group's empty body: the group reads no surface press, so its stop does not take the focus, and the field keeps +// egui's focus and takes typing. A press on the body's empty room still focuses the group's panel. +#[trial] +fn a_click_on_an_app_field_in_the_body_keeps_its_focus() { + use std::cell::RefCell; + let text = Rc::new(RefCell::new(String::new())); + let field = Rc::new(Cell::new(None::<(egui::Id, Rect)>)); + let surface = Rc::new(Cell::new(false)); + let (typed, shown, pressed) = (Rc::clone(&text), Rc::clone(&field), Rc::clone(&surface)); + let panels = vec![PanelTitle::new(PanelKey::new(1), "Color")]; + let mut fixture = Fixture::new(move |ui| { + let group = DockGroup::new(1usize, Titles::from_content(panels.clone()), panels[0].key); + let (typed, shown, pressed) = (Rc::clone(&typed), Rc::clone(&shown), Rc::clone(&pressed)); + group.show_at(ui, Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(300.0, 240.0)), move |ui, cx| { + pressed.set(pressed.get() || cx.surface_pressed()); + let response = egui::TextEdit::singleline(&mut *typed.borrow_mut()).desired_width(120.0).show(ui).response; + shown.set(Some((response.id, response.rect))); + }); + }); + fixture.settle(); + let Some((field_id, field_rect)) = field.get() else { panic!("the body shows the app's field") }; + let empty = Pos2::new(field_rect.center().x, field_rect.bottom() + 80.0); + + // Typing a character at a time with a click before each. The clicks are a second apart, so egui sees no double + // click (which would select the word). + for (clicks, typed) in ["a", "1"].into_iter().enumerate() { + fixture.advance(1.0).click_at(field_rect.center()).settle(); + assert!(!surface.get(), "click {clicks} on the field is no press on the group's empty body"); + assert_eq!(fixture.focused(), Some(field_id), "click {clicks} leaves egui's focus on the app's field"); + fixture.type_text(typed).settle(); + } + let on_field = fixture.focused_stop(); + assert!(text.borrow().contains("a1"), "the field takes the typing after each click: {:?}", text.borrow()); + + fixture.click_at(empty).settle(); + let group_stop = fixture.focused_stop(); + assert!(surface.get(), "a press on the empty body is the group's surface press"); + assert!(group_stop.is_some() && fixture.focused() != Some(field_id), "a press on the empty body focuses the group's panel"); + assert_ne!(on_field, group_stop, "the clicks on the field left the group's stop unfocused"); +}