Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge origin/main into codex/paste-svg-paths

moppeeeraaa9981867a4
diff --cc crates/ui-egui/src/canvas.rs
index 0d3296cf6,5d8218b77..dd23faa03
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@@ -1826,55 -1909,77 +1909,128 @@@ mod tests
(s.horizontal_units, s.vertical_units)
}
+ const ART: &str = r##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 50" width="100pt" height="50pt"><path d="M0,0 C30,0 70,50 100,50" fill="none" stroke="#000" stroke-width="2"/></svg>"##;
+
+ #[test]
+ fn pasting_svg_from_another_app_places_editable_paths_in_the_middle_of_the_view() {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({})).unwrap();
+ let mut h = harness(app);
+ let undo = h.state().session.doc().unwrap().history.undo.len();
+ h.event(egui::Event::Paste(ART.into()));
+ h.run_steps(2);
+ let app = h.state();
+ let st = app.session.doc().unwrap();
+ assert_eq!(st.history.undo.len(), undo + 1, "one undo step");
+ let id = *st.selection.items.first().expect("the pasted path is selected");
+ let it = st.doc.item(id).unwrap();
+ assert_eq!(it.shape, designcraft_doc::Shape::Path);
+ assert_eq!(it.path.subpaths[0].anchors.len(), 2);
+ assert!((it.stroke.weight - 2.0).abs() < 1e-9);
+ // Centred where the view's centre falls on the spread.
+ let rect = app.canvas_rect.unwrap();
+ let xf = Xf::new(rect, app.view().unwrap());
+ let (_, centre) = CanvasLayout::new(&st.doc, false).spread_at(xf.to_canvas(rect.center())).unwrap();
+ assert!((it.bounds().center() - centre).hypot() < 0.01, "{:?} vs {centre:?}", it.bounds());
- // Plain text pasted with no text being edited changes nothing.
- let before = st.doc.clone();
++ // Plain text isn't art: it goes to Edit › Paste, as on main, and never becomes paths.
++ let paths = |app: &DesignApp| {
++ let d = &app.session.doc().unwrap().doc;
++ d.spreads.iter().flat_map(|sp| sp.items.iter()).filter(|it| it.shape == designcraft_doc::Shape::Path).count()
++ };
++ let before = paths(h.state());
+ h.event(egui::Event::Paste("just words".into()));
+ h.run_steps(2);
- assert!(std::sync::Arc::ptr_eq(&before, &h.state().session.doc().unwrap().doc));
++ assert_eq!(paths(h.state()), before);
+ }
+
+ #[test]
- fn edit_paste_with_nothing_copied_here_asks_for_the_system_clipboard() {
- let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
++ fn edit_paste_from_a_menu_pastes_svg_on_the_system_clipboard_as_paths() {
++ let services = crate::Services { clipboard_text: Some(Box::new(|| Some(ART.to_string()))), ..Default::default() };
++ let mut app = DesignApp::new(designcraft_engine::Session::new(), services);
+ app.run("file.new", json!({})).unwrap();
- assert!(!crate::menus::enabled(&app, "edit.paste"), "nothing copied in DesignCraft");
- assert!(crate::menus::menu_enabled(&app, "edit.paste"), "the system clipboard may hold art");
+ crate::menus::activate(&mut app, "edit.paste", &Value::Null);
- assert!(app.request_system_paste);
- let ctx = egui::Context::default();
- let mut out = ctx.run_ui(egui::RawInput::default(), |ui| app.logic(ui.ctx()));
- out.textures_delta.clear();
- assert!(out.viewport_output[&egui::ViewportId::ROOT].commands.contains(&egui::ViewportCommand::RequestPaste));
- assert!(!app.request_system_paste);
- // With objects copied here, Edit › Paste pastes them as before.
- let r = app.run("frame.create", json!({"rect": [10, 10, 50, 50]})).unwrap();
- app.run("selection.set", json!({"ids": [r["id"]]})).unwrap();
- app.run("edit.copy", json!({})).unwrap();
++ let st = app.session.doc().unwrap();
++ let id = *st.selection.items.first().expect("the pasted path is selected");
++ assert_eq!(st.doc.item(id).unwrap().shape, designcraft_doc::Shape::Path, "paths, not a text frame of markup");
++ // Typing: the markup is text, pasted at the insertion point.
++ let sid = app.run("frame.create", json!({"rect": [10, 10, 300, 300], "content": "text", "text": ""})).unwrap()["story"].clone();
++ app.run("text.select", json!({"story": sid, "anchor": 0, "focus": 0})).unwrap();
+ crate::menus::activate(&mut app, "edit.paste", &Value::Null);
- assert!(!app.request_system_paste);
++ let d = &app.session.doc().unwrap().doc;
++ assert!(d.story(designcraft_doc::StoryId(sid.as_u64().unwrap())).unwrap().text.contains("<svg"));
++ }
++
+ #[test]
+ fn selected_merged_owners_paint_the_same_row_and_column_targets() {
+ for column in [true, false] {
+ for merged in [false, true] {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"pages": 1})).unwrap();
+ app.run("frame.create", json!({"rect": [36, 36, 300, 300], "content": "text"})).unwrap();
+ app.run("table.insert", json!({"rows": 2, "cols": 2})).unwrap();
+ if merged {
+ let range = if column { json!({"rows": [0, 0], "cols": [0, 1]}) } else { json!({"rows": [0, 1], "cols": [0, 0]}) };
+ app.run("table.select", range).unwrap();
+ app.run("table.merge", json!({})).unwrap();
+ }
+ app.run("table.select", json!({"rows": [1, 1], "cols": [1, 1]})).unwrap();
+ app.run(if column { "table.selectColumn" } else { "table.selectRow" }, json!({})).unwrap();
+ let st = app.session.doc().unwrap();
+ let selection = st.selection.clone();
+ let ts = selection.text.unwrap();
+ let selected = selection.cells.unwrap();
+ assert_eq!(
+ selected.range,
+ if column { designcraft_doc::CellRange::new(0, 1, 1, 1) } else { designcraft_doc::CellRange::new(1, 0, 1, 1) }
+ );
+ let layout = CanvasLayout::new(&st.doc, st.editing_parents);
+ let cs = app.session.cache.get(&st.doc, ts.story, None);
+ let ft = &cs.frames[0];
+ let table = ft.tables.iter().find(|t| t.table == selected.table).unwrap();
+ let (a, _) = item_canvas_xf(&st.doc, &layout, ft.frame).unwrap();
+ let m = a * st.doc.text_xf(st.doc.item(ft.frame).unwrap());
+ let xf = Xf { min: pos2(0.0, 0.0), origin: Point::new(0.0, 0.0), zoom: 1.0, rot: 0, pivot: pos2(0.0, 0.0) };
+ let owners = if merged {
+ [(0, 0), (1, 1)]
+ } else if column {
+ [(0, 1), (1, 1)]
+ } else {
+ [(1, 0), (1, 1)]
+ };
+ let expected: Vec<_> = owners
+ .into_iter()
+ .map(|(r, c)| {
+ let rect = table.cells.iter().find(|cell| cell.row == r && cell.col == c).unwrap().rect;
+ vec![
+ xf.to_screen(m * Point::new(rect.x0, rect.y0)),
+ xf.to_screen(m * Point::new(rect.x1, rect.y0)),
+ xf.to_screen(m * Point::new(rect.x1, rect.y1)),
+ xf.to_screen(m * Point::new(rect.x0, rect.y1)),
+ ]
+ })
+ .collect();
+ let ctx = egui::Context::default();
+ let mut output = ctx.run_ui(egui::RawInput::default(), |ui| {
+ draw_text_selection(&app, ui.painter(), &xf, &st.doc, &layout, ts, Some(selected));
+ });
+ output.textures_delta.clear();
+ let tint = Color32::from_rgba_unmultiplied(80, 140, 255, 90);
+ let painted: Vec<_> = output
+ .shapes
+ .into_iter()
+ .filter_map(|s| match s.shape {
+ egui::Shape::Path(path) if path.fill == tint => {
+ assert!(path.closed);
+ assert_eq!(path.stroke, egui::epaint::PathStroke::NONE);
+ Some(path.points)
+ }
+ _ => None,
+ })
+ .collect();
+ assert_eq!(painted, expected, "only the two touched owners are tinted; column={column}, merged={merged}");
+ assert_eq!(app.session.doc().unwrap().selection, selection);
+ }
+ }
}
#[test]
diff --cc crates/ui-egui/src/control.rs
index bf5aa84f4,15da79df8..5e8cc8ca0
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@@ -173,17 -189,9 +189,17 @@@ fn handle_request(app: &mut DesignApp,
ctx.request_repaint();
ok(Value::Null)
}
+ // What Ctrl/Cmd+V delivers from the system clipboard: text, or SVG markup copied in an
+ // illustration app (pasted as editable paths when no text is being edited).
+ "ui.paste" => {
+ let Some(text) = s("text") else { return err("missing `text`") };
+ app.synthetic.push(egui::Event::Paste(text.to_string()));
+ ctx.request_repaint();
+ ok(Value::Null)
+ }
"ui.move" => {
let pos = egui::pos2(p.get("x").and_then(Value::as_f64).unwrap_or(0.0) as f32, p.get("y").and_then(Value::as_f64).unwrap_or(0.0) as f32);
- app.synthetic.push(egui::Event::PointerMoved(pos));
+ app.control_synthetic.push(egui::Event::PointerMoved(pos));
ctx.request_repaint();
ok(Value::Null)
}
diff --cc crates/ui-egui/src/lib.rs
index ee5c98ed5,1db2f591d..bdaf57588
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@@ -614,10 -637,35 +637,50 @@@ impl DesignApp
if let Some(r) = menus::run_ui(self, id, &params) {
return r;
}
+ // Only user-initiated runs touch the system clipboard, never the control channel's.
+ let system_clipboard = !self.in_control && !self.control_input_frame.load(std::sync::atomic::Ordering::Relaxed);
+ let params = if system_clipboard { self.with_system_clipboard(id, params) } else { params };
++ // Vector art copied in another app (Illustrator, VectorCraft, Figma) is SVG markup on the
++ // system clipboard: with no insertion point it pastes as editable paths, not as text.
++ let typing = self.session.active().is_some_and(|d| d.selection.text.is_some());
++ let (id, params) = match params.get("text").and_then(Value::as_str) {
++ Some(t) if id == "edit.paste" && !typing && designcraft_engine::cmd::is_svg_markup(t) => {
++ let mut p = json!({"svg": t});
++ for k in ["spread", "at"] {
++ if let Some(v) = params.get(k) {
++ p[k] = v.clone();
++ }
++ }
++ ("edit.pasteSvg", p)
++ }
++ _ => (id, params),
++ };
let r = self.session.execute(id, &params).map_err(|e| e.to_string());
self.after_engine();
- if let Err(e) = &r {
- self.status(e.clone());
+ match &r {
+ 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") => {
+ 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 => {
+ self.object_clip_text = self.services.clipboard_text.as_mut().and_then(|f| f());
+ None
+ }
+ };
+ if let (Some(t), Some(ctx)) = (text, &self.egui_ctx) {
+ ctx.copy_text(t);
+ ctx.request_repaint();
+ }
+ }
+ Ok(_) => {}
}
r
}
diff --cc docs/control-protocol.md
index 506984fd6,439987252..911fe74af
--- a/docs/control-protocol.md
+++ b/docs/control-protocol.md
@@@ -37,9 -37,8 +37,9 @@@ so only enable it while you use it. Tra
| `ui.inspect` | — | Tool, UI state, view (zoom/origin), canvas rect, perf |
| `ui.menu.list` / `ui.tool.list` | — | Menu tree / Tools panel groups |
| `ui.tool.select` | `{tool}` | Select a tool (`selection`, `type`, `rectangleFrame`, …) |
- | `ui.pointer` | `{events:[{kind: down\|drag\|up\|move\|doubleclick, x, y, space?: "screen"\|"canvas"}], mods?}` | Drive the active tool through the same code path as the mouse |
+ | `ui.pointer` | `{events:[{kind: down\|drag\|up\|move\|doubleclick, x, y, space?: "screen"\|"canvas", clicks?}], mods?}` | Drive the active tool through the same code path as the mouse; `clicks` on a `down` is the press count (3 = triple click); returns the selection, the tool and its cursor at the last event (e.g. `LoadedText`, `ThreadLink`, `Unthread`) |
| `ui.key` / `ui.text` | `{key, shift?, alt?, cmd?}` / `{text}` | Synthetic keyboard input (typing into a text frame) |
+| `ui.paste` | `{text}` | What Ctrl/Cmd+V delivers from the system clipboard: text into the text being edited, or SVG markup (vector art copied in Illustrator, VectorCraft or Figma) as editable paths in the middle of the view |
| `ui.move` / `ui.click` / `ui.drag` | screen points, `button?: left\|right\|middle` | Real egui pointer input — reaches every widget, menu and panel |
| `ui.set` | `{brightness?, panel?, rulers?, guides?, frameEdges?, baselineGrid?, textThreads?, screenMode?, zoom?, page?, fit?}` | UI state |
| `ui.dialog.open` | `{id, fields?}` | Open a dialog by id (e.g. `paragraphStyleOptions` or `characterStyleOptions` with `{name, section}`, or `{new: true}` for a new style) |