PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(ui): Layer › New Fill Layer › Solid Color opens the Color Picker, creating the layer on OK (#5144) (#5166)
Nigel Ihza Firdausy9628290b3661
diff --git a/crates/ui-egui/src/color_picker_ui.rs b/crates/ui-egui/src/color_picker_ui.rsindex 09a3ce8d4..05c4dd5c1 100644--- a/crates/ui-egui/src/color_picker_ui.rs+++ b/crates/ui-egui/src/color_picker_ui.rs@@ -783,6 +783,9 @@ pub fn confirm(app: &mut PhotocraftApp, f: &Map<String, Value>) -> Result<Value,if crate::layer_style::color_picker::owns(f) {return crate::layer_style::color_picker::confirm(app, f);}+ if crate::solid_fill_ui::owns_new(f) {+ return crate::solid_fill_ui::confirm_new(app, f);+ }if crate::solid_fill_ui::owns(f) {return crate::solid_fill_ui::confirm(app, f);}diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex 2839e51f1..f2baba7cf 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -405,6 +405,13 @@ pub(crate) fn invoke_unguarded(app: &mut PhotocraftApp, ctx: &egui::Context, id:}Ok(Value::Null)}+ // Only interactive creation opens the Color Picker; it creates the layer on OK,+ // so Cancel leaves neither an orphan layer nor an undo/redo history entry.+ // Explicit parameters continue straight to the engine.+ "layer.newFillLayer.solidColor" if params.as_object().is_none_or(|o| o.is_empty()) => {+ let dialog = crate::solid_fill_ui::open_new(app).ok_or("no document is open")?;+ Ok(json!({"dialog": dialog}))+ }// Solid Color fills and solid-colour shapes use the Color Picker, Pattern fills the Pattern// Fill dialog; other adjustment / fill / shape controls use Properties."layer.layerContentOptions" => {diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 7f2eadd08..177ad521b 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -2607,10 +2607,12 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {app.ui.dock.collapsed.retain(|g| *g != crate::dock::Group::Properties);continue;}- if p.is_null() {- // Context-menu items behave like their menu-bar twins (dialogs included).+ if p.is_null() || id == "layer.newFillLayer.solidColor" {+ // Context-menu items and the Layers footer's Solid Color… action behave like+ // their menu-bar twins. In particular, a new Solid Color fill opens its picker.let ctx = ui.ctx().clone();- if let Err(e) = crate::menus::invoke(app, &ctx, &id, json!({})) {+ let params = if p.is_null() { json!({}) } else { p };+ if let Err(e) = crate::menus::invoke(app, &ctx, &id, params) {app.ui.status = e;app.ui.status_error = true;}diff --git a/crates/ui-egui/src/solid_fill_ui.rs b/crates/ui-egui/src/solid_fill_ui.rsindex 2ae44d50c..5d9112aba 100644--- a/crates/ui-egui/src/solid_fill_ui.rs+++ b/crates/ui-egui/src/solid_fill_ui.rs@@ -30,6 +30,36 @@ pub(crate) fn open(app: &mut PhotocraftApp) -> Option<u64> {Some(color_picker_ui::open_for_command(app, "Color Picker (Solid Color)", color.to_rgb(), cmds::SET, params))}+/// Creating a Solid Color fill is transactional: the document is unchanged until OK.+/// The picker is seeded with the foreground colour, matching the engine's default.+pub(crate) fn open_new(app: &mut PhotocraftApp) -> Option<u64> {+ let document = app.session.active()?.doc.id.0;+ let foreground = app.session.tools.foreground;+ Some(color_picker_ui::open_for_command(+ app,+ "Color Picker (Solid Color)",+ [foreground[0], foreground[1], foreground[2]],+ "layer.newFillLayer.solidColor",+ json!({"document": document}),+ ))+}++/// A creation picker is distinct from the editor opened by a fill layer's thumbnail.+pub(crate) fn owns_new(f: &Map<String, Value>) -> bool {+ f.get("__command").and_then(Value::as_str) == Some("layer.newFillLayer.solidColor")+ && f.get("__params").and_then(|p| p.get("document")).and_then(Value::as_u64).is_some()+}++pub(crate) fn confirm_new(app: &mut PhotocraftApp, f: &Map<String, Value>) -> Result<Value, String> {+ let expected = f.get("__params").and_then(|p| p.get("document")).and_then(Value::as_u64).ok_or("missing Solid Color document")?;+ let st = app.session.active().ok_or("no document open")?;+ if st.doc.id.0 != expected {+ return Err("the target document is no longer active".into());+ }+ let color = f.get("color").and_then(Value::as_str).ok_or("missing Solid Color colour")?;+ app.run("layer.newFillLayer.solidColor", json!({"color": color}))+}+pub(crate) fn owns(f: &Map<String, Value>) -> bool {f.get("__command").and_then(Value::as_str) == Some(cmds::SET)}diff --git a/crates/ui-egui/src/solid_fill_ui/tests.rs b/crates/ui-egui/src/solid_fill_ui/tests.rsindex 9b919be93..331787515 100644--- a/crates/ui-egui/src/solid_fill_ui/tests.rs+++ b/crates/ui-egui/src/solid_fill_ui/tests.rs@@ -41,6 +41,82 @@ fn row(h: &Harness<'_, PhotocraftApp>, id: LayerId) -> crate::layer_row_ui::RowRcrate::layer_row_ui::recorded(&h.ctx).into_iter().find(|r| r.layer == id.0).unwrap()}+#[test]+fn new_solid_color_from_menu_cancel_keeps_document_and_history_unchanged() {+ let (mut h, _) = harness(1.0);+ let ctx = h.ctx.clone();+ let app = h.state_mut();+ let before = app.session.active().unwrap().doc.clone();+ let active = app.session.active().unwrap().active_layer;+ let steps = app.session.active().unwrap().history.entries().len();++ let opened = crate::menus::invoke(app, &ctx, "layer.newFillLayer.solidColor", json!({})).unwrap();+ assert!(opened.get("layer").is_none(), "no layer exists until OK");+ let dialog = opened["dialog"].as_u64().unwrap();+ let picker = app.ui.dialogs.last().unwrap();+ assert_eq!(picker.id, dialog);+ assert!(owns_new(&picker.fields), "new Solid Color fill opens the shared Color Picker");+ assert_eq!(picker.fields["__params"]["document"], before.id.0);+ assert!(Arc::ptr_eq(&before, &app.session.active().unwrap().doc));+ assert_eq!(app.session.active().unwrap().history.entries().len(), steps);++ app.ui.dialog_mut(dialog).unwrap().fields.insert("color".into(), json!("#ff8800"));+ crate::dialogs::cancel(app, dialog).unwrap();+ assert!(app.ui.dialogs.iter().all(|d| d.id != dialog));+ assert!(Arc::ptr_eq(&before, &app.session.active().unwrap().doc), "Cancel must not create or delete a layer");+ assert_eq!(app.session.active().unwrap().active_layer, active);+ assert_eq!(app.session.active().unwrap().history.entries().len(), steps, "Cancel must not record a history entry");+}++#[test]+fn new_solid_color_from_menu_ok_creates_one_layer_with_chosen_or_default_color() {+ let (mut h, _) = harness(1.0);+ let ctx = h.ctx.clone();+ let app = h.state_mut();+ let steps = app.session.active().unwrap().history.entries().len();++ // OK with the initial foreground colour still creates the fill layer.+ let first = crate::menus::invoke(app, &ctx, "layer.newFillLayer.solidColor", json!({})).unwrap();+ let created = crate::dialogs::confirm(app, first["dialog"].as_u64().unwrap()).unwrap();+ let id = LayerId(created["layer"].as_u64().unwrap());+ assert!(matches!(app.session.active().unwrap().doc.layer(id).unwrap().content, LayerContent::Fill(Fill::Solid(_))));+ assert_eq!(app.session.active().unwrap().history.entries().len(), steps + 1);++ let second = crate::menus::invoke(app, &ctx, "layer.newFillLayer.solidColor", json!({})).unwrap();+ let dialog = second["dialog"].as_u64().unwrap();+ app.ui.dialog_mut(dialog).unwrap().fields.insert("color".into(), json!("#ff8800"));+ let created = crate::dialogs::confirm(app, dialog).unwrap();+ let id = LayerId(created["layer"].as_u64().unwrap());+ let LayerContent::Fill(Fill::Solid(color)) = app.session.active().unwrap().doc.layer(id).unwrap().content else {+ panic!("expected the newly created Solid Color fill");+ };+ assert_eq!(color.to_rgba8()[..3], [255, 136, 0]);+ assert_eq!(app.session.active().unwrap().active_layer, Some(id));+ assert_eq!(app.session.active().unwrap().history.entries().len(), steps + 2);++ let dialogs = app.ui.dialogs.len();+ let explicit = crate::menus::invoke(app, &ctx, "layer.newFillLayer.solidColor", json!({"color": "#123456"})).unwrap();+ assert!(explicit["layer"].as_u64().is_some());+ assert!(explicit.get("dialog").is_none());+ assert_eq!(app.ui.dialogs.len(), dialogs, "parameterised creation stays non-interactive");+ assert_eq!(app.session.active().unwrap().history.entries().len(), steps + 3);+}++#[test]+fn new_solid_color_picker_rejects_a_different_active_document() {+ let (mut h, _) = harness(1.0);+ let ctx = h.ctx.clone();+ let app = h.state_mut();+ let dialog = crate::menus::invoke(app, &ctx, "layer.newFillLayer.solidColor", json!({})).unwrap()["dialog"].as_u64().unwrap();+ app.ui.dialog_mut(dialog).unwrap().fields.insert("color".into(), json!("#ff8800"));+ app.run("file.new", json!({"width": 4, "height": 4})).unwrap();+ let before = app.session.active().unwrap().doc.clone();+ let steps = app.session.active().unwrap().history.entries().len();+ assert!(crate::dialogs::confirm(app, dialog).is_err());+ assert!(Arc::ptr_eq(&before, &app.session.active().unwrap().doc));+ assert_eq!(app.session.active().unwrap().history.entries().len(), steps);+}+#[test]fn only_the_colour_thumbnail_opens_the_solid_fill_picker() {for ppp in [1.0, 2.0] {