From 6f31184efe81e5c35fa4411bef82b85cf0dd96e1 Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 13:06:58 +0000 Subject: [PATCH] merge-fwb3: FW12: text a claim or a framework taker takes leaves egui's input The hook keeps every text event and records whether a claim took its press; route::begin withdraws a claimed text, or one routed to a taker other than the focused text field's egui widget (a menu level, a driven list, digits, type-ahead), together with the held presses (withdraw_taken, egui input::withdraw). A menu letter that runs a row and returns focus to a text field in the same pass no longer types into the field. --- crates/ui-contract/src/runtime/egui/input.rs | 12 ++++-- crates/ui-contract/src/runtime/keys.rs | 10 ++++- crates/ui-contract/src/runtime/keys/input.rs | 19 +++++----- crates/ui-contract/src/runtime/keys/route.rs | 40 +++++++++++++------- 4 files changed, 52 insertions(+), 29 deletions(-) diff --git a/crates/ui-contract/src/runtime/egui/input.rs b/crates/ui-contract/src/runtime/egui/input.rs index 061dd3947..2b15b82dd 100644 --- a/crates/ui-contract/src/runtime/egui/input.rs +++ b/crates/ui-contract/src/runtime/egui/input.rs @@ -43,10 +43,10 @@ pub(crate) fn hold_back_presses(input: &mut RawInput, frame: u64, held: &mut Hel !held.events.is_empty() } -/// Takes key presses out of the running pass's input: `places` counts the input's key presses, in order. Each press -/// goes with its release when that is in the input too; otherwise its key is no longer down. Returns, for each -/// place, whether the release is still to come. -pub(crate) fn withdraw_presses(ctx: &Context, places: &[usize]) -> Vec { +/// Takes key presses and texts out of the running pass's input: `places` counts the input's key presses, in order, +/// and `texts` its text events. Each press goes with its release when that is in the input too; otherwise its key is +/// no longer down. Returns, for each place, whether the release is still to come. +pub(crate) fn withdraw(ctx: &Context, places: &[usize], texts: &[usize]) -> Vec { ctx.input_mut(|input| { let presses: Vec<(usize, egui::Key)> = input .events @@ -58,6 +58,10 @@ pub(crate) fn withdraw_presses(ctx: &Context, places: &[usize]) -> Vec { }) .collect(); let mut gone = vec![false; input.events.len()]; + let text_events = input.events.iter().enumerate().filter(|(_, event)| matches!(event, Event::Text(_))).map(|(at, _)| at); + for (nth, at) in text_events.enumerate() { + gone[at] |= texts.contains(&nth); + } let mut open = Vec::with_capacity(places.len()); for (at, key) in places.iter().filter_map(|place| presses.get(*place).copied()) { gone[at] = true; diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index 10bb180a4..a2d6b54cd 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -123,7 +123,8 @@ struct Batch { shortcuts: Vec, /// The text of the paste the focused component takes. paste: Option, - texts: Vec, + /// Every text event of the pass, in order: its place here is its place among the text events in egui's input. + texts: Vec, clips: Vec, access: Vec, escapes: usize, @@ -144,6 +145,13 @@ struct Batch { refused: bool, } +/// One text event of the pass. +struct Typed { + text: String, + /// A claim took the press that typed it: the text is the claim's too, so it reaches no field. + claimed: bool, +} + /// A key press a following text event may belong to. struct LastPress { input: KeyInput, diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index f2a204e61..7e1dda8fa 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -1,7 +1,7 @@ //! The input hook's key steps (H3): what it strips from each raw key event and records. use super::route::layout_of; -use super::{AltTrack, Batch, LastPress, MenuKey, NavKeys, Press, Shortcut, Stage, TabPress, current, with}; +use super::{AltTrack, Batch, LastPress, MenuKey, NavKeys, Press, Shortcut, Stage, TabPress, Typed, current, with}; use crate::runtime::commands::{self, EditCommands, FirePhase, NavCommand, WindowShortcut}; use crate::runtime::egui::input::{self as raw_input, KeyEvent, RawInput}; use crate::runtime::env::Env; @@ -105,19 +105,18 @@ impl Strip<'_> { } KeyEvent::Text(text) => { alt.other_key = true; - if let Some(LastPress { input, pressed, claimed }) = batch.last_press.take() { - if let Some(physical) = input.physical.filter(|_| !alt.right) { - batch.learned.push((physical, input.mods, text.clone())); + let mut claimed = false; + if let Some(last) = batch.last_press.take() { + if let Some(physical) = last.input.physical.filter(|_| !alt.right) { + batch.learned.push((physical, last.input.mods, text.clone())); } - if claimed { - // The claim took the press, and with it what the press typed: no field gets the character. - return false; - } - if let Some(press) = pressed.and_then(|at| batch.presses.get_mut(at)) { + claimed = last.claimed; + if let Some(press) = last.pressed.and_then(|at| batch.presses.get_mut(at)) { press.text = Some(batch.texts.len()); } } - batch.texts.push(text); + // Every text stays for now: the begin hook's routing decides which reader gets it (`route::begin`). + batch.texts.push(Typed { text, claimed }); true } KeyEvent::Clip { command, text } if (self.edits)(command) => { diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index db6405f41..ad347e342 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -51,8 +51,10 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting { let mut consumed = vec![Taken::No; batch.presses.len()]; let driven = target.as_ref().and_then(|rec| Driven::of(ctx, rec)); let field_list = target.as_ref().filter(|_| driven.is_none()).and_then(|rec| list_of_field(ctx, rec)); - // Whether a component took each text of the pass, as typing, type-ahead or digits. + // Whether a component took each text of the pass, as typing, type-ahead or digits; and whether the framework took + // it from egui's input, so no egui text widget reads it in this pass (`withdraw_taken`). let mut taken_texts = vec![false; batch.texts.len()]; + let mut withdrawn_texts: Vec = batch.texts.iter().enumerate().filter(|(_, typed)| typed.claimed).map(|(at, _)| at).collect(); if let Some(rec) = &target { for (press, taken) in batch.presses.iter().zip(&mut consumed).filter(|(press, _)| !press.canvas) { *taken = if stale_repeat(&mut stale, press) { @@ -62,9 +64,19 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting { apply_press(ctx, rec, press, route, &mut routed, &mut activated) }; } - for (text, taken) in batch.texts.iter().zip(&mut taken_texts) { - let route = text_route(ctx, rec, driven, text); + // The focused stop reads its typing from egui's input when it is a text field (its egui text widget) or an app's + // own widget; any other taker reads the text as an intent only, and the text leaves egui's input. + let reads_egui = rec.stop.host || rec.stop.keys.owner == OwnerClass::Text; + for (at, (typed, taken)) in batch.texts.iter().zip(&mut taken_texts).enumerate() { + if typed.claimed { + *taken = true; + continue; + } + let route = text_route(ctx, rec, driven, &typed.text); *taken = !route.is_empty(); + if route.iter().any(|(to, _)| !(reads_egui && *to == rec.stop.id)) { + withdrawn_texts.push(at); + } for (to, intent) in route { push(&mut routed, Some(to), intent, false); } @@ -95,7 +107,7 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting { push(&mut routed, focused, Intent::Edit(command), false); } let (shortcuts, held) = owned_shortcuts(ctx, target.as_ref(), &batch, &consumed, &taken_texts); - withdraw_held(ctx, target.as_ref(), &batch, &held); + withdraw_taken(ctx, target.as_ref(), &batch, &held, &withdrawn_texts); let pasted = batch.paste.as_deref().zip(focused).map(|(text, id)| (id, Arc::::from(text))); let escapes = craft_behavior::focus::route_escapes(batch.escapes, press_captured); for _ in 0..escapes.to_components { @@ -290,20 +302,20 @@ pub fn focus_holds(ctx: &Context, chord: Chord) -> bool { holds_press(owner, Some(&rec), &press, taken, typed) } -/// Takes the presses the focused component holds out of the running pass's egui input, where the app reads its own -/// keys: one press, one action. A held press whose release has not come yet leaves its release to the strip. The -/// app's own widget (`Stop::host`) reads its keys from egui, so its presses stay. -fn withdraw_held(ctx: &Context, target: Option<&StopRec>, batch: &Batch, held: &[bool]) { - if target.is_some_and(|rec| rec.stop.host) { - return; - } +/// Takes what the framework handed to one taker out of the running pass's egui input, where the app and egui's text +/// widgets read their keys: one press, one action, and one text, one reader. `held` marks the presses the focused +/// component holds; the app's own widget (`Stop::host`) reads its keys from egui, so its presses stay. `texts` are the +/// places in `Batch::texts` of the texts a claim or a framework taker took. A held press whose release has not come yet +/// leaves its release to the strip. +fn withdraw_taken(ctx: &Context, target: Option<&StopRec>, batch: &Batch, held: &[bool], texts: &[usize]) { + let host = target.is_some_and(|rec| rec.stop.host); let presses: Vec<&Press> = - batch.presses.iter().zip(held).filter(|(press, held)| **held && press.in_egui.is_some()).map(|(press, _)| press).collect(); - if presses.is_empty() { + batch.presses.iter().zip(held).filter(|(press, held)| !host && **held && press.in_egui.is_some()).map(|(press, _)| press).collect(); + if presses.is_empty() && texts.is_empty() { return; } let places: Vec = presses.iter().filter_map(|press| press.in_egui).collect(); - let open = crate::runtime::egui::input::withdraw_presses(ctx, &places); + let open = crate::runtime::egui::input::withdraw(ctx, &places, texts); current(ctx, |stage| { for key in presses.iter().zip(open).filter(|(_, open)| *open).filter_map(|(press, _)| press.release) { if !stage.consumed.contains(&key) {