Merge pull request #9589 from dotCooCoo/fix/width-point-before-revert

Protect Width Point Edit drafts after File Revert
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 08:38:32 +07:00
commit b7bf55193f
2 files changed
+61 -9

No files matched your search

+10 -9
View File
@@ -354,8 +354,8 @@ impl Target {
struct Form {
/// The path whose stroke carries the points.
path: u64,
/// The uid of the document the dialog opened in.
doc: Option<u64>,
/// The document's UID and revert count when the dialog opened.
doc: Option<(u64, u64)>,
/// Adjust Adjoining Width Points.
adjust: bool,
target: Target,
@@ -684,8 +684,9 @@ impl StackDialog for WidthPointDialog {
/// Adjoining adjustment defaults to false. Either half of a discontinuous point opens both.
pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
let id = p.get("id").and_then(Value::as_u64).ok_or("give the path `id`")?;
let doc = &app.session.active().ok_or("no document")?.doc;
let id = vectorcraft_tools::distort::stroke_owner(doc, NodeId(id)).0;
let active = app.session.active().ok_or("no document")?;
let document = (active.uid, active.reverts);
let id = vectorcraft_tools::distort::stroke_owner(&active.doc, NodeId(id)).0;
let given = |key: &str| p.get(key).is_some_and(|v| !v.is_null());
let mut index = match p.get("index").filter(|v| !v.is_null()) {
Some(v) => Some(v.as_u64().and_then(|i| usize::try_from(i).ok()).ok_or("no such width point")?),
@@ -695,7 +696,6 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
return Err("give only one of `index`, `indices` and `t`".into());
}
let adjust = p.get("adjustAdjoining").and_then(Value::as_bool).unwrap_or(false);
let uid = app.session.active().map(|st| st.uid);
let mut several = None;
if let Some(indices) = p.get("indices").filter(|v| !v.is_null()) {
match target(app, id, indices)? {
@@ -711,7 +711,7 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
}
None => one_point(app, id, index, p)?,
};
let dialog = WidthPointDialog::new(Form { path: id, doc: uid, adjust, target }, stroke_lengths(app));
let dialog = WidthPointDialog::new(Form { path: id, doc: Some(document), adjust, target }, stroke_lengths(app));
app.dialogs().ok_or("no window")?.with(|host| host.open(dialog));
Ok(Value::Null)
}
@@ -749,10 +749,11 @@ fn one_point(app: &VectorcraftApp, id: u64, index: Option<usize>, p: &Value) ->
Ok(Target::One { index, t, at, sides: Sides::of(l, r), out, single: Single::None })
}
/// Whether the document the dialog opened in (`doc`) is still the active one.
fn same_document(app: &VectorcraftApp, doc: Option<u64>) -> Result<(), String> {
/// Whether the document the dialog opened in is active and has not been reloaded by Revert.
fn same_document(app: &VectorcraftApp, doc: Option<(u64, u64)>) -> Result<(), String> {
match (doc, app.session.active()) {
(Some(uid), Some(st)) if st.uid == uid => Ok(()),
(Some((uid, reverts)), Some(st)) if (st.uid, st.reverts) == (uid, reverts) => Ok(()),
(Some((uid, _)), Some(st)) if st.uid == uid => Err(tl!("The document was reverted. Reopen Width Point Edit.").to_owned()),
_ => Err("no such width point".into()),
}
}
@@ -851,6 +851,57 @@ fn second_document(app: &mut VectorcraftApp, id: u64) {
app.run("stroke.widthProfile.set", json!({"ids": [other], "points": points})).unwrap();
}
struct SavedFile(std::path::PathBuf);
impl Drop for SavedFile {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.0);
}
}
fn exercise_reverted_point(prospective: bool) {
let (mut app, id) = if prospective { creation_app() } else { app() };
app.services.read = Some(Box::new(|path: &str| std::fs::read(path).map_err(|e| e.to_string())));
app.services.write = Some(Box::new(|path: &str, bytes: &[u8]| std::fs::write(path, bytes).map_err(|e| e.to_string())));
let path = SavedFile(std::env::temp_dir().join(format!("vc-ui-width-point-revert-{}-{prospective}.vectorcraft", std::process::id())));
app.run("file.saveAs", json!({"path": path.0})).unwrap();
assert!(path.0.is_file());
let saved = points(&app, id);
let state = app.session.active().unwrap();
let (document, reverts) = (state.uid, state.reverts);
app.run("shape.rectangle", json!({"x": 30, "y": 30, "width": 10, "height": 10})).unwrap();
let target = if prospective { json!({"id": id, "t": 0.4}) } else { json!({"id": id, "index": 1}) };
app.run("ui.widthPointEdit", target.clone()).unwrap();
set(&app, "side1", json!(7));
app.run("file.revert", json!({"confirmed": true})).unwrap();
let state = app.session.active().unwrap();
assert_eq!((state.uid, state.reverts), (document, reverts + 1));
assert_eq!(points(&app, id), saved, "the reloaded path still looks like the original target");
let undo = state.history.undo.len();
let accepted = ok(&mut app);
assert_eq!(points(&app, id), saved, "the old Width Point draft leaves the reloaded path alone");
assert_eq!(app.session.active().unwrap().history.undo.len(), undo);
assert!(accepted.is_err() && is_open(&app));
cancel(&app);
app.run("ui.widthPointEdit", target).unwrap();
set(&app, "side1", json!(7));
ok(&mut app).unwrap();
assert_ne!(points(&app, id), saved);
assert_eq!(app.session.active().unwrap().history.undo.len(), undo + 1);
}
#[test]
fn existing_width_point_draft_cannot_edit_a_reverted_path() {
exercise_reverted_point(false);
}
#[test]
fn prospective_width_point_draft_cannot_edit_a_reverted_path() {
exercise_reverted_point(true);
}
#[test]
fn ok_and_delete_act_only_in_the_document_the_dialog_opened_in() {
for opened in [json!({"indices": [1, 3]}), json!({"index": 2})] {