diff --git a/crates/ui-egui/src/dialogs/color_picker.rs b/crates/ui-egui/src/dialogs/color_picker.rs index a8c8bf1c0d..6863ee28a7 100644 --- a/crates/ui-egui/src/dialogs/color_picker.rs +++ b/crates/ui-egui/src/dialogs/color_picker.rs @@ -3,7 +3,8 @@ //! as one undo step through `paint.setFill` / `paint.setStroke` and makes the colour the Color //! Guide's base colour; Cancel puts the art back. Opened for an artboard (Properties › Artboards › //! Fill), it shows the colour on the artboard's paper and OK keeps it through -//! `artboard.setProps {index, color}`. Preview and OK require the opening document to be active. +//! `artboard.setProps {index, color}`. For the fill, the stroke and an artboard, preview and OK +//! require the opening document to be active and not reverted since the picker opened. //! //! A command dialog's colour row raises a `ColorRequest`; [`pump`] opens a picker over that dialog //! and answers the row with its OK. Every picker has the Color Swatches view; one over Recolor @@ -38,10 +39,10 @@ use crate::{VectorcraftApp, screen_color}; /// What a picker's OK writes. #[derive(Clone, Debug, PartialEq)] pub(crate) enum Target { - /// The fill proxy. - Fill, - /// The stroke proxy. - Stroke, + /// The fill proxy of the document it opened in (`None` with no document open). + Fill { document: Option, reverts: u64 }, + /// The stroke proxy of the document it opened in (`None` with no document open). + Stroke { document: Option, reverts: u64 }, /// The background colour of this artboard. Artboard { document: u64, reverts: u64, index: usize }, /// A colour row `key` of the open dialog `parent`. @@ -54,10 +55,15 @@ impl Target { } 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.")), + let (document, reverts) = match self { + Self::Fill { document, reverts } | Self::Stroke { document, reverts } => (*document, *reverts), + Self::Artboard { document, reverts, .. } => (Some(*document), *reverts), + Self::Answer { .. } => return None, + }; + match (app.session.active(), document) { + (None, None) => None, + (Some(st), Some(document)) if st.uid == document && st.reverts == reverts => None, + (Some(st), Some(document)) 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")), } } @@ -214,10 +220,11 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { let _ = app.session.cancel_interaction(); } } + let (document, reverts) = app.session.active().map_or((None, 0), |st| (Some(st.uid), st.reverts)); let target = match artboard { Some((document, reverts, index, _)) => Target::Artboard { document, reverts, index }, - None if stroke => Target::Stroke, - None => Target::Fill, + None if stroke => Target::Stroke { document, reverts }, + None => Target::Fill { document, reverts }, }; // OK with the artboard's color (or the white that stands in for none) unchanged keeps the artboard as it is. let keep = artboard.is_some() && p.get("color").is_none(); @@ -320,8 +327,8 @@ fn finish( fn paint_command(target: &Target, c: Color) -> (&'static str, Value) { match target { Target::Artboard { index, .. } => ("artboard.setProps", json!({ "index": index, "color": color_json(&c) })), - Target::Stroke => ("paint.setStroke", json!({ "color": color_json(&c) })), - Target::Fill | Target::Answer { .. } => ("paint.setFill", json!({ "color": color_json(&c) })), + Target::Stroke { .. } => ("paint.setStroke", json!({ "color": color_json(&c) })), + Target::Fill { .. } | Target::Answer { .. } => ("paint.setFill", json!({ "color": color_json(&c) })), } } @@ -352,7 +359,7 @@ fn preview(app: &mut VectorcraftApp, open: &mut Open, picked: &PickedColor) { } let label = match open.target { Target::Artboard { .. } => "Artboard Options", - Target::Stroke => "Stroke Color", + Target::Stroke { .. } => "Stroke Color", _ => "Fill Color", }; open.preview.run(app, true, label, cmd, ¶ms); diff --git a/crates/ui-egui/src/dialogs/color_picker/tests.rs b/crates/ui-egui/src/dialogs/color_picker/tests.rs index a2d239eb0f..1abafe7025 100644 --- a/crates/ui-egui/src/dialogs/color_picker/tests.rs +++ b/crates/ui-egui/src/dialogs/color_picker/tests.rs @@ -61,6 +61,129 @@ fn two_documents_with_artboards() -> (VectorcraftApp, egui::Context) { (app, ctx) } +/// The fill (or stroke) colour of the active document's selection. +fn paint_hex(app: &VectorcraftApp, stroke: bool) -> Option { + let (fill, line) = crate::panels::current_paints(app); + if stroke { line } else { fill }.color().map(|c| c.to_hex()) +} + +/// Two documents, each with a selected rectangle; the first is active. +fn two_documents_with_selected_art() -> (VectorcraftApp, egui::Context) { + let (mut app, ctx) = app(); + app.run("file.new", json!({"width": 300, "height": 300})).unwrap(); + app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 60, "height": 60})).unwrap(); + app.run("document.activate", json!({"index": 0})).unwrap(); + (app, ctx) +} + +fn picker_params(stroke: bool) -> Value { + json!({ "stroke": stroke }) +} + +#[test] +fn fill_and_stroke_pickers_preview_only_in_their_opening_document() { + for stroke in [false, true] { + let (mut app, ctx) = two_documents_with_selected_art(); + app.run("ui.colorPicker", picker_params(stroke)).unwrap(); + set_hex(&mut app, &ctx, "#ff0000"); + assert_eq!(paint_hex(&app, stroke).as_deref(), Some("#ff0000"), "the opening document previews"); + + app.run("document.activate", json!({"index": 1})).unwrap(); + let other = app.session.doc().unwrap().doc.layers.clone(); + let before = undo_len(&app); + set_hex(&mut app, &ctx, "#00ff00"); + assert_eq!(app.session.doc().unwrap().doc.layers, other, "the other document never previews the color"); + assert!(!app.session.in_interaction()); + assert_eq!(undo_len(&app), before); + + app.run("document.activate", json!({"index": 0})).unwrap(); + assert_ne!(paint_hex(&app, stroke).as_deref(), Some("#ff0000"), "leaving the source rolled its preview back"); + let before = undo_len(&app); + ok(&mut app, &ctx); + assert_eq!(paint_hex(&app, stroke).as_deref(), Some("#00ff00"), "OK back in the opening document applies"); + assert_eq!(undo_len(&app), before + 1); + } +} + +#[test] +fn confirming_a_fill_or_stroke_picker_in_another_document_leaves_both_unchanged() { + for stroke in [false, true] { + let (mut app, ctx) = two_documents_with_selected_art(); + let source = app.session.doc().unwrap().doc.layers.clone(); + let source_undo = undo_len(&app); + app.run("ui.colorPicker", json!({"stroke": stroke, "color": "#00ff00"})).unwrap(); + frame(&mut app, &ctx); + app.run("document.activate", json!({"index": 1})).unwrap(); + let destination = app.session.doc().unwrap().doc.layers.clone(); + let destination_undo = undo_len(&app); + ok(&mut app, &ctx); + assert_eq!(app.ui.status, "The active document changed while the Color Picker was open"); + assert_eq!(app.session.doc().unwrap().doc.layers, destination); + assert_eq!(undo_len(&app), destination_undo); + + app.run("document.activate", json!({"index": 0})).unwrap(); + assert_eq!(app.session.doc().unwrap().doc.layers, source); + assert_eq!(undo_len(&app), source_undo); + } +} + +#[test] +fn canceling_a_fill_or_stroke_picker_preserves_the_other_documents_interaction() { + for stroke in [false, true] { + let (mut app, ctx) = two_documents_with_selected_art(); + app.run("ui.colorPicker", picker_params(stroke)).unwrap(); + set_hex(&mut app, &ctx, "#ff0000"); + app.run("document.activate", json!({"index": 1})).unwrap(); + app.begin_interaction("Other edit").unwrap(); + app.session.preview("paint.setFill", &json!({"color": "#445566"})).unwrap(); + + cancel(&mut app, &ctx); + assert!(app.session.in_interaction(), "the current document keeps its own interaction"); + assert_eq!(paint_hex(&app, false).as_deref(), Some("#445566")); + } +} + +#[test] +fn a_picker_opened_without_a_document_leaves_a_document_opened_later_unchanged() { + let ctx = crate::testing::ui_context(); + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.ui_ctx = Some(ctx.clone()); + app.run("ui.colorPicker", json!({"color": "#00ff00"})).unwrap(); + frame(&mut app, &ctx); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap(); + let art = app.session.doc().unwrap().doc.layers.clone(); + let steps = undo_len(&app); + ok(&mut app, &ctx); + assert_eq!(app.ui.status, "The active document changed while the Color Picker was open"); + assert_eq!(app.session.doc().unwrap().doc.layers, art); + assert_eq!(undo_len(&app), steps); +} + +#[test] +fn a_fill_or_stroke_picker_leaves_a_reverted_document_unchanged() { + for stroke in [false, true] { + let (mut app, ctx) = app(); + let dir = vectorcraft_testkit::TempDir::new("paint-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("ui.colorPicker", picker_params(stroke)).unwrap(); + set_hex(&mut app, &ctx, "#abcdef"); + assert_eq!(paint_hex(&app, stroke).as_deref(), Some("#abcdef")); + + app.run("file.revert", json!({"confirmed": true})).unwrap(); + app.run("select.all", json!({})).unwrap(); + let restored = app.session.doc().unwrap().doc.layers.clone(); + let steps = undo_len(&app); + frame(&mut app, &ctx); + ok(&mut app, &ctx); + assert!(app.ui.status.contains("document was reverted")); + assert_eq!(app.session.doc().unwrap().doc.layers, restored); + assert_eq!(undo_len(&app), steps); + } +} + /// The selected art takes each colour as it is picked; OK keeps the last as one undo step and /// Cancel puts the art back. #[test]