Merge pull request #9674 from dotCooCoo/fix/graphic-style-options-restored-definitions

Keep Graphic Style Options off definitions restored by File Revert
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-26 21:38:29 -08:00
commit f9d0dbd701
1 file changed
+58 -5
@@ -7,7 +7,8 @@
//! OK, not by a field, so a name another style has refuses the OK and keeps the dialog open.
use craft::ui::contract::Bind;
use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind, ParamResult};
use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamKind, ParamResult};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use super::host;
@@ -15,8 +16,10 @@ use crate::VectorcraftApp;
/// The dialog kind of Graphic Style Options.
pub const KIND: &str = "graphicStyleOptions";
const OPENING: &str = "__opening";
/// What OK does with the name.
#[derive(Deserialize, Serialize)]
enum Target {
/// Name the style made from the selection.
New,
@@ -40,15 +43,24 @@ 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 active = app.session.active().ok_or("no document open")?;
let mut fields = DialogFields::from_pairs([("name", name.clone())]);
fields.set(OPENING, FieldValue::Json(json!({"uid": active.uid, "reverts": active.reverts, "target": target})));
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| {
let dialog = HostedParamDialog::command(&ctx, KIND, fields, 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| {
if !app.session.active().is_some_and(|active| active.uid == document) {
let ok = |app: &mut VectorcraftApp, result: &ParamResult| {
let Some(FieldValue::Json(opening)) = result.fields.get(OPENING) else { return Err("the graphic style target is missing".into()) };
let document = opening["uid"].as_u64().ok_or("the graphic style target is missing")?;
let target = Target::deserialize(&opening["target"]).map_err(|e| e.to_string())?;
let active = app.session.active();
if active.is_some_and(|active| active.uid == document && Some(active.reverts) != opening["reverts"].as_u64()) {
return Err(tl!("The document was reverted. Reopen this dialog to continue.").to_owned());
}
if !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(|_| ())
@@ -164,4 +176,45 @@ mod tests {
accept(&mut app, id);
assert!(names(&app).iter().any(|name| name == "Source style"));
}
#[test]
fn graphic_style_options_leave_restored_definitions_alone() {
for action in ["rename", "merge", "new"] {
let mut app = app();
let art = app.session.active().unwrap().selection.objects[0];
app.run("graphicStyle.new", json!({"name": "First"})).unwrap();
app.run("paint.setFill", json!({"ids": [art.0], "color": "#abcdef"})).unwrap();
app.run("graphicStyle.new", json!({"name": "Second"})).unwrap();
let saved = names(&app);
let dir = vectorcraft_testkit::TempDir::new("graphic-style-revert");
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string())));
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string())));
app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap();
app.run("object.move", json!({"ids": [art.0], "dx": 10, "dy": 0})).unwrap();
let open_action = |app: &mut VectorcraftApp| match action {
"rename" => open(app, Some("First")),
"merge" => open_merge(app, vec!["First".into(), "Second".into()]),
_ => open(app, None),
};
open_action(&mut app).unwrap();
let id = options(&app);
set(&app, id, "name", json!("Draft Style"));
app.run("file.revert", json!({"confirmed": true})).unwrap();
app.run("select.set", json!({"ids": [art.0]})).unwrap();
let steps = app.session.active().unwrap().history.undo.len();
accept(&mut app, id);
assert_eq!(names(&app), saved, "{action}");
assert_eq!(app.session.active().unwrap().history.undo.len(), steps);
assert_eq!(options(&app), id);
let ctx = app.ui_ctx.clone().unwrap();
app.dialogs().unwrap().with(|h| h.cancel_by_id(&ctx, id));
open_action(&mut app).unwrap();
let id = options(&app);
set(&app, id, "name", json!("Current Style"));
accept(&mut app, id);
assert!(names(&app).iter().any(|name| name == "Current Style"), "{action}");
}
}
}