From 57c1d36fc0f050f60563a2039a457939b117fff0 Mon Sep 17 00:00:00 2001 From: mo <131036780+mbolling@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:04:19 -0500 Subject: [PATCH] fix(ui-egui): a paste bound to another key reads the system clipboard first The shortcut loop ran every paste command as already read, but only the ones from this frame's clipboard events are. A paste command the user bound to a key of its own fired from the key press with nothing read, so it pasted what this window held. It is now held like a menu paste. Co-Authored-By: Claude Fable 5.2 --- crates/ui-egui/src/menus.rs | 7 ++++-- crates/ui-egui/tests/it/context_menu.rs | 31 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 3182945a3..9843f0ab4 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -983,6 +983,8 @@ pub fn shortcuts(app: &mut CadApp, ctx: &egui::Context) { } // Cmd/Ctrl+C, X and V arrive as clipboard events, not key presses. let mut fire: Vec = crate::clipboard::shortcut_commands(app, ctx).into_iter().map(str::to_string).collect(); + // The first `read` commands come from this frame's clipboard events, whose `Paste` has been read. + let read = fire.len(); ctx.input_mut(|i| { for (s, id) in &pairs { if i.consume_shortcut(s) && !fire.contains(id) { @@ -990,9 +992,10 @@ pub fn shortcuts(app: &mut CadApp, ctx: &egui::Context) { } } }); - for id in fire { + for (n, id) in fire.into_iter().enumerate() { let id = id.as_str(); - if let Some(id) = crate::clipboard::paste_id(id) { + // A paste command bound to another key has read nothing yet: it is held like a menu paste. + if let Some(id) = crate::clipboard::paste_id(id).filter(|_| n < read) { crate::clipboard::run_read(app, id); continue; } diff --git a/crates/ui-egui/tests/it/context_menu.rs b/crates/ui-egui/tests/it/context_menu.rs index ce2f16fb5..8f88a8faf 100644 --- a/crates/ui-egui/tests/it/context_menu.rs +++ b/crates/ui-egui/tests/it/context_menu.rs @@ -456,3 +456,34 @@ fn without_late_answers_a_paste_after_the_next_frame_is_ordinary() { frame(&mut app, paste()); assert_eq!(running(&app).as_deref(), Some("pasteclip"), "the Ctrl+V pastes"); } + +/// A paste command the user bound to a key of its own reads the system clipboard first, like a +/// menu paste: the key press is not a `Paste` event, so nothing has been read when it fires. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_paste_bound_to_another_key_asks_the_window_for_the_system_clipboard() { + let mut app = CadApp::new(Session::new(), Services::default()); + app.run("line", json!({ "points": [[0, 0], [10, 0]] })).unwrap(); + let h = app.session.doc().unwrap().model.handles(); + app.session.set_selection(h); + app.run("copyclip", json!({})).unwrap(); + app.ui.shortcuts.insert("pasteclip".into(), "F9".into()); + let ctx = egui::Context::default(); + let mut now = 0.0; + let mut frame = |app: &mut CadApp, events: Vec| { + now += 0.01; + ctx.begin_pass(egui::RawInput { events, time: Some(now), ..Default::default() }); + cadcraft_ui_egui::menus::shortcuts(app, &ctx); + let mut out = ctx.end_pass(); + out.textures_delta.clear(); + out.viewport_output.into_values().flat_map(|v| v.commands).any(|c| c == egui::ViewportCommand::RequestPaste) + }; + let running = |app: &CadApp| app.session.running.as_ref().map(|r| r.id.clone()); + let f9 = egui::Event::Key { key: egui::Key::F9, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE }; + + let asked = frame(&mut app, vec![f9]); + assert_eq!(running(&app), None, "held until the system clipboard has been read"); + assert!(asked || frame(&mut app, vec![]), "the window is asked for the clipboard text"); + frame(&mut app, vec![egui::Event::Paste("text from another program".into())]); + assert_eq!(running(&app).as_deref(), Some("pasteclip"), "the text arrived: paste runs"); +}