mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
Merge pull request #3312 from dylanmtaylor/filmcraft/foreign-press
A press on an app's own egui widget in a container body is not a press on the container's surface
This commit is contained in:
2 files changed
+48
-5
No files matched your search
@@ -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::<SurfacePress>(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
|
||||
|
||||
@@ -206,3 +206,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");
|
||||
}
|
||||
Reference in new issue
Block a user