diff --git a/crates/tools/src/distort/width.rs b/crates/tools/src/distort/width.rs index 23e7bdf263..b2050dbbdf 100644 --- a/crates/tools/src/distort/width.rs +++ b/crates/tools/src/distort/width.rs @@ -392,11 +392,11 @@ impl Tool for WidthTool { if !self.is_selected(spot.owner, w.t) { self.selected = Some((spot.owner, vec![w.t])); } + // Every selected entry goes to the dialog, which opens one point (or the two + // halves of a discontinuous point) as `index` would. let indices = self.selected_indices(cx, spot.owner); - let profile = stroke_of(cx.doc, spot.owner).and_then(|s| s.profile.as_ref()); - let several = profile.is_some_and(|pr| indices.iter().any(|i| (pr.points[*i].0 - w.t).abs() > SAME_T)); let mut params = json!({"id": spot.owner.0, "adjustAdjoining": ev.mods.shift}); - if several { + if indices.len() > 1 { params["indices"] = json!(indices); } else { params["index"] = json!(w.index); @@ -1120,6 +1120,19 @@ mod tests { let acts = t.pointer(&c2, &ev(PointerKind::DoubleClick, 200.0, 200.0, none)); let [Action::Dialog(_, v)] = acts.as_slice() else { panic!("expected Width Point Edit: {acts:?}") }; assert_eq!(v["indices"], json!([1, 2, 3])); + // Three points at nearly one place are selected together and all go to the dialog. + let (mut d3, _) = doc_line(); + let points = vec![(0.0, 1.0, 1.0), (0.5, 1.0, 1.0), (0.5000005, 2.0, 2.0), (0.5000009, 3.0, 3.0), (1.0, 1.0, 1.0)]; + d3.node_mut(id).unwrap().appearance.stroke_mut().unwrap().profile = Some(WidthProfile { points }); + let c3 = cx(&d3, &s, &p); + let mut t = WidthTool::default(); + for _ in 0..2 { + t.pointer(&c3, &ev(PointerKind::Down, 200.0, 200.0, none)); + t.pointer(&c3, &ev(PointerKind::Up, 200.0, 200.0, none)); + } + let acts = t.pointer(&c3, &ev(PointerKind::DoubleClick, 200.0, 200.0, none)); + let [Action::Dialog(_, v)] = acts.as_slice() else { panic!("expected Width Point Edit: {acts:?}") }; + assert_eq!(v["indices"], json!([1, 2, 3])); // A click on one of the selected points, then a double-click on it later, edits that point. let mut t = WidthTool::default(); click(&mut t, 150.0, none); diff --git a/crates/ui-egui/src/dialogs/width_point.rs b/crates/ui-egui/src/dialogs/width_point.rs index 4acf0d2d3b..56caf05845 100644 --- a/crates/ui-egui/src/dialogs/width_point.rs +++ b/crates/ui-egui/src/dialogs/width_point.rs @@ -30,6 +30,9 @@ //! entry, both halves of a discontinuous point), `side1`, `side2`, `totalWidth` (null when the //! points differ), `linked`, `adjustAdjoining`, and with a discontinuous point `outSide1`, //! `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. use egui::vec2; use serde_json::{Value, json}; @@ -57,6 +60,11 @@ const POINTS: &str = "__points"; const OPEN: &str = "__open"; /// Whether they include a discontinuous point (two sets of side widths shown). const DISCONTINUOUS: &str = "__discontinuous"; +/// The uid of the document the dialog opened in. +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 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. @@ -74,6 +82,11 @@ fn point(app: &VectorcraftApp, id: u64, index: usize) -> Option<(f64, f64, f64)> st.profile.as_ref()?.points.get(index).map(|&(t, l, r)| (t, l * half, r * half)) } +/// Width point `index` of `id` as [`point`] reads it, as an array. +fn entry_at(app: &VectorcraftApp, id: u64, index: usize) -> Option<[f64; 3]> { + point(app, id, index).map(|(t, l, r)| [t, l, r]) +} + /// The incoming and outgoing halves of the discontinuous point that width point `index` of `id` /// belongs to, or none when it is a continuous point. The halves are paired by /// [`width_point_pair`], as `stroke.widthPoint.set` pairs them, so either half finds the same pair. @@ -121,12 +134,19 @@ fn shown(es: &[Entry], set: usize) -> [Option; 3] { [common(|e| e.left), common(|e| e.right), common(|e| e.left + e.right)] } -/// Keep `es` as the working widths of `d`, and show them in its fields. -fn store(d: &mut Dialog, es: &[Entry]) { +/// Keep `es` as the working widths of `d`, and show them in its fields. A value set straight into a +/// field other than `typed` that OK refuses, and that is not the value the field showed, stays for +/// OK to refuse. +fn store(d: &mut Dialog, es: &[Entry], typed: Option<&str>) { + let old = entries(d, POINTS); d.fields.insert(POINTS.into(), entries_json(es)); let sets = if d.bool(DISCONTINUOUS) { 2 } else { 1 }; for (set, keys) in MANY.iter().enumerate().take(sets) { - for (key, value) in keys.iter().zip(shown(es, set)) { + for ((key, value), was) in keys.iter().zip(shown(es, set)).zip(shown(&old, set)) { + let showed = reading(d, key).ok().flatten().zip(was).is_some_and(|(v, s)| (v - s).abs() <= SAME_WIDTH); + if Some(*key) != typed && number(d, key).is_err() && !showed { + continue; + } d.fields.insert((*key).into(), json!(value)); } } @@ -156,8 +176,8 @@ fn apply(es: &mut [Entry], set: usize, edit: Edit, linked: bool) { } } -/// The number in field `key` (none when it is null or absent). -fn number(d: &Dialog, key: &str) -> Result, String> { +/// The width in field `key`, any finite width from nothing up (none when it is null or absent). +fn reading(d: &Dialog, key: &str) -> Result, String> { let v = match d.fields.get(key) { None | Some(Value::Null) => return Ok(None), Some(Value::Number(n)) => n.as_f64(), @@ -165,14 +185,25 @@ fn number(d: &Dialog, key: &str) -> Result, String> { Some(_) => None, }; match v { - Some(v) if v.is_finite() && (0.0..=1.0e5).contains(&v) => Ok(Some(v)), + Some(v) if v.is_finite() && v >= 0.0 => Ok(Some(v)), _ => Err("invalid stroke widths".into()), } } +/// The largest side width or Total Width a value typed in can set (points). +const MAX_WIDTH: f64 = 1.0e5; + +/// The number in field `key` that a value typed in can set (none when it is null or absent). +fn number(d: &Dialog, key: &str) -> Result, String> { + match reading(d, key)? { + Some(v) if v > MAX_WIDTH => Err("invalid stroke widths".into()), + v => Ok(v), + } +} + /// The widths OK sets on several points: the working widths, with any value set straight into a /// field (`ui.dialog.set`) that they do not show yet applied as typing it would, Total Width before -/// the sides. +/// the sides. A value the field shows changes nothing, even a Total Width above [`MAX_WIDTH`]. fn edited(d: &Dialog) -> Result, String> { let mut es = entries(d, POINTS); let sets = if d.bool(DISCONTINUOUS) { 2 } else { 1 }; @@ -180,8 +211,13 @@ fn edited(d: &Dialog) -> Result, String> { for (set, shown) in before.iter().enumerate() { let [k1, k2, kt, kl] = MANY[set]; for (key, at) in [(kt, 2), (k1, 0), (k2, 1)] { - let Some(v) = number(d, key)? else { continue }; - if shown[at].is_some_and(|s| (s - v).abs() <= SAME_WIDTH) { + let Some(v) = reading(d, key)? else { continue }; + let same = shown[at].filter(|s| (s - v).abs() <= SAME_WIDTH); + // Only a value above the limit that the points already have passes it. + if v > MAX_WIDTH && !same.is_some_and(|s| s > MAX_WIDTH) { + return Err("invalid stroke widths".into()); + } + if same.is_some() { continue; } let edit = match at { @@ -197,7 +233,8 @@ fn edited(d: &Dialog) -> Result, String> { /// After `ui.dialog.set` sets `field` of several points' dialog: a side width or Total Width applies /// as typing it does, so a value equal to the one shown still sets that side of every point (both -/// sides with the link on). A null or unreadable value is left for OK to skip or refuse. +/// sides with the link on). A null or unreadable value is left for OK to skip or refuse, and a value +/// set later into another field keeps it. pub(super) fn note_set(d: &mut Dialog, field: &str) { if !d.fields.contains_key(OPEN) { return; @@ -213,7 +250,7 @@ pub(super) fn note_set(d: &mut Dialog, field: &str) { }; let mut es = entries(d, POINTS); apply(&mut es, set, edit, d.bool(keys[3])); - store(d, &es); + store(d, &es, Some(field)); return; } } @@ -298,13 +335,16 @@ fn target(app: &VectorcraftApp, id: u64, indices: &Value) -> Result = es.iter().map(|e| e.index).collect(); - let mut d = - Dialog::new(KIND, json!({"id": id, "indices": indices, "adjustAdjoining": adjust, DISCONTINUOUS: discontinuous, OPEN: entries_json(es)})); + let doc = app.session.active().map(|st| st.uid); + let mut d = Dialog::new( + KIND, + json!({"id": id, "indices": indices, "adjustAdjoining": adjust, DISCONTINUOUS: discontinuous, OPEN: entries_json(es), DOC: doc}), + ); for (set, keys) in MANY.iter().enumerate().take(if discontinuous { 2 } else { 1 }) { let linked = es.iter().filter(|e| e.sets[set]).all(|e| (e.left - e.right).abs() < 1e-9); d.fields.insert(keys[3].into(), json!(linked)); } - store(&mut d, es); + store(&mut d, es, None); app.ui.dialog = Some(d); } @@ -321,10 +361,10 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { Some(v) => Some(v.as_u64().and_then(|i| usize::try_from(i).ok()).ok_or("no such width point")?), None => None, }; + if ["index", "indices", "t"].iter().filter(|key| given(key)).count() > 1 { + return Err("give only one of `index`, `indices` and `t`".into()); + } if let Some(indices) = p.get("indices").filter(|v| !v.is_null()) { - if given("index") || given("t") { - return Err("give only one of `index`, `indices` and `t`".into()); - } match target(app, id, indices)? { Target::One(i) => index = Some(i), Target::Several(es) => { @@ -353,7 +393,10 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { (t, l, r) }; let adjust = p.get("adjustAdjoining").and_then(Value::as_bool).unwrap_or(false); - let mut fields = json!({"id": id, "index": index, "t": t, "side1": l, "side2": r, "linked": (l - r).abs() < 1e-9, "adjustAdjoining": adjust}); + let doc = app.session.active().map(|st| st.uid); + let at: Vec<_> = index.into_iter().chain(pair.map(|(_, out)| out)).filter_map(|i| entry_at(app, id, i)).collect(); + let linked = (l - r).abs() < 1e-9; + let mut fields = json!({"id": id, "index": index, "t": t, "side1": l, "side2": r, "linked": linked, "adjustAdjoining": adjust, DOC: doc, AT: at}); if let Some((_, out)) = pair { let (_, l, r) = point(app, id, out).ok_or("no such width point")?; for (key, value) in [ @@ -420,14 +463,14 @@ fn sides(ui: &mut egui::Ui, d: &mut Dialog, unit: Unit, keys: [&str; 3], ids: [& fn sides_of_several(ui: &mut egui::Ui, d: &mut Dialog, unit: Unit, set: usize) { let [k1, k2, kt, kl] = MANY[set]; let ids = MANY_IDS[set]; - let value = |key: &str| number(d, key).ok().flatten(); + let value = |key: &str| reading(d, key).ok().flatten(); let (s1, s2, total, linked) = (value(k1), value(k2), value(kt), d.bool(kl)); let mut edit = None; grid(ui, |ui| { label(ui, tl!("Side 1:")); ui.horizontal(|ui| { if let Some(v) = widgets::num_field(ui, ids[0], s1, unit, 90.0) { - edit = Some(Edit::Side1(v)); + edit = Some((Edit::Side1(v), k1)); } if widgets::icon_button(ui, if linked { "link" } else { "link-2-off" }, tl!("Keep both sides the same width"), linked, 22.0).clicked() { d.fields.insert(kl.into(), json!(!linked)); @@ -436,20 +479,25 @@ fn sides_of_several(ui: &mut egui::Ui, d: &mut Dialog, unit: Unit, set: usize) { ui.end_row(); label(ui, tl!("Side 2:")); if let Some(v) = widgets::num_field(ui, ids[1], s2, unit, 90.0) { - edit = Some(Edit::Side2(v)); + edit = Some((Edit::Side2(v), k2)); } ui.end_row(); label(ui, tl!("Total Width:")); if let Some(v) = widgets::num_field(ui, ids[2], total, unit, 90.0) { - edit = Some(Edit::Total(v)); + edit = Some((Edit::Total(v), kt)); } ui.end_row(); }); - if let Some(edit) = edit { + if let Some((Edit::Side1(v) | Edit::Side2(v) | Edit::Total(v), key)) = edit + && !(0.0..=MAX_WIDTH).contains(&v) + { + // Not a width it can set: the field keeps it for OK to refuse. + d.fields.insert(key.into(), if v.is_finite() { json!(v) } else { json!(v.to_string()) }); + } else if let Some((edit, key)) = edit { // Values set straight into the fields count first, so typing does not lose them. let mut es = edited(d).unwrap_or_else(|_| entries(d, POINTS)); apply(&mut es, set, edit, linked); - store(d, &es); + store(d, &es, Some(key)); } } @@ -491,8 +539,16 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { false } +/// Whether the document the dialog opened in is still the active one. +fn same_document(app: &VectorcraftApp, d: &Dialog) -> Result<(), String> { + match (d.fields.get(DOC).and_then(Value::as_u64), app.session.active()) { + (Some(uid), Some(st)) if st.uid == uid => Ok(()), + _ => 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. +/// wide as when the dialog opened, exactly. fn opened_profile(app: &VectorcraftApp, id: u64, open: &[Entry]) -> Result<(WidthProfile, f64), String> { const NONE: &str = "no such width point"; let st = app.session.active().and_then(|s| s.doc.node(NodeId(id))).and_then(|n| n.appearance.stroke()).ok_or(NONE)?; @@ -506,7 +562,8 @@ fn opened_profile(app: &VectorcraftApp, id: u64, open: &[Entry]) -> Result<(Widt } for o in open { let &(t, l, r) = profile.points.get(o.index).ok_or(NONE)?; - if (t - o.t).abs() > SAME_T || (l * half - o.left).abs() > SAME_WIDTH || (r * half - o.right).abs() > SAME_WIDTH { + // The entries were read from this profile at this weight, so an unchanged point compares equal. + if t != o.t || l * half != o.left || r * half != o.right { return Err(NONE.into()); } } @@ -578,8 +635,9 @@ fn edited_profile(app: &VectorcraftApp, id: u64, open: &[Entry], new: &[Entry], fn confirm_several(app: &mut VectorcraftApp, d: &Dialog) -> Result { let id = d.fields.get("id").and_then(Value::as_u64).ok_or("no such width point")?; let open = entries(d, OPEN); + same_document(app, d)?; + opened_profile(app, id, &open)?; if d.bool("discard") { - opened_profile(app, id, &open)?; let indices: Vec = open.iter().map(|e| e.index).collect(); return run_and_close(app, "stroke.widthPoint.remove", json!({"id": id, "indices": indices})); } @@ -593,18 +651,38 @@ fn confirm_several(app: &mut VectorcraftApp, d: &Dialog) -> Result) -> Result<(), String> { + const NONE: &str = "no such width point"; + let id = d.fields.get("id").and_then(Value::as_u64).ok_or(NONE)?; + let at: Vec<[f64; 3]> = d.fields.get(AT).cloned().and_then(|v| serde_json::from_value(v).ok()).ok_or(NONE)?; + let now: Vec<_> = std::iter::once(index).chain(out).map(|i| entry_at(app, id, i)).collect::>().ok_or(NONE)?; + // The widths are read at the stroke's current weight, so a changed weight changes them too, and + // a continuous point must still be one. + if now != at || halves(app, id, index) != out.map(|o| (index, o)) { + return Err(NONE.into()); + } + Ok(()) +} + fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { if d.fields.contains_key(OPEN) { return confirm_several(app, d); } let (id, index) = (d.fields.get("id").cloned().unwrap_or_default(), d.fields.get("index").cloned().unwrap_or_default()); let out = d.fields.get("outIndex").filter(|_| paired(d)).cloned(); + if d.bool("discard") && index.is_null() { + // Deleting a prospective point discards the dialog; no document point exists yet. + app.ui.dialog = None; + return Ok(Value::Null); + } + // OK and Delete act only on the document and the points the dialog opened on. + same_document(app, 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()))?; + } if d.bool("discard") { - if index.is_null() { - // Deleting a prospective point discards the dialog; no document point exists yet. - app.ui.dialog = None; - return Ok(Value::Null); - } let p = match out { Some(out) => json!({"id": id, "indices": [index, out]}), None => json!({"id": id, "index": index}), diff --git a/crates/ui-egui/src/dialogs/width_point/tests.rs b/crates/ui-egui/src/dialogs/width_point/tests.rs index d939cb5a6c..0653c5fb19 100644 --- a/crates/ui-egui/src/dialogs/width_point/tests.rs +++ b/crates/ui-egui/src/dialogs/width_point/tests.rs @@ -840,6 +840,24 @@ fn double_clicking_one_of_several_selected_points_and_typing_edits_them_all() { assert_eq!(points(&app, id), vec![(0.0, 1.0, 1.0), (0.25, 1.0, 0.4), (0.5, 0.8, 0.8), (0.75, 1.0, 1.2), (1.0, 1.0, 1.0)]); } +#[test] +fn a_negative_width_typed_for_several_points_is_refused() { + let (mut app, id) = three_app(); + let before = app.session.doc().unwrap().doc.clone(); + let ctx = egui::Context::default(); + crate::theme::install_fonts(&ctx); + crate::theme::apply(&ctx, Default::default()); + app.run("ui.widthPointEdit", json!({"id": id, "indices": [1, 3]})).unwrap(); + for _ in 0..5 { + creation_dialog_frame(&mut app, &ctx, vec![]); + } + // The blank Side 1 has the focus. + creation_dialog_frame(&mut app, &ctx, vec![egui::Event::Text("-3 pt".into())]); + creation_dialog_click(&mut app, &ctx, "OK"); + assert!(app.ui.dialog.is_some(), "OK refuses it and the dialog stays open"); + assert_eq!(app.session.doc().unwrap().doc, before); +} + #[test] fn several_points_refuse_bad_indices() { let (mut app, id) = three_app(); @@ -854,6 +872,7 @@ fn several_points_refuse_bad_indices() { json!({"id": id, "indices": 1}), json!({"id": id, "indices": [1, 3], "index": 1}), json!({"id": id, "indices": [1, 3], "t": 0.5}), + json!({"id": id, "index": 1, "t": 0.5}), ] { assert!(app.run("ui.widthPointEdit", p.clone()).is_err(), "{p}"); assert!(app.ui.dialog.is_none(), "{p}"); @@ -885,3 +904,146 @@ fn several_points_refuse_bad_indices() { assert_eq!(app.session.doc().unwrap().doc, before); } } + +#[test] +fn a_total_width_above_the_side_limit_does_not_block_ok() { + // Two points of 60000 + 60000 pt on the 10 pt stroke: Total Width shows 120000 pt. + let (mut app, id) = three_app(); + let wide = json!([[0, 1, 1], [0.25, 12000, 12000], [0.5, 0.8, 0.8], [0.75, 12000, 12000], [1, 1, 1]]); + app.run("stroke.widthProfile.set", json!({"ids": [id], "points": wide})).unwrap(); + let before = app.session.doc().unwrap().doc.clone(); + app.run("ui.widthPointEdit", json!({"id": id, "indices": [1, 3]})).unwrap(); + assert_eq!(app.ui.dialog.as_ref().unwrap().fields["totalWidth"], json!(120000.0)); + super::super::confirm(&mut app).unwrap(); + assert!(app.ui.dialog.is_none()); + assert_eq!(app.session.doc().unwrap().doc, before); + // A side width typed in applies with the total shown. + app.run("ui.widthPointEdit", json!({"id": id, "indices": [1, 3]})).unwrap(); + let d = app.ui.dialog.as_mut().unwrap(); + d.fields.insert("linked".into(), json!(false)); + super::super::set_field(d, "side1".into(), json!(5)); + super::super::confirm(&mut app).unwrap(); + assert!(app.ui.dialog.is_none()); + assert_eq!(points(&app, id), vec![(0.0, 1.0, 1.0), (0.25, 1.0, 12000.0), (0.5, 0.8, 0.8), (0.75, 1.0, 12000.0), (1.0, 1.0, 1.0)]); + // A value set above the limit is refused even next to the 100000 pt shown. + let (mut app, id) = three_app(); + let edge = json!([[0, 1, 1], [0.25, 20000, 0.4], [0.5, 0.8, 0.8], [0.75, 20000, 1.2], [1, 1, 1]]); + app.run("stroke.widthProfile.set", json!({"ids": [id], "points": edge})).unwrap(); + app.run("ui.widthPointEdit", json!({"id": id, "indices": [1, 3]})).unwrap(); + app.ui.dialog.as_mut().unwrap().fields.insert("side1".into(), json!(100000.0000005)); + assert!(super::super::confirm(&mut app).is_err()); +} + +#[test] +fn an_invalid_width_stays_for_ok_to_refuse() { + let (mut app, id) = three_app(); + let before = app.session.doc().unwrap().doc.clone(); + app.run("ui.widthPointEdit", json!({"id": id, "indices": [1, 3]})).unwrap(); + let d = app.ui.dialog.as_mut().unwrap(); + super::super::set_field(d, "side1".into(), json!("wide")); + super::super::set_field(d, "side2".into(), json!("3 pt")); + assert_eq!(d.fields["side1"], json!("wide")); + assert!(super::super::confirm(&mut app).is_err()); + assert!(app.ui.dialog.is_some()); + assert_eq!(app.session.doc().unwrap().doc, before); + // Corrected, it applies with the Side 2 already set. + super::super::set_field(app.ui.dialog.as_mut().unwrap(), "side1".into(), json!(5)); + super::super::confirm(&mut app).unwrap(); + assert_eq!(points(&app, id), vec![(0.0, 1.0, 1.0), (0.25, 1.0, 0.6), (0.5, 0.8, 0.8), (0.75, 1.0, 0.6), (1.0, 1.0, 1.0)]); +} + +#[test] +fn several_points_refuse_any_change_since_the_dialog_opened() { + let (mut app, id) = three_app(); + // A change too small to show in the fields. + app.run("ui.widthPointEdit", json!({"id": id, "indices": [1, 3]})).unwrap(); + let nudged = json!([[0, 1, 1], [0.25, 0.8000001, 0.4], [0.5, 0.8, 0.8], [0.75, 0.4, 1.2], [1, 1, 1]]); + app.run("stroke.widthProfile.set", json!({"ids": [id], "points": nudged})).unwrap(); + let changed = app.session.doc().unwrap().doc.clone(); + for field in ["side2", "discard"] { + 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(), "{field}"); + assert_eq!(app.session.doc().unwrap().doc, changed); + } + app.run("edit.undo", json!({})).unwrap(); + // OK with no width changed still checks the points. + app.run("ui.widthPointEdit", json!({"id": id, "indices": [1, 3]})).unwrap(); + app.run("stroke.widthPoint.set", json!({"id": id, "index": 1, "t": 0.3, "left": 4, "right": 2})).unwrap(); + assert!(super::super::confirm(&mut app).is_err()); + assert!(app.ui.dialog.is_some()); +} + +/// Open a second document holding [`three_app`]'s line with the same id, and make it active. +fn second_document(app: &mut VectorcraftApp, id: u64) { + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + let other = app.run("shape.line", json!({"x1": 100, "y1": 150, "x2": 300, "y2": 150})).unwrap()["id"].as_u64().unwrap(); + assert_eq!(other, id, "the same id in both documents"); + app.run("stroke.set", json!({"ids": [other], "weight": 10})).unwrap(); + let points = 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": points})).unwrap(); +} + +#[test] +fn ok_and_delete_act_only_in_the_document_the_dialog_opened_in() { + for opened in [json!({"indices": [1, 3]}), json!({"index": 2})] { + for field in ["side1", "discard"] { + let (mut app, id) = three_app(); + let mut p = opened.clone(); + p["id"] = json!(id); + app.run("ui.widthPointEdit", p).unwrap(); + let d = app.ui.dialog.take(); + second_document(&mut app, id); + let other = app.session.doc().unwrap().doc.clone(); + app.ui.dialog = d; + 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(), "{opened} {field}"); + assert_eq!(app.session.doc().unwrap().doc, other, "{opened} {field}"); + } + } +} + +#[test] +fn one_point_refuses_a_change_since_the_dialog_opened() { + for field in ["side1", "discard"] { + // A point added before the opened one moves it to the next index. + let (mut app, id) = app(); + app.run("ui.widthPointEdit", json!({"id": id, "index": 1})).unwrap(); + app.run("stroke.widthPoint.set", json!({"id": id, "t": 0.25, "left": 3, "right": 3})).unwrap(); + let added = 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(), "{field}"); + assert_eq!(app.session.doc().unwrap().doc, added, "{field}"); + // The outgoing half of a discontinuous point changed. + let (mut app, id) = pair_app(); + app.run("ui.widthPointEdit", json!({"id": id, "index": 1})).unwrap(); + app.run("stroke.widthProfile.set", json!({"ids": [id], "points": [[0, 1, 1], [0.5, 0.8, 0.8], [0.5, 0.2, 1.2], [1, 1, 1]]})).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(), "{field}"); + assert_eq!(app.session.doc().unwrap().doc, changed, "{field}"); + // The stroke weight changed. + let (mut app, id) = self::app(); + app.run("ui.widthPointEdit", json!({"id": id, "index": 1})).unwrap(); + app.run("stroke.set", json!({"ids": [id], "weight": 20})).unwrap(); + let heavier = 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(), "{field}"); + assert_eq!(app.session.doc().unwrap().doc, heavier, "{field}"); + // A later point moved onto the opened one makes it one half of a discontinuous point. + let (mut app, id) = self::app(); + app.run("stroke.widthPoint.set", json!({"id": id, "t": 0.75, "left": 3, "right": 3})).unwrap(); + app.run("ui.widthPointEdit", json!({"id": id, "index": 1})).unwrap(); + app.run("stroke.widthPoint.set", json!({"id": id, "index": 2, "t": 0.5, "left": 3, "right": 3})).unwrap(); + let paired = app.session.doc().unwrap().doc.clone(); + assert_eq!(points(&app, id)[1], (0.5, 0.8, 0.8), "the opened point keeps its index"); + 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(), "{field}"); + assert_eq!(app.session.doc().unwrap().doc, paired, "{field}"); + } +} diff --git a/docs/control-protocol.md b/docs/control-protocol.md index bdb55b4060..0b9afb37cd 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -610,7 +610,7 @@ sets both halves in one `stroke.widthPoint.set` (one undo step), and Delete remo `ui.widthPointEdit {id, indices: [i…]}` opens it on several existing width points, as double-clicking one of several Shift-selected width points does. `indices` must be a nonempty array of profile indices; repeated indices count once, either half of a discontinuous point brings the other, and an index that is not a nonnegative whole number or not a -width point of the stroke is an error, as is giving `indices` with `index` or `t`. When the indices name one width point +width point of the stroke is an error, as is giving more than one of `index`, `indices` and `t`. When the indices name one width point (both halves of a discontinuous point count as one) it opens as `index` would. The fields are `indices` (every profile entry edited, in order), `side1`, `side2` and `totalWidth` (null where the points differ), `linked` and `adjustAdjoining`, and with a discontinuous point among them `outSide1`, `outSide2`, `outTotalWidth` and `outLinked` for the outgoing halves; the continuous points take both sets. @@ -619,7 +619,8 @@ as it was; `totalWidth` scales each point's own two sides to that total; a null `stroke.widthProfile.set` (one undo step), or nothing when no width changed, and Delete removes every point with `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. +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. 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 5fce827462..2683fff05d 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -7812,11 +7812,13 @@ Width Point Edit on either half shows both halves as Incoming and Outgoing, with removes both. `ui.widthPointEdit {id, indices: [i…]}` (or double-clicking one of several Shift-selected width points) opens Width Point Edit on several points: Side 1, Side 2 and Total Width are blank (null) where the points differ, a side width -typed in sets that side of every point and keeps each point's other side, and Total Width scales each point's own two -sides. OK sets them all with 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 `indices` with `index` or `t`, are errors that open no -dialog. +typed in sets that side of every point (both sides while the link is on, which it is when every point has equal sides; +turn it off to keep each point's other side), and Total Width scales each point's own two sides. OK sets them all with +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. `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.