diff --git a/crates/ui-egui/src/dialogs/color_picker.rs b/crates/ui-egui/src/dialogs/color_picker.rs index f9649382bf..a8c8bf1c0d 100644 --- a/crates/ui-egui/src/dialogs/color_picker.rs +++ b/crates/ui-egui/src/dialogs/color_picker.rs @@ -43,15 +43,23 @@ pub(crate) enum Target { /// The stroke proxy. Stroke, /// The background colour of this artboard. - Artboard { document: u64, index: usize }, + Artboard { document: u64, reverts: u64, index: usize }, /// A colour row `key` of the open dialog `parent`. Answer { parent: DialogId, key: String }, } impl Target { fn in_active_document(&self, app: &VectorcraftApp) -> bool { - let Self::Artboard { document, .. } = self else { return true }; - app.session.active().is_some_and(|st| st.uid == *document) + self.document_problem(app).is_none() + } + + fn document_problem(&self, app: &VectorcraftApp) -> Option<&'static str> { + let Self::Artboard { document, reverts, .. } = self else { return None }; + match app.session.active() { + Some(st) if st.uid == *document && st.reverts == *reverts => None, + Some(st) if st.uid == *document => Some(tl!("The document was reverted. Reopen this dialog to continue.")), + _ => Some(tl!("The active document changed while the Color Picker was open")), + } } } @@ -160,14 +168,14 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { let found = index.and_then(|i| { let st = app.session.active()?; let color = st.doc.artboards.get(i)?.color.unwrap_or(Color::WHITE); - Some((st.uid, i, color)) + Some((st.uid, st.reverts, i, color)) }); Some(found.ok_or("no such artboard")?) } }; let color = match (p.get("color"), artboard) { (Some(v), _) => color_value(v).ok_or_else(|| format!("bad color {v}"))?, - (None, Some((_, _, c))) => c, + (None, Some((_, _, _, c))) => c, (None, None) => proxy_color(app, stroke), }; let dialogs = app.dialogs().ok_or("no window")?; @@ -195,18 +203,19 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { dialog = dialog.kept(model_values(&color)); } // The host replaces a picker only for the same document and artboard. - if let Some((document, index, _)) = artboard { + if let Some((document, _, index, _)) = artboard { dialog = dialog.instance((document, index)); } let id = dialogs.with(|h| h.open(dialog)); // A picker the open replaced leaves its preview behind: end it. - let before = app.color_dialogs.pickers.len(); - app.color_dialogs.pickers.retain(|o| o.id != id); - if app.color_dialogs.pickers.len() != before { - let _ = app.session.cancel_interaction(); + if let Some(at) = app.color_dialogs.pickers.iter().position(|open| open.id == id) { + let retired = app.color_dialogs.pickers.remove(at); + if retired.target.in_active_document(app) { + let _ = app.session.cancel_interaction(); + } } let target = match artboard { - Some((document, index, _)) => Target::Artboard { document, index }, + Some((document, reverts, index, _)) => Target::Artboard { document, reverts, index }, None if stroke => Target::Stroke, None => Target::Fill, }; @@ -284,10 +293,10 @@ fn finish( open: &mut Open, result: Option>, ) { - // Switching documents rolls back the source preview. Do not settle it against the new drawing. - if !open.target.in_active_document(app) { + // Switching or restoring a drawing ends the preview belonging to its old contents. + if let Some(reason) = open.target.document_problem(app) { if matches!(result, Some(DialogResult::Ok(_))) { - app.status("The active document changed while the Color Picker was open"); + app.status(reason); } return; } diff --git a/crates/ui-egui/src/dialogs/color_picker/tests.rs b/crates/ui-egui/src/dialogs/color_picker/tests.rs index 26e3bf486b..a2d239eb0f 100644 --- a/crates/ui-egui/src/dialogs/color_picker/tests.rs +++ b/crates/ui-egui/src/dialogs/color_picker/tests.rs @@ -287,3 +287,44 @@ fn sampler_above_the_field_previews_and_cancel_restores_the_original() { cancel(&mut app, &ctx); assert_eq!(fill_hex(&app), original); } + +#[test] +fn the_artboard_picker_leaves_restored_artboards_unchanged() { + for accept in [true, false] { + let (mut app, ctx) = app(); + app.run("artboard.new", json!({})).unwrap(); + app.run("artboard.setProps", json!({"index": 1, "color": "#123456"})).unwrap(); + let dir = vectorcraft_testkit::TempDir::new("artboard-picker-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("artboard.setProps", json!({"index": 1, "color": "#ff0000"})).unwrap(); + app.run("ui.colorPicker", json!({"artboard": 1})).unwrap(); + set_hex(&mut app, &ctx, "#abcdef"); + assert_eq!(artboard_color(&app).map(|c| c.to_hex()).as_deref(), Some("#abcdef")); + + app.run("file.revert", json!({"confirmed": true})).unwrap(); + let restored = artboard_color(&app); + assert_eq!(restored.map(|c| c.to_hex()).as_deref(), Some("#123456")); + let steps = undo_len(&app); + frame(&mut app, &ctx); + assert_eq!(artboard_color(&app), restored); + assert!(!app.session.in_interaction()); + if accept { + ok(&mut app, &ctx); + assert!(app.ui.status.contains("document was reverted")); + } else { + app.begin_interaction("New drawing edit").unwrap(); + cancel(&mut app, &ctx); + assert!(app.session.in_interaction()); + app.session.cancel_interaction().unwrap(); + } + assert_eq!(artboard_color(&app), restored); + assert_eq!(undo_len(&app), steps); + + app.run("ui.colorPicker", json!({"artboard": 1})).unwrap(); + set_hex(&mut app, &ctx, "#abcdef"); + ok(&mut app, &ctx); + assert_eq!(artboard_color(&app).map(|c| c.to_hex()).as_deref(), Some("#abcdef")); + } +}