From 7a8dfd61f0a616fac323c71743920b3a64dad0ff Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Mon, 23 Nov 2026 03:26:51 +0100 Subject: [PATCH 01/36] Text fields: keep the text on Escape (keeps_on_escape) and report text_lost_focus TextField::keeps_on_escape() lets a live filter keep its query: Escape commits an open typing run once, ends editing by letting focus go after the enclosing scopes ran, and leaves the same Escape to the enclosing scope. EditResponse::text_lost_focus reports that the text part lost key focus in the pass, edited or not. Defaults for plain and search fields are unchanged. --- crates/ui-contract/src/contract/cx/context.rs | 7 ++ crates/ui-contract/src/contract/cx/edit.rs | 1 + crates/ui-contract/src/contract/text.rs | 27 +++++- crates/ui-contract/src/contract/values.rs | 7 ++ .../src/primitives/text_adapter.rs | 9 +- .../ui-contract/src/primitives/text_input.rs | 5 ++ crates/ui-contract/src/runtime/focus.rs | 11 +++ .../ui-contract/src/runtime/keys/intents.rs | 8 ++ crates/ui-widgets/src/text/text_field.rs | 10 +++ crates/ui-widgets/tests/it/text/text_field.rs | 90 +++++++++++++++++++ 10 files changed, 170 insertions(+), 5 deletions(-) diff --git a/crates/ui-contract/src/contract/cx/context.rs b/crates/ui-contract/src/contract/cx/context.rs index efb5f8c605..5950565b6c 100644 --- a/crates/ui-contract/src/contract/cx/context.rs +++ b/crates/ui-contract/src/contract/cx/context.rs @@ -42,6 +42,8 @@ pub(crate) trait TextResult { fn tab(&self) -> Option; /// The part's caret as a byte offset into its text after the pass; `None` while its widget has no caret. fn caret(&self) -> Option; + /// Whether the part lost key focus this pass, edited or not. + fn text_lost_focus(&self) -> bool; fn as_any(&self) -> &dyn Any; } @@ -51,6 +53,7 @@ pub(crate) struct RecordedText { pub(crate) left_by_tab: Option, pub(crate) tab: Option, pub(crate) caret: Option, + pub(crate) text_lost_focus: bool, } impl TextResult for RecordedText { @@ -70,6 +73,10 @@ impl TextResult for RecordedText { self.caret } + fn text_lost_focus(&self) -> bool { + self.text_lost_focus + } + fn as_any(&self) -> &dyn Any { self } diff --git a/crates/ui-contract/src/contract/cx/edit.rs b/crates/ui-contract/src/contract/cx/edit.rs index 58cab318ba..389fe0b209 100644 --- a/crates/ui-contract/src/contract/cx/edit.rs +++ b/crates/ui-contract/src/contract/cx/edit.rs @@ -241,6 +241,7 @@ impl Edit for Cx<'_, C> { response.submitted = self.core.submitted; response.submit_mods = self.core.texts.as_ref().map_or(craft_keymap::Mods::NONE, |(_, text)| text.submit_mods()); response.left_by_tab = self.core.texts.as_ref().and_then(|(_, text)| text.left_by_tab()); + response.text_lost_focus = self.core.texts.as_ref().is_some_and(|(_, text)| text.text_lost_focus()); response.tab = self.core.texts.as_ref().and_then(|(_, text)| text.tab()); response.caret = self.core.texts.as_ref().and_then(|(_, text)| text.caret()); response.unparsed = self.core.text_invalid; diff --git a/crates/ui-contract/src/contract/text.rs b/crates/ui-contract/src/contract/text.rs index e76026fef6..eab6368203 100644 --- a/crates/ui-contract/src/contract/text.rs +++ b/crates/ui-contract/src/contract/text.rs @@ -167,6 +167,7 @@ type Resolve = Box, bool) -> Resolved(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| { let value = value.unwrap_or(&fallback); let mut typing = TypingRun::new(codec).live(live); @@ -208,6 +209,15 @@ fn resolver(codec: K, options: &TextOptions, rect: R events.push(TextEvent::Cancel(CancelReason::Escape)); } } + Some(End::Escape) if keep_on_escape => { + // The text stays: an open run commits once, neither submitted nor cancelled. + escaped = true; + if let (Ended::Commit(committed), note) = typing.commit_tagged(&mut run) { + committed_note = Some(note); + state.normalised = Some(typing.codec().normalise(&draft_text, &committed)); + events.push(TextEvent::Commit(committed)); + } + } Some(End::Escape) => { escaped = true; if clear_on_escape && !run.is_open() { @@ -535,6 +545,17 @@ pub(crate) fn run( } } 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; + 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. + cx.core.intents.pass_on(craft_behavior::Intent::Cancel); + crate::runtime::focus::surrender_after_scopes(&ctx, widget); + } if resolved.refused { crate::runtime::frame::refuse(&ctx); } @@ -551,7 +572,7 @@ pub(crate) fn run( cx.core.text_invalid = state.invalid; cx.core.text_unparsed = part.options.unparsed.clone().filter(|_| state.invalid); cx.core.escape_to_list = state.completions.open || list_open; - if state.draft.is_some() || (part.options.clear_on_escape && !out.layout.full().is_empty()) { + if state.draft.is_some() || (part.options.clear_on_escape && !out.layout.full().is_empty()) || (keeps_escape && out.focused) { keys::hold_escape(&ctx, id); } let completions = state.completions.items.clone(); @@ -566,7 +587,9 @@ pub(crate) fn run( outcome.clicked = out.clicked; // The focus service moved focus off the widget with Tab before this pass's widgets ran. let left_by_tab = if was_focused && !out.focused { crate::runtime::focus::tabbed_off(&ctx, widget) } else { None }; - cx.core.texts = Some((part.key, Box::new(RecordedText { outcome, submit_mods: resolved.submit_mods, left_by_tab, tab: out.tab, caret }))); + let text_lost_focus = was_focused && !out.focused; + cx.core.texts = + Some((part.key, Box::new(RecordedText { outcome, submit_mods: resolved.submit_mods, left_by_tab, tab: out.tab, caret, text_lost_focus }))); apply_events(cx, resolved.events); cx.core.memory.shown::(state_id).seen = shown_copy(cx); cx.core.submitted |= resolved.submitted; diff --git a/crates/ui-contract/src/contract/values.rs b/crates/ui-contract/src/contract/values.rs index 91390e6b14..6eefc5c808 100644 --- a/crates/ui-contract/src/contract/values.rs +++ b/crates/ui-contract/src/contract/values.rs @@ -635,6 +635,10 @@ pub struct EditResponse { /// Tab (`Some(true)`) or Shift+Tab (`Some(false)`) moved focus off the text part in this pass, which ended its /// edit; a list's rename continues on the next or previous row. pub left_by_tab: Option, + /// The text part lost key focus in this pass, with or without an edit: a click elsewhere, Tab, or an Escape that + /// keeps the text (`keep_on_escape`). It decides nothing; `submitted`, `escaped` and `left_by_tab` stay their own + /// facts. False for a component with no text part. + pub text_lost_focus: bool, /// The edit was abandoned with Escape. pub escaped: bool, /// A text part holds text that does not parse: the value is the last one that did, and a form that commits the @@ -680,6 +684,7 @@ impl EditResponse { submitted: false, submit_mods: craft_keymap::Mods::NONE, left_by_tab: None, + text_lost_focus: false, escaped: false, unparsed: false, validity, @@ -702,6 +707,7 @@ impl EditResponse { submitted: false, submit_mods: craft_keymap::Mods::NONE, left_by_tab: None, + text_lost_focus: false, escaped: false, unparsed: false, validity: Validity::Ok, @@ -742,6 +748,7 @@ impl EditResponse { submitted: self.submitted, submit_mods: self.submit_mods, left_by_tab: self.left_by_tab, + text_lost_focus: self.text_lost_focus, escaped: self.escaped, unparsed: self.unparsed, validity, diff --git a/crates/ui-contract/src/primitives/text_adapter.rs b/crates/ui-contract/src/primitives/text_adapter.rs index 66e1cbf418..fa009371c4 100644 --- a/crates/ui-contract/src/primitives/text_adapter.rs +++ b/crates/ui-contract/src/primitives/text_adapter.rs @@ -471,8 +471,11 @@ 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. + // the typing with it (Word, and the old apps). Outside dialogs Escape reverts the draft first. A field that keeps + // its text on Escape takes every Escape while editable and hands it on after committing (`keep_on_escape`). + let keep_on_escape = input.options.keep_on_escape && !input.options.clear_on_escape && input.enabled && !input.read_only; let dialog_escape = focused_before + && !keep_on_escape && state.draft.is_some() && state.preedit.is_none() && !input.options.clear_on_escape @@ -483,8 +486,8 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut 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 { draft_takes || state.preedit.is_some() }; + let takes_escape = keep_on_escape + || 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/primitives/text_input.rs b/crates/ui-contract/src/primitives/text_input.rs index ec344c9917..3603770feb 100644 --- a/crates/ui-contract/src/primitives/text_input.rs +++ b/crates/ui-contract/src/primitives/text_input.rs @@ -64,6 +64,10 @@ pub struct TextOptions { pub live: bool, /// Escape clears the text and commits the empty text while text shows, and goes on to the host once it is empty. pub clear_on_escape: bool, + /// Escape keeps the text: an open typing run commits once, editing ends by leaving focus, and the same Escape + /// goes on to the enclosing scope. A live filter keeps its query this way. Ignored with `clear_on_escape`, and on a + /// disabled or read-only part. + pub keep_on_escape: bool, /// Monospace text. pub mono: bool, /// The type role the text is set in; a monospace text takes the mono role's face and size instead. @@ -122,6 +126,7 @@ impl Default for TextOptions { mask: false, live: false, clear_on_escape: false, + keep_on_escape: false, mono: false, role: TypeRole::Body, ink: None, diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 3fe97a2692..aec7695779 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -175,6 +175,8 @@ pub(crate) struct State { pub(super) arrived: Option<(u64, StopKey, FocusOrigin)>, /// The running pass's begin hook left a Tab to egui (`NavKeys::tab_to_egui`); its end hook takes it. pub(super) tab_to_egui: bool, + /// A text widget that gave its Escape on to the enclosing scopes and lets focus go at the end of the pass. + surrender_after_scopes: Option, } fn viewport(ctx: &Context) -> ViewportId { @@ -950,6 +952,12 @@ pub(crate) fn begin(ctx: &Context, tab_all_controls: bool, dialog_tab: TabReach) } } +/// Releases the text widget `widget` at the end of this pass, after the enclosing scopes have had its Escape: a scope +/// deciding whether to close still finds the focused stop. No key is replayed and no other widget loses focus. +pub(crate) fn surrender_after_scopes(ctx: &Context, widget: Id) { + with(ctx, |state| state.surrender_after_scopes = Some(widget)); +} + /// The end hook: applies the press's focus move, returns focus to the opener of a scope that went away, /// moves it off a stop that was not registered, and records what the next input hook reads. pub(crate) fn end(ctx: &Context) { @@ -961,6 +969,9 @@ pub(crate) fn end(ctx: &Context) { { egui_focus::surrender(ctx, id); } + if let Some(widget) = with(ctx, |state| state.surrender_after_scopes.take()) { + egui_focus::surrender(ctx, widget); + } let app_widget = |id: Id| { let made = crate::runtime::pointer::made_by_component(ctx, viewport, id); with(ctx, |state| state.app_widget(id, made)) diff --git a/crates/ui-contract/src/runtime/keys/intents.rs b/crates/ui-contract/src/runtime/keys/intents.rs index 192e5ebdbc..b8dd338679 100644 --- a/crates/ui-contract/src/runtime/keys/intents.rs +++ b/crates/ui-contract/src/runtime/keys/intents.rs @@ -70,6 +70,14 @@ impl Intents { } } + /// Leaves an intent this pass's taker took to the enclosing scope, in the same pass: the next taker gets it. No + /// input is queued or copied. + pub(crate) fn pass_on(&mut self, intent: Intent) { + if let Some((_, taken)) = self.items.iter_mut().find(|(item, taken)| *taken && item.same_kind(&intent)) { + *taken = false; + } + } + /// Takes an intent equal to `intent` in every field, once per pass: `Intent::delete(false, Granularity::Grapheme)` /// takes Backspace and leaves Delete, which `take` would also take. pub fn take_exact(&mut self, intent: &Intent) -> bool { diff --git a/crates/ui-widgets/src/text/text_field.rs b/crates/ui-widgets/src/text/text_field.rs index 89a3edc1fd..b1407dde92 100644 --- a/crates/ui-widgets/src/text/text_field.rs +++ b/crates/ui-widgets/src/text/text_field.rs @@ -86,6 +86,16 @@ pub struct TextField<'a, C: Codec + Clone + 'static = PlainText> { } impl<'a> TextField<'a, PlainText> { + /// Escape keeps the current text and ends editing by leaving focus: a live filter keeps its query. An open typing + /// run commits once, without submitting or cancelling, and the same Escape goes on to the enclosing scope. Plain + /// fields otherwise restore the run's start and search fields clear; neither default changes. + #[must_use] + pub fn keeps_on_escape(mut self) -> Self { + self.options.keep_on_escape = true; + self.options.clear_on_escape = false; + self + } + /// A plain text field named `label`. pub fn new(label: impl Into