From 7f5d0a82035378782c2c9ee6c29e21bc9d483f75 Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Sun, 22 Nov 2026 14:22:34 +0100 Subject: [PATCH 1/2] Close a context menu after Escape cancels its held child press --- .../src/contract/overlay/dismissal.rs | 15 ++- crates/ui-contract/src/primitives/sensing.rs | 23 +++- crates/ui-contract/src/runtime/overlay.rs | 5 + .../tests/it/menu/context_capture.rs | 89 ++++++++++++++ .../tests/it/menu/context_capture_guards.rs | 109 ++++++++++++++++++ .../tests/it/menu/context_capture_nested.rs | 81 +++++++++++++ .../it/menu/context_capture_text_native.rs | 68 +++++++++++ crates/ui-widgets/tests/it/menu/mod.rs | 5 + 8 files changed, 393 insertions(+), 2 deletions(-) create mode 100644 crates/ui-widgets/tests/it/menu/context_capture.rs create mode 100644 crates/ui-widgets/tests/it/menu/context_capture_guards.rs create mode 100644 crates/ui-widgets/tests/it/menu/context_capture_nested.rs create mode 100644 crates/ui-widgets/tests/it/menu/context_capture_text_native.rs diff --git a/crates/ui-contract/src/contract/overlay/dismissal.rs b/crates/ui-contract/src/contract/overlay/dismissal.rs index e2c8379e3..811e8aebc 100644 --- a/crates/ui-contract/src/contract/overlay/dismissal.rs +++ b/crates/ui-contract/src/contract/overlay/dismissal.rs @@ -126,7 +126,20 @@ fn escape(ctx: &Context, layer: &Layer<'_>, scope: Option, inside: } // The focus may be in no record of the layer's host: the Escape is read from the events of the frame. crate::runtime::replay::note_input(ctx, crate::runtime::inputs::Input::Sent); - let step = keys::scope_escape(ctx, policy.cancel, pointer::press_captured(ctx), false)?; + // A Menu may close after its OWN child's captured press has already been cancelled by this Escape. The + // first Escape still goes only to that press: do not manufacture a component Cancel or relax another scope's + // capture priority. Require the current actual area and the arena's same-pass Escape outcome, not a latched + // native press target, old registration or merely a capture that has disappeared. + let cancelled_menu_press = policy.kind == craft_behavior::ScopeKind::Menu + && policy.cancel == CancelRule::Close + && layer.overlay.rank == Rank::Popup + && matches!(pointer::cancel_of_pass(ctx), Some(craft_behavior::PressCancel::Escape)) + && overlay::current_egui_layer(ctx, layer.id).is_some_and(|area| crate::primitives::sensing::escape_cancelled_in_layer(ctx, area)); + let step = if cancelled_menu_press { + craft_behavior::focus::EscapeStep::CloseScope + } else { + keys::scope_escape(ctx, policy.cancel, pointer::press_captured(ctx), false)? + }; matches!(step, craft_behavior::focus::EscapeStep::CloseScope).then_some(CloseReason::Escape) } diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index d9e8d7461..de09e0b65 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -237,6 +237,8 @@ impl Hovered { #[derive(Clone, Copy, Debug, Default)] pub(crate) struct Outcome { pass: u64, + /// The layer in which this outcome was actually resolved, even if an early result is later reused. + layer: Option, pub hover: Hovered, /// A shape of the arena lies under the pointer, on top or not. pub contains: bool, @@ -983,7 +985,7 @@ fn outcome_in(ctx: &Context, read: &PassRead, arena: ArenaId, layer: Layer, enab None if others_press => Hovered::default(), None => under, }; - let outcome = Outcome { pass: pass_nr, hover, contains, target: step.target, phase: step.phase, dragging, activated: step.activated, xf }; + let outcome = Outcome { pass: pass_nr, layer: Some(layer), hover, contains, target: step.target, phase: step.phase, dragging, activated: step.activated, xf }; state.outcome = Some(outcome); outcome }); @@ -1027,6 +1029,25 @@ fn any_press_open(ctx: &Context) -> bool { PerViewport::::with(ctx, pass::current_viewport(ctx), |arenas| arenas.by_root.values().any(|state| state.machine.target().is_some())) } +/// Whether an actually captured press in exactly `layer` already ended by Escape in this pass. This observes +/// the arena's existing outcome after its body ran; it neither delivers a key nor changes the press machine. +pub(crate) fn escape_cancelled_in_layer(ctx: &Context, layer: Layer) -> bool { + let viewport = pass::current_viewport(ctx); + let pass_nr = pass::pass_nr(ctx, viewport); + PerViewport::::with(ctx, viewport, |arenas| { + arenas.by_root.values().any(|state| { + state.pass == pass_nr + && state.machine.target().is_none() + && state.outcome.is_some_and(|outcome| { + outcome.pass == pass_nr + && outcome.layer == Some(layer) + && outcome.target.is_some() + && matches!(outcome.phase, Some(PressPhase::Cancel { reason: craft_behavior::PressCancel::Escape, .. })) + }) + }) + }) +} + /// The arena's outcome of the running pass, idle when the root has not run yet. pub(crate) fn read_outcome(ctx: &Context, arena: ArenaId) -> Outcome { let pass_nr = pass::pass_nr(ctx, pass::current_viewport(ctx)); diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index ea79ef4d5..48b456bf5 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -462,6 +462,11 @@ pub fn entry(ctx: &Context, id: Id) -> Option { with_pass(ctx, |pass| pass.last.iter().find(|entry| entry.id == id).cloned()) } +/// The actual egui layer registered for `id` in this pass, never an old or exiting entry or an OS-window placeholder. +pub(crate) fn current_egui_layer(ctx: &Context, id: Id) -> Option { + with_pass(ctx, |pass| pass.current.iter().find(|entry| entry.id == id && !entry.exiting && !entry.window).map(|entry| entry.layer)) +} + /// The side of its anchor the layer `id` was placed on in this pass, once its host placed it by a known size. pub(crate) fn placed_side(ctx: &Context, id: Id) -> Option { with_pass(ctx, |pass| pass.sides.iter().rev().find(|(layer, _)| *layer == id).map(|(_, side)| *side)) diff --git a/crates/ui-widgets/tests/it/menu/context_capture.rs b/crates/ui-widgets/tests/it/menu/context_capture.rs new file mode 100644 index 000000000..e1233edb0 --- /dev/null +++ b/crates/ui-widgets/tests/it/menu/context_capture.rs @@ -0,0 +1,89 @@ +//! Canonical SDK menu ownership is not assumed to fix captured-pointer Escape: exercise the actual public route. +use craft_behavior::MotionMode; +use craft_ui::choice::Checkbox; +use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; +use craft_ui::menu::Menu; +use craft_ui::runtime::overlay::CloseReason; +use craft_ui::testing::input::key_press; +use craft_ui::testing::{Fixture, pointer, trial}; +use egui::{Event, Id, Key, Modifiers, Rect}; +use std::cell::RefCell; +use std::rc::Rc; + +#[derive(Default)] +struct State { + header: Option, + owner: Option, + checkbox: Option, + checked: bool, + commits: usize, + closes: Vec, +} +fn open_from_real_header(fixture: &mut Fixture, state: &Rc>) { + let at = state.borrow().header.expect("real native header response").center(); + fixture.frame(vec![pointer::moved(at)]); + fixture.frame(vec![pointer::secondary(at, true)]); + fixture.frame(vec![pointer::secondary(at, false)]); + fixture.settle(); + assert!(state.borrow().checkbox.is_some(), "canonical SDK context menu has its genuine Checkbox child"); +} + +#[trial] +fn canonical_context_owner_closes_on_one_held_checkbox_escape_without_replay_and_recovers_fresh() { + let state = Rc::new(RefCell::new(State::default())); + let held = state.clone(); + let mut fixture = Fixture::new(move |ui| { + let mut state = held.borrow_mut(); + let live = !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()); + let header = ui.button("Real section header"); + if live { + state.header = Some(header.rect); + state.checkbox = None; + state.owner = None; + } + // This REPLACES the native popup owner; it is not an inline menu nested inside a native callback. + let output = Menu::context("Section options", &header).id_salt("real-header-menu").show(ui, |ui, _| { + let mut value = state.checked; + let shown = Checkbox::new(Bind::bound(&mut value)).label("Solo Mode").id_salt("solo").show(ui); + if !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()) { + state.checked = value; + state.commits += usize::from(shown.events.committed().is_some()); + state.checkbox = Some(shown.response.interact_rect); + state.owner = Some(shown.response.id); + } + }); + if live && let Some(why) = output.output.closed { + state.closes.push(why); + } + }); + fixture.motion(MotionMode::Off).settle(); + open_from_real_header(&mut fixture, &state); + let (owner, at) = { + let state = state.borrow(); + (state.owner.expect("published actual Checkbox response"), state.checkbox.expect("genuine child bounds").center()) + }; + fixture.frame(vec![pointer::moved(at), pointer::press(at)]); + let target = craft_ui::runtime::pointer::with_pass_pointer(fixture.ctx(), |pointer| pointer.press_targets.click); + assert_eq!(target, Some(owner), "the actual press winner is the genuine non-text Checkbox child"); + assert!(fixture.ctx().input(|input| input.pointer.primary_down()), "physical primary remains held"); + assert!(!state.borrow().checked); + assert_eq!(state.borrow().commits, 0); + // Exactly one physical Escape down and its keyup, while primary stays DOWN. No extra Escape or idle frame. + fixture.frame(vec![key_press(Key::Escape, Modifiers::NONE)]); + assert!(fixture.ctx().input(|input| input.pointer.primary_down())); + assert!(!state.borrow().checked); + assert_eq!(state.borrow().commits, 0); + fixture.frame(vec![Event::Key { + key: Key::Escape, physical_key: Some(Key::Escape), pressed: false, repeat: false, modifiers: Modifiers::NONE, + }]); + assert!(state.borrow().checkbox.is_none(), "the genuine SDK owner must close by the original keyup boundary"); + assert_eq!(state.borrow().closes, vec![CloseReason::Escape]); + fixture.frame(vec![pointer::release(at)]); + assert!(!state.borrow().checked, "the old held release cannot replay a toggle"); + assert_eq!(state.borrow().commits, 0); + open_from_real_header(&mut fixture, &state); + let fresh = state.borrow().checkbox.expect("fresh physical secondary click reopens the real owner").center(); + fixture.click_at(fresh); + assert!(state.borrow().checked, "fresh real input still works"); + assert_eq!(state.borrow().commits, 1, "one fresh release gives exactly one committed value"); +} diff --git a/crates/ui-widgets/tests/it/menu/context_capture_guards.rs b/crates/ui-widgets/tests/it/menu/context_capture_guards.rs new file mode 100644 index 000000000..3af754833 --- /dev/null +++ b/crates/ui-widgets/tests/it/menu/context_capture_guards.rs @@ -0,0 +1,109 @@ +//! Physical layer-host policy controls. The unchanged context_capture test remains the canonical public Menu oracle. +use craft_behavior::{CancelRule, MotionMode, Sense}; +use craft_ui::choice::Checkbox; +use craft_ui::contract::{Bind, Common, Component, Container, ContainerProps, Cx, Mount, NoValue, Outcome, Overlay, Props as _, Role, Show as _, ShowContainer as _, Trigger}; +use craft_ui::runtime::focus::{FocusPolicy, Initial}; +use craft_ui::runtime::overlay::CloseReason; +use craft_ui::style::StyleCx; +use craft_ui::testing::input::key_press; +use craft_ui::testing::{Fixture, pointer, trial}; +use craft_ui_test_kit::contract::PlainStyle; +use egui::{Event, Id, Key, Modifiers, Rect, Response, Vec2}; +use std::cell::RefCell; +use std::rc::Rc; + +// A test-only public container supplies policy variants the Menu builder intentionally keeps private. The shared +// host, real native opener and genuine Checkbox lifecycle are unmodified; no press or popup state is injected. +#[derive(craft_ui::contract::Component)] +#[component(container)] +struct PolicyLayer { + #[common] + common: Common, + #[container] + props: ContainerProps, + closed: Option, +} +struct PolicyOut { response: Response, closed: Option } +impl Outcome for PolicyOut { + type Value = NoValue; + fn response(&self) -> &Response { &self.response } + fn unshown(response: Response) -> Option { Some(Self { response, closed: None }) } +} +impl Component for PolicyLayer { + const ROLE: Role = Role::GROUP; + const STOPS: bool = false; + type Style = PlainStyle; + type Output = PolicyOut; + fn style(_: &StyleCx<'_>, _: PolicyLayerStyleKey) -> PlainStyle { PlainStyle::min(Vec2::new(210.0, 60.0)) } + fn sense(&self) -> Sense { Sense::empty() } + fn finish(self, cx: Cx<'_, Self>) -> PolicyOut { PolicyOut { response: cx.response.clone(), closed: self.closed } } +} +impl Container for PolicyLayer { + fn closed(&mut self, why: CloseReason) { self.closed.get_or_insert(why); } +} +#[derive(Default)] +struct State { header: Option, checkbox: Option, owner: Option, checked: bool, commits: usize, closes: Vec } +fn open(fixture: &mut Fixture, state: &Rc>) { + let at = state.borrow().header.expect("genuine native header").center(); + fixture.frame(vec![pointer::moved(at)]); + fixture.frame(vec![pointer::secondary(at, true)]); + fixture.frame(vec![pointer::secondary(at, false)]); + fixture.settle(); + assert!(state.borrow().checkbox.is_some(), "the actual opener displays the Checkbox in the policy layer"); +} +fn held_escape_guard(policy: FocusPolicy, modal_rank: bool, closes: bool) { + let state = Rc::new(RefCell::new(State::default())); + let held = state.clone(); + let mut fixture = Fixture::new(move |ui| { + let mut state = held.borrow_mut(); + let live = !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()); + let header = ui.button("Real policy header"); + if live { state.header = Some(header.rect); state.checkbox = None; state.owner = None; } + let overlay = if modal_rank { Overlay::dialog() } else { Overlay::menu() }; + let props = ContainerProps::new().mount(Mount::Layer(overlay.focus(policy).opened_by(&header, Trigger::Context))); + let output = PolicyLayer { common: Common::new(), props, closed: None }.id_salt("policy-layer").show(ui, |ui, _| { + let mut value = state.checked; + let shown = Checkbox::new(Bind::bound(&mut value)).label("Physical policy checkbox").id_salt("policy-checkbox").show(ui); + if !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()) { + state.checked = value; + state.commits += usize::from(shown.events.committed().is_some()); + state.checkbox = Some(shown.response.interact_rect); + state.owner = Some(shown.response.id); + } + }); + if live && let Some(why) = output.output.closed { state.closes.push(why); } + }); + fixture.motion(MotionMode::Off).settle(); + open(&mut fixture, &state); + let (owner, at) = { let state = state.borrow(); (state.owner.expect("actual response"), state.checkbox.expect("actual child").center()) }; + fixture.frame(vec![pointer::moved(at), pointer::press(at)]); + assert_eq!(craft_ui::runtime::pointer::with_pass_pointer(fixture.ctx(), |p| p.press_targets.click), Some(owner)); + assert!(fixture.ctx().input(|input| input.pointer.primary_down())); + assert!(!state.borrow().checked); + assert_eq!(state.borrow().commits, 0); + fixture.frame(vec![key_press(Key::Escape, Modifiers::NONE)]); + assert!(fixture.ctx().input(|input| input.pointer.primary_down())); + assert!(!state.borrow().checked); + assert_eq!(state.borrow().commits, 0); + fixture.frame(vec![Event::Key { key: Key::Escape, physical_key: Some(Key::Escape), pressed: false, repeat: false, modifiers: Modifiers::NONE }]); + assert_eq!(state.borrow().checkbox.is_none(), closes, "only a Close Menu may close after its own cancelled press"); + assert_eq!(state.borrow().closes, if closes { vec![CloseReason::Escape] } else { vec![] }); + fixture.frame(vec![pointer::release(at)]); + assert!(!state.borrow().checked, "the cancelled old release does not toggle even when the layer stays open"); + assert_eq!(state.borrow().commits, 0); + if closes { open(&mut fixture, &state); } + let fresh = state.borrow().checkbox.expect("the retained or freshly reopened body").center(); + fixture.click_at(fresh); + assert!(state.borrow().checked, "fresh physical input still works"); + assert_eq!(state.borrow().commits, 1); +} +#[trial] +fn close_menu_observes_its_own_completed_escape_cancel() { held_escape_guard(FocusPolicy::MENU.initial(Initial::Default), false, true); } +#[trial] +fn ignore_menu_cancels_the_child_but_does_not_close_or_replay() { held_escape_guard(FocusPolicy::MENU.initial(Initial::Default).cancel(CancelRule::Ignore), false, false); } +#[trial] +fn clear_then_close_menu_does_not_promote_capture_escape_to_close() { held_escape_guard(FocusPolicy::MENU.initial(Initial::Default).cancel(CancelRule::ClearThenClose), false, false); } +#[trial] +fn popup_policy_keeps_existing_capture_priority_and_recovers_fresh() { held_escape_guard(FocusPolicy::POPUP, false, false); } +#[trial] +fn dialog_policy_keeps_existing_capture_priority_and_recovers_fresh() { held_escape_guard(FocusPolicy::DIALOG, true, false); } diff --git a/crates/ui-widgets/tests/it/menu/context_capture_nested.rs b/crates/ui-widgets/tests/it/menu/context_capture_nested.rs new file mode 100644 index 000000000..ebd179bab --- /dev/null +++ b/crates/ui-widgets/tests/it/menu/context_capture_nested.rs @@ -0,0 +1,81 @@ +//! One cancelled press closes only its actual top Menu, not its parent Menu family. +use craft_behavior::MotionMode; +use craft_ui::choice::Checkbox; +use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; +use craft_ui::menu::Menu; +use craft_ui::runtime::overlay::CloseReason; +use craft_ui::testing::input::key_press; +use craft_ui::testing::{Fixture, pointer, trial}; +use egui::{Event, Id, Key, Modifiers, Rect}; +use std::cell::RefCell; +use std::rc::Rc; +#[derive(Default)] +struct State { + header: Option, nested_header: Option, checkbox: Option, owner: Option, + checked: bool, commits: usize, outer_closes: Vec, inner_closes: Vec, +} +fn secondary_open(fixture: &mut Fixture, at: egui::Pos2) { + fixture.frame(vec![pointer::moved(at)]); + fixture.frame(vec![pointer::secondary(at, true)]); + fixture.frame(vec![pointer::secondary(at, false)]); + fixture.settle(); +} +#[trial] +fn held_escape_closes_only_the_actual_top_menu_not_its_parent_and_recovers_fresh() { + let state = Rc::new(RefCell::new(State::default())); + let held = state.clone(); + let mut fixture = Fixture::new(move |ui| { + let mut state = held.borrow_mut(); + let live = !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()); + let header = ui.button("Real outer section header"); + if live { state.header = Some(header.rect); state.nested_header = None; state.checkbox = None; state.owner = None; } + let outer = Menu::context("Outer section options", &header).id_salt("outer-real-menu").show(ui, |ui, _| { + let nested = ui.button("Real inner section header"); + if !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()) { state.nested_header = Some(nested.rect); } + let inner = Menu::context("Inner section options", &nested).id_salt("inner-real-menu").show(ui, |ui, _| { + let mut value = state.checked; + let shown = Checkbox::new(Bind::bound(&mut value)).label("Nested genuine checkbox").id_salt("nested-solo").show(ui); + if !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()) { + state.checked = value; + state.commits += usize::from(shown.events.committed().is_some()); + state.checkbox = Some(shown.response.interact_rect); + state.owner = Some(shown.response.id); + } + }); + if !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()) && let Some(why) = inner.output.closed { + state.inner_closes.push(why); + } + }); + if live && let Some(why) = outer.output.closed { state.outer_closes.push(why); } + }); + fixture.motion(MotionMode::Off).settle(); + let header = state.borrow().header.expect("real outer opener").center(); + secondary_open(&mut fixture, header); + let inner_header = state.borrow().nested_header.expect("actual outer body").center(); + secondary_open(&mut fixture, inner_header); + let (owner, at) = { let state = state.borrow(); (state.owner.expect("actual inner Checkbox"), state.checkbox.expect("actual inner body").center()) }; + fixture.frame(vec![pointer::moved(at), pointer::press(at)]); + assert_eq!(craft_ui::runtime::pointer::with_pass_pointer(fixture.ctx(), |p| p.press_targets.click), Some(owner)); + assert!(fixture.ctx().input(|input| input.pointer.primary_down())); + assert!(!state.borrow().checked); + assert_eq!(state.borrow().commits, 0); + fixture.frame(vec![key_press(Key::Escape, Modifiers::NONE)]); + assert!(fixture.ctx().input(|input| input.pointer.primary_down())); + assert!(!state.borrow().checked); + assert_eq!(state.borrow().commits, 0); + fixture.frame(vec![Event::Key { key: Key::Escape, physical_key: Some(Key::Escape), pressed: false, repeat: false, modifiers: Modifiers::NONE }]); + assert!(state.borrow().checkbox.is_none(), "the actual top Menu closes by the same keyup boundary"); + assert!(state.borrow().nested_header.is_some(), "its distinct parent Menu remains open"); + assert!(state.borrow().outer_closes.is_empty(), "the cancelled inner press does not dismiss an unrelated parent owner"); + assert_eq!(state.borrow().inner_closes, vec![CloseReason::Escape]); + fixture.frame(vec![pointer::release(at)]); + assert!(!state.borrow().checked); + assert_eq!(state.borrow().commits, 0); + assert!(state.borrow().nested_header.is_some(), "the old release does not dismiss the parent"); + let again = state.borrow().nested_header.expect("parent retains the real inner opener").center(); + secondary_open(&mut fixture, again); + let fresh = state.borrow().checkbox.expect("fresh real reopen").center(); + fixture.click_at(fresh); + assert!(state.borrow().checked); + assert_eq!(state.borrow().commits, 1); +} diff --git a/crates/ui-widgets/tests/it/menu/context_capture_text_native.rs b/crates/ui-widgets/tests/it/menu/context_capture_text_native.rs new file mode 100644 index 000000000..5bd14f835 --- /dev/null +++ b/crates/ui-widgets/tests/it/menu/context_capture_text_native.rs @@ -0,0 +1,68 @@ +//! A native-owned Menu text press keeps draft Escape precedence; the separate SDK-arena Checkbox oracle is unchanged. +use craft_behavior::MotionMode; +use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; +use craft_ui::menu::Menu; +use craft_ui::text::TextField; +use craft_ui::testing::input::key_press; +use craft_ui::testing::{Fixture, pointer, trial}; +use egui::{Event, Id, Key, Modifiers, Rect}; +use std::cell::RefCell; +use std::rc::Rc; +struct State { header: Option, field: Option, root: Option, pressed: bool, value: String, commits: usize } +#[trial] +fn a_menu_native_text_press_preserves_draft_escape_without_owner_close_or_blur_commit() { + let state = Rc::new(RefCell::new(State { header: None, field: None, root: None, pressed: false, value: "seed".to_owned(), commits: 0 })); + let held = state.clone(); + let mut fixture = Fixture::new(move |ui| { + let mut state = held.borrow_mut(); + let live = !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()); + let header = ui.button("Real text editor header"); + if live { state.header = Some(header.rect); state.field = None; state.pressed = false; } + Menu::context("Editor options", &header).id_salt("real-text-menu").show(ui, |ui, _| { + let mut value = state.value.clone(); + let shown = TextField::new("Draft inside Menu", Bind::bound(&mut value)).id_salt("draft-field").show(ui); + if !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()) { + state.value = value; + state.field = Some(shown.response.interact_rect); + state.root = Some(shown.response.id); + // The SDK rewrites this flag from its actual arena phase, clearing native flags first. This is not + // pointer.press_targets, whose native target may remain latched after cancellation. + state.pressed = shown.response.is_pointer_button_down_on(); + state.commits += usize::from(shown.events.committed().is_some()); + } + }); + }); + fixture.motion(MotionMode::Off).settle(); + let header = state.borrow().header.expect("real header").center(); + fixture.frame(vec![pointer::moved(header)]); + fixture.frame(vec![pointer::secondary(header, true)]); + fixture.frame(vec![pointer::secondary(header, false)]); + fixture.settle(); + let field = state.borrow().field.expect("actual canonical Menu editor").center(); + fixture.click_at(field); + fixture.type_text("Q7"); + assert!(fixture.texts().iter().any(|run| run.text.contains("Q7")), "a real typed draft was painted"); + assert_eq!(state.borrow().commits, 0, "typing did not already commit by blur"); + let native = fixture.ctx().memory(|memory| memory.focused()).expect("real typing established the actual native text owner"); + assert_ne!(Some(native), state.borrow().root, "the native editor is distinct from its SDK component root"); + let at = state.borrow().field.expect("same live editor").center(); + fixture.frame(vec![pointer::moved(at), pointer::press(at)]); + let target = craft_ui::runtime::pointer::with_pass_pointer(fixture.ctx(), |pointer| pointer.press_targets.click); + assert_eq!(target, Some(native), "this fresh physical press actually selects the same native text owner that typed the draft"); + assert!(!state.borrow().pressed, "a registered native text press intentionally goes to no SDK root arena"); + assert!(fixture.ctx().input(|input| input.pointer.primary_down())); + fixture.frame(vec![key_press(Key::Escape, Modifiers::NONE)]); + assert!(fixture.ctx().input(|input| input.pointer.primary_down())); + assert_eq!(state.borrow().commits, 0); + fixture.frame(vec![Event::Key { key: Key::Escape, physical_key: Some(Key::Escape), pressed: false, repeat: false, modifiers: Modifiers::NONE }]); + assert!(state.borrow().field.is_some(), "the existing editor/draft capture precedence must not become a Menu close"); + assert_eq!(state.borrow().commits, 0, "no draft blur commit was manufactured"); + fixture.frame(vec![pointer::release(at)]); + assert_eq!(state.borrow().commits, 0, "the cancelled old release does not commit"); + fixture.type_text("Z8"); + assert!(fixture.texts().iter().any(|run| run.text.contains("Z8")), "fresh real editor input still works"); + fixture.frame(vec![key_press(Key::Enter, Modifiers::NONE)]); + fixture.frame(vec![Event::Key { key: Key::Enter, physical_key: Some(Key::Enter), pressed: false, repeat: false, modifiers: Modifiers::NONE }]); + assert_eq!(state.borrow().commits, 1, "only the fresh explicit Enter commits once"); + assert!(state.borrow().value.contains("Z8")); +} diff --git a/crates/ui-widgets/tests/it/menu/mod.rs b/crates/ui-widgets/tests/it/menu/mod.rs index 457121437..ca5d7bd8b 100644 --- a/crates/ui-widgets/tests/it/menu/mod.rs +++ b/crates/ui-widgets/tests/it/menu/mod.rs @@ -4,6 +4,11 @@ pub(crate) mod bar; pub(crate) mod closed; +pub(crate) mod context_capture; +pub(crate) mod context_capture_guards; +pub(crate) mod context_capture_nested; +pub(crate) mod context_capture_text_native; + pub(crate) mod companion; pub(crate) mod native; From 6b6b976f8f5485e8d61e5fb1c7717ea78e54bac4 Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Sun, 22 Nov 2026 15:04:46 +0100 Subject: [PATCH 2/2] Format held-child Escape regression controls --- crates/ui-contract/src/primitives/sensing.rs | 12 ++- .../tests/it/menu/context_capture.rs | 4 +- .../tests/it/menu/context_capture_guards.rs | 82 +++++++++++++++---- .../tests/it/menu/context_capture_nested.rs | 35 ++++++-- .../it/menu/context_capture_text_native.rs | 17 +++- 5 files changed, 118 insertions(+), 32 deletions(-) diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index de09e0b65..7be2e1e8a 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -985,7 +985,17 @@ fn outcome_in(ctx: &Context, read: &PassRead, arena: ArenaId, layer: Layer, enab None if others_press => Hovered::default(), None => under, }; - let outcome = Outcome { pass: pass_nr, layer: Some(layer), hover, contains, target: step.target, phase: step.phase, dragging, activated: step.activated, xf }; + let outcome = Outcome { + pass: pass_nr, + layer: Some(layer), + hover, + contains, + target: step.target, + phase: step.phase, + dragging, + activated: step.activated, + xf, + }; state.outcome = Some(outcome); outcome }); diff --git a/crates/ui-widgets/tests/it/menu/context_capture.rs b/crates/ui-widgets/tests/it/menu/context_capture.rs index e1233edb0..4824e732b 100644 --- a/crates/ui-widgets/tests/it/menu/context_capture.rs +++ b/crates/ui-widgets/tests/it/menu/context_capture.rs @@ -73,9 +73,7 @@ fn canonical_context_owner_closes_on_one_held_checkbox_escape_without_replay_and assert!(fixture.ctx().input(|input| input.pointer.primary_down())); assert!(!state.borrow().checked); assert_eq!(state.borrow().commits, 0); - fixture.frame(vec![Event::Key { - key: Key::Escape, physical_key: Some(Key::Escape), pressed: false, repeat: false, modifiers: Modifiers::NONE, - }]); + fixture.frame(vec![Event::Key { key: Key::Escape, physical_key: Some(Key::Escape), pressed: false, repeat: false, modifiers: Modifiers::NONE }]); assert!(state.borrow().checkbox.is_none(), "the genuine SDK owner must close by the original keyup boundary"); assert_eq!(state.borrow().closes, vec![CloseReason::Escape]); fixture.frame(vec![pointer::release(at)]); diff --git a/crates/ui-widgets/tests/it/menu/context_capture_guards.rs b/crates/ui-widgets/tests/it/menu/context_capture_guards.rs index 3af754833..c4e128072 100644 --- a/crates/ui-widgets/tests/it/menu/context_capture_guards.rs +++ b/crates/ui-widgets/tests/it/menu/context_capture_guards.rs @@ -1,7 +1,10 @@ //! Physical layer-host policy controls. The unchanged context_capture test remains the canonical public Menu oracle. use craft_behavior::{CancelRule, MotionMode, Sense}; use craft_ui::choice::Checkbox; -use craft_ui::contract::{Bind, Common, Component, Container, ContainerProps, Cx, Mount, NoValue, Outcome, Overlay, Props as _, Role, Show as _, ShowContainer as _, Trigger}; +use craft_ui::contract::{ + Bind, Common, Component, Container, ContainerProps, Cx, Mount, NoValue, Outcome, Overlay, Props as _, Role, Show as _, ShowContainer as _, + Trigger, +}; use craft_ui::runtime::focus::{FocusPolicy, Initial}; use craft_ui::runtime::overlay::CloseReason; use craft_ui::style::StyleCx; @@ -23,26 +26,48 @@ struct PolicyLayer { props: ContainerProps, closed: Option, } -struct PolicyOut { response: Response, closed: Option } +struct PolicyOut { + response: Response, + closed: Option, +} impl Outcome for PolicyOut { type Value = NoValue; - fn response(&self) -> &Response { &self.response } - fn unshown(response: Response) -> Option { Some(Self { response, closed: None }) } + fn response(&self) -> &Response { + &self.response + } + fn unshown(response: Response) -> Option { + Some(Self { response, closed: None }) + } } impl Component for PolicyLayer { const ROLE: Role = Role::GROUP; const STOPS: bool = false; type Style = PlainStyle; type Output = PolicyOut; - fn style(_: &StyleCx<'_>, _: PolicyLayerStyleKey) -> PlainStyle { PlainStyle::min(Vec2::new(210.0, 60.0)) } - fn sense(&self) -> Sense { Sense::empty() } - fn finish(self, cx: Cx<'_, Self>) -> PolicyOut { PolicyOut { response: cx.response.clone(), closed: self.closed } } + fn style(_: &StyleCx<'_>, _: PolicyLayerStyleKey) -> PlainStyle { + PlainStyle::min(Vec2::new(210.0, 60.0)) + } + fn sense(&self) -> Sense { + Sense::empty() + } + fn finish(self, cx: Cx<'_, Self>) -> PolicyOut { + PolicyOut { response: cx.response.clone(), closed: self.closed } + } } impl Container for PolicyLayer { - fn closed(&mut self, why: CloseReason) { self.closed.get_or_insert(why); } + fn closed(&mut self, why: CloseReason) { + self.closed.get_or_insert(why); + } } #[derive(Default)] -struct State { header: Option, checkbox: Option, owner: Option, checked: bool, commits: usize, closes: Vec } +struct State { + header: Option, + checkbox: Option, + owner: Option, + checked: bool, + commits: usize, + closes: Vec, +} fn open(fixture: &mut Fixture, state: &Rc>) { let at = state.borrow().header.expect("genuine native header").center(); fixture.frame(vec![pointer::moved(at)]); @@ -58,7 +83,11 @@ fn held_escape_guard(policy: FocusPolicy, modal_rank: bool, closes: bool) { let mut state = held.borrow_mut(); let live = !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()); let header = ui.button("Real policy header"); - if live { state.header = Some(header.rect); state.checkbox = None; state.owner = None; } + if live { + state.header = Some(header.rect); + state.checkbox = None; + state.owner = None; + } let overlay = if modal_rank { Overlay::dialog() } else { Overlay::menu() }; let props = ContainerProps::new().mount(Mount::Layer(overlay.focus(policy).opened_by(&header, Trigger::Context))); let output = PolicyLayer { common: Common::new(), props, closed: None }.id_salt("policy-layer").show(ui, |ui, _| { @@ -71,11 +100,16 @@ fn held_escape_guard(policy: FocusPolicy, modal_rank: bool, closes: bool) { state.owner = Some(shown.response.id); } }); - if live && let Some(why) = output.output.closed { state.closes.push(why); } + if live && let Some(why) = output.output.closed { + state.closes.push(why); + } }); fixture.motion(MotionMode::Off).settle(); open(&mut fixture, &state); - let (owner, at) = { let state = state.borrow(); (state.owner.expect("actual response"), state.checkbox.expect("actual child").center()) }; + let (owner, at) = { + let state = state.borrow(); + (state.owner.expect("actual response"), state.checkbox.expect("actual child").center()) + }; fixture.frame(vec![pointer::moved(at), pointer::press(at)]); assert_eq!(craft_ui::runtime::pointer::with_pass_pointer(fixture.ctx(), |p| p.press_targets.click), Some(owner)); assert!(fixture.ctx().input(|input| input.pointer.primary_down())); @@ -91,19 +125,31 @@ fn held_escape_guard(policy: FocusPolicy, modal_rank: bool, closes: bool) { fixture.frame(vec![pointer::release(at)]); assert!(!state.borrow().checked, "the cancelled old release does not toggle even when the layer stays open"); assert_eq!(state.borrow().commits, 0); - if closes { open(&mut fixture, &state); } + if closes { + open(&mut fixture, &state); + } let fresh = state.borrow().checkbox.expect("the retained or freshly reopened body").center(); fixture.click_at(fresh); assert!(state.borrow().checked, "fresh physical input still works"); assert_eq!(state.borrow().commits, 1); } #[trial] -fn close_menu_observes_its_own_completed_escape_cancel() { held_escape_guard(FocusPolicy::MENU.initial(Initial::Default), false, true); } +fn close_menu_observes_its_own_completed_escape_cancel() { + held_escape_guard(FocusPolicy::MENU.initial(Initial::Default), false, true); +} #[trial] -fn ignore_menu_cancels_the_child_but_does_not_close_or_replay() { held_escape_guard(FocusPolicy::MENU.initial(Initial::Default).cancel(CancelRule::Ignore), false, false); } +fn ignore_menu_cancels_the_child_but_does_not_close_or_replay() { + held_escape_guard(FocusPolicy::MENU.initial(Initial::Default).cancel(CancelRule::Ignore), false, false); +} #[trial] -fn clear_then_close_menu_does_not_promote_capture_escape_to_close() { held_escape_guard(FocusPolicy::MENU.initial(Initial::Default).cancel(CancelRule::ClearThenClose), false, false); } +fn clear_then_close_menu_does_not_promote_capture_escape_to_close() { + held_escape_guard(FocusPolicy::MENU.initial(Initial::Default).cancel(CancelRule::ClearThenClose), false, false); +} #[trial] -fn popup_policy_keeps_existing_capture_priority_and_recovers_fresh() { held_escape_guard(FocusPolicy::POPUP, false, false); } +fn popup_policy_keeps_existing_capture_priority_and_recovers_fresh() { + held_escape_guard(FocusPolicy::POPUP, false, false); +} #[trial] -fn dialog_policy_keeps_existing_capture_priority_and_recovers_fresh() { held_escape_guard(FocusPolicy::DIALOG, true, false); } +fn dialog_policy_keeps_existing_capture_priority_and_recovers_fresh() { + held_escape_guard(FocusPolicy::DIALOG, true, false); +} diff --git a/crates/ui-widgets/tests/it/menu/context_capture_nested.rs b/crates/ui-widgets/tests/it/menu/context_capture_nested.rs index ebd179bab..c205b4cb7 100644 --- a/crates/ui-widgets/tests/it/menu/context_capture_nested.rs +++ b/crates/ui-widgets/tests/it/menu/context_capture_nested.rs @@ -11,8 +11,14 @@ use std::cell::RefCell; use std::rc::Rc; #[derive(Default)] struct State { - header: Option, nested_header: Option, checkbox: Option, owner: Option, - checked: bool, commits: usize, outer_closes: Vec, inner_closes: Vec, + header: Option, + nested_header: Option, + checkbox: Option, + owner: Option, + checked: bool, + commits: usize, + outer_closes: Vec, + inner_closes: Vec, } fn secondary_open(fixture: &mut Fixture, at: egui::Pos2) { fixture.frame(vec![pointer::moved(at)]); @@ -28,10 +34,17 @@ fn held_escape_closes_only_the_actual_top_menu_not_its_parent_and_recovers_fresh let mut state = held.borrow_mut(); let live = !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()); let header = ui.button("Real outer section header"); - if live { state.header = Some(header.rect); state.nested_header = None; state.checkbox = None; state.owner = None; } + if live { + state.header = Some(header.rect); + state.nested_header = None; + state.checkbox = None; + state.owner = None; + } let outer = Menu::context("Outer section options", &header).id_salt("outer-real-menu").show(ui, |ui, _| { let nested = ui.button("Real inner section header"); - if !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()) { state.nested_header = Some(nested.rect); } + if !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()) { + state.nested_header = Some(nested.rect); + } let inner = Menu::context("Inner section options", &nested).id_salt("inner-real-menu").show(ui, |ui, _| { let mut value = state.checked; let shown = Checkbox::new(Bind::bound(&mut value)).label("Nested genuine checkbox").id_salt("nested-solo").show(ui); @@ -42,18 +55,26 @@ fn held_escape_closes_only_the_actual_top_menu_not_its_parent_and_recovers_fresh state.owner = Some(shown.response.id); } }); - if !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()) && let Some(why) = inner.output.closed { + if !ui.is_sizing_pass() + && !craft_ui::runtime::measure::active(ui.ctx()) + && let Some(why) = inner.output.closed + { state.inner_closes.push(why); } }); - if live && let Some(why) = outer.output.closed { state.outer_closes.push(why); } + if live && let Some(why) = outer.output.closed { + state.outer_closes.push(why); + } }); fixture.motion(MotionMode::Off).settle(); let header = state.borrow().header.expect("real outer opener").center(); secondary_open(&mut fixture, header); let inner_header = state.borrow().nested_header.expect("actual outer body").center(); secondary_open(&mut fixture, inner_header); - let (owner, at) = { let state = state.borrow(); (state.owner.expect("actual inner Checkbox"), state.checkbox.expect("actual inner body").center()) }; + let (owner, at) = { + let state = state.borrow(); + (state.owner.expect("actual inner Checkbox"), state.checkbox.expect("actual inner body").center()) + }; fixture.frame(vec![pointer::moved(at), pointer::press(at)]); assert_eq!(craft_ui::runtime::pointer::with_pass_pointer(fixture.ctx(), |p| p.press_targets.click), Some(owner)); assert!(fixture.ctx().input(|input| input.pointer.primary_down())); diff --git a/crates/ui-widgets/tests/it/menu/context_capture_text_native.rs b/crates/ui-widgets/tests/it/menu/context_capture_text_native.rs index 5bd14f835..a2fa8af94 100644 --- a/crates/ui-widgets/tests/it/menu/context_capture_text_native.rs +++ b/crates/ui-widgets/tests/it/menu/context_capture_text_native.rs @@ -2,13 +2,20 @@ use craft_behavior::MotionMode; use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; use craft_ui::menu::Menu; -use craft_ui::text::TextField; use craft_ui::testing::input::key_press; use craft_ui::testing::{Fixture, pointer, trial}; +use craft_ui::text::TextField; use egui::{Event, Id, Key, Modifiers, Rect}; use std::cell::RefCell; use std::rc::Rc; -struct State { header: Option, field: Option, root: Option, pressed: bool, value: String, commits: usize } +struct State { + header: Option, + field: Option, + root: Option, + pressed: bool, + value: String, + commits: usize, +} #[trial] fn a_menu_native_text_press_preserves_draft_escape_without_owner_close_or_blur_commit() { let state = Rc::new(RefCell::new(State { header: None, field: None, root: None, pressed: false, value: "seed".to_owned(), commits: 0 })); @@ -17,7 +24,11 @@ fn a_menu_native_text_press_preserves_draft_escape_without_owner_close_or_blur_c let mut state = held.borrow_mut(); let live = !ui.is_sizing_pass() && !craft_ui::runtime::measure::active(ui.ctx()); let header = ui.button("Real text editor header"); - if live { state.header = Some(header.rect); state.field = None; state.pressed = false; } + if live { + state.header = Some(header.rect); + state.field = None; + state.pressed = false; + } Menu::context("Editor options", &header).id_salt("real-text-menu").show(ui, |ui, _| { let mut value = state.value.clone(); let shown = TextField::new("Draft inside Menu", Bind::bound(&mut value)).id_salt("draft-field").show(ui);