From 165233bb579f80c18bd6699539dd35cf3c62cb9c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 18:29:01 -0800 Subject: [PATCH] Keep Define Grid options with the drawing contents that opened them --- .../ui-egui/src/dialogs/perspective_grid.rs | 16 +++++---- .../ui-egui/src/dialogs/tests_perspective.rs | 33 +++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/dialogs/perspective_grid.rs b/crates/ui-egui/src/dialogs/perspective_grid.rs index d75a811a74..f899a9d7e9 100644 --- a/crates/ui-egui/src/dialogs/perspective_grid.rs +++ b/crates/ui-egui/src/dialogs/perspective_grid.rs @@ -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, + /// 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, @@ -196,7 +196,7 @@ impl GridDialog { fn new( def: GridDefinition, mode: GridMode, - source_document: Option, + source_document: Option<(u64, u64)>, (ppi, bare_points): (f64, bool), presets: Vec, new_name: String, @@ -527,12 +527,16 @@ enum Next { Presets(String), } -fn file(app: &mut VectorcraftApp, mode: &GridMode, source_document: Option, def: &GridDefinition) -> Result { +fn file(app: &mut VectorcraftApp, mode: &GridMode, source_document: Option<(u64, u64)>, def: &GridDefinition) -> Result { 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); } diff --git a/crates/ui-egui/src/dialogs/tests_perspective.rs b/crates/ui-egui/src/dialogs/tests_perspective.rs index abbd37dfb7..ce286be347 100644 --- a/crates/ui-egui/src/dialogs/tests_perspective.rs +++ b/crates/ui-egui/src/dialogs/tests_perspective.rs @@ -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();