Merge pull request #9514 from dotCooCoo/fix/graphic-style-options-document

Keep Graphic Style Options in its original drawing
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 08:28:19 +07:00
commit 28b1ee3c0a
1 file changed
+33 -1
@@ -40,13 +40,19 @@ impl Target {
/// Open the dialog with `name` in its field; OK runs `target`.
fn open_with(app: &mut VectorcraftApp, name: String, target: Target) -> Result<Value, String> {
let document = app.session.active().ok_or("no document open")?.uid;
let params = vec![Param::new("name", tl!("Style Name:").to_string(), ParamKind::text(name.clone()))];
let ctx = app.ui_ctx.clone().ok_or("no window")?;
let dialog = HostedParamDialog::command(&ctx, KIND, DialogFields::from_pairs([("name", name)]), move |fields| {
ParamDialog::command(KIND, tl!("Graphic Style Options").to_string(), params.clone()).values(Bind::bound(fields))
});
// An `Err` (a name another style has) keeps the dialog open with the reason.
let ok = move |app: &mut VectorcraftApp, result: &ParamResult| target.run(app, result.fields.text("name").unwrap_or_default()).map(|_| ());
let ok = move |app: &mut VectorcraftApp, result: &ParamResult| {
if !app.session.active().is_some_and(|active| active.uid == document) {
return Err(tl!("This graphic style belongs to the document that opened the dialog.").to_owned());
}
target.run(app, result.fields.text("name").unwrap_or_default()).map(|_| ())
};
host::open_held(app, dialog, ok).ok_or("no window")?;
Ok(Value::Null)
}
@@ -132,4 +138,30 @@ mod tests {
assert_eq!(names(&app).last().unwrap(), "Halo");
assert!(app.run("ui.graphicStyleOptions", json!({"name": "nope"})).is_err());
}
#[test]
fn renaming_a_style_does_not_change_a_matching_name_in_another_drawing() {
let mut app = app();
app.run("graphicStyle.new", json!({"name": "Twin"})).unwrap();
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 30, "height": 30})).unwrap();
app.run("graphicStyle.new", json!({"name": "Twin"})).unwrap();
let activate = |app: &mut VectorcraftApp, index| app.run("document.activate", json!({"index": index})).unwrap();
let other = names(&app);
let undo = app.session.active().unwrap().history.undo.len();
activate(&mut app, 0);
open(&mut app, Some("Twin")).unwrap();
let id = options(&app);
set(&app, id, "name", json!("Source style"));
activate(&mut app, 1);
accept(&mut app, id);
assert_eq!(names(&app), other);
assert_eq!(app.session.active().unwrap().history.undo.len(), undo);
assert_eq!(options(&app), id);
activate(&mut app, 0);
accept(&mut app, id);
assert!(names(&app).iter().any(|name| name == "Source style"));
}
}