ui-contract, ui-test-kit: a press egui gives a component's text widget (a focused field's) is no press on its container's own surface, so a second click into a focused field in a dock group no longer hands the focus to the panel's tab, whose Escape and Enter then never reached the window; test kit Windows::root_env sends an app's root values

This commit is contained in:
bflatastic
2026-11-24 02:03:20 +07:00
parent 5353053e5a
commit 3a90546e2a
4 changed files with 100 additions and 9 deletions
@@ -75,7 +75,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, and the press lies in the
/// (`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
/// (`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
@@ -90,11 +91,11 @@ impl BodyHooks<'_> {
areas::layer_at(&ctx, pos) == Some(layer) && rect.contains(at)
};
let surface = if sensed {
pass.frame
.as_ref()
.and_then(|frame| frame.press)
.filter(|press| press.button == PointerButton::Primary)
.filter(|press| inside(press.origin) && pointer::press_winner(&ctx, press.origin, pass.press_targets).is_none())
pass.frame.as_ref().and_then(|frame| frame.press).filter(|press| press.button == PointerButton::Primary).filter(|press| {
inside(press.origin)
&& pointer::press_winner(&ctx, press.origin, pass.press_targets).is_none()
&& !pointer::text_takes_press(&ctx, pass.press_targets)
})
} else {
None
};
@@ -1100,6 +1100,14 @@ fn registered_in(layers: &LayerRecord, widget: Id) -> bool {
layers.values().any(|roots| roots.values().any(|root| root.widgets.contains(&widget)))
}
/// Whether egui gives the latest press to a component's text widget (a focused field's), as `targets` name its
/// widgets: the press is that text's, though it goes to no root and has no winner (`find_under`).
pub(crate) fn text_takes_press(ctx: &Context, targets: PressTargets) -> bool {
PerViewport::<Shapes>::with(ctx, pass::current_viewport(ctx), |record| {
targets.click.is_some_and(|widget| !record.last_grounds.contains(&widget) && record.last_texts.contains_key(&widget))
})
}
/// Whether the egui widget `widget`, one of the latest press's targets, is one no component registered or made for its
/// text, and no layer shows beneath everything on it, in the pass the press was hit-tested against: a raw egui widget.
pub(crate) fn foreign_target(ctx: &Context, widget: Id) -> bool {
+77 -1
View File
@@ -2,6 +2,7 @@
//! docking the whole window by dragging its title row. A context menu opened in a palette places its popup window as
//! it places the menu in the main window.
use craft_behavior::Intent;
use craft_behavior::dock::{Action, Column, DockEdge, Floating, Group, Layout, Node, Permissions, Workspace};
use craft_behavior::placement::Placement;
use craft_host::target::{Backend, Caps, Os, ShellInfo};
@@ -14,12 +15,15 @@ use craft_ui::menu::{Menu, MenuItem};
use craft_ui::patterns::dock::DockArea;
use craft_ui::popover::DropVeil;
use craft_ui::runtime::InstallSpec;
use craft_ui::runtime::drag;
use craft_ui::runtime::env::RootEnv;
use craft_ui::runtime::{drag, keys};
use craft_ui::testing::access::AccessTree;
use craft_ui::testing::focus::key_release;
use craft_ui::testing::input::key_press;
use craft_ui::testing::pointer::{moved, press, release, secondary};
use craft_ui::testing::trial;
use craft_ui::testing::windows::{FakeSystem, HeldButtons};
use craft_ui::text::NumberField;
use craft_ui_test_kit::windows::{AppKitWindow, FilesOver, Out, Windows};
use egui::{Event, HoveredFile, Key, Modifiers, Pos2, Rect, Ui, Vec2, ViewportBuilder, ViewportCommand, ViewportEvent, ViewportId, ViewportInfo};
use std::sync::Arc;
@@ -1382,6 +1386,78 @@ fn a_file_dropped_on_a_native_float_reaches_window_files_once() {
assert_eq!(reached(true), 0, "a file a target in the window took does not");
}
/// A palette whose panel holds an Opacity field, then reads the keys its OS window's pass left, as an app passes a
/// panel window's leftover Enter and Escape on to its main window: how many of each it read.
#[derive(Default)]
struct FieldPalette {
opacity: Option<f64>,
submits: usize,
cancels: usize,
}
impl DockPanels<u32> for FieldPalette {
fn body(&mut self, _: &u32, ui: &mut Ui, _: &mut BodyCx<'_>) {
NumberField::new("Opacity", Bind::bound(&mut self.opacity)).show(ui);
if ui.ctx().viewport_id() != ViewportId::ROOT {
self.submits += usize::from(keys::with_window_intents(ui.ctx(), false, |window| window.take(Intent::Submit)));
self.cancels += usize::from(keys::with_window_intents(ui.ctx(), false, |window| window.take(Intent::Cancel)));
}
}
}
/// Enter and Escape in a field of a native palette are the field's, with `RootEnv::escape_leaves_fields` off or on:
/// neither reaches the palette window's leftover keys. A press on the field once it has the focus, which egui gives the
/// field's text widget, is no press on the group's empty body, which would hand the focus to the panel's tab. Once
/// Escape has taken the focus from the field, Enter is the window's.
#[trial]
fn enter_and_escape_in_a_native_palettes_field_stay_the_fields() {
for leaves in [false, true] {
let windows = Windows::new();
windows.root_info(info(true, ROOT_AT));
windows.child_info(info(true, PALETTE_AT));
let mut root = RootEnv::new();
root.escape_leaves_fields = leaves;
windows.root_env(root);
let mut workspace = Arc::new(Workspace::new(Layout::new(None, vec![float()])));
let mut palette = FieldPalette { opacity: Some(50.0), ..FieldPalette::default() };
let mut frame = |palette: &mut FieldPalette, child: Vec<Event>| {
windows.frame(vec![], child, |ui| {
let _ =
DockArea::new("Native dock", Bind::bound(&mut workspace), &Specs::with(Permissions::ALL), &mut *palette, DockConfig::default())
.show(ui);
})
};
let mut out = frame(&mut palette, vec![]);
for _ in 0..3 {
out = frame(&mut palette, vec![]);
}
let child = out.trees.iter().find(|(viewport, _)| *viewport != ViewportId::ROOT).expect("the palette has an OS window");
let tree = &child.1;
let field = tree
.keys()
.into_iter()
.find(|key| tree.name_of(*key) == Some("Opacity") && tree.role_of(*key) != Some(Role::LABEL))
.and_then(|key| tree.bounds_of(key))
.expect("the palette shows the field")
.center();
for key in [Key::Enter, Key::Escape] {
for events in [vec![moved(field), press(field)], vec![release(field)], vec![], vec![]] {
frame(&mut palette, events);
}
for events in [vec![key_press(key, Modifiers::NONE)], vec![key_release(key, Modifiers::NONE)], vec![], vec![]] {
frame(&mut palette, events);
}
assert_eq!((palette.submits, palette.cancels), (0, 0), "leaves {leaves}: {key:?} in the field is the field's");
}
if leaves {
for events in [vec![key_press(Key::Enter, Modifiers::NONE)], vec![key_release(Key::Enter, Modifiers::NONE)], vec![]] {
frame(&mut palette, events);
}
assert_eq!(palette.submits, 1, "an Enter after Escape left the field is the window's");
}
}
}
/// A palette float made in the app window.
fn float() -> Floating<u32> {
Floating::single(104, vec![1], 0, Rect::from_min_size(Pos2::new(40.0, 40.0), Vec2::new(300.0, 200.0)))
+8 -2
View File
@@ -264,6 +264,7 @@ pub struct Windows {
/// The installed context.
pub ctx: Context,
root_info: RefCell<ViewportInfo>,
root: RefCell<RootEnv>,
}
impl Default for Windows {
@@ -292,7 +293,7 @@ impl Windows {
COMMANDS.with(|kept| kept.borrow_mut().clear());
BUILDERS.with(|kept| kept.borrow_mut().clear());
NOW.with(|now| now.set(0.0));
Windows { ctx, root_info: RefCell::new(ViewportInfo::default()) }
Windows { ctx, root_info: RefCell::new(ViewportInfo::default()), root: RefCell::new(RootEnv::default()) }
}
/// The window facts child passes report from now on.
@@ -311,6 +312,11 @@ impl Windows {
*self.root_info.borrow_mut() = info;
}
/// The app's root values every frame gives from now on, the defaults until then.
pub fn root_env(&self, root: RootEnv) {
*self.root.borrow_mut() = root;
}
/// One frame: `root` events go to the root viewport, `child` events to the child's next pass. Returns the
/// window commands of the frame.
pub fn frame(&self, root: Vec<Event>, child: Vec<Event>, ui: impl FnMut(&mut Ui)) -> Out {
@@ -337,7 +343,7 @@ impl Windows {
root_dropped.into_iter().map(|name| Arc::new(DroppedPath(name.into())) as egui::DroppedFileHandle).collect();
CHILD_FILES.with(|files| *files.borrow_mut() = child_files);
TREES.with(|kept| kept.borrow_mut().clear());
set_input(&self.ctx, FrameInput::new(RootEnv::default()));
set_input(&self.ctx, FrameInput::new(self.root.borrow().clone()));
let mut input = RawInput {
screen_rect: Some(Rect::from_min_size(Pos2::ZERO, ROOT_SIZE)),
events: root,