From 3ed4e4ac1c581f4ac7d05ad76c7cfc01c1aab0e5 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Thu, 19 Nov 2026 16:12:12 +0000 Subject: [PATCH] fwstub-1: one Escape cancels a dialog while its focused field holds a draft: the draft ends as for Escape and the Escape goes to the dialog (focus::escape_closes_dialog; the gesture step and the text adapter leave it untaken); fields outside dialogs still revert first; trial Co-Authored-By: Claude Opus 5.5 --- .../ui-contract/src/contract/steps/update.rs | 11 ++++++++- .../src/primitives/text_adapter.rs | 15 +++++++++++- crates/ui-contract/src/runtime/focus.rs | 23 +++++++++++++++++++ .../tests/it/form_dialogs/dialog.rs | 23 +++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/crates/ui-contract/src/contract/steps/update.rs b/crates/ui-contract/src/contract/steps/update.rs index 372c9155d..71ed35afb 100644 --- a/crates/ui-contract/src/contract/steps/update.rs +++ b/crates/ui-contract/src/contract/steps/update.rs @@ -49,7 +49,16 @@ pub(crate) fn gesture_gate(comp: &C, cx: &mut Cx<'_, C>) { pub(crate) fn edit_intents(comp: &mut C, cx: &mut Cx<'_, C>) { use crate::contract::cx::Edit as _; if cx.gesture().is_some_and(|phase| matches!(phase, GesturePhase::Begin | GesturePhase::Update)) { - let reason = if !cx.core.escape_to_list && cx.core.intents.take(Intent::Cancel) { + // Typing in a dialog whose Escape closes it ends with the dialog: the typing is cancelled and the Escape is + // left for the dialog, so one Escape cancels both (`focus::escape_closes_dialog`). + let dialog_escape = !cx.core.escape_to_list + && matches!(C::INPUT, Input::Text) + && cx.core.intents.untaken(&Intent::Cancel) + && focus::escape_closes_dialog(&cx.core.ctx); + let reason = if dialog_escape { + cx.cancel(CancelReason::Escape); + None + } else if !cx.core.escape_to_list && cx.core.intents.take(Intent::Cancel) { Some(CancelReason::Escape) } else { cx.core.press_cancelled().map(|reason| match reason { diff --git a/crates/ui-contract/src/primitives/text_adapter.rs b/crates/ui-contract/src/primitives/text_adapter.rs index 5daac3b00..87117a0f8 100644 --- a/crates/ui-contract/src/primitives/text_adapter.rs +++ b/crates/ui-contract/src/primitives/text_adapter.rs @@ -470,8 +470,21 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut } // A clearing field takes Escape while it shows text, with or without a draft, and passes it on once it is empty: // 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() + && state.preedit.is_none() + && !input.options.clear_on_escape + && input.intents.untaken(&Intent::Cancel) + && crate::runtime::focus::escape_closes_dialog(ctx); + 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; let takes_escape = - if input.options.clear_on_escape { !base.is_empty() || state.preedit.is_some() } else { state.draft.is_some() || state.preedit.is_some() }; + if input.options.clear_on_escape { !base.is_empty() || state.preedit.is_some() } else { draft_takes || state.preedit.is_some() }; let list_first = input.list_open && !input.options.clear_on_escape; if focused_before && takes_escape && !state.completions.open && !list_first && input.intents.take(Intent::Cancel) { // Escape over a composition alone ends the composition and leaves the text. diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index cca631d59..3146eadfc 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -897,6 +897,29 @@ pub fn focus_in_scope(ctx: &Context, scope: Id) -> Option { Some(with(ctx, |state| state.within(rec.scope, scope))) } +/// Whether the focused stop's innermost layer-like scope (groups and panes skipped) is a dialog whose Escape closes it +/// and which is the top layer, so the dialog's own Escape rule decides this pass's Escape. A text field holding a +/// draft there passes Escape on: one Escape cancels the dialog and its typing goes with it, as Word's dialogs do. A +/// field in a popup or menu inside the dialog, or in no dialog (a ribbon box), keeps the Escape to revert its draft. +pub(crate) fn escape_closes_dialog(ctx: &Context) -> bool { + let Some(rec) = focused_rec(ctx) else { + return false; + }; + let dialog = with(ctx, |state| { + let mut at = rec.scope; + while let Some(id) = at { + let scope = state.scopes.get(&id).or_else(|| state.last_scopes.get(&id))?; + match scope.policy.kind { + craft_behavior::ScopeKind::Group | craft_behavior::ScopeKind::Pane => at = scope.parent, + craft_behavior::ScopeKind::Dialog => return (scope.policy.cancel == craft_behavior::CancelRule::Close).then_some(id), + _ => return None, + } + } + None + }); + dialog.is_some_and(|id| crate::runtime::overlay::top(ctx) == Some(id)) +} + /// Whether the focus is in the focus scope of the layer `layer`, or, for an `Initial::KeepAnchor` scope, on the stop /// the layer opened from: that scope leaves the focus on its opener while its list is in use. `None` when nothing /// is focused. The layer's dismissal and its Escape both read it. diff --git a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs index 02d833c11..61029a9d3 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs @@ -555,6 +555,29 @@ fn enter_on_a_focused_cancel_cancels_the_form() { assert!(matches!(closed, Some((reason, false)) if !matches!(reason, CloseReason::Activated | CloseReason::Enter)), "Enter cancels: {closed:?}"); } +// 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. +#[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()))); + } + }); + 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` // lacks returns to its opening value, and a key without a row added since the open goes. #[trial]