diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index a75ce8ea6..696f74b9e 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -97,7 +97,8 @@ pub(crate) struct FocusedRecord { pub(crate) canvas: Option, /// egui focus is on a widget no framework component made, or an egui popup is open, and no framework press or /// drag is captured: an app's own focused widget or popup keeps the keys a focused component keeps (Tab, Escape, - /// the clipboard events). Written by the end hook after its focus moves. + /// the plain Enter, the clipboard events), and no window intent comes of them. Written by the end hook after its + /// focus moves. pub(crate) foreign: bool, /// egui focus is on a widget no framework component made, and no framework press or drag is captured: egui /// moves focus among the app's own widgets with the arrows and the Tab it keeps. Written by the end hook. diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index db3f8a949..26cc8794f 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -42,8 +42,8 @@ struct Strip<'a> { holds_drafts: bool, /// A dialog keeps a canvas open and Space belongs to it: the press is the canvas's momentary command chord. canvas_space: bool, - /// An app's own egui widget or popup has the focus (`FocusedRecord::foreign`): Tab, Escape and the clipboard - /// events are its own. + /// An app's own egui widget or popup has the focus (`FocusedRecord::foreign`): Tab, Escape, the plain Enter and the + /// clipboard events are its own, and no window intent comes of them. foreign: bool, /// The focused stop is the app's own egui widget (`Stop::host`, `FocusedRecord::host`), which reads its keys from /// egui: its Escape stays there, as an app's own focused widget's does. Tab and the arrows follow its `KeyUse`, as @@ -348,6 +348,7 @@ impl Strip<'_> { let ctrl_tab = key == Key::Tab && (if self.os == Os::Mac { mods.ctrl && !mods.cmd } else { mods.cmd }); let ctrl_tab = ctrl_tab && self.pages && !self.keeps_tab; let bare_tab = key == Key::Tab && !mods.cmd && !mods.ctrl && !mods.alt; + let plain_enter = key == Key::Enter && mods.is_none(); // Alt and Shift ride along, as with Enter: a canvas tool reads them; Cmd and Ctrl chords are the app's. While // the focused stop records, Escape with any modifier held is a chord for the recorder; only plain Escape is a // scope escape. Over an open press any Escape is the press's cancel: a borrowed tool's drag holds Cmd. Shift or @@ -379,7 +380,7 @@ impl Strip<'_> { PressUse::Edit(*command) } else if key == Key::Space && self.canvas_space { PressUse::CanvasSpace - } else if (self.foreign && (bare_tab || escape)) || (self.host && escape && !self.press_captured) || canvas_tab || egui_edit { + } else if (self.foreign && (bare_tab || escape || plain_enter)) || (self.host && escape && !self.press_captured) || canvas_tab || egui_edit { PressUse::Egui } else if bare_tab && self.egui_tab && !self.keeps_tab { PressUse::TabToEgui diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index 71f62c579..a6bb427c0 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -236,6 +236,41 @@ fn a_focused_egui_field_keeps_tab_escape_and_the_clipboard() { assert_eq!(seen(&host), (vec![Key::Escape], 0), "the field's Escape is egui's, and no window Cancel"); } +/// The Enter presses in egui's input and the window Submits taken after one Enter, with the host's field focused or +/// nothing focused. +fn enter_with(field_focused: bool) -> (usize, usize) { + let counts = Rc::new(Cell::new((0usize, 0usize))); + let counted = Rc::clone(&counts); + let (_host, mut base) = host_window(); + let mut show = move |ui: &mut Ui| { + let enters = ui.input(|input| input.events.iter().filter(|event| matches!(event, Event::Key { key: Key::Enter, pressed: true, .. })).count()); + let submit = with_window_intents(ui.ctx(), false, |intents| intents.take(Intent::Submit)); + let (seen, taken) = counted.get(); + counted.set((seen + enters, taken + usize::from(submit))); + base(ui); + }; + let mut frames = Frames::new(); + frames.frame(Vec::new(), &mut show); + if field_focused { + fixture::host_focus(&frames.ctx, field()); + frames.frame(Vec::new(), &mut show); + assert_eq!(egui_focused(&frames.ctx), Some(field()), "the host's field holds egui focus"); + } + counts.set((0, 0)); + press_key(&mut frames, &mut show, Key::Enter); + frames.idle(2, &mut show); + counts.get() +} + +/// An app's own focused widget keeps the plain Enter as it keeps Escape: it reads it from egui once, and no window +/// Submit comes of it, so an app that turns the window's Submit back into an Enter does not get two. With nothing +/// focused the plain Enter is the window's Submit and stays in egui's input too. +#[trial] +fn a_focused_egui_field_keeps_enter_and_the_window_gets_no_submit() { + assert_eq!(enter_with(true), (1, 0), "the field reads one Enter and the window takes no Submit"); + assert_eq!(enter_with(false), (1, 1), "with nothing focused, Enter is the window's Submit and stays in egui"); +} + #[trial] fn an_open_egui_popup_keeps_escape() { let popup = named_id("host popup"); diff --git a/docs/craft-framework.md b/docs/craft-framework.md index a717ace9a..0b7dd03f7 100644 --- a/docs/craft-framework.md +++ b/docs/craft-framework.md @@ -169,7 +169,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. ` | App modals | A modal the app draws with its own egui code blocks nothing outside egui by itself. While one shows, the app names its window each frame with `FrameInput::app_modal(Some(viewport))`: the app's other windows, panels floated into windows of their own among them, are then blocked as under a framework app-modal layer (their presses, keys, wheel and text are dropped, a press there brings the modal's window forward, and they refuse to close). | | Input pacing | One press per pass: extra presses in one frame wait for the next passes. A press outside an open framework menu closes the menu and goes no further. After an OS resize of the window, the held pointer is released. | -The window's keys are the ones the runtime routes to no component: an Escape or a plain Enter while no framework stop is focused, and the arrows' nudge. `with_window_intents` offers them to its closure while nothing has framework focus. The closure takes what it handles (`Intents::take(Intent::Cancel)`; `Intent` and the other types `Intents` reads are in `ui::runtime::keys`) and returns whether it took anything; the rest stays for later readers. A framework scope that ran earlier in the pass takes them first (an open dialog takes the Escape that closes it), so the app reads them last in its UI to see what no component took. App code that read `Key::Escape`, `Key::Tab`, F6 or F10 from egui while none of its own widgets had focus reads these intents and `FrameOutput::chrome_toggles` instead; its own focused widgets and popups keep reading Tab and Escape from egui. +The window's keys are the ones the runtime routes to no component: an Escape or a plain Enter while nothing has the focus, neither a framework stop nor one of the app's own widgets or popups, and the arrows' nudge. The window's plain Enter also stays in egui's input. `with_window_intents` offers them to its closure while nothing has framework focus. The closure takes what it handles (`Intents::take(Intent::Cancel)`; `Intent` and the other types `Intents` reads are in `ui::runtime::keys`) and returns whether it took anything; the rest stays for later readers. A framework scope that ran earlier in the pass takes them first (an open dialog takes the Escape that closes it), so the app reads them last in its UI to see what no component took. App code that read `Key::Escape`, `Key::Tab`, F6 or F10 from egui while none of its own widgets had focus reads these intents and `FrameOutput::chrome_toggles` instead; its own focused widgets and popups keep reading Tab, Escape and the plain Enter from egui, and no window intent comes of them. Framework dialogs the app keeps as values run on a `DialogHost`: the app calls `run(ui)` once per pass after its own UI and reads results with `take`.