mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
Merge pull request #3058 from storytold/fix/menu-escape-held-child
Context menus close after Escape cancels a held child press
This commit is contained in:
@@ -129,7 +129,20 @@ fn escape(ctx: &Context, layer: &Layer<'_>, scope: Option<FocusPolicy>, 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)
|
||||
}
|
||||
|
||||
|
||||
@@ -244,6 +244,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<Layer>,
|
||||
pub hover: Hovered,
|
||||
/// A shape of the arena lies under the pointer, on top or not.
|
||||
pub contains: bool,
|
||||
@@ -1110,8 +1112,18 @@ 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, aside, xf };
|
||||
let outcome = Outcome {
|
||||
pass: pass_nr,
|
||||
layer: Some(layer),
|
||||
hover,
|
||||
contains,
|
||||
target: step.target,
|
||||
phase: step.phase,
|
||||
dragging,
|
||||
activated: step.activated,
|
||||
aside,
|
||||
xf,
|
||||
};
|
||||
state.outcome = Some(outcome);
|
||||
outcome
|
||||
});
|
||||
@@ -1164,6 +1176,25 @@ fn note_pressed_parts(ctx: &Context) {
|
||||
crate::runtime::pointer::set_pressed_parts(ctx, parts);
|
||||
}
|
||||
|
||||
/// 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::<Arenas>::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));
|
||||
|
||||
@@ -501,6 +501,11 @@ pub fn entry(ctx: &Context, id: Id) -> Option<OpenEntry> {
|
||||
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<Layer> {
|
||||
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<Side> {
|
||||
with_pass(ctx, |pass| pass.sides.iter().rev().find(|(layer, _)| *layer == id).map(|(_, side)| *side))
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
//! 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<Rect>,
|
||||
owner: Option<Id>,
|
||||
checkbox: Option<Rect>,
|
||||
checked: bool,
|
||||
commits: usize,
|
||||
closes: Vec<CloseReason>,
|
||||
}
|
||||
fn open_from_real_header(fixture: &mut Fixture, state: &Rc<RefCell<State>>) {
|
||||
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");
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
//! 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<CloseReason>,
|
||||
}
|
||||
struct PolicyOut {
|
||||
response: Response,
|
||||
closed: Option<CloseReason>,
|
||||
}
|
||||
impl Outcome for PolicyOut {
|
||||
type Value = NoValue;
|
||||
fn response(&self) -> &Response {
|
||||
&self.response
|
||||
}
|
||||
fn unshown(response: Response) -> Option<Self> {
|
||||
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<Rect>,
|
||||
checkbox: Option<Rect>,
|
||||
owner: Option<Id>,
|
||||
checked: bool,
|
||||
commits: usize,
|
||||
closes: Vec<CloseReason>,
|
||||
}
|
||||
fn open(fixture: &mut Fixture, state: &Rc<RefCell<State>>) {
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
//! 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<Rect>,
|
||||
nested_header: Option<Rect>,
|
||||
checkbox: Option<Rect>,
|
||||
owner: Option<Id>,
|
||||
checked: bool,
|
||||
commits: usize,
|
||||
outer_closes: Vec<CloseReason>,
|
||||
inner_closes: Vec<CloseReason>,
|
||||
}
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
//! 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::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<Rect>,
|
||||
field: Option<Rect>,
|
||||
root: Option<Id>,
|
||||
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"));
|
||||
}
|
||||
@@ -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 icon_row;
|
||||
|
||||
Reference in New Issue
Block a user