ui-contract: with RootEnv::escape_leaves_fields, Escape in a field of a dialog that Escape closes stays the dialog's with nothing typed too: the field took it to give up the focus and the dialog stayed open

This commit is contained in:
bflatastic
2026-11-24 00:57:52 +07:00
parent 4a6ea8a984
commit 662ff9264a
2 changed files with 36 additions and 21 deletions
@@ -481,19 +481,20 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut
// a list open on it (a search box's results) then gets the Escape that finds the field empty and closes.
// A draft in a dialog whose Escape closes it does not take the Escape: the one Escape cancels the dialog and
// the typing with it (Word, and the old apps). Outside dialogs Escape reverts the draft first.
let dialog_escape = focused_before
&& state.draft.is_some()
let closes_dialog = focused_before
&& state.preedit.is_none()
&& !input.options.clear_on_escape
&& input.intents.untaken(&Intent::Cancel)
&& crate::runtime::focus::escape_closes_dialog(ctx);
let dialog_escape = closes_dialog && state.draft.is_some();
if dialog_escape {
// The draft ends as for Escape (its typing is dropped), and the Escape stays for the dialog.
state.end = Some(End::Escape);
}
let draft_takes = state.draft.is_some() && !dialog_escape;
// With `RootEnv::escape_leaves_fields` a field takes Escape with nothing to revert too, and gives up the focus.
let leaves = !input.options.clear_on_escape && !dialog_escape && crate::runtime::env::Env::of(input.ui).escape_leaves_fields;
// With `RootEnv::escape_leaves_fields` a field takes Escape with nothing to revert too, and gives up the focus;
// in a dialog that Escape closes, the Escape stays the dialog's.
let leaves = !input.options.clear_on_escape && !closes_dialog && crate::runtime::env::Env::of(input.ui).escape_leaves_fields;
let takes_escape =
if input.options.clear_on_escape { !base.is_empty() || state.preedit.is_some() } else { draft_takes || state.preedit.is_some() || leaves };
let list_first = input.list_open && !input.options.clear_on_escape;
@@ -588,26 +588,40 @@ fn enter_on_a_focused_cancel_cancels_the_form() {
}
// One Escape in a number field holding typed text cancels the dialog, as in Word and the old apps: the draft does
// not take the Escape first, and the dialog answers no OK, so the typing goes with it.
// not take the Escape first, and the dialog answers no OK, so the typing goes with it. A field with nothing typed
// leaves the Escape to the dialog too, also where Escape otherwise takes a field's focus (`escape_leaves_fields`).
#[trial]
fn one_escape_over_a_typed_draft_cancels_the_dialog() {
let seen = Rc::new(Cell::new(None::<(CloseReason, bool)>));
let out = seen.clone();
let mut fixture = Fixture::new(move |ui| {
let dialog = ParamDialog::new("blur", "Blur", Param::parse_list(SPEC)).show(ui);
if let Some(closed) = dialog.closed {
out.set(Some((closed, dialog.result.is_some())));
for leaves in [false, true] {
for typed in [true, false] {
let seen = Rc::new(Cell::new(None::<(CloseReason, bool)>));
let out = seen.clone();
let mut fixture = Fixture::new(move |ui| {
let dialog = ParamDialog::new("blur", "Blur", Param::parse_list(SPEC)).show(ui);
if let Some(closed) = dialog.closed {
out.set(Some((closed, dialog.result.is_some())));
}
});
let mut root = craft_ui::runtime::env::RootEnv::new();
root.escape_leaves_fields = leaves;
fixture.root(root).settle();
let field = fixture.by_role(Role::SPIN_BUTTON).into_iter().next().expect("the radius row has a number field");
fixture.click(&field).settle();
if typed {
fixture.key(Key::A, Modifiers::COMMAND);
fixture.type_text("55").settle();
let draft =
fixture.by_role(Role::SPIN_BUTTON).into_iter().next().and_then(|field| fixture.tree().value_of(field.node).map(str::to_owned));
assert_eq!(draft.as_deref(), Some("55"), "the field shows the typing as a draft");
}
fixture.key(Key::Escape, Modifiers::NONE).settle();
assert!(
matches!(seen.get(), Some((CloseReason::Escape, false))),
"leaves {leaves}, typed {typed}: one Escape cancels the dialog: {:?}",
seen.get()
);
}
});
fixture.settle();
let field = fixture.by_role(Role::SPIN_BUTTON).into_iter().next().expect("the radius row has a number field");
fixture.click(&field).settle();
fixture.key(Key::A, Modifiers::COMMAND);
fixture.type_text("55").settle();
let typed = fixture.by_role(Role::SPIN_BUTTON).into_iter().next().and_then(|field| fixture.tree().value_of(field.node).map(str::to_owned));
assert_eq!(typed.as_deref(), Some("55"), "the field shows the typing as a draft");
fixture.key(Key::Escape, Modifiers::NONE).settle();
assert!(matches!(seen.get(), Some((CloseReason::Escape, false))), "one Escape cancels the dialog: {:?}", seen.get());
}
}
// Alt turns Cancel into Reset, which restores the opening values with the `reset_to` ones over them: a key `reset_to`