mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 11:05:03 +00:00
Transform: a corner kind the rectangle already has leaves it unchanged
A kind-only object.setLiveShape call now leaves each rectangle whose corners already have the kind as it is, even when its transform keeps a scale, and changes only the other targets. The Corner menu applies a pending tool edit before it reads the selected rectangles. The popup test checks that the list opened before it picks the Chamfer row.
This commit is contained in:
@@ -265,18 +265,21 @@ 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)
|
||||
&& 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 unchanged: BTreeSet<NodeId> = ids.iter().copied().filter(has_values).collect();
|
||||
if ids.iter().all(|id| unchanged.contains(id)) {
|
||||
return ok();
|
||||
}
|
||||
let ids: Vec<NodeId> = ids.into_iter().filter(|id| !unchanged.contains(id)).collect();
|
||||
s.edit("Live Shape", |d, sel| {
|
||||
for id in &ids {
|
||||
let Some(NodeKind::Path { path, live, .. }) = d.node_mut(*id).map(|n| &mut n.kind) else { continue };
|
||||
|
||||
@@ -81,6 +81,38 @@ fn rectangle_kind_menu_targets_all_or_one_corner_and_skips_noop_undo() {
|
||||
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);
|
||||
}
|
||||
|
||||
/// #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() {
|
||||
|
||||
@@ -142,6 +142,10 @@ fn set_rectangle_kinds(app: &mut VectorcraftApp, kind: CornerKind, corner: Optio
|
||||
/// step, or none when nothing changes.
|
||||
fn set_rectangle_corners(app: &mut VectorcraftApp, mut p: Value, corner: Option<usize>) {
|
||||
p["corners"] = corner.map_or(json!([0, 1, 2, 3]), |k| json!([k]));
|
||||
// The edit the tool computes on another thread comes first: the rectangles are the ones
|
||||
// selected after it.
|
||||
let r = app.session.finish_tool_edit();
|
||||
crate::canvas::apply_requests(app, r);
|
||||
match app.session.active() {
|
||||
// The command refuses the selection, with its message, while part of a symbol's art is
|
||||
// selected.
|
||||
|
||||
@@ -184,6 +184,53 @@ fn a_rectangle_corner_kind_without_a_selected_rectangle_changes_nothing() {
|
||||
assert_eq!(st.history.undo.len(), steps);
|
||||
}
|
||||
|
||||
/// A corner kind picked while a Shape Builder merge is computed on another thread applies the
|
||||
/// merge first, and sets the kind on the live rectangles selected after it.
|
||||
#[test]
|
||||
fn a_rectangle_corner_kind_applies_a_merge_on_another_thread_first() {
|
||||
use vectorcraft_geom::shapes::CornerKind::Chamfer;
|
||||
use vectorcraft_tools::{Mods, PointerEvent, PointerKind};
|
||||
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 800, "height": 200})).unwrap();
|
||||
for k in 0..40 {
|
||||
app.run("shape.rectangle", json!({"x": k * 10, "y": 0, "width": 20, "height": 20})).unwrap();
|
||||
}
|
||||
app.run("select.all", json!({})).unwrap();
|
||||
app.run("tool.select", json!({"tool": "shapeBuilder"})).unwrap();
|
||||
let view = app.view_info();
|
||||
let at = |kind| PointerEvent {
|
||||
kind,
|
||||
pos: vectorcraft_geom::Point::new(15.0, 10.0),
|
||||
mods: Mods::default(),
|
||||
pressure: 1.0,
|
||||
backquote: false,
|
||||
pen: None,
|
||||
};
|
||||
crate::canvas::dispatch(&mut app, &at(PointerKind::Move), view);
|
||||
app.session.tool_poll(view, true).unwrap();
|
||||
crate::canvas::dispatch(&mut app, &at(PointerKind::Down), view);
|
||||
crate::canvas::dispatch(&mut app, &at(PointerKind::Up), view);
|
||||
assert!(app.session.tool_editing());
|
||||
crate::panels::set_rectangle_kinds(&mut app, Chamfer, None);
|
||||
assert!(!app.session.tool_editing());
|
||||
let st = app.session.active().unwrap();
|
||||
let labels: Vec<_> = st.history.undo.iter().rev().take(2).map(|e| e.label.as_str()).collect();
|
||||
assert_eq!(labels, ["Live Shape", "Shape Builder"]);
|
||||
let kinds: Vec<_> = st
|
||||
.selection
|
||||
.objects
|
||||
.iter()
|
||||
.filter_map(|id| match &st.doc.node(*id)?.kind {
|
||||
NodeKind::Path { live: Some(l), .. } => match &**l {
|
||||
LiveShape::Rectangle { kinds, .. } => Some(*kinds),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert!(!kinds.is_empty() && kinds.iter().all(|k| *k == [Chamfer; 4]), "{kinds:?}");
|
||||
}
|
||||
|
||||
/// #3451: a live ellipse shows pie start/end angles and Invert Pie in the Transform panel and
|
||||
/// the Properties panel's Transform section; typed angles update the shape.
|
||||
#[test]
|
||||
|
||||
@@ -774,7 +774,11 @@ fn the_shape_properties_popup_changes_a_rectangle_corner_type() {
|
||||
assert_eq!(kinds(&app), ([Chamfer; 4], [10.0; 4]), "all four corners, radii kept");
|
||||
assert_eq!(steps(&app), before + 1, "one undo step");
|
||||
let texts = click(&mut app, at(&texts, "Chamfer"));
|
||||
click(&mut app, entry(&texts, "Chamfer"));
|
||||
// The list is open: its Chamfer row shows as well as the closed menu's, and the pick closes it.
|
||||
assert_eq!(texts.iter().filter(|(t, _)| t == "Chamfer").count(), 2, "{texts:?}");
|
||||
assert!(texts.iter().any(|(t, _)| t == "Inverted Round"), "{texts:?}");
|
||||
let texts = click(&mut app, entry(&texts, "Chamfer"));
|
||||
assert!(!texts.iter().any(|(t, _)| t == "Inverted Round"), "the pick closes the list: {texts:?}");
|
||||
assert_eq!(steps(&app), before + 1, "the same kind adds no undo step");
|
||||
|
||||
app.run("prefs.set", json!({"key": "singleCornerRadius", "value": false})).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user