Merge pull request #9535 from dotCooCoo/fix/brush-options-original-document

Keep Brush Options in its original drawing
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 07:31:11 +07:00
commit f5dfdb3688
1 file changed
+36 -3
+36 -3
View File
@@ -163,12 +163,13 @@ pub fn open(app: &mut VectorcraftApp, name: Option<&str>) -> Result<Value, Strin
/// `object_keys` are the option keys of Options of Selected Object, which the dialog then records
/// with the values it opened with. OK reaches [`ok`] once the dialog's own rows accept it.
fn open_dialog(app: &mut VectorcraftApp, f: &Map<String, Value>, object_keys: Option<&[&str]>) -> Result<Value, String> {
let source = app.session.active().ok_or("no document open")?;
let document = source.uid;
let ty = f.get("__type").and_then(Value::as_str).unwrap_or_default();
let object = object_keys.is_some();
let ctx = app.ui_ctx.clone().ok_or("no window")?;
// Modal: no edit changes the document while the dialog is open, so the previews read this snapshot.
let doc = app.session.active().map(|st| st.doc.clone());
let rows = rows(ty, object, doc, crate::canvas::rulers::unit_kind(app.session.general_unit()));
let rows = rows(ty, object, Some(source.doc.clone()), crate::canvas::rulers::unit_kind(app.session.general_unit()));
let title = Label::from(title(ty, object));
let mut initial = form::fields_of(&rows, f);
// The hidden values the rows read whole stay JSON: an untagged read makes an array a list of values.
@@ -192,7 +193,15 @@ fn open_dialog(app: &mut VectorcraftApp, f: &Map<String, Value>, object_keys: Op
}
})
.held_ok();
app.ui.brush_options = Some(host::open_held(app, dialog, ok).ok_or("no window")?);
app.ui.brush_options = Some(
host::open_held(app, dialog, move |app, result: &ParamResult| {
if !object && app.session.active().is_none_or(|st| st.uid != document) {
return Err(tl!("Return to the original drawing to edit its brush").into());
}
ok(app, result)
})
.ok_or("no window")?,
);
Ok(Value::Null)
}
@@ -672,3 +681,27 @@ fn confirm(app: &mut VectorcraftApp, d: &DialogFields) -> Result<(), String> {
#[cfg(test)]
mod tests;
#[cfg(test)]
mod document_tests {
use super::*;
use crate::dialogs::tests_export;
#[test]
fn brush_options_does_not_rename_the_other_drawings_brush() {
let (mut app, _) = tests_export::app(1);
open(&mut app, Some("Diamonds")).unwrap();
tests_export::set(&mut app, "name", json!("Source diamonds"));
app.run("file.new", json!({"width": 100, "height": 100})).unwrap();
let previous = app.run("brush.get", json!({"name": "Diamonds"})).unwrap();
let undo = app.session.active().unwrap().history.undo.len();
let error = tests_export::try_confirm(&mut app).expect_err("Brush Options must refuse the other drawing");
assert!(error.contains("original drawing"), "{error}");
assert_eq!(app.run("brush.get", json!({"name": "Diamonds"})).unwrap(), previous);
assert_eq!(app.session.active().unwrap().history.undo.len(), undo);
assert_eq!(tests_export::fields(&app)["name"], "Source diamonds");
app.run("document.activate", json!({"index": 0})).unwrap();
tests_export::try_confirm(&mut app).unwrap();
assert_eq!(app.run("brush.get", json!({"name": "Source diamonds"})).unwrap()["name"], "Source diamonds");
}
}