diff --git a/crates/ui-contract/src/runtime/egui/input.rs b/crates/ui-contract/src/runtime/egui/input.rs index d327db5e0..cc1d56138 100644 --- a/crates/ui-contract/src/runtime/egui/input.rs +++ b/crates/ui-contract/src/runtime/egui/input.rs @@ -187,10 +187,11 @@ pub(crate) fn key_event(event: &Event, os: Os, latin: &[(egui::Key, char)], held Event::Key { pressed: false, .. } => read(event).map_or(KeyEvent::Other, |input| KeyEvent::Release { input }), Event::Text(text) => KeyEvent::Text(text.clone()), Event::ModifiersChanged(modifiers) => KeyEvent::Modifiers(Mods::from_egui(modifiers, os)), - // PLAN: kb.C10c maps `Event::PasteImage` here; this egui has no such event that I could verify. Event::Copy => KeyEvent::Clip { command: craft_behavior::EditCommand::Copy, text: None, mods: held }, Event::Cut => KeyEvent::Clip { command: craft_behavior::EditCommand::Cut, text: None, mods: held }, Event::Paste(text) => KeyEvent::Clip { command: craft_behavior::EditCommand::Paste, text: Some(text.clone()), mods: held }, + // The platform sends it in place of `Paste` for a clipboard with a picture and no text. + Event::PasteImage(_) => KeyEvent::Clip { command: craft_behavior::EditCommand::Paste, text: None, mods: held }, Event::PointerButton { pressed: true, .. } => KeyEvent::PointerPress, Event::AccessKitActionRequest(request) => KeyEvent::Access(Action(request.clone())), _ => KeyEvent::Other, diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index f34c4ab4b..57839f7e7 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -32,6 +32,7 @@ pub use craft_behavior::{AccessRequest, Intent, MoveMods, NavMove, PartRef}; pub use input::held; #[doc(hidden)] pub use input::keyed; +pub use input::textless_paste; pub(crate) use input::{claims_press, count_sent, input_hook, keys_state, queue_nav, sent, take_menu_keys, take_nav}; pub use intents::Intents; #[doc(hidden)] @@ -266,6 +267,8 @@ pub(crate) struct Stage { down: Vec, /// The input hook read a key press, typed text or a clipboard event in this pass, withheld ones included. keyed: bool, + /// The input hook read a paste with no text (the clipboard held none) in this pass. + textless_paste: bool, /// How many inputs of the viewport held an event sent to the focus (`count_sent`). sent: u64, /// The keys whose press the strip took and whose release it is still to drop. diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index 16db1bcf4..6c23ba746 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -472,7 +472,10 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec Some(false), KeyEvent::Press { .. } | KeyEvent::Text(_) => Some(true), @@ -613,10 +618,11 @@ pub fn held(ctx: &Context) -> Vec { down } -/// The key state, as the tracked input `Keys` compares it: the keys down, whether a key arrived and Caps Lock. -pub(crate) fn keys_state(ctx: &Context) -> (Vec, bool, bool) { - let (down, keyed) = current(ctx, |stage| (stage.down.clone(), stage.keyed)); - (down, keyed, crate::runtime::lock_keys::caps_on(ctx)) +/// The key state, as the tracked input `Keys` compares it: the keys down, whether a key arrived, whether a paste with no +/// text arrived and Caps Lock. +pub(crate) fn keys_state(ctx: &Context) -> (Vec, bool, bool, bool) { + let (down, keyed, textless_paste) = current(ctx, |stage| (stage.down.clone(), stage.keyed, stage.textless_paste)); + (down, keyed, textless_paste, crate::runtime::lock_keys::caps_on(ctx)) } /// Counts an input of `viewport` whose `events` hold one sent to the focus: a key, text, IME, clipboard or AccessKit @@ -653,6 +659,14 @@ pub fn keyed(ctx: &Context) -> bool { current(ctx, |stage| stage.keyed) } +/// Whether the running viewport's input hook read a paste with no text in this pass: the paste chord came while the +/// clipboard held no text (a picture, a PDF), so the platform sent no text to paste. A focused text field takes the +/// event, so an app asks this, not egui's events. +pub fn textless_paste(ctx: &Context) -> bool { + crate::runtime::replay::note_input(ctx, crate::runtime::inputs::Input::Keys); + current(ctx, |stage| stage.textless_paste) +} + /// Takes the Tab and F6 moves the input hook recorded, for the focus service's begin hook. The pass's root env is /// installed by now, so a bare Tab that passes the chrome gate and has a chrome command bound to it becomes that /// command's toggle for the running viewport; any other Tab moves focus. A repeat of a toggling press is dropped: it diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index 85aee51ad..a20139eb7 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -477,6 +477,29 @@ fn a_host_canvas_reads_the_clipboard_of_its_edit_set_with_nothing_focused() { assert!(!clipboard_with_canvas(KeyUse::CANVAS, chord).1.is_empty(), "a canvas without the command: the chord fires the app's Paste"); } +/// A clipboard with a picture and no text sends a paste with no text: it fires the app's Paste as the chord does, and +/// `textless_paste` reads it in that pass only. +#[trial] +fn a_paste_with_no_text_is_the_paste_chord_and_reads_as_textless() { + let picture = Event::PasteImage(Arc::new(egui::ColorImage::filled([1, 1], egui::Color32::WHITE))); + assert!(!clipboard_with_canvas(KeyUse::CANVAS, picture.clone()).1.is_empty(), "the picture's paste fires the app's Paste"); + let read = Rc::new(Cell::new(false)); + let seen = Rc::clone(&read); + let (_, mut base) = host_window(); + let mut show = move |ui: &mut Ui| { + seen.set(crate::runtime::keys::textless_paste(ui.ctx())); + base(ui); + }; + let mut frames = Frames::new(); + frames.frame(Vec::new(), &mut show); + frames.frame(vec![Event::Paste("x".to_owned())], &mut show); + assert!(!read.get(), "a paste with text"); + frames.frame(vec![picture], &mut show); + assert!(read.get(), "a paste with no text"); + frames.frame(Vec::new(), &mut show); + assert!(!read.get(), "the pass after it"); +} + /// What an F6 press did under `overrides`: whether it reached egui, and the ids of the commands it fired. fn f6_under(overrides: Option) -> (bool, Vec) { let (host, mut show) = host_window();