From a1af5a5cc8b73c75628286b2edc1f312d5b134f0 Mon Sep 17 00:00:00 2001 From: Belligerently Date: Thu, 5 Nov 2026 19:05:00 -0500 Subject: [PATCH] Handle platform clipboard events for node shortcuts --- crates/ui-egui/src/graph_view.rs | 6 +- crates/ui-egui/src/menus.rs | 28 +++- crates/ui-egui/tests/clipboard_shortcuts.rs | 150 ++++++++++++++++++++ 3 files changed, 180 insertions(+), 4 deletions(-) create mode 100644 crates/ui-egui/tests/clipboard_shortcuts.rs diff --git a/crates/ui-egui/src/graph_view.rs b/crates/ui-egui/src/graph_view.rs index 285171c3..06a7a2d8 100644 --- a/crates/ui-egui/src/graph_view.rs +++ b/crates/ui-egui/src/graph_view.rs @@ -1258,7 +1258,11 @@ fn node_menu(app: &mut MaterialCraftApp, ui: &mut Ui, id: NodeId) { } let mut item = |ui: &mut Ui, label: &str, cmd: &str, params: serde_json::Value| { if ui.button(tr(label)).clicked() { - app.run(cmd, params); + if cmd == "edit.copy" { + crate::menus::invoke(app, ui.ctx(), cmd); + } else { + app.run(cmd, params); + } ui.close(); } }; diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 3878d455..616a0549 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -119,6 +119,14 @@ pub fn invoke(app: &mut MaterialCraftApp, ctx: &egui::Context, id: &str) { return; } match id { + "edit.copy" | "edit.cut" => { + if let Some(result) = app.run(id, Value::Null) + && let Some(clip) = result.get("clip") + { + // Native paste emits no event when the system clipboard is empty. + ctx.copy_text(clip.to_string()); + } + } "ui.newGraph" => app.ui.windows.new_graph = true, "ui.export" => app.ui.windows.export = true, "ui.quit" => ctx.send_viewport_cmd(egui::ViewportCommand::Close), @@ -248,7 +256,22 @@ pub fn shortcuts(app: &mut MaterialCraftApp, ctx: &egui::Context) { if ctx.egui_wants_keyboard_input() { return; } - if !ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { pressed: true, .. }))) { + let mut fire: Vec<&str> = Vec::new(); + // Native and web backends translate clipboard chords into these events rather than keys. + // Leave them for TextEdit when it owns the keyboard (the guard above). + ctx.input_mut(|i| { + i.events.retain(|event| { + let id = match event { + egui::Event::Copy => "edit.copy", + egui::Event::Cut => "edit.cut", + egui::Event::Paste(_) => "edit.paste", + _ => return true, + }; + fire.push(id); + false + }); + }); + if fire.is_empty() && !ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { pressed: true, .. }))) { return; } // Selected wires take Delete and Backspace before the node commands do, and X swaps them. @@ -270,10 +293,9 @@ pub fn shortcuts(app: &mut MaterialCraftApp, ctx: &egui::Context) { } // Longer chords first, so Ctrl+Shift+S is not taken by Ctrl+S. table.sort_by_key(|(_, sc)| std::cmp::Reverse(sc.modifiers.ctrl as u8 + sc.modifiers.shift as u8 + sc.modifiers.alt as u8 + sc.modifiers.command as u8)); - let mut fire: Vec<&str> = Vec::new(); ctx.input_mut(|i| { for (id, sc) in &table { - if i.consume_shortcut(sc) { + if i.consume_shortcut(sc) && !fire.contains(id) { fire.push(*id); } } diff --git a/crates/ui-egui/tests/clipboard_shortcuts.rs b/crates/ui-egui/tests/clipboard_shortcuts.rs new file mode 100644 index 00000000..af10a332 --- /dev/null +++ b/crates/ui-egui/tests/clipboard_shortcuts.rs @@ -0,0 +1,150 @@ +use egui::{Event, Key, Modifiers, OutputCommand, vec2}; +use egui_kittest::{Harness, kittest::Queryable}; +use materialcraft_ui_egui::{MaterialCraftApp, UiState}; +use serde_json::json; + +fn harness(render: bool) -> Harness<'static, MaterialCraftApp> { + let mut app = MaterialCraftApp::headless(materialcraft_engine::new_session(), UiState::default()); + app.session.preview_size = Some(32); + let source = app.run("graph.node.add", json!({"def": "uniform", "at": [0, 0]})).unwrap()["id"].as_u64().unwrap(); + let levels = app.run("graph.node.add", json!({"def": "levels", "connectFrom": {"node": source}})).unwrap()["id"].as_u64().unwrap(); + app.run("graph.select", json!({"ids": [source, levels]})).unwrap(); + let builder = Harness::builder().with_size(vec2(1600.0, 960.0)); + let builder = if render { builder.wgpu() } else { builder }; + let mut h = builder.build_ui_state( + |ui, app: &mut MaterialCraftApp| { + app.step(&ui.ctx().clone()); + app.draw(ui); + }, + app, + ); + h.run_steps(8); + h +} + +fn copied_text(h: &Harness<'static, MaterialCraftApp>) -> String { + h.output() + .platform_output + .commands + .iter() + .find_map(|c| if let OutputCommand::CopyText(text) = c { Some(text.clone()) } else { None }) + .expect("node copy must populate the system clipboard so native paste generates an event") +} + +fn copy_paste_cut(h: &mut Harness<'static, MaterialCraftApp>) { + h.event(Event::Copy); + h.step(); + assert!(h.state().session.clipboard.is_some(), "native Copy event did not copy nodes"); + let payload = copied_text(h); + assert_eq!(serde_json::from_str::(&payload).unwrap()["nodes"].as_array().unwrap().len(), 2); + h.event(Event::Paste(payload)); + h.step(); + assert_eq!(h.state().session.graph().unwrap().nodes.len(), 4); + assert_eq!(h.state().session.graph().unwrap().connections.len(), 2); + h.event(Event::Cut); + h.step(); + assert_eq!(h.state().session.graph().unwrap().nodes.len(), 2); + let payload = copied_text(h); + h.event(Event::Paste(payload)); + h.step(); + assert_eq!(h.state().session.graph().unwrap().nodes.len(), 4); + assert_eq!(h.state().session.graph().unwrap().connections.len(), 2); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(h.state().session.graph().unwrap().nodes.len(), 2); + h.state_mut().run("edit.redo", json!({})).unwrap(); + assert_eq!(h.state().session.graph().unwrap().nodes.len(), 4); +} + +#[test] +fn native_clipboard_events_edit_nodes_and_preserve_links() { + copy_paste_cut(&mut harness(false)); +} + +#[test] +fn menu_copy_also_populates_system_clipboard() { + let mut h = harness(false); + h.get_by_label("Edit").click(); + h.run_steps(4); + h.get_by_label("Copy Ctrl+C").click(); + h.step(); + assert!(!copied_text(&h).is_empty()); +} + +fn chord(key: Key) -> Event { + Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::COMMAND } +} + +#[test] +fn mixed_clipboard_and_key_events_do_not_paste_twice() { + let mut h = harness(false); + h.input_mut().events.extend([Event::ModifiersChanged(Modifiers::COMMAND), Event::Copy, chord(Key::C)]); + h.step(); + let payload = copied_text(&h); + h.input_mut().events.extend([Event::Paste(payload), chord(Key::V)]); + h.step(); + assert_eq!(h.state().session.graph().unwrap().nodes.len(), 4); +} + +#[test] +fn raw_command_chords_continue_to_edit_nodes() { + let mut h = harness(false); + h.input_mut().events.extend([Event::ModifiersChanged(Modifiers::COMMAND), chord(Key::C)]); + h.step(); + assert!(!copied_text(&h).is_empty()); + h.input_mut().events.push(chord(Key::V)); + h.step(); + assert_eq!(h.state().session.graph().unwrap().nodes.len(), 4); + h.input_mut().events.push(chord(Key::X)); + h.step(); + assert_eq!(h.state().session.graph().unwrap().nodes.len(), 2); +} + +#[test] +fn node_context_copy_populates_system_clipboard() { + let mut h = harness(false); + let pos = h.state().graph.rects[0].1.center(); + h.event(Event::PointerMoved(pos)); + h.step(); + for pressed in [true, false] { + h.event(Event::PointerButton { pos, button: egui::PointerButton::Secondary, pressed, modifiers: Modifiers::NONE }); + h.step(); + } + h.run_steps(4); + h.get_by_label("Copy selection").click(); + h.step(); + assert!(!copied_text(&h).is_empty()); +} + +#[test] +fn clipboard_events_keep_text_editing_and_do_not_touch_nodes() { + let mut h = harness(false); + h.get_by_label("Node label").click(); + h.step(); + h.key_press_modifiers(Modifiers::COMMAND, Key::A); + h.step(); + h.event(Event::Paste("Edited label".into())); + h.step(); + let selected = *h.state().session.selection.last().unwrap(); + assert_eq!(h.state().session.graph().unwrap().node(selected).unwrap().label, "Edited label"); + h.key_press_modifiers(Modifiers::COMMAND, Key::A); + h.step(); + h.event(Event::Copy); + h.step(); + assert_eq!(copied_text(&h), "Edited label"); + assert!(h.state().session.clipboard.is_none()); + h.event(Event::Cut); + h.step(); + assert_eq!(h.state().session.graph().unwrap().nodes.len(), 2); + assert_eq!(h.state().session.graph().unwrap().node(selected).unwrap().label, ""); +} + +#[test] +#[ignore = "requires an offscreen GPU"] +fn clipboard_graph_snapshot() { + let mut h = harness(true); + copy_paste_cut(&mut h); + h.run_steps(10); + let dir = std::path::PathBuf::from(std::env::var_os("MC_SNAPSHOT_DIR").expect("MC_SNAPSHOT_DIR")); + std::fs::create_dir_all(&dir).unwrap(); + h.render().unwrap().save(dir.join("clipboard-shortcuts.png")).unwrap(); +}