Merge pull request #9634 from dotCooCoo/fix/define-grid-after-revert

Keep Define Grid options from replacing a restored grid
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-26 21:40:28 -08:00
commit d95b164b09
2 files changed
+43 -6

No files matched your search

+10 -6
View File
@@ -134,7 +134,7 @@ fn open_on(app: &mut VectorcraftApp, def: GridDefinition, mode: GridMode) -> Res
let dialog = GridDialog::new(
def,
mode,
app.session.active().map(|st| st.uid),
app.session.active().map(|st| (st.uid, st.reverts)),
(general_lengths(app).pixels_per_inch, app.session.prefs.numbers_without_units_are_points),
app.session.perspective_presets(),
app.session.new_perspective_preset_name(),
@@ -172,8 +172,8 @@ pub fn open_edit(app: &mut VectorcraftApp, original: Option<&str>, base: &str) -
pub(super) struct GridDialog {
id: DialogId,
mode: GridMode,
/// The document whose grid Define Grid edits.
source_document: Option<u64>,
/// The document and loaded contents whose grid Define Grid edits.
source_document: Option<(u64, u64)>,
draft: GridDraft,
/// The presets the Preset row offers, as the document and the preferences have them.
presets: Vec<GridDefinition>,
@@ -196,7 +196,7 @@ impl GridDialog {
fn new(
def: GridDefinition,
mode: GridMode,
source_document: Option<u64>,
source_document: Option<(u64, u64)>,
(ppi, bare_points): (f64, bool),
presets: Vec<GridDefinition>,
new_name: String,
@@ -527,12 +527,16 @@ enum Next {
Presets(String),
}
fn file(app: &mut VectorcraftApp, mode: &GridMode, source_document: Option<u64>, def: &GridDefinition) -> Result<Next, String> {
fn file(app: &mut VectorcraftApp, mode: &GridMode, source_document: Option<(u64, u64)>, def: &GridDefinition) -> Result<Next, String> {
let mut params = serde_json::to_value(def).map_err(|e| e.to_string())?;
if *mode == GridMode::Define {
if app.session.active().map(|st| st.uid) != source_document {
let active = app.session.active().map(|st| (st.uid, st.reverts));
if active.map(|(uid, _)| uid) != source_document.map(|(uid, _)| uid) {
return Err(tl!("Define Grid can only change the document where it was opened.").to_owned());
}
if active != source_document {
return Err(tl!("The document was reverted. Reopen this dialog to continue.").to_owned());
}
app.run("perspective.grid.define", params)?;
return Ok(Next::Close);
}
@@ -137,6 +137,39 @@ fn define_grid_applies_only_to_the_document_where_it_opened() {
assert_eq!(grid(&app).right_color.hex(), "#112233");
}
#[test]
fn define_grid_requires_a_fresh_opening_after_native_revert() {
let mut app = app();
let dir = vectorcraft_testkit::TempDir::new("define-grid-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("perspective.grid.define", json!({"rightColor": "#123456"})).unwrap();
app.run("file.saveAs", json!({"path": dir.join("Grid.vectorcraft")})).unwrap();
app.run("perspective.grid.define", json!({"rightColor": "#765432"})).unwrap();
let source = app.session.active().unwrap();
let (uid, reverts) = (source.uid, source.reverts);
app.run("ui.perspectiveGridDialog", json!({})).unwrap();
set(&mut app, "rightColor", json!("#abcdef")).unwrap();
app.run("file.revert", json!({"confirmed": true})).unwrap();
let restored = app.session.active().unwrap();
assert_eq!((restored.uid, restored.reverts), (uid, reverts + 1));
let undo = restored.history.undo.len();
ok(&mut app);
assert_eq!(grid(&app).right_color.hex(), "#123456");
assert_eq!(app.session.active().unwrap().history.undo.len(), undo);
let id = dialog_id(super::perspective_grid::KIND);
assert!(matches!(host(&app, |h| h.held_state(id)), Some(HeldOk::Refused(_))));
let ctx = window(&app);
assert!(host(&app, |h| h.cancel_by_id(&ctx, id)));
frame(&mut app);
app.run("ui.perspectiveGridDialog", json!({})).unwrap();
set(&mut app, "rightColor", json!("#abcdef")).unwrap();
ok(&mut app);
assert_eq!(grid(&app).right_color.hex(), "#abcdef");
}
#[test]
fn define_grid_loads_and_saves_presets() {
let mut app = app();