Merge pull request #13371 from RickiNano/fix/13353-triangle-layer-name

Triangle tool: shape layers are named Triangle N, not Polygon N (#13353)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-12 21:21:04 +01:00
commit 134208d7fd
4 files changed
+96 -4

No files matched your search

+24 -2
View File
@@ -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<Value> {
/// The path `shape.create` draws for live-shape params `p` (the Shape tools preview it while dragging).
pub fn shape_path(p: &Value) -> Result<Path> {
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",
@@ -443,7 +460,8 @@ pub(super) fn shape_create(s: &mut Session, p: &Value) -> Result<Value> {
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)?;
@@ -786,6 +804,10 @@ pub(super) fn selection_to_shape(s: &mut Session, p: &Value) -> Result<Value> {
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::*;
@@ -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);
}
+1 -1
View File
@@ -124,7 +124,7 @@ pub fn geometry(f: &Map<String, Value>) -> Result<Value, String> {
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));
+1 -1
View File
@@ -129,7 +129,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)}),