From 8c738353e5db35e9dc043c045f09b539c95ae209 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 16:50:13 -0800 Subject: [PATCH] Width Point Edit: OK and Delete act only on the stroke and the paired points it opened on - OK and Delete on several width points are refused when a selected point became, or stopped being, one half of a discontinuous point since the dialog opened, for example after a point was added at its place. Before, Delete removed only the selected half and left the new one, and OK edited only the selected half. - OK and Delete are refused when `ui.dialog.set` changed `id`, `index`, `outIndex` or `indices` after the dialog opened. Before, a changed `id` sent the edit or the removal to another stroke whose width points matched. --- crates/ui-egui/src/dialogs/width_point.rs | 44 ++++++++++-- .../ui-egui/src/dialogs/width_point/tests.rs | 67 +++++++++++++++++++ docs/control-protocol.md | 4 +- docs/mcp.md | 5 +- 4 files changed, 113 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/dialogs/width_point.rs b/crates/ui-egui/src/dialogs/width_point.rs index 56caf05845..65d5680671 100644 --- a/crates/ui-egui/src/dialogs/width_point.rs +++ b/crates/ui-egui/src/dialogs/width_point.rs @@ -32,7 +32,9 @@ //! `outSide2`, `outTotalWidth` and `outLinked`. //! //! OK and Delete on existing points, one or several, are refused when those points changed since -//! the dialog opened or another document is active. +//! the dialog opened (their place, their widths, or which of them pair as a discontinuous point) or +//! another document is active. OK and Delete are also refused when `id`, `index`, `outIndex` or +//! `indices` no longer holds the value it had when the dialog opened. use egui::vec2; use serde_json::{Value, json}; @@ -65,6 +67,10 @@ const DOC: &str = "__doc"; /// The place and side widths `[t, left, right]` (points) of the existing point the one-point dialog /// opened on, and of the outgoing half of a discontinuous point. const AT: &str = "__at"; +/// The values of [`TARGET_KEYS`] when the dialog opened. +const TARGET: &str = "__target"; +/// The fields that name the stroke and the points OK and Delete act on. +const TARGET_KEYS: [&str; 4] = ["id", "index", "outIndex", "indices"]; /// The fields of each set of side widths of several points: Side 1, Side 2, Total Width and the /// link, for the incoming set and the outgoing set. @@ -296,6 +302,14 @@ fn pairs(points: &[(f64, f64, f64)]) -> Vec> { .collect() } +/// The sets of side widths that edit width point `i`, `[incoming, outgoing]`, with the points +/// paired as `pairs` pairs them: an outgoing half is not edited by the incoming set, and an +/// incoming half not by the outgoing set. +fn sets(pairs: &[Option], i: usize) -> [bool; 2] { + let outgoing = i.checked_sub(1).is_some_and(|a| pairs[a] == Some(i)); + [!outgoing, pairs[i].is_none()] +} + /// The width points `indices` of `id` names, each discontinuous point's two halves together. fn target(app: &VectorcraftApp, id: u64, indices: &Value) -> Result { const NONE: &str = "no such width point"; @@ -326,7 +340,7 @@ fn target(app: &VectorcraftApp, id: u64, indices: &Value) -> Result Value { + json!(TARGET_KEYS.map(|key| d.fields.get(key).cloned().unwrap_or(Value::Null))) +} + /// Open Width Point Edit for `index`, or a prospective point at fraction `t` without an index /// (`{id, index?, t?, adjustAdjoining?}`), or for several existing points (`{id, indices, /// adjustAdjoining?}`). Opening only samples the stroke: confirmation creates the point. @@ -409,7 +429,9 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { fields[key] = value; } } - app.ui.dialog = Some(Dialog::new(KIND, fields)); + let mut d = Dialog::new(KIND, fields); + d.fields.insert(TARGET.into(), targets(&d)); + app.ui.dialog = Some(d); Ok(Value::Null) } @@ -547,6 +569,12 @@ fn same_document(app: &VectorcraftApp, d: &Dialog) -> Result<(), String> { } } +/// Whether the dialog still names the stroke and points it opened on: `ui.dialog.set` can set +/// `id`, `index`, `outIndex` and `indices`, and OK and Delete refuse any other value. +fn same_target(d: &Dialog) -> Result<(), String> { + if d.fields.get(TARGET) == Some(&targets(d)) { Ok(()) } else { Err("no such width point".into()) } +} + /// `id`'s width profile and the stroke's half weight, when the points `open` are still where and as /// wide as when the dialog opened, exactly. fn opened_profile(app: &VectorcraftApp, id: u64, open: &[Entry]) -> Result<(WidthProfile, f64), String> { @@ -567,6 +595,12 @@ fn opened_profile(app: &VectorcraftApp, id: u64, open: &[Entry]) -> Result<(Widt return Err(NONE.into()); } } + // Each point is still a continuous point or the same half of a discontinuous one: a point added + // at the place of one would leave it changing only one half. + let pairs = pairs(&profile.points); + if open.iter().any(|o| o.sets != sets(&pairs, o.index)) { + return Err(NONE.into()); + } Ok((profile, half)) } @@ -636,6 +670,7 @@ fn confirm_several(app: &mut VectorcraftApp, d: &Dialog) -> Result = open.iter().map(|e| e.index).collect(); @@ -677,8 +712,9 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { app.ui.dialog = None; return Ok(Value::Null); } - // OK and Delete act only on the document and the points the dialog opened on. + // OK and Delete act only on the document, the stroke and the points the dialog opened on. same_document(app, d)?; + same_target(d)?; if let Some(index) = index.as_u64().and_then(|i| usize::try_from(i).ok()) { unchanged_point(app, d, index, out.as_ref().and_then(Value::as_u64).and_then(|i| usize::try_from(i).ok()))?; } diff --git a/crates/ui-egui/src/dialogs/width_point/tests.rs b/crates/ui-egui/src/dialogs/width_point/tests.rs index 0653c5fb19..ddaec66c1a 100644 --- a/crates/ui-egui/src/dialogs/width_point/tests.rs +++ b/crates/ui-egui/src/dialogs/width_point/tests.rs @@ -1047,3 +1047,70 @@ fn one_point_refuses_a_change_since_the_dialog_opened() { assert_eq!(app.session.doc().unwrap().doc, paired, "{field}"); } } + +#[test] +fn several_points_refuse_a_change_in_which_points_pair() { + // A point added at a selected point's place, after it, pairs with it; a third point at a + // discontinuous point's place unpairs its halves. The selected points keep their index, place + // and widths. + let cases = [ + ( + three_app as fn() -> (VectorcraftApp, u64), + json!([1, 3]), + json!([[0, 1, 1], [0.25, 0.8, 0.4], [0.5, 0.8, 0.8], [0.75, 0.4, 1.2], [0.75, 0.3, 0.3], [1, 1, 1]]), + ), + (pair_app, json!([0, 1]), json!([[0, 1, 1], [0.5, 0.8, 0.8], [0.5, 0.4, 1.2], [0.5, 0.3, 0.3], [1, 1, 1]])), + ]; + for (make, indices, points) in cases { + for field in ["side2", "discard"] { + let (mut app, id) = make(); + app.run("ui.widthPointEdit", json!({"id": id, "indices": indices})).unwrap(); + assert!(app.ui.dialog.as_ref().unwrap().fields.contains_key(OPEN), "{indices}"); + app.run("stroke.widthProfile.set", json!({"ids": [id], "points": points})).unwrap(); + let changed = app.session.doc().unwrap().doc.clone(); + let d = app.ui.dialog.as_mut().unwrap(); + d.fields.insert(field.into(), if field == "discard" { json!(true) } else { json!(5) }); + assert!(super::super::confirm(&mut app).is_err(), "{indices} {field}"); + assert_eq!(app.session.doc().unwrap().doc, changed, "{indices} {field}"); + } + } +} + +#[test] +fn ok_and_delete_refuse_another_stroke_or_point_set_into_the_dialog() { + let set = |app: &mut VectorcraftApp, field: &str, value: Value| { + let (req, _) = crate::control::ControlRequest::new("ui.dialog.set", json!({"field": field, "value": value})); + let crate::control::Outcome::Done(r) = crate::control::handle(app, &egui::Context::default(), &req) else { + panic!("expected an immediate reply") + }; + assert_eq!(r["ok"], json!(true), "{field}: {r}"); + }; + // `field` set to `value` on the dialog opened with `opened`, then `action` and OK. + let cases = [ + (json!({"indices": [1, 3]}), "id", None, "side1"), + (json!({"indices": [1, 3]}), "id", None, "discard"), + (json!({"indices": [1, 3]}), "indices", Some(json!([1, 2])), "side1"), + (json!({"index": 2}), "id", None, "side1"), + (json!({"index": 2}), "id", None, "discard"), + (json!({"index": 2}), "index", Some(json!(1)), "discard"), + (json!({"t": 0.4}), "id", None, "side1"), + ]; + for (opened, field, value, action) in cases { + let (mut app, id) = three_app(); + // Another stroke in the document with the same weight and width points. + let other = app.run("shape.line", json!({"x1": 100, "y1": 250, "x2": 300, "y2": 250})).unwrap()["id"].as_u64().unwrap(); + app.run("stroke.set", json!({"ids": [other], "weight": 10})).unwrap(); + let profile = json!([[0, 1, 1], [0.25, 0.8, 0.4], [0.5, 0.8, 0.8], [0.75, 0.4, 1.2], [1, 1, 1]]); + app.run("stroke.widthProfile.set", json!({"ids": [other], "points": profile})).unwrap(); + let before = app.session.doc().unwrap().doc.clone(); + let mut p = opened.clone(); + p["id"] = json!(id); + app.run("ui.widthPointEdit", p).unwrap(); + set(&mut app, field, value.unwrap_or(json!(other))); + let d = app.ui.dialog.as_mut().unwrap(); + d.fields.insert(action.into(), if action == "discard" { json!(true) } else { json!(5) }); + assert!(super::super::confirm(&mut app).is_err(), "{opened} {field} {action}"); + assert_eq!(app.session.doc().unwrap().doc, before, "{opened} {field} {action}"); + assert!(app.ui.dialog.is_some(), "{opened} {field} {action}"); + } +} diff --git a/docs/control-protocol.md b/docs/control-protocol.md index dafab93a58..c8b9137af3 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -656,7 +656,9 @@ as it was; `totalWidth` scales each point's own two sides to that total; a null `stroke.widthPoint.remove {id, indices}`. With `adjustAdjoining`, each changed point changes the nearest unselected point at another place before and after it in proportion to its change since the dialog opened; a point next to two changed points takes both changes. These rules for several points are VectorCraft's own. Confirm and Delete on existing -points, one or several, are refused when those points changed since the dialog opened or another document is active. +points, one or several, are refused when those points changed since the dialog opened (their place, their widths, or +which of them pair as a discontinuous point) or another document is active. Confirm and Delete are also refused when +`ui.dialog.set` changed `id`, `index`, `outIndex` or `indices` from the value it had when the dialog opened. Corners: double-clicking a Live Corners widget with the Selection or Direct Selection tool, or `ui.corners {id?, corners?}`, opens the `corners` dialog for a path's corners (the Direct-Selected ones, else every corner; fields `id`, diff --git a/docs/mcp.md b/docs/mcp.md index e7abf476a9..422dbd4e5e 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -8611,8 +8611,9 @@ turn it off to keep each point's other side), and Total Width scales each point' one `stroke.widthProfile.set` (one undo step, none when nothing changed), and Delete removes them all; with a discontinuous point among them, Incoming and Outgoing edit its two halves and both edit the continuous points. Malformed or out-of-range `indices`, or more than one of `index`, `indices` and `t`, are errors that open no dialog. OK -and Delete on existing points are refused when those points changed since the dialog opened or another document is -active. +and Delete on existing points are refused when those points changed since the dialog opened (their place, their widths, +or which of them pair as a discontinuous point) or another document is active. OK and Delete are also refused when +`id`, `index`, `outIndex` or `indices` no longer holds the value it had when the dialog opened. `document.inspect` reports a path's `strokeOptions.widthPoints` (`[t, left, right]`, factors of half the weight; null for a uniform stroke). `stroke.widthPoint.remove {id, index | indices}` deletes points; `stroke.widthProfile.set {ids, points}` replaces them all, as dragging several Shift-selected points with the Width tool does in one undo step.