Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Address CodeRabbit review: refuse a bent full cylinder warp, report path type errors

David Manncd8f87966636
diff --git a/crates/engine/src/warp_cmds.rs b/crates/engine/src/warp_cmds.rs
index 242897671..2a89ce5d5 100644
--- a/crates/engine/src/warp_cmds.rs
+++ b/crates/engine/src/warp_cmds.rs
@@ -77,6 +77,9 @@ pub fn warp_from_params(cmd: &str, p: &Value, rect: [f64; 4]) -> Result<Warp> {
let mut w: Warp = serde_json::from_value(w.clone()).map_err(|e| bad(cmd, format!("bad `warp`: {e}")))?;
// A cylinder is its values: the mesh is rebuilt from them, whatever was passed.
if w.style == WarpStyle::Cylinder {
+ if w.vertical || w.h_distort != 0.0 || w.v_distort != 0.0 {
+ return Err(bad(cmd, CYLINDER_SHAPE));
+ }
w = Warp::cylinder(&w.values, w.bounds).ok_or_else(|| bad(cmd, CYLINDER_VALUES))?;
}
if w.mesh.as_ref().is_some_and(|m| !m.is_valid()) {
@@ -96,7 +99,7 @@ pub fn warp_from_params(cmd: &str, p: &Value, rect: [f64; 4]) -> Result<Warp> {
w.vertical = p.get("vertical").and_then(Value::as_bool).unwrap_or(false);
if style == WarpStyle::Cylinder {
if w.vertical || w.h_distort != 0.0 || w.v_distort != 0.0 {
- return Err(bad(cmd, "a cylinder is horizontal, with no hDistort or vDistort"));
+ return Err(bad(cmd, CYLINDER_SHAPE));
}
let values: Option<Vec<f64>> = p.get("values").and_then(Value::as_array).and_then(|a| a.iter().map(Value::as_f64).collect());
return values.and_then(|v| Warp::cylinder(&v, rect)).ok_or_else(|| bad(cmd, CYLINDER_VALUES));
@@ -123,6 +126,7 @@ pub fn warp_from_params(cmd: &str, p: &Value, rect: [f64; 4]) -> Result<Warp> {
const CYLINDER_VALUES: &str =
"a cylinder needs `values`: seven numbers [left, bottom, right, top, top depth, bottom depth, wrap 0..1], the right silhouette right of the left one";
+const CYLINDER_SHAPE: &str = "a cylinder is horizontal, with no hDistort or vDistort";
/// A document-space cylinder `w` stored on a smart object, whose placement's inverse is `inv` and
/// whose warp box `w.bounds` lands on `sb` in source space. A placement that only scales and
@@ -746,6 +750,9 @@ mod tests {
json!({"style": "cylinder", "values": CYL, "vertical": true}),
json!({"style": "cylinder", "values": CYL, "hDistort": 10}),
json!({"warp": {"style": "cylinder", "bounds": [10.0, 10.0, 60.0, 40.0]}}),
+ json!({"warp": {"style": "cylinder", "values": CYL, "bounds": [10.0, 10.0, 60.0, 40.0], "vertical": true}}),
+ json!({"warp": {"style": "cylinder", "values": CYL, "bounds": [10.0, 10.0, 60.0, 40.0], "hDistort": 10.0}}),
+ json!({"warp": {"style": "cylinder", "values": CYL, "bounds": [10.0, 10.0, 60.0, 40.0], "vDistort": -5.0}}),
] {
let e = s.execute("edit.transform.warp", p.clone()).unwrap_err().to_string();
assert!(e.contains("cylinder"), "{p}: {e}");
diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs
index b4727f9c5..7b54469be 100644
--- a/crates/ui-egui/src/type_tool.rs
+++ b/crates/ui-egui/src/type_tool.rs
@@ -424,14 +424,17 @@ pub fn pointer_up(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2]) {
if let Ok(v) = app.run("type.create", p)
&& let Some(id) = v.get("layer").and_then(serde_json::Value::as_u64)
{
- match &target {
- Some(PathClick::Area { shape }) => {
- let _ = app.run("type.fillPath", json!({"layer": id, "shape": shape, "coalesce": key}));
- }
+ // A failed follow-up leaves point text; say why instead of passing over it.
+ let follow_up = match &target {
+ Some(PathClick::Area { shape }) => app.run("type.fillPath", json!({"layer": id, "shape": shape, "coalesce": key})),
Some(PathClick::OnPath { shape, subpath, start }) => {
- let _ = app.run("type.attachToPath", json!({"layer": id, "shape": shape, "subpath": subpath, "start": start, "coalesce": key}));
+ app.run("type.attachToPath", json!({"layer": id, "shape": shape, "subpath": subpath, "start": start, "coalesce": key}))
}
- None => {}
+ None => Ok(serde_json::Value::Null),
+ };
+ if let Err(e) = follow_up {
+ app.ui.status = e;
+ app.ui.status_error = true;
}
if o.type_aa != "sharp" {
let _ = app.run("type.edit", json!({"layer": id, "antialias": o.type_aa, "coalesce": key}));