From 38337c8c6d98ed91bf539fdd8d3b92edefe6efdd Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sun, 1 Nov 2026 00:15:09 -0600 Subject: [PATCH] Copy CSS: write a live rounded rectangle's border-radius (FredApps review) A shape layer that is still a live rectangle with rounded corners now gets `border-radius` before its fill, as Photoshop writes it: one value when all four corners are equal, else top-left, top-right, bottom-right, bottom-left, in px. Sharp rectangles write none. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/layer_css_cmds.rs | 21 ++++++++++++++----- crates/engine/src/cmd/layer_css_cmds/tests.rs | 19 +++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/layer_css_cmds.rs b/crates/engine/src/cmd/layer_css_cmds.rs index de9008b36c4..2554f024b97 100644 --- a/crates/engine/src/cmd/layer_css_cmds.rs +++ b/crates/engine/src/cmd/layer_css_cmds.rs @@ -8,7 +8,7 @@ //! stack as `z-index`, counted from the bottom layer as 1. use photocraft_doc::text::{TextAlign, TextShape}; -use photocraft_doc::{Document, Fill, Layer, LayerContent, LayerId}; +use photocraft_doc::{Document, Fill, Layer, LayerContent, LayerId, LiveShape}; use photocraft_geom::Point; use serde_json::{Value, json}; @@ -123,10 +123,21 @@ pub fn rule(doc: &Document, z: usize, l: &Layer) -> Option { _ => sized = false, } } - LayerContent::Shape(sh) => match &sh.fill { - Some(Fill::Solid(c)) => props.push(("background-color", rgb(c))), - _ => props.push(("background-image", image(l))), - }, + LayerContent::Shape(sh) => { + // A live rounded rectangle's corners, before the fill as Photoshop writes them: one + // value when all four are equal, else top-left, top-right, bottom-right, bottom-left. + if let Some(LiveShape::Rect { radii, .. }) = &sh.live + && radii.iter().any(|&r| r > 0.0) + { + let px = |r: f64| format!("{}px", number(r.max(0.0))); + let all = if radii.iter().all(|&r| r == radii[0]) { px(radii[0]) } else { radii.map(px).join(" ") }; + props.push(("border-radius", all)); + } + match &sh.fill { + Some(Fill::Solid(c)) => props.push(("background-color", rgb(c))), + _ => props.push(("background-image", image(l))), + } + } LayerContent::Fill(f) => { // A fill layer without a shape covers the canvas. if bounds.is_empty() { diff --git a/crates/engine/src/cmd/layer_css_cmds/tests.rs b/crates/engine/src/cmd/layer_css_cmds/tests.rs index f41bb90b81e..06a46b12246 100644 --- a/crates/engine/src/cmd/layer_css_cmds/tests.rs +++ b/crates/engine/src/cmd/layer_css_cmds/tests.rs @@ -151,3 +151,22 @@ fn bad_params_and_layers_without_css_are_errors() { let mut empty = Session::new(); assert!(empty.execute(ID, json!({})).is_err(), "no document"); } + +/// A live rounded rectangle gives its corners as Photoshop writes them, before the fill: one +/// value when all four are equal, else top-left, top-right, bottom-right, bottom-left; a sharp +/// one writes none. +#[test] +fn a_rounded_rectangle_writes_its_border_radius() { + let mut s = session("Box"); + let id = s.execute("shape.create", json!({"kind": "roundedRect", "rect": [40, 30, 120, 80], "radius": 12, "fill": "#ff0000"})).unwrap()["layer"] + .as_u64() + .unwrap(); + let css = s.execute(ID, json!({"layer": id})).unwrap()["css"].as_str().unwrap().to_string(); + assert!(css.contains(" {\n border-radius: 12px;\n background-color: rgb(255, 0, 0);\n position: absolute;\n"), "{css}"); + s.execute("shape.edit", json!({"layer": id, "radii": [20, 0, 10.5, 5]})).unwrap(); + let css = s.execute(ID, json!({"layer": id})).unwrap()["css"].as_str().unwrap().to_string(); + assert!(css.contains(" border-radius: 20px 0px 10.5px 5px;\n background-color: rgb(255, 0, 0);\n"), "{css}"); + s.execute("shape.edit", json!({"layer": id, "radii": [0, 0, 0, 0]})).unwrap(); + let css = s.execute(ID, json!({"layer": id})).unwrap()["css"].as_str().unwrap().to_string(); + assert!(!css.contains("border-radius") && css.contains(" background-color: rgb(255, 0, 0);\n"), "{css}"); +}