ui-contract: a picture paste with no text is the Paste clip with no text; keys::textless_paste reads it in its pass

This commit is contained in:
bflatastic
2026-11-22 08:32:04 +07:00
parent baa57be35c
commit 7e97036522
4 changed files with 47 additions and 6 deletions
+2 -1
View File
@@ -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,
+3
View File
@@ -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<Key>,
/// 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.
+19 -5
View File
@@ -472,7 +472,10 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
if press {
overlay::note_dropped_press(ctx, viewport);
}
with(ctx, viewport, |stage| stage.keyed = false);
with(ctx, viewport, |stage| {
stage.keyed = false;
stage.textless_paste = false;
});
return (removed || trimmed, Vec::new());
}
let os = crate::runtime::windows::os_of(ctx);
@@ -519,6 +522,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
let mut visible = None;
let mut later = Vec::new();
stage.keyed = false;
stage.textless_paste = false;
let mut place = 0;
events.retain(|arrived| {
// Every event the IME engine left counts, as the engine counts them (`ime::take_placed`).
@@ -533,6 +537,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
batch.focus_lost |= matches!(arrived.event, egui::Event::WindowFocused(false));
note_down(&mut stage.down, &read);
stage.keyed |= matches!(read, KeyEvent::Press { .. } | KeyEvent::Text(_) | KeyEvent::Clip { .. });
stage.textless_paste |= matches!(read, KeyEvent::Clip { command: EditCommand::Paste, text: None, .. });
visible = match &read {
KeyEvent::PointerPress => Some(false),
KeyEvent::Press { .. } | KeyEvent::Text(_) => Some(true),
@@ -613,10 +618,11 @@ pub fn held(ctx: &Context) -> Vec<Key> {
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<Key>, 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<Key>, 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
@@ -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<craft_keymap::Overrides>) -> (bool, Vec<String>) {
let (host, mut show) = host_window();