diff --git a/crates/ui-egui/src/keys.rs b/crates/ui-egui/src/keys.rs index dcbc101b1d..18db674df5 100644 --- a/crates/ui-egui/src/keys.rs +++ b/crates/ui-egui/src/keys.rs @@ -133,6 +133,13 @@ fn equation_key(app: &mut WordApp, key: Key, m: Modifiers) -> bool { true } +/// Whether releasing `key` with `modifiers` is the plain Paste shortcut (Mod+V or the Paste +/// key) — not Mod+Shift+V (Paste Formatting) or Mod+Alt+V (Paste Special) — so a clipboard +/// picture may be pasted on it (#45). +fn is_paste_release(key: Key, modifiers: egui::Modifiers) -> bool { + (modifiers.command || key == Key::Paste) && !modifiers.alt && !modifiers.shift +} + /// Events for the focused canvas: text, editing keys, clipboard, IME. While keytips are showing /// every key belongs to them (letters pick badges, Escape cancels), so nothing here runs. pub fn canvas_events(app: &mut WordApp, ctx: &egui::Context) { @@ -152,7 +159,7 @@ pub fn canvas_events(app: &mut WordApp, ctx: &egui::Context) { // release; paste the clipboard's picture then (#45). A paste event before the release // means it was text. (The web reads pasted pictures itself.) if let egui::Event::Key { key: key @ (Key::V | Key::Paste), pressed: false, modifiers, .. } = &e { - let paste_keys = (modifiers.command || *key == Key::Paste) && !modifiers.alt; + let paste_keys = is_paste_release(*key, *modifiers); let pasted = std::mem::take(&mut app.canvas.pasted); if !pasted && paste_keys && app.session.math.is_none() && app.services.clipboard_picture.is_some() { let _ = app.run("edit.paste", json!({})); @@ -265,3 +272,18 @@ pub fn global_shortcuts(app: &mut WordApp, ctx: &egui::Context) { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_plain_paste_releases_paste_a_clipboard_picture() { + let m = |command, shift, alt| egui::Modifiers { command, shift, alt, ..Default::default() }; + assert!(is_paste_release(Key::V, m(true, false, false)), "Mod+V"); + assert!(is_paste_release(Key::Paste, m(false, false, false)), "the Paste key"); + assert!(!is_paste_release(Key::V, m(true, true, false)), "Mod+Shift+V is Paste Formatting"); + assert!(!is_paste_release(Key::V, m(true, false, true)), "Mod+Alt+V is Paste Special"); + assert!(!is_paste_release(Key::V, m(false, false, false)), "a plain V"); + } +}