Text fields: Escape keeps the text only while the field can be edited

The typing run took the keep-on-escape branch from the option alone, while the adapter also needs the part to be enabled and editable. A field that went read-only or disabled in the middle of a run still got the Escape the adapter treats as a plain field's, so the draft committed and no cancel was emitted. The resolver now takes the same editable check the adapter makes, so such a field cancels the run and gives back the text from before it. The new trial covers a field made read-only mid-run.
This commit is contained in:
skylartaylor committed 2026-11-23 20:18:07 +01:00
1 parent d0e17aad77
commit f996bdca9d
2 files changed
+39 -5

No files matched your search

+6 -5
View File
@@ -165,15 +165,16 @@ struct Resolved<V> {
/// 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<V> = Box<dyn FnOnce(&mut TextState, Option<&V>, bool) -> Resolved<V>>;
type Resolve<V> = Box<dyn FnOnce(&mut TextState, Option<&V>, bool, bool) -> Resolved<V>>;
/// 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<K: Codec + Clone + 'static>(codec: K, options: &TextOptions, rect: Rect, fallback: K::Value) -> Resolve<K::Value> {
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<C: Component>(
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.
@@ -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());
}