Merge #3366 again: the author's fix and test for a paste bound to another key

This commit is contained in:
Tanish Manku
2026-11-24 15:32:25 -07:00
2 changed files with 37 additions and 8 deletions
+6 -8
View File
@@ -982,8 +982,9 @@ pub fn shortcuts(app: &mut CadApp, ctx: &egui::Context) {
return;
}
// Cmd/Ctrl+C, X and V arrive as clipboard events, not key presses.
let clip: Vec<String> = crate::clipboard::shortcut_commands(app, ctx).into_iter().map(str::to_string).collect();
let mut fire = clip.clone();
let mut fire: Vec<String> = 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) {
@@ -991,13 +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();
// Only a paste that came as a clipboard event has the system clipboard's content with it:
// one fired by a key the user bound asks for the clipboard like the menu does.
if clip.iter().any(|c| c == id)
&& 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;
}
+31
View File
@@ -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<egui::Event>| {
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");
}