diff --git a/crates/ui-contract/src/contract/text.rs b/crates/ui-contract/src/contract/text.rs index dcd8dffb69..eca93efa7f 100644 --- a/crates/ui-contract/src/contract/text.rs +++ b/crates/ui-contract/src/contract/text.rs @@ -165,15 +165,16 @@ struct Resolved { /// The typing run over the state the adapter recorded this pass, the component's value (`None` while mixed) and /// whether a commit is due. -type Resolve = Box, bool) -> Resolved>; +type Resolve = Box, bool, bool) -> Resolved>; /// Turns what the adapter recorded this pass into the typing run's events: a parsed draft applies in the pass it /// is typed, and a commit proposes and commits in one pass. `fallback` is the value the part shows, the run's start -/// while the component's value is mixed. +/// while the component's value is mixed. The closure's last argument says whether the part keeps its text on Escape +/// this pass: the option alone is not enough, since a part that went read-only or disabled mid-run takes Escape as a +/// plain field does. fn resolver(codec: K, options: &TextOptions, rect: Rect, fallback: K::Value) -> Resolve { let (live, limit, clear_on_escape) = (options.live, options.limit, options.clear_on_escape); - let keep_on_escape = options.keep_on_escape && !clear_on_escape; - Box::new(move |state, value, commit_now| { + Box::new(move |state, value, commit_now, keep_on_escape| { let value = value.unwrap_or(&fallback); let mut typing = TypingRun::new(codec).live(live); if let Some(limit) = limit { @@ -587,8 +588,8 @@ pub(crate) fn run( state.end = Some(End::Rewrite { refuses: false }); } } - let resolved = resolve(&mut state, cx.value(), commit_now); let keeps_escape = part.options.keep_on_escape && !part.options.clear_on_escape && !read_only && cx.core.resolved.enabled && part.options.enabled; + let resolved = resolve(&mut state, cx.value(), commit_now, keeps_escape); if resolved.escaped && keeps_escape && !state.invalid { // The same Escape goes on to the enclosing scope in this pass, which still finds this focused stop; the field // lets focus go once the scopes have run. diff --git a/crates/ui-widgets/tests/it/text/text_field.rs b/crates/ui-widgets/tests/it/text/text_field.rs index ac545ee555..8ec8ae9450 100644 --- a/crates/ui-widgets/tests/it/text/text_field.rs +++ b/crates/ui-widgets/tests/it/text/text_field.rs @@ -303,3 +303,36 @@ fn text_lost_focus_reports_a_focus_loss_without_an_edit() { assert_eq!(lost.get(), 1, "Tab off the unedited field reports one loss"); assert_eq!(*first.borrow(), "one", "and edits nothing"); } + +/// A field that keeps its text on Escape does so only while it can be edited: one that went read-only in the middle of +/// a typing run takes Escape as a plain field does, so the run is cancelled and the text from before it comes back, +/// not committed. +#[trial] +fn a_field_that_went_read_only_mid_run_gives_back_the_text_on_escape() { + let text = Rc::new(RefCell::new(String::from("old"))); + let read_only = Rc::new(Cell::new(false)); + let events = Rc::new(RefCell::new(Vec::new())); + let (shown, locked, log) = (text.clone(), read_only.clone(), events.clone()); + let mut fixture = Fixture::new(move |ui| { + let mut held = shown.borrow_mut(); + let out = TextField::new("Filter", Bind::bound(&mut *held)).keeps_on_escape().select_on_focus().read_only(locked.get()).show(ui); + log.borrow_mut().extend(out.events.iter().map(|event| match event { + craft_behavior::EditEvent::Commit { .. } => "commit", + craft_behavior::EditEvent::Cancel { .. } => "cancel", + _ => "other", + })); + }); + fixture.settle(); + fixture.key(Key::Tab, Modifiers::NONE); + fixture.type_text("new"); + fixture.idle(2); + assert_eq!(*text.borrow(), "new", "the typing reached the binding"); + read_only.set(true); + fixture.idle(2); + events.borrow_mut().clear(); + fixture.key(Key::Escape, Modifiers::NONE); + fixture.idle(3); + assert_eq!(*text.borrow(), "old", "Escape on a read-only field gives back the text from before the run"); + assert!(!events.borrow().contains(&"commit"), "the run is not committed: {:?}", events.borrow()); + assert!(events.borrow().contains(&"cancel"), "the run is cancelled: {:?}", events.borrow()); +}