diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index 5d66fc3256..6933d0efc3 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -225,7 +225,8 @@ impl Strip<'_> { KeyEvent::Clip { .. } if self.in_menu => false, KeyEvent::Clip { command, pasted, mods } => { // The platform sends the clipboard event in place of the chord: an unlisted one is the chord. The - // web sends the chord's press as well. A paste's content goes with the command the chord fires. + // web sends the chord's press as well, so one event is one shortcut, at the press the web sent or at + // the event. A paste's content goes with the command the chord fires. let left = pasted.clone(); if let Some(pasted) = pasted.filter(|_| command == EditCommand::Paste) { let content = batch.app_paste.get_or_insert_default(); @@ -237,13 +238,17 @@ impl Strip<'_> { .shortcuts .iter() .any(|shortcut| shortcut.phase == FirePhase::Press && shortcut.input.mods == input.mods && shortcut.input.typed == Some(key)); - if !sent && self.records { - // A component that reads every chord gets the event as the chord's press, the way the key made it. + // A component that reads every chord gets the event as the chord's press, the way the key made it. + if self.records && !sent { self.press(batch, key, &input, false, at); - } else if !sent { - batch.shortcuts.push(Shortcut { input, phase: FirePhase::Press, press: None, canvas: false, at, place: batch.place }); + } + if !self.records { + if !sent { + batch.shortcuts.push(Shortcut { input, phase: FirePhase::Press, press: None, canvas: false, at, place: batch.place }); + } // No component took it, so a host that reads what is left over (`leftover_events`) gets the event back, - // in its place among the events egui keeps. + // in its place among the events egui keeps. Every such event stays, whatever shortcut it shares with + // another: two pastes of one pass are two pastes, and the second one's chord is the first one's. let text = left.as_ref().and_then(|left| left.text.as_deref()); let event = match (command, left.as_ref().and_then(|left| left.image.as_ref())) { (EditCommand::Cut, _) => Event::Cut, diff --git a/crates/ui-contract/tests/it/runtime/leftover_events.rs b/crates/ui-contract/tests/it/runtime/leftover_events.rs index af72fe37f9..ed4435fbcd 100644 --- a/crates/ui-contract/tests/it/runtime/leftover_events.rs +++ b/crates/ui-contract/tests/it/runtime/leftover_events.rs @@ -5,10 +5,20 @@ use craft_ui::runtime::commands::{AppCatalog, Binding, CommandDef, CommandInput, use craft_ui::runtime::frame::take_output; use craft_ui::runtime::keys::leftover_events; use craft_ui::testing::focus::key_release; -use craft_ui::testing::input::key_press; +use craft_ui::testing::input::{key_press, key_repeat, text}; use craft_ui::testing::{Frames, trial}; use egui::{Event, Key, Modifiers, Ui}; -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; + +/// The presses and releases of `events`, in order, each with whether it is a press. +fn key_events(events: Vec) -> Vec<(Key, bool)> { + events.into_iter().filter_map(|event| if let Event::Key { key, pressed, .. } = event { Some((key, pressed)) } else { None }).collect() +} + +/// The texts of `events` the host pastes, in order. +fn pasted(events: Vec) -> Vec { + events.into_iter().filter_map(|event| if let Event::Paste(text) = event { Some(text) } else { None }).collect() +} /// With nothing focused, a host reading the leftover events after the window's content gets the press and release of /// a key no command is bound to, and neither the press nor the release of a key whose command fired. @@ -38,3 +48,20 @@ fn leftover_events_holds_the_keys_no_command_took() { frames.frame(vec![key_release(Key::F4, Modifiers::NONE)], &mut show); assert_eq!(keys(left.take()), [(Key::F4, true), (Key::F4, false)], "an unbound key's are left over"); } + +/// With nothing focused and nothing bound, both pastes of a pass are the host's: the second one reaches it as the first +/// does, though its chord is the shortcut the first one made. +#[trial] +fn leftover_events_holds_both_pastes_of_a_pass() { + let mut frames = Frames::new(); + let left = RefCell::new(Vec::new()); + let mut show = |ui: &mut Ui| { + left.borrow_mut().extend(leftover_events(ui.ctx())); + }; + frames.idle(2, &mut show); + take_output(&frames.ctx); + left.borrow_mut().clear(); + + frames.frame(vec![Event::Paste("a".to_owned()), Event::Paste("b".to_owned())], &mut show); + assert_eq!(pasted(left.take()), ["a".to_owned(), "b".to_owned()], "both pastes are left over, in the order they arrived"); +}