Merge pull request #8527 from dotCooCoo/feat/transform-rectangle-corner-type

Transform: Rectangle Properties has a Corner menu, one per corner when Corner Radius is unlinked
This commit is contained in:
BFlat
2026-11-20 03:46:22 +07:00
committed by GitHub
9 changed files with 444 additions and 18 deletions
+1 -1
View File
@@ -305,7 +305,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Live Shape Properties",
[],
None,
"{id?, ids?, radius?: pt (or % of each corner's maximum when `relativeRadii` is true), kind?: \"round\"|\"invertedRound\"|\"chamfer\", relativeRadii?: bool (Relative rounding, as in the reference app: store radii as fractions of each corner's maximum so they scale with the shape), corners?: [i…], items?: [{id, corners?: [i…]}] (several objects, each with its own corners, in place of ids and corners: the canvas's corner widgets across a selection), sides?: n, polygonAngle?: degrees (counter-clockwise from its first vertex straight up), polygonRadius?: pt (centre to vertex), sideLength?: pt (sets the radius), makeSidesEqual?: true (a polygon scaled unevenly: drops the uneven scale and shear, keeping its centre, angle and mean radius), pieStart?, pieEnd?: degrees (an ellipse's pie, counter-clockwise from 3 o'clock; 0 to 360 is the whole ellipse), invertPie?: true (swaps them: the other part of the ellipse), constrainPie?: bool (Constrain Pie Angle: with one of pieStart and pieEnd, not both and not with invertPie, the other angle moves by as much, so each ellipse keeps its span), lineLength?: pt (a live line's length, above 0; its first end stays), lineAngle?: degrees (a live line's angle, counter-clockwise from 3 o'clock, from its first end to its second; it turns about its midpoint), rectangleAngle?: degrees (a live rectangle's, counterclockwise from level), ellipseAngle?: degrees (a live ellipse's, counterclockwise from level; its pie turns with it)} (rectangleAngle, ellipseAngle and polygonAngle turn each shape of that kind about its own center and leave other objects as they are; a pixel-aligned shape then goes back on the pixel grid, as after other edits, which can move its center or change its size; an angle that is not a number is an error, and `null` counts as an omitted key; a shape that a turn would take past the coordinate limit (±4,000,000 pt) is an error that changes nothing; Live Corners on any path: radius and kind set its corners (anchors that aren't smooth, where two sides, straight or curved (#1144), meet at an angle): `corners` (anchor indices of the path with its corners uncut, counting every subpath's anchors in order: a rectangle's 0 top-left, 1 top-right, 2 bottom-right, 3 bottom-left; a polygon's from the first vertex clockwise), else the corners with a Direct-Selected anchor, else every corner; each radius is drawn no larger than half the corner's shorter side allows; a path that isn't a live shape keeps its uncut outline so its corners stay editable, and is plain again once none is cut; sides: a polygon's, which keep the radius they shared; the polygon params apply in that order: sides, makeSidesEqual, polygonAngle, then sideLength or else polygonRadius; a line's lineLength applies before its lineAngle)",
"{id?, ids?, radius?: pt (or % of each corner's maximum when `relativeRadii` is true), kind?: \"round\"|\"invertedRound\"|\"chamfer\", relativeRadii?: bool (Relative rounding, as in the reference app: store radii as fractions of each corner's maximum so they scale with the shape), corners?: [i…], rectanglesOnly?: bool (only the live rectangles among the targets; the other objects stay as they are), items?: [{id, corners?: [i…]}] (several objects, each with its own corners, in place of ids and corners: the canvas's corner widgets across a selection), sides?: n, polygonAngle?: degrees (counter-clockwise from its first vertex straight up), polygonRadius?: pt (centre to vertex), sideLength?: pt (sets the radius), makeSidesEqual?: true (a polygon scaled unevenly: drops the uneven scale and shear, keeping its centre, angle and mean radius), pieStart?, pieEnd?: degrees (an ellipse's pie, counter-clockwise from 3 o'clock; 0 to 360 is the whole ellipse), invertPie?: true (swaps them: the other part of the ellipse), constrainPie?: bool (Constrain Pie Angle: with one of pieStart and pieEnd, not both and not with invertPie, the other angle moves by as much, so each ellipse keeps its span), lineLength?: pt (a live line's length, above 0; its first end stays), lineAngle?: degrees (a live line's angle, counter-clockwise from 3 o'clock, from its first end to its second; it turns about its midpoint), rectangleAngle?: degrees (a live rectangle's, counterclockwise from level), ellipseAngle?: degrees (a live ellipse's, counterclockwise from level; its pie turns with it)} (rectangleAngle, ellipseAngle and polygonAngle turn each shape of that kind about its own center and leave other objects as they are; a pixel-aligned shape then goes back on the pixel grid, as after other edits, which can move its center or change its size; an angle that is not a number is an error, and `null` counts as an omitted key; a shape that a turn would take past the coordinate limit (±4,000,000 pt) is an error that changes nothing; Live Corners on any path: radius and kind set its corners (anchors that aren't smooth, where two sides, straight or curved (#1144), meet at an angle): `corners` (anchor indices of the path with its corners uncut, counting every subpath's anchors in order: a rectangle's 0 top-left, 1 top-right, 2 bottom-right, 3 bottom-left; a polygon's from the first vertex clockwise), else the corners with a Direct-Selected anchor, else every corner; each radius is drawn no larger than half the corner's shorter side allows; a path that isn't a live shape keeps its uncut outline so its corners stay editable, and is plain again once none is cut; sides: a polygon's, which keep the radius they shared; the polygon params apply in that order: sides, makeSidesEqual, polygonAngle, then sideLength or else polygonRadius; a line's lineLength applies before its lineAngle)",
has_selection,
set_live_shape
),
+20 -6
View File
@@ -124,7 +124,7 @@ fn line_edit(l: &LiveShape, len: Option<f64>, angle: Option<f64>) -> Option<Live
}
pub(super) fn set_live_shape(s: &mut Session, p: &Value) -> Result<Value> {
check_switches(p, &["constrainPie", "invertPie", "makeSidesEqual", "relativeRadii"], "object.setLiveShape")?;
check_switches(p, &["constrainPie", "invertPie", "makeSidesEqual", "rectanglesOnly", "relativeRadii"], "object.setLiveShape")?;
check_numbers(p, &["radius", "sides"], "object.setLiveShape")?;
// `items`: objects each with its own `corners`; else `ids` (or the selection) sharing them.
let items = match p.get("items").filter(|v| !v.is_null()) {
@@ -235,6 +235,13 @@ pub(super) fn set_live_shape(s: &mut Session, p: &Value) -> Result<Value> {
// When every value given is the current one, the document and its undo history stay as they
// are.
let st = s.doc()?;
// `rectanglesOnly`: the live rectangles among the targets; the other objects stay as they are.
let ids: Vec<NodeId> = if p.get("rectanglesOnly").and_then(Value::as_bool) == Some(true) {
let rectangle = |id: &NodeId| matches!(st.doc.node(*id).map(|n| &n.kind), Some(NodeKind::Path { live: Some(l), .. }) if matches!(**l, LiveShape::Rectangle { .. }));
ids.into_iter().filter(rectangle).collect()
} else {
ids
};
// The lines that Line Length or Line Angle changes, as they become. A line that would reach
// past the document's coordinate limit is an error before anything changes.
let mut lines = std::collections::BTreeMap::new();
@@ -278,16 +285,23 @@ pub(super) fn set_live_shape(s: &mut Session, p: &Value) -> Result<Value> {
}
let Some(c) = LiveCorners::new(path, live.as_deref()) else { return true };
let picked = corners_of(*id).unwrap_or_else(|| c.picked(st.selection.anchors.get(id)));
// Setting corners folds a scale left in a rectangle's transform into its size, which
// changes it.
live.as_deref().is_none_or(|l| l.folded() == *l)
// Setting a radius folds a scale left in a rectangle's transform into its size, which
// changes it. A kind the corners already have leaves the rectangle as it is.
(radius.is_none() || live.as_deref().is_none_or(|l| l.folded() == *l))
&& picked.iter().all(|k| {
*k < c.base.anchor_count()
&& radius.is_none_or(|r| (c.drawn_radius(*k) - r.max(0.0)).abs() < 1e-9)
// A radius stored in document units can be larger than it draws; a relative
// one is a fraction, so it is compared as drawn.
&& radius.is_none_or(|r| {
let now = if c.radii_relative() { c.drawn_radius(*k) } else { c.radius(*k) };
(now - r.max(0.0)).abs() < 1e-9
})
&& kind.is_none_or(|kd| c.kind(*k) == kd)
})
};
if ids.iter().all(has_values) {
// The objects every value given already matches stay as they are, beside others that change.
let ids: Vec<NodeId> = ids.into_iter().filter(|id| !has_values(id)).collect();
if ids.is_empty() {
return ok();
}
s.edit("Live Shape", |d, sel| {
+101
View File
@@ -59,6 +59,107 @@ fn a_whole_rectangle_rounds_all_corners_and_given_corners_alone() {
assert_eq!(corners(&s, id).1[3], CornerKind::Chamfer);
}
/// The Corner menus of Rectangle Properties: a kind for all four corners or one, set on corners
/// with no radius too and leaving the radii as they are, in one undo step; the kind the corners
/// already have adds no step.
#[test]
fn rectangle_kind_menu_targets_all_or_one_corner_and_skips_noop_undo() {
let (mut s, id) = session();
run(&mut s, json!({"corners": [1], "radius": 12}));
let steps = |s: &Session| s.doc().unwrap().history.undo.len();
let undo = steps(&s);
run(&mut s, json!({"corners": [0, 1, 2, 3], "kind": "invertedRound"}));
assert_eq!(corners(&s, id).0, [0.0, 12.0, 0.0, 0.0]);
assert_eq!(corners(&s, id).1, [CornerKind::InvertedRound; 4]);
assert_eq!(steps(&s), undo + 1);
run(&mut s, json!({"corners": [0, 1, 2, 3], "kind": "invertedRound"}));
run(&mut s, json!({"corners": [2], "kind": "invertedRound"}));
assert_eq!(steps(&s), undo + 1, "no step when nothing changes");
run(&mut s, json!({"corners": [2], "kind": "chamfer"}));
assert_eq!(corners(&s, id).1, [CornerKind::InvertedRound, CornerKind::InvertedRound, CornerKind::Chamfer, CornerKind::InvertedRound]);
assert_eq!(corners(&s, id).0, [0.0, 12.0, 0.0, 0.0]);
assert_eq!(steps(&s), undo + 2);
}
/// A kind the corners already have leaves a rectangle saved stretched by an older version as it
/// is, alone or beside a rectangle the kind changes: its scale stays in its transform.
#[test]
fn a_kind_the_corners_have_leaves_a_stretched_rectangle() {
let (mut s, id) = session();
let other = NodeId(s.execute("shape.rectangle", &json!({"x": 200, "y": 10, "width": 40, "height": 40})).unwrap()["id"].as_u64().unwrap());
let stretched = LiveShape::Rectangle {
w: 50.0,
h: 60.0,
radii: [10.0; 4],
kinds: [CornerKind::Chamfer; 4],
radii_relative: false,
xf: Affine::translate((10.0, 10.0)) * Affine::scale_non_uniform(2.0, 1.0),
};
{
let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
let NodeKind::Path { path, live: Some(live), .. } = &mut d.node_mut(id).unwrap().kind else { panic!("not live") };
**live = stretched.clone();
*path = live.to_path();
}
let live = |s: &Session| node_path(s, id).1;
let steps = |s: &Session| s.doc().unwrap().history.undo.len();
let undo = steps(&s);
run(&mut s, json!({"ids": [id.0], "kind": "chamfer"}));
assert_eq!(live(&s), Some(stretched.clone()));
assert_eq!(steps(&s), undo, "no step when nothing changes");
run(&mut s, json!({"ids": [id.0, other.0], "kind": "chamfer"}));
assert_eq!(live(&s), Some(stretched));
assert_eq!(corners(&s, other).1, [CornerKind::Chamfer; 4]);
assert_eq!(steps(&s), undo + 1);
}
/// A radius is stored on a corner that stores a larger radius drawn at the new value, alone or
/// beside another rectangle; the radius every target already stores adds no undo step.
#[test]
fn a_radius_reaches_a_rectangle_that_already_draws_it() {
let (mut s, id) = session();
let other = NodeId(s.execute("shape.rectangle", &json!({"x": 200, "y": 10, "width": 100, "height": 60})).unwrap()["id"].as_u64().unwrap());
// 80 pt on every corner draws at 30 pt, half the 60 pt side.
run(&mut s, json!({"ids": [id.0], "radius": 80}));
assert_eq!(corners(&s, id).0, [80.0; 4]);
let (path, live) = node_path(&s, id);
let drawn = LiveCorners::new(&path, live.as_ref()).unwrap().drawn_radius(0);
assert!((drawn - 30.0).abs() < 1e-9, "draws at {drawn}");
let steps = |s: &Session| s.doc().unwrap().history.undo.len();
let undo = steps(&s);
run(&mut s, json!({"ids": [id.0], "corners": [1], "radius": 30}));
assert_eq!(corners(&s, id).0, [80.0, 30.0, 80.0, 80.0]);
assert_eq!(steps(&s), undo + 1);
run(&mut s, json!({"ids": [id.0, other.0], "corners": [0], "radius": 30}));
assert_eq!(corners(&s, id).0[0], 30.0);
assert_eq!(corners(&s, other).0[0], 30.0);
// The radius both already store adds no step.
run(&mut s, json!({"ids": [id.0, other.0], "corners": [0], "radius": 30}));
assert_eq!(steps(&s), undo + 2);
}
/// `rectanglesOnly` sets the live rectangles among the selection and leaves a polygon as it is; an
/// action that records it holds no ids, so playing it sets the rectangles selected then.
#[test]
fn rectangles_only_sets_the_selected_rectangles_when_an_action_plays() {
let (mut s, id) = session();
let polygon = NodeId(s.execute("shape.polygon", &json!({"cx": 300, "cy": 300, "radius": 40, "sides": 6})).unwrap()["id"].as_u64().unwrap());
s.execute("select.set", &json!({"ids": [id.0, polygon.0]})).unwrap();
let start = s.journal.len();
run(&mut s, json!({"rectanglesOnly": true, "corners": [0], "kind": "chamfer"}));
assert_eq!(corners(&s, id).1[0], CornerKind::Chamfer);
let NodeKind::Path { live: Some(l), .. } = &s.doc().unwrap().doc.node(polygon).unwrap().kind else { panic!("not live") };
let LiveShape::Polygon { kinds, .. } = &**l else { panic!("not a polygon") };
assert!(kinds.iter().all(|k| *k == CornerKind::Round), "the polygon keeps its corners: {kinds:?}");
let action = s.journal_for_action(start);
assert_eq!(action.len(), 1);
assert_eq!(action[0].1.get("ids"), None);
let other = NodeId(s.execute("shape.rectangle", &json!({"x": 200, "y": 10, "width": 40, "height": 40})).unwrap()["id"].as_u64().unwrap());
s.execute("select.set", &json!({"ids": [other.0, polygon.0]})).unwrap();
s.replay(&action[0].0, &action[0].1).unwrap();
assert_eq!(corners(&s, other).1[0], CornerKind::Chamfer);
}
/// #938: `items` rounds several objects in one step, each its own corners (or all of them).
#[test]
fn items_round_several_objects_each_its_own_corners() {