DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Copy and paste keys: copied objects paste with Ctrl+V on Linux and Windows
aionggfd825af13379
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex ef1d1622a..20c97e7fc 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -431,6 +431,9 @@ pub struct DesignApp {/// A control-channel request is being handled: commands it runs never read or write the/// user's system clipboard (a control client must not see or replace it).pub(crate) in_control: bool,+ /// What the last object copy put on the system clipboard (no native menu): without it the+ /// paste keys produce no paste event. It is never pasted as text.+ object_clip_text: Option<String>,}impl DesignApp {@@ -469,6 +472,7 @@ impl DesignApp {last_recovery: 0.0,egui_ctx: None,in_control: false,+ object_clip_text: None,}}@@ -521,8 +525,19 @@ impl DesignApp {Err(e) => self.status(e.clone()),// Copied text goes to the system clipboard too, whichever way Copy was chosen.Ok(v) if system_clipboard && matches!(id, "edit.copy" | "edit.cut") => {- if let (Some(t), Some(ctx)) = (v.get("text").and_then(Value::as_str), &self.egui_ctx) {- ctx.copy_text(t.to_string());+ let text = match v.get("text").and_then(Value::as_str) {+ Some(t) => {+ self.object_clip_text = None;+ Some(t.to_string())+ }+ None if !self.native_menu => {+ self.object_clip_text = Some(self.copied_objects_text());+ self.object_clip_text.clone()+ }+ None => None,+ };+ if let (Some(t), Some(ctx)) = (text, &self.egui_ctx) {+ ctx.copy_text(t);ctx.request_repaint();}}@@ -534,17 +549,55 @@ impl DesignApp {/// Paste into text without the clipboard's text (a menu, not the paste keys): read it here.fn with_system_clipboard(&mut self, id: &str, mut params: Value) -> Value {let typing = self.session.active().is_some_and(|d| d.selection.text.is_some());- if typing- && matches!(id, "edit.paste" | "edit.pasteWithoutFormatting")- && params.get("text").is_none()+ if !typing || !matches!(id, "edit.paste" | "edit.pasteWithoutFormatting") {+ return params;+ }+ if params.get("text").is_none()&& let Some(text) = self.services.clipboard_text.as_mut().and_then(|f| f())&& let Some(o) = params.as_object_mut(){o.insert("text".into(), Value::String(text));}+ // Still what an object copy put there: paste those objects, not the placeholder text.+ let norm = |t: &str| t.replace("\r\n", "\n");+ if let Some(copied) = &self.object_clip_text+ && params.get("text").and_then(Value::as_str).is_some_and(|t| norm(t) == norm(copied))+ && let Some(o) = params.as_object_mut()+ {+ o.remove("text");+ }params}+ /// Plain text for the system clipboard after copying objects: the copied text frames' stories,+ /// or U+FFFC (object replacement character) when none has text.+ fn copied_objects_text(&self) -> String {+ let mut stories = Vec::new();+ if let Some(clip) = &self.session.clipboard+ && let Some(sp) = clip.spreads.first()+ {+ for it in &sp.items {+ it.walk(&mut |i| {+ if let Some(tf) = i.text_frame()+ && !stories.contains(&tf.story)+ {+ stories.push(tf.story);+ }+ });+ }+ }+ let text = stories+ .iter()+ .filter_map(|id| self.session.clipboard.as_ref()?.story(*id))+ .map(|st| {+ st.text.chars().filter(|c| !('\u{E000}'..='\u{E1FF}').contains(c)).map(|c| if c == '\u{2028}' { '\n' } else { c }).collect::<String>()+ })+ .filter(|t| !t.is_empty())+ .collect::<Vec<_>>()+ .join("\n");+ if text.is_empty() { '\u{FFFC}'.to_string() } else { text }+ }+/// Handle requests produced by tools/commands (dialogs, view changes, file pickers).pub fn after_engine(&mut self) {let reqs = std::mem::take(&mut self.session.ui_requests);diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex bfb7625e9..56bfe5424 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -2415,6 +2415,56 @@ mod tests {assert_eq!(app.session.tool_id(), "rectangleFrame");}+ #[test]+ fn copy_and_paste_keys_work_for_objects_without_a_native_menu() {+ // Without a native menu the paste key arrives only as a paste event, which needs text on+ // the system clipboard (#164, #185).+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.session.execute("file.new", &json!({})).unwrap();+ let ctx = egui::Context::default();+ let frame = |app: &mut crate::DesignApp, events: Vec<egui::Event>| {+ let input = egui::RawInput {+ screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1400.0, 900.0))),+ events,+ ..Default::default()+ };+ let mut out = ctx.run_ui(input, |ui| {+ app.logic(&ui.ctx().clone());+ app.ui(ui);+ });+ out.textures_delta.clear();+ out.platform_output.commands+ };+ let count = |app: &crate::DesignApp| app.session.active().unwrap().doc.spreads[0].items.len();+ frame(&mut app, vec![]);+ frame(&mut app, vec![]);+ let id = app.session.execute("frame.create", &json!({"rect": [72, 72, 200, 160]})).unwrap()["id"].clone();+ app.session.execute("selection.set", &json!({"ids": [id]})).unwrap();+ let n = count(&app);+ let copied = frame(&mut app, vec![egui::Event::Copy])+ .into_iter()+ .find_map(|c| match c {+ egui::OutputCommand::CopyText(t) => Some(t),+ _ => None,+ })+ .expect("copying objects puts text on the system clipboard");+ assert!(!copied.is_empty());+ frame(&mut app, vec![egui::Event::Paste(copied.clone())]);+ assert_eq!(count(&app), n + 1, "the paste key pastes the copied rectangle");++ // Pasting into text anchors the copied object; the clipboard placeholder isn't typed.+ let r = app.session.execute("frame.create", &json!({"rect": [72, 300, 400, 400], "content": "text", "text": "Hello"})).unwrap();+ let story = r["story"].as_u64().unwrap();+ app.session.execute("tool.select", &json!({"tool": "type"})).unwrap();+ app.session.execute("text.select", &json!({"story": story, "anchor": 5, "focus": 5})).unwrap();+ frame(&mut app, vec![egui::Event::Paste(copied.clone())]);+ // Shift pastes without formatting.+ frame(&mut app, vec![egui::Event::ModifiersChanged(egui::Modifiers::SHIFT), egui::Event::Paste(copied)]);+ let text = app.session.active().unwrap().doc.story(designcraft_doc::StoryId(story)).unwrap().text.clone();+ assert!(text.starts_with("Hello") && !text.contains('\u{FFFC}'), "{text:?}");+ assert!(text.contains(designcraft_doc::OBJECT_MARK), "{text:?}");+ }+#[test]fn sample_scripts_run() {let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());