diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index 7cedba6..c17eb10 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -423,6 +423,10 @@ impl Editor { if self.layout.dragging() || self.marquee.is_some() || self.panning { return; } + if ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Copy))) { + self.copy(ctx); + return; + } let command = ctx.input(|i| i.modifiers.command); if command { if ctx.input(|i| i.key_pressed(egui::Key::Z)) { @@ -490,7 +494,7 @@ impl Editor { ui.set_width(600.0); ui.heading("Paste layer(s) values"); ui.label(format!("Selected destinations: {}",p.targets.iter().map(|i|label(*i)).collect::>().join(", "))); - ui.label("Paste draw.io clipboard data or SVG XML, or import an SVG file."); + ui.label("Paste Midra Layout values, draw.io clipboard data or SVG XML, or import SVG."); egui::ScrollArea::vertical().max_height((ctx.screen_rect().height()-360.0).max(150.0)).show(ui,|ui| { if ui.add(egui::TextEdit::multiline(&mut p.text).desired_rows(3).desired_width(f32::INFINITY)).changed() {p.shapes.clear();p.error.clear();} ui.horizontal(|ui| { @@ -1715,6 +1719,37 @@ mod tests { assert_eq!(app.content_hit(covered, origin, 1.0), None); } #[test] + fn native_copy_paste_round_trip_supports_single_and_multiple_layers() { + for selection in [[false, true], [true, true]] { + let mut app = Editor::default(); + app.layout.set_selection(selection); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let before = *app.layout.layers(); + let output = frame(&mut app, &ctx, vec![egui::Event::Copy]); + let text = output + .platform_output + .commands + .iter() + .find_map(|c| match c { + egui::OutputCommand::CopyText(text) => Some(text.clone()), + _ => None, + }) + .unwrap(); + app.layout.nudge(50.0, 30.0).unwrap(); + let moved = *app.layout.layers(); + frame(&mut app, &ctx, vec![egui::Event::Paste(text)]); + let p = app.paste.as_ref().unwrap(); + assert!(p.error.is_empty(), "{}", p.error); + let values = p.values(&app.layout).unwrap(); + assert_eq!(values.len(), selection.iter().filter(|v| **v).count()); + app.layout.apply_import(&values).unwrap(); + assert_eq!(*app.layout.layers(), before); + app.layout.undo(); + assert_eq!(*app.layout.layers(), moved); + } + } + #[test] fn rounded_fields_do_not_quantize_unchanged_values_and_copy_uses_pixels() { let mut app = Editor::default(); let ctx = egui::Context::default(); diff --git a/crates/midra-layout/src/import.rs b/crates/midra-layout/src/import.rs index 787284f..9f696ca 100644 --- a/crates/midra-layout/src/import.rs +++ b/crates/midra-layout/src/import.rs @@ -47,6 +47,9 @@ pub fn parse_values(text: &str) -> Result { if text.len() > 1_048_576 { return Err("Import text exceeds 1 MiB.".into()); } + if text.contains(" — visible box (device container unverified)") { + return parse_midra_values(text); + } if text.trim_start().starts_with('<') && roxmltree::Document::parse(text).is_ok_and(|d| d.root_element().has_tag_name("svg")) { @@ -68,6 +71,62 @@ pub fn parse_values(text: &str) -> Result { .into(), }) } +/// Read the human-readable position values emitted by the app's copy actions. +fn parse_midra_values(text: &str) -> Result { + let lines: Vec<_> = text + .lines() + .map(str::trim) + .filter(|line| !line.is_empty()) + .collect(); + let (blocks, remainder) = lines.as_chunks::<5>(); + if !remainder.is_empty() || blocks.is_empty() || blocks.len() > 256 { + return Err("Incomplete Midra values: each layer needs its name, Horizontal, Vertical, Width and Height (up to 256 layers).".into()); + } + let mut shapes = Vec::new(); + for block in blocks { + let id = block[0] + .strip_suffix(" — visible box (device container unverified)") + .filter(|id| !id.is_empty()) + .ok_or("Invalid Midra layer heading.")?; + let mut values = [0.0; 4]; + for (j, field) in ["Horizontal:", "Vertical:", "Width:", "Height:"] + .iter() + .enumerate() + { + values[j] = block[j + 1] + .strip_prefix(field) + .and_then(|v| v.trim().strip_suffix("px")) + .ok_or_else(|| format!("Missing or invalid {field} value in {id}."))? + .trim() + .parse::() + .map_err(|_| format!("Invalid {field} number in {id}."))?; + if !values[j].is_finite() { + return Err(format!("{field} must be finite in {id}.")); + } + } + let [horizontal, vertical, width, height] = values; + let geometry = Geometry { + x: horizontal - width / 2.0, + y: vertical - height / 2.0, + width, + height, + } + .validate()?; + shapes.push(DetectedShape { + id: id.into(), + geometry, + outline: [0.0; 2], + background: false, + background_only: false, + }); + } + Ok(ImportDocument { + shapes, + svg: false, + note: "Midra Layout: center positions converted to top-left bounds. Copied values are rounded to whole pixels; source formats and crop settings are not copied.".into(), + }) +} + #[derive(Clone, Copy)] struct Matrix { x: f64, diff --git a/crates/midra-layout/tests/import.rs b/crates/midra-layout/tests/import.rs index e17b0b2..5ccbf02 100644 --- a/crates/midra-layout/tests/import.rs +++ b/crates/midra-layout/tests/import.rs @@ -181,3 +181,31 @@ fn drawio_auto_detection_and_svg_text_preserve_fractional_bounds() { assert!(d.svg); assert_eq!(d.shapes[0].geometry.x, 12.25); } + +#[test] +fn midra_values_convert_centers_and_reject_incomplete_or_invalid_batches() { + let one = "01 / Presenter — visible box (device container unverified)\nHorizontal: -10 px\nVertical: 200 px\nWidth: 301 px\nHeight: 251 px"; + let doc = parse_values(&format!("{one}\r\n\r\n{one}")).unwrap(); + assert_eq!(doc.shapes.len(), 2); + assert_eq!( + doc.shapes[0].geometry, + Geometry { + x: -160.5, + y: 74.5, + width: 301.0, + height: 251.0 + } + ); + for bad in [ + one.replace("Width: 301 px", "Width: NaN px"), + one.replace("Height: 251 px", "Height: -1 px"), + one.replace("Vertical: 200 px", "Vertical: inf px"), + one.replace("Width: 301 px", "Width: 301 mm"), + one.replace("Width: 301 px", "Horizontal: 301 px"), + one.replace("Height: 251 px", ""), + format!("{one}\n\nunexpected content"), + ] { + assert!(parse_values(&bad).is_err(), "accepted {bad}"); + assert!(parse_values(&format!("{one}\n\n{bad}")).is_err()); + } +} diff --git a/docs/midra-layout.md b/docs/midra-layout.md index a9a6aa8..c7bd2e2 100644 --- a/docs/midra-layout.md +++ b/docs/midra-layout.md @@ -14,7 +14,7 @@ cargo run --locked -p midra-layout 1. Select the destination layers on the canvas or in the right-hand list (Shift-click to select both). -2. Copy rectangles in draw.io, then press **Ctrl/Cmd-V** with the viewport +2. Copy layer values in Midra Layout or rectangles in draw.io, then press **Ctrl/Cmd-V** with the viewport focused. Alternatively, click **Paste layer(s) values**, paste into the text area and click **Read geometry**. **Import SVG…** opens an SVG file in the same review window; raw SVG XML can also be pasted. @@ -125,7 +125,12 @@ clipboard testing against a running draw.io instance remains to be done. coordinates, display a warning and remain selectable through the layer list. - **Copy position values** (or **Copy selected positions** for multiple layers) copies labeled plain text with each selected layer's center position and - dimensions. This is not a native WebRCS clipboard payload. + dimensions. **Ctrl/Cmd-C** does the same when the viewport has focus. These + values can be pasted back using **Ctrl/Cmd-V** or **Paste layer(s) values**, with + the usual destination mapping and exact layer-count requirement. Center values + are converted back to top-left bounds; whole-pixel export rounding still applies + (an odd width/height can produce a half-pixel top-left). Source formats and crop + settings are not copied. This is not a native WebRCS clipboard payload. ## Align selected layers