From e2eedc1918cffec3077d57d0687c6da0ad3792dd Mon Sep 17 00:00:00 2001 From: mo <131036780+mbolling@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:16:54 -0500 Subject: [PATCH] fix(ui-egui): a held paste stays with the drawing that asked for it A paste asked for from a menu or the command line waits for the system clipboard for up to half a second. If another drawing became the shown one in that time (a file read in the background finished opening), the held paste ran there. It now remembers its drawing and is dropped, with a line in the command history, when another one is shown. Co-Authored-By: Claude Fable 5.2 --- crates/ui-egui/src/clipboard.rs | 19 ++++++++-- crates/ui-egui/tests/it/context_menu.rs | 48 +++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/clipboard.rs b/crates/ui-egui/src/clipboard.rs index 4f775567d..db5cb4022 100644 --- a/crates/ui-egui/src/clipboard.rs +++ b/crates/ui-egui/src/clipboard.rs @@ -106,6 +106,8 @@ struct Pending { id: &'static str, /// The command line text it was typed as, when it was. line: Option, + /// The drawing it was asked for in (its `uid`): it pastes there or not at all. + doc: Option, } /// Paste commands waiting for the system clipboard ([`defer`], [`pump`]). @@ -164,6 +166,11 @@ impl Paste { } } +/// The drawing shown now, as a held paste remembers it. +fn active_doc(app: &CadApp) -> Option { + app.session.state().ok().map(|s| s.uid) +} + pub(crate) fn paste_id(id: &str) -> Option<&'static str> { ["pasteclip", "pasteblock", "pasteorig"].into_iter().find(|p| *p == id) } @@ -181,7 +188,7 @@ pub fn offered(session: &Session, spec: &CommandSpec) -> bool { pub fn defer(app: &mut CadApp, id: &str, line: Option<&str>) -> bool { let Some(id) = paste_id(id).filter(|_| ON_DEMAND && !app.paste.running) else { return false }; app.paste.cancel(); - app.paste.request = Some(Pending { id, line: line.map(str::to_string) }); + app.paste.request = Some(Pending { id, line: line.map(str::to_string), doc: active_doc(app) }); true } @@ -202,7 +209,7 @@ fn run_now(app: &mut CadApp, p: Pending) { /// Run `id`, a paste command whose `Paste` event has been read. pub fn run_read(app: &mut CadApp, id: &'static str) { - run_now(app, Pending { id, line: None }); + run_now(app, Pending { id, line: None, doc: active_doc(app) }); } /// Once per frame: ask the window for the system clipboard for a newly held paste, and give up on @@ -214,6 +221,14 @@ pub fn pump(app: &mut CadApp, ctx: &egui::Context) { } let (frame, late) = (app.paste.frame, app.paste.late); app.paste.sent.retain(|r| if late { r.until > now } else { r.frame + 1 >= frame }); + // A held paste belongs to the drawing it was asked for in. If another one is shown by now (a + // file read in the background finished opening, or the tab was switched), it is dropped + // rather than run there. + let held = app.paste.request.as_ref().or(app.paste.wait.as_ref().map(|(p, _, _)| p)); + if held.is_some_and(|p| p.doc != active_doc(app)) { + app.paste.cancel(); + app.session.echo("Paste cancelled: another drawing became active before the clipboard was read."); + } if let Some(p) = app.paste.request.take() { // A focused text field would take the `Paste` itself. if ctx.egui_wants_keyboard_input() { diff --git a/crates/ui-egui/tests/it/context_menu.rs b/crates/ui-egui/tests/it/context_menu.rs index 8f88a8faf..cbf04f508 100644 --- a/crates/ui-egui/tests/it/context_menu.rs +++ b/crates/ui-egui/tests/it/context_menu.rs @@ -487,3 +487,51 @@ fn a_paste_bound_to_another_key_asks_the_window_for_the_system_clipboard() { 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"); } + +/// A held paste belongs to the drawing it was asked for in: when another drawing is shown before +/// the clipboard has been read (a background open finished), neither the answer nor the wait +/// running out pastes into that one. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_held_paste_does_not_run_in_another_drawing() { + 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(); + let ctx = egui::Context::default(); + let mut now = 0.0; + let mut frame = |app: &mut CadApp, events: Vec, dt: f64| { + now += dt; + ctx.begin_pass(egui::RawInput { events, time: Some(now), ..Default::default() }); + cadcraft_ui_egui::menus::shortcuts(app, &ctx); + ctx.end_pass().textures_delta.clear(); + }; + let running = |app: &CadApp| app.session.running.as_ref().map(|r| r.id.clone()); + let paste = || vec![egui::Event::Paste("text from another program".into())]; + let first = app.session.active; + + // The answer arrives after the switch. + app.start("pasteclip"); + frame(&mut app, vec![], 0.01); + app.session.new_drawing(false); + assert_ne!(app.session.active, first, "the new drawing is the one shown"); + frame(&mut app, paste(), 0.01); + assert_eq!(running(&app), None, "the answer does not paste into the other drawing"); + frame(&mut app, vec![], 5.0); + assert_eq!(running(&app), None, "nor does the wait running out"); + + // No answer: the wait runs out after the switch. + app.session.active = first; + app.start("pasteclip"); + frame(&mut app, vec![], 0.01); + app.session.new_drawing(false); + frame(&mut app, vec![], 5.0); + assert_eq!(running(&app), None, "the fallback does not paste into the other drawing"); + + // Back in a drawing that stays shown, a paste still runs. + app.start("pasteclip"); + frame(&mut app, vec![], 0.01); + frame(&mut app, paste(), 0.01); + assert_eq!(running(&app).as_deref(), Some("pasteclip")); +}