From fc4b1c7bb7cb3a90a49f3b3c448b3a4b3557bf11 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 21 Nov 2026 14:23:03 -0500 Subject: [PATCH] Restore field edits before closing modeless dialogs --- .../ui-contract/src/contract/steps/update.rs | 2 +- .../src/primitives/text_adapter.rs | 4 +-- crates/ui-contract/src/runtime/keys.rs | 2 +- crates/ui-contract/src/runtime/keys/route.rs | 13 ++++---- .../tests/it/form_dialogs/dialog.rs | 30 +++++++++++++++++++ 5 files changed, 42 insertions(+), 9 deletions(-) diff --git a/crates/ui-contract/src/contract/steps/update.rs b/crates/ui-contract/src/contract/steps/update.rs index 4d30e75dd..ea695f917 100644 --- a/crates/ui-contract/src/contract/steps/update.rs +++ b/crates/ui-contract/src/contract/steps/update.rs @@ -51,7 +51,7 @@ 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 escapes = !cx.core.escape_to_list && !crate::runtime::keys::focus_in_dialog(&cx.core.ctx); + let escapes = !cx.core.escape_to_list && !crate::runtime::keys::focus_in_modal_dialog(&cx.core.ctx); let reason = if escapes && cx.core.intents.take(Intent::Cancel) { Some(CancelReason::Escape) } else { diff --git a/crates/ui-contract/src/primitives/text_adapter.rs b/crates/ui-contract/src/primitives/text_adapter.rs index 1e5235865..91cb79f1e 100644 --- a/crates/ui-contract/src/primitives/text_adapter.rs +++ b/crates/ui-contract/src/primitives/text_adapter.rs @@ -371,8 +371,8 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut None => {} } // A clearing field takes Escape while it shows text, with or without a draft, and passes it on once it is empty. In - // a dialog a draft leaves Escape to the dialog's Cancel, which discards it with the dialog's other edits. - let draft_takes = state.draft.is_some() && !(focused_before && keys::focus_in_dialog(ctx)); + // a modal dialog a draft leaves Escape to the dialog's Cancel, which discards it with the dialog's other edits. + let draft_takes = state.draft.is_some() && !(focused_before && keys::focus_in_modal_dialog(ctx)); let takes_escape = if input.options.clear_on_escape { !base.is_empty() || state.preedit.is_some() } else { draft_takes || state.preedit.is_some() }; if focused_before && takes_escape && !state.completions.open && !input.list_open && input.intents.take(Intent::Cancel) { diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index 47708d5f9..c0caa2e35 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -44,7 +44,7 @@ pub(crate) use intents::{ pub use intents::{with_canvas_window_intents, with_window_intents}; pub(crate) use nodes::{redirect_node, register_expanded, register_node}; pub use route::focus_holds; -pub(crate) use route::{activated_member, begin, focus_in_dialog, layout_of, owner_at, pasted, pasted_text, queue_edit, routed_to, take_shortcuts}; +pub(crate) use route::{activated_member, begin, focus_in_modal_dialog, layout_of, owner_at, pasted, pasted_text, queue_edit, routed_to, take_shortcuts}; /// A menu-bar key of an in-window bar (spec 9.7). F10 reaches the bar on every OS; Alt keys only off a Mac. #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index 9e5aad354..e7eba2d13 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -244,11 +244,14 @@ pub(crate) fn owner_at(ctx: &Context, target: Option<&StopRec>, scopes: &[Id], b key_owner(layer == overlay::LayerKeys::Blocked, &present) } -/// Whether the focused stop of the running viewport is in a dialog layer (`OwnerClass::Dialog`): Escape there is the -/// dialog's Cancel, which discards a field's draft with the rest of the dialog's edits, so a draft does not hold it. -pub(crate) fn focus_in_dialog(ctx: &Context) -> bool { - focus::focused_rec(ctx) - .is_some_and(|rec| overlay::key_layer(ctx, &focus::scope_chain(ctx, &rec), None) == overlay::LayerKeys::Class(OwnerClass::Dialog)) +/// A modal dialog's Cancel discards its edits together, so a field's draft does not hold Escape there. A modeless +/// window's edits belong to the app, and its field must cancel its own draft first. +pub(crate) fn focus_in_modal_dialog(ctx: &Context) -> bool { + focus::focused_rec(ctx).is_some_and(|rec| { + let scopes = focus::scope_chain(ctx, &rec); + overlay::key_layer(ctx, &scopes, None) == overlay::LayerKeys::Class(OwnerClass::Dialog) + && scopes.iter().find_map(|id| overlay::entry(ctx, *id)).is_some_and(|entry| entry.rank.is_modal()) + }) } /// The shortcut events the key owner leaves to the app (9.3), and the presses the focused component holds diff --git a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs index f871fcb89..6032f3e4b 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs @@ -832,3 +832,33 @@ fn escape_in_a_text_field_closes_the_form_dialog_typed_or_not() { } } } + +/// A modeless form keeps its other edits when Escape cancels an active text edit. +#[trial] +fn escape_in_a_modeless_form_restores_the_field_before_closing() { + let open = Rc::new(Cell::new(true)); + let value = Rc::new(RefCell::new(String::from("Original"))); + let (shown, text) = (Rc::clone(&open), Rc::clone(&value)); + let mut fixture = Fixture::new(move |ui| { + let mut is_open = shown.get(); + let mut text = text.borrow_mut(); + craft_ui::patterns::form_dialogs::FormDialog::new("Tool", Bind::bound(&mut *text)) + .opened(Bind::bound(&mut is_open)) + .shaped(|dialog| dialog.modeless()) + .show(ui, |ui, _, text| { + craft_ui::text::TextField::new("Name", Bind::bound(text)).show(ui); + true + }); + shown.set(is_open); + }); + fixture.idle(10); + let field = fixture.by_label("Name").expect("the field is shown"); + fixture.click(&field).idle(2); + fixture.type_text("x").idle(2); + assert_ne!(&*value.borrow(), "Original", "typing changed the value"); + fixture.key(Key::Escape, Modifiers::NONE).idle(2); + assert!(open.get(), "the first Escape leaves the window open"); + assert_eq!(&*value.borrow(), "Original", "the field's edit is restored"); + fixture.key(Key::Escape, Modifiers::NONE).idle(6); + assert!(!open.get(), "the second Escape closes the window"); +}