From 23a66cb159bda97106dd9e74dd7936c80ee0be34 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Wed, 11 Nov 2026 22:14:01 +0100 Subject: [PATCH] Triangle tool: shape layers are named Triangle N, not Polygon N (#13353) The Triangle tool and Create Triangle send `shape.create` with `kind: "triangle"`. The engine builds it as a 3-sided live polygon, the same as before, and names the layer from the default base name "Triangle". Polygon N stays for polygons, and each base name is counted separately. An explicit `name` still wins. --- crates/engine/src/cmd/vector_cmds/shape.rs | 26 ++++++- .../vector_cmds/triangle_layer_name_tests.rs | 70 +++++++++++++++++++ crates/ui-egui/src/dialogs/shape_dialog.rs | 2 +- crates/ui-egui/src/tools/vector_ui/shapes.rs | 2 +- 4 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 crates/engine/src/cmd/vector_cmds/triangle_layer_name_tests.rs diff --git a/crates/engine/src/cmd/vector_cmds/shape.rs b/crates/engine/src/cmd/vector_cmds/shape.rs index 547ad47eca5..a191e238493 100644 --- a/crates/engine/src/cmd/vector_cmds/shape.rs +++ b/crates/engine/src/cmd/vector_cmds/shape.rs @@ -6,6 +6,7 @@ use super::{ Affine, Color, Document, EngineError, Fill, FillRule, Layer, LayerContent, LayerId, LiveShape, Path, PathOp, Point, Result, SHAPE_BLOCKS, Session, ShapeLayer, Value, bad, json, sel, vector, }; +use std::borrow::Cow; fn active_shape(s: &Session) -> std::result::Result<&ShapeLayer, String> { let d = s.active().ok_or("no document open")?; @@ -70,7 +71,22 @@ pub(super) fn paste_shape_stroke(s: &mut Session, p: &Value) -> Result { /// The path `shape.create` draws for live-shape params `p` (the Shape tools preview it while dragging). pub fn shape_path(p: &Value) -> Result { let kind = p.get("kind").and_then(Value::as_str).unwrap_or("rect"); - Ok(vector::shapes::live_path(&live_from_params("shape.create", kind, p, None)?)) + let (kind, p) = triangle_as_polygon(kind, p); + Ok(vector::shapes::live_path(&live_from_params("shape.create", kind, &p, None)?)) +} + +/// `kind: "triangle"` (the Triangle tool, Create Triangle) is a 3-sided polygon: the live-shape +/// kind and params to build it from. Its own kind only names the new layer (Triangle N). +fn triangle_as_polygon<'a>(kind: &'a str, p: &'a Value) -> (&'a str, Cow<'a, Value>) { + if kind != "triangle" { + return (kind, Cow::Borrowed(p)); + } + let mut q = p.clone(); + if let Some(o) = q.as_object_mut() { + o.insert("sides".into(), json!(3)); + o.remove("starRatio"); + } + ("polygon", Cow::Owned(q)) } fn live_kind(l: &LiveShape) -> &'static str { @@ -426,6 +442,7 @@ fn default_name(kind: &str) -> &'static str { "roundedRect" | "roundRect" => "Rounded Rectangle", "ellipse" => "Ellipse", "polygon" => "Polygon", + "triangle" => "Triangle", "star" => "Star", "line" => "Line", _ => "Shape", @@ -442,7 +459,8 @@ pub(super) fn shape_create(s: &mut Session, p: &Value) -> Result { let v = p.get("path").ok_or_else(|| bad(CMD, "kind \"path\" needs `path`"))?; (parse_path(v).map_err(|e| bad(CMD, e))?, None) } else { - let l = live_from_params(CMD, kind, p, None)?; + let (live_kind, q) = triangle_as_polygon(kind, p); + let l = live_from_params(CMD, live_kind, &q, None)?; (vector::shapes::live_path(&l), Some(l)) }; let (fill, stroke) = new_fill_stroke(s, CMD, p)?; @@ -782,6 +800,10 @@ pub(super) fn selection_to_shape(s: &mut Session, p: &Value) -> Result { shape_create(s, &q) } +#[cfg(test)] +#[path = "triangle_layer_name_tests.rs"] +mod triangle_layer_name_tests; + #[cfg(test)] mod arrow_affine_regression_tests { use super::*; diff --git a/crates/engine/src/cmd/vector_cmds/triangle_layer_name_tests.rs b/crates/engine/src/cmd/vector_cmds/triangle_layer_name_tests.rs new file mode 100644 index 00000000000..5cfddca5ba6 --- /dev/null +++ b/crates/engine/src/cmd/vector_cmds/triangle_layer_name_tests.rs @@ -0,0 +1,70 @@ +//! The Triangle tool and Create Triangle name their shape layers Triangle N, not Polygon N (#13353). + +use serde_json::{Value, json}; + +use super::super::{LayerContent, LayerId, LiveShape, shape_path}; +use crate::Session; + +fn session() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 200, "height": 160, "background": "white"})).unwrap(); + s +} + +/// The params the Triangle tool's drag and Create Triangle's OK send. +fn triangle(rect: [f64; 4]) -> Value { + json!({"kind": "triangle", "rect": rect}) +} + +fn name(info: &Value) -> &str { + info["name"].as_str().unwrap() +} + +#[test] +fn triangles_are_named_triangle_n() { + let mut s = session(); + let first = s.execute("shape.create", triangle([20.0, 20.0, 60.0, 40.0])).unwrap(); + let second = s.execute("shape.create", triangle([90.0, 30.0, 50.0, 50.0])).unwrap(); + assert_eq!((name(&first), name(&second)), ("Triangle 1", "Triangle 2")); +} + +#[test] +fn a_triangle_is_a_live_three_sided_polygon() { + let mut s = session(); + // Any `sides` or `starRatio` sent along is ignored: a triangle has 3 sides and no star. + let p = json!({"kind": "triangle", "rect": [20, 20, 60, 40], "sides": 7, "starRatio": 0.3, "cornerRadius": 4}); + let id = s.execute("shape.create", p.clone()).unwrap()["layer"].as_u64().unwrap(); + let LayerContent::Shape(sh) = &s.active().unwrap().doc.layer(LayerId(id)).unwrap().content else { panic!("not a shape layer") }; + assert_eq!(sh.live, Some(LiveShape::Polygon { rect: [20.0, 20.0, 60.0, 40.0], sides: 3, star_ratio: 1.0, corner_radius: 4.0 })); + // The drag's preview and Path mode draw the same outline. + let polygon = json!({"kind": "polygon", "rect": [20, 20, 60, 40], "sides": 3, "cornerRadius": 4}); + assert_eq!(shape_path(&p).unwrap(), shape_path(&polygon).unwrap()); + assert_eq!(sh.path, shape_path(&polygon).unwrap()); +} + +#[test] +fn polygons_and_triangles_count_on_their_own() { + let mut s = session(); + let tri = s.execute("shape.create", triangle([20.0, 20.0, 60.0, 40.0])).unwrap(); + let poly = s.execute("shape.create", json!({"kind": "polygon", "rect": [90, 30, 50, 50], "sides": 5})).unwrap(); + let three = s.execute("shape.create", json!({"kind": "polygon", "rect": [90, 90, 50, 50], "sides": 3})).unwrap(); + assert_eq!((name(&tri), name(&poly), name(&three)), ("Triangle 1", "Polygon 1", "Polygon 2")); +} + +#[test] +fn an_explicit_name_wins() { + let mut s = session(); + let info = s.execute("shape.create", json!({"kind": "triangle", "rect": [20, 20, 60, 40], "name": "Roof"})).unwrap(); + assert_eq!(name(&info), "Roof"); +} + +#[test] +fn a_triangle_without_a_rect_is_refused() { + let mut s = session(); + let history = s.active().unwrap().history.past_len(); + for p in [json!({"kind": "triangle"}), json!({"kind": "triangle", "rect": "x"})] { + assert!(s.execute("shape.create", p.clone()).is_err(), "{p}"); + } + assert!(shape_path(&json!({"kind": "triangle"})).is_err()); + assert_eq!(s.active().unwrap().history.past_len(), history); +} diff --git a/crates/ui-egui/src/dialogs/shape_dialog.rs b/crates/ui-egui/src/dialogs/shape_dialog.rs index fc9a3e9ea67..95180b8ec05 100644 --- a/crates/ui-egui/src/dialogs/shape_dialog.rs +++ b/crates/ui-egui/src/dialogs/shape_dialog.rs @@ -124,7 +124,7 @@ pub fn geometry(f: &Map) -> Result { if radii.iter().any(|r| *r > 0.0) { json!({"kind": "roundedRect", "rect": rect, "radii": radii}) } else { json!({"kind": "rect", "rect": rect}) } } "ellipse" => json!({"kind": "ellipse", "rect": rect}), - "triangle" => json!({"kind": "polygon", "rect": rect, "sides": 3}), + "triangle" => json!({"kind": "triangle", "rect": rect}), "polygon" => { let sides = num(f, "sides").map_or(5.0, |s| s.round().clamp(3.0, 100.0)); let ratio = num(f, "starRatio").map_or(100.0, |r| r.clamp(1.0, 100.0)); diff --git a/crates/ui-egui/src/tools/vector_ui/shapes.rs b/crates/ui-egui/src/tools/vector_ui/shapes.rs index 0776227d04b..9c2028919f7 100644 --- a/crates/ui-egui/src/tools/vector_ui/shapes.rs +++ b/crates/ui-egui/src/tools/vector_ui/shapes.rs @@ -116,7 +116,7 @@ pub(super) fn shape_geometry(o: &ToolOptions, tool: Tool, start: [f64; 2], end: Tool::Rectangle if o.corner_radius > 0.0 => json!({"kind": "roundedRect", "rect": rect, "radii": vec![o.corner_radius; 4]}), Tool::Rectangle | Tool::CustomShape => json!({"kind": "rect", "rect": rect}), Tool::EllipseShape => json!({"kind": "ellipse", "rect": rect}), - Tool::Triangle => json!({"kind": "polygon", "rect": rect, "sides": 3}), + Tool::Triangle => json!({"kind": "triangle", "rect": rect}), // PhotoCraft's star defaults; the Star Tool's own options are not measured yet (#11377). Tool::Star => json!({"kind": "star", "rect": rect, "sides": 5, "starRatio": 0.5}), _ => json!({"kind": "polygon", "rect": rect, "sides": o.polygon_sides.max(3)}),