From e2aa47c78055ea9a5cc32ccdfe09e358d77f6eec Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 21 Nov 2026 04:56:13 -0600 Subject: [PATCH] Layer Style: a new Drop Shadow starts with the last committed values A new Drop Shadow now starts from the values of the last Drop Shadow committed with OK, kept in the framework's saved memory so they survive a restart, over the usual defaults. An effect lit by the shared light keeps the document's Global Light angle. Cancel stores nothing, and Reset to Default still writes the Make Default or factory values. Fixes #14877 --- crates/ui-egui/src/dialogs/layer_style.rs | 43 +++++++++- .../drop_shadow_last_values_tests.rs | 86 +++++++++++++++++++ 2 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/dialogs/layer_style/drop_shadow_last_values_tests.rs diff --git a/crates/ui-egui/src/dialogs/layer_style.rs b/crates/ui-egui/src/dialogs/layer_style.rs index 4a5aa47dd8..6a43647c80 100644 --- a/crates/ui-egui/src/dialogs/layer_style.rs +++ b/crates/ui-egui/src/dialogs/layer_style.rs @@ -37,6 +37,8 @@ use crate::{PhotocraftApp, color_picker_ui}; mod bevel_reset_angle_tests; pub(crate) mod color_picker; mod contour; +#[cfg(test)] +mod drop_shadow_last_values_tests; mod gradient_field; #[cfg(test)] mod gradient_method_tests; @@ -481,7 +483,8 @@ pub fn open(app: &mut PhotocraftApp, select: Option<&str>) -> Option { let (light, mode, doc) = (st.doc.global_light.angle, st.doc.mode, st.doc.id); let user: Map = app.session.presets.layer_defaults.iter().map(|(k, v)| (k.clone(), v.clone())).collect(); let keys = blend_keys(); - let specs = specs(&keys, &user, light); + let mut specs = specs(&keys, &user, light); + start_from_last_committed(app, &mut specs, light); let mut draft = LayerStyleDraft::new(draft_of(&layer, light, mode)); match select { None => { @@ -536,6 +539,41 @@ pub(crate) fn open_layer(app: &PhotocraftApp) -> Option { (app.session.active()?.doc.id == doc).then_some(layer) } +/// The effects whose new instance starts with the values of the last one committed with OK, also after a restart, as +/// the reference's do (#14877, Drop Shadow). +const STARTS_FROM_LAST: &[EffectKind] = &[EffectKind::DropShadow]; + +/// Where the last committed values of `kind` are kept (the framework's saved memory, so they survive a restart). +fn last_committed_key(kind: EffectKind) -> craft::ui::runtime::memory::SaveKey { + craft::ui::runtime::memory::SaveKey::named(format!("photocraft.layerStyle.lastCommitted.{}", kind.key())) +} + +/// A new instance of the [`STARTS_FROM_LAST`] kinds starts from the last committed values over the usual defaults; an +/// effect lit by the shared light keeps the document's light angle. Reset to Default still writes the Make Default (or +/// factory) values, which it reads from the engine. +fn start_from_last_committed(app: &PhotocraftApp, specs: &mut [EffectSpec], light: f32) { + for spec in specs.iter_mut().filter(|s| STARTS_FROM_LAST.contains(&s.kind)) { + let Some(last) = craft::ui::runtime::memory::saved_value::>(app.ctx(), &last_committed_key(spec.kind)) else { continue }; + let mut values = style_params_json(&spec.defaults); + if let (Some(o), Some(l)) = (values.as_object_mut(), last.as_object()) { + o.extend(l.iter().map(|(k, v)| (k.clone(), v.clone()))); + } + if values.get("useGlobalLight").and_then(Value::as_bool) == Some(true) { + values["angle"] = json!(light); + } + spec.defaults = style_params(spec.kind, &values, None).over(&spec.defaults); + } +} + +/// After an OK: keeps the values of the committed style's last instance of each [`STARTS_FROM_LAST`] kind. +fn keep_last_committed(app: &PhotocraftApp, style: &LayerStyle) { + for kind in STARTS_FROM_LAST { + if let Some(e) = style.effects.iter().rev().find(|e| e.kind == *kind) { + craft::ui::runtime::memory::set_saved_value::>(app.ctx(), &last_committed_key(*kind), Some(style_params_json(&e.params))); + } + } +} + /// The effect pages with the user's defaults (Make Default) over the factory ones. fn specs(keys: &HashMap, user: &Map, light: f32) -> Vec { KINDS @@ -859,8 +897,9 @@ fn finish(app: &mut PhotocraftApp, doc: photocraft_doc::DocId, layer: LayerId, s return fail(app, "Layer Style target document changed".into()); }; if let Err(error) = apply(style, layer, Some(light), |id, p| app.run(id, p)) { - fail(app, error); + return fail(app, error); } + keep_last_committed(app, style); } /// Applies a style preset to the draft without touching the document: Cancel leaves the document diff --git a/crates/ui-egui/src/dialogs/layer_style/drop_shadow_last_values_tests.rs b/crates/ui-egui/src/dialogs/layer_style/drop_shadow_last_values_tests.rs new file mode 100644 index 0000000000..186ffe1775 --- /dev/null +++ b/crates/ui-egui/src/dialogs/layer_style/drop_shadow_last_values_tests.rs @@ -0,0 +1,86 @@ +//! #14877: a new Drop Shadow starts with the values of the last Drop Shadow committed with OK, also on another +//! document, with that document's Global Light angle; Cancel doesn't change them, and Reset to Default still gives +//! the factory values. + +use craft::imaging::layer_style::{EffectKind, StyleValue}; +use serde_json::json; + +use super::reset_to_default; +use super::tests::{app_with_layer, cancel, defaults, draft, edit, first_of, ok}; +use super::{DialogId, open}; +use crate::PhotocraftApp; + +const KEYS: [&str; 4] = ["opacity", "distance", "spread", "size"]; + +/// Opens the dialog on a new Drop Shadow of the active layer. +fn new_shadow(app: &mut PhotocraftApp) -> DialogId { + open(app, Some(EffectKind::DropShadow.key())).unwrap() +} + +fn values(app: &PhotocraftApp, id: DialogId) -> Vec> { + let shadow = first_of(app, id, EffectKind::DropShadow); + let e = draft(app, id).style.effect(shadow).unwrap(); + KEYS.iter().map(|k| e.params.number(k)).collect() +} + +fn angle(app: &PhotocraftApp, id: DialogId) -> Option { + let shadow = first_of(app, id, EffectKind::DropShadow); + draft(app, id).style.effect(shadow).and_then(|e| e.params.number("angle")) +} + +/// OK on a new Drop Shadow with Opacity 50, Distance 10, Spread 20, Size 15. +fn commit_shadow(app: &mut PhotocraftApp) { + let id = new_shadow(app); + let shadow = first_of(app, id, EffectKind::DropShadow); + edit(app, id, |d| { + for (k, v) in KEYS.iter().zip([50.0, 10.0, 20.0, 15.0]) { + d.set_param(shadow, k, StyleValue::Number(v)); + } + }); + ok(app, id); +} + +/// A new document with a layer and its Global Light at `angle`. +fn other_document(app: &mut PhotocraftApp, angle: f64) { + app.run("file.new", json!({"width": 48, "height": 48})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("layer.layerStyle.globalLight", json!({"angle": angle})).unwrap(); +} + +#[test] +fn a_new_drop_shadow_starts_with_the_last_committed_values_and_the_documents_light() { + let mut app = app_with_layer(48); + let first = new_shadow(&mut app); + let factory = values(&app, first); + cancel(&mut app, first); + commit_shadow(&mut app); + other_document(&mut app, 30.0); + let id = new_shadow(&mut app); + assert_eq!(values(&app, id), vec![Some(50.0), Some(10.0), Some(20.0), Some(15.0)], "factory was {factory:?}"); + assert_eq!(angle(&app, id), Some(30.0), "the angle is the new document's Global Light"); +} + +#[test] +fn cancel_does_not_change_the_starting_values() { + let mut app = app_with_layer(48); + commit_shadow(&mut app); + other_document(&mut app, 120.0); + let id = new_shadow(&mut app); + let shadow = first_of(&app, id, EffectKind::DropShadow); + edit(&mut app, id, |d| d.set_param(shadow, "opacity", StyleValue::Number(90.0))); + cancel(&mut app, id); + let id = new_shadow(&mut app); + assert_eq!(values(&app, id)[0], Some(50.0), "the cancelled 90 is not reused"); +} + +#[test] +fn reset_to_default_still_gives_the_factory_values() { + let mut app = app_with_layer(48); + commit_shadow(&mut app); + other_document(&mut app, 120.0); + let id = new_shadow(&mut app); + let shadow = first_of(&app, id, EffectKind::DropShadow); + reset_to_default(&mut app, id, shadow); + let want = defaults(EffectKind::DropShadow); + assert_eq!(values(&app, id), KEYS.iter().map(|k| want.number(k)).collect::>()); +}