From f5f4c2ff22e0c05f7455c0e832290b404e92b6de Mon Sep 17 00:00:00 2001 From: Dan Date: Sat, 14 Nov 2026 22:21:07 +0000 Subject: [PATCH 1/6] Apply Layer Style dialog in one history state --- crates/engine/src/cmd/layer_menu_cmds.rs | 79 ++++++----- crates/engine/src/layer_style.rs | 32 ++++- crates/ui-egui/src/dialogs/layer_style.rs | 18 +-- .../dialogs/layer_style/ok_history_tests.rs | 123 ++++++++++++++++++ 4 files changed, 203 insertions(+), 49 deletions(-) create mode 100644 crates/ui-egui/src/dialogs/layer_style/ok_history_tests.rs diff --git a/crates/engine/src/cmd/layer_menu_cmds.rs b/crates/engine/src/cmd/layer_menu_cmds.rs index 58f253bb68..7024d9e981 100644 --- a/crates/engine/src/cmd/layer_menu_cmds.rs +++ b/crates/engine/src/cmd/layer_menu_cmds.rs @@ -522,49 +522,52 @@ fn native(_: &Session) -> Enabled { // ---------- layer style ---------- fn blending_options(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "layer.layerStyle.blendingOptions"; let id = layer_param(s, p)?; + s.edit("Blending Options", |doc, _| apply_blending_options(doc, id, p))?; + Ok(Value::Null) +} + +/// Apply parsed blending settings inside the caller's document edit. +pub(crate) fn apply_blending_options(doc: &mut Document, id: LayerId, p: &Value) -> Result<()> { + const CMD: &str = "layer.layerStyle.blendingOptions"; let blend = match p.get("blend").and_then(Value::as_str) { Some(b) => Some(crate::commands::blend_from_str(b).ok_or_else(|| bad(CMD, format!("unknown blend mode `{b}`")))?), None => None, }; let opacity = p.get("opacity").and_then(Value::as_f64).map(|v| (v as f32 / 100.0).clamp(0.0, 1.0)); let fill = p.get("fillOpacity").and_then(Value::as_f64).map(|v| (v as f32 / 100.0).clamp(0.0, 1.0)); - let mode = s.active().ok_or(EngineError::NoDocument)?.doc.mode; + let mode = doc.mode; let blend_if = match p.get("blendIf") { None => None, Some(v) => Some(blend_if_entries(v, mode)?), }; let adv = advanced_params(p, mode)?; - s.edit("Blending Options", |doc, _| { - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - if let Some(b) = blend { - if b == BlendMode::PassThrough && !l.is_group() { - return Err(bad(CMD, "Pass Through is for groups")); + let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; + if let Some(b) = blend { + if b == BlendMode::PassThrough && !l.is_group() { + return Err(bad(CMD, "Pass Through is for groups")); + } + l.blend = b; + } + if let Some(o) = opacity { + l.opacity = o; + } + if let Some(f) = fill { + l.fill_opacity = f; + } + match &blend_if { + // `null`: Blend If back to the defaults. + Some(None) => l.blend_if = BlendIf::default(), + Some(Some(entries)) => { + for (i, this, under) in entries { + let [cur_this, cur_under] = l.blend_if.get(*i); + l.blend_if.set(*i, [this.unwrap_or(cur_this), under.unwrap_or(cur_under)]); } - l.blend = b; } - if let Some(o) = opacity { - l.opacity = o; - } - if let Some(f) = fill { - l.fill_opacity = f; - } - match &blend_if { - // `null`: Blend If back to the defaults. - Some(None) => l.blend_if = BlendIf::default(), - Some(Some(entries)) => { - for (i, this, under) in entries { - let [cur_this, cur_under] = l.blend_if.get(*i); - l.blend_if.set(*i, [this.unwrap_or(cur_this), under.unwrap_or(cur_under)]); - } - } - None => {} - } - adv.apply(l); - Ok(()) - })?; - Ok(Value::Null) + None => {} + } + adv.apply(l); + Ok(()) } /// The Advanced Blending parameters of `layer.layerStyle.blendingOptions`; `None` = keep. @@ -716,7 +719,16 @@ fn blend_range(v: &Value) -> Option { } fn global_light(s: &mut Session, p: &Value) -> Result { - let cur = s.active().ok_or(EngineError::NoDocument)?.doc.global_light; + let current = s.active().ok_or(EngineError::NoDocument)?.doc.global_light; + let light = global_light_params(current, p)?; + s.edit("Global Light", |doc, _| { + doc.global_light = light; + Ok(()) + })?; + Ok(json!({"angle": light.angle, "altitude": light.altitude})) +} + +pub(crate) fn global_light_params(cur: photocraft_doc::effects::GlobalLight, p: &Value) -> Result { // Read as f64 and reject what f32 can't hold: 1e39 would become inf, and rem_euclid NaN. let f32_param = |key: &str, default: f32| match p.get(key).and_then(Value::as_f64) { None => Ok(default), @@ -726,12 +738,7 @@ fn global_light(s: &mut Session, p: &Value) -> Result { let angle = f32_param("angle", cur.angle)?; let altitude = f32_param("altitude", cur.altitude)?.clamp(0.0, 90.0); let angle = (angle + 180.0).rem_euclid(360.0) - 180.0; - s.edit("Global Light", |doc, _| { - doc.global_light.angle = angle; - doc.global_light.altitude = altitude; - Ok(()) - })?; - Ok(json!({"angle": angle, "altitude": altitude})) + Ok(photocraft_doc::effects::GlobalLight { angle, altitude }) } fn scale_effects(s: &mut Session, p: &Value) -> Result { diff --git a/crates/engine/src/layer_style.rs b/crates/engine/src/layer_style.rs index 9f7c2c7873..b1a3ee645e 100644 --- a/crates/engine/src/layer_style.rs +++ b/crates/engine/src/layer_style.rs @@ -981,8 +981,8 @@ fn same_kind(a: &Effect, b: &Effect) -> bool { /// automation mistake can't ask for an unbounded document change. const MAX_EFFECTS: usize = 256; -/// Replaces a layer's whole effect list in one history step: the Layer Style dialog's -/// apply. Each entry builds an effect from `params`, or edits the snapshot it carried +/// Applies blending options, effects, and shared light changes in one history step. +/// Each entry builds an effect from `params`, or edits the snapshot it carried /// (`fx`: the effect JSON as the dialog loaded it, kept verbatim for everything the /// dialog doesn't model) with the params present. Unknown kinds, kind/snapshot /// mismatches and malformed snapshots are errors, never panics. @@ -1037,9 +1037,33 @@ fn replace_effects(s: &mut Session, p: &Value) -> Result { } list.push(fx); } + let options = |key: &str| match p.get(key) { + None => Ok(None), + Some(value) if value.is_object() => Ok(Some(value)), + Some(_) => Err(bad(CMD, format!("`{key}` must be an object"))), + }; + let blending = options("blendingOptions")?; + let light = options("globalLight")? + .map(|params| { + let current = s.active().ok_or(EngineError::NoDocument)?.doc.global_light; + crate::layer_menu_cmds::global_light_params(current, params) + }) + .transpose()?; let count = list.len(); - let label = if list.is_empty() { "Clear Layer Style" } else { "Edit Layer Style" }; + let label = if blending.is_some() || light.is_some() { + "Layer Style" + } else if list.is_empty() { + "Clear Layer Style" + } else { + "Edit Layer Style" + }; s.edit(label, |doc, _| { + if let Some(params) = blending { + crate::layer_menu_cmds::apply_blending_options(doc, id, params)?; + } + if let Some(light) = light { + doc.global_light = light; + } for pat in &pats { crate::pattern_cmds::ensure_in_doc(doc, pat); } @@ -1190,7 +1214,7 @@ pub fn specs() -> Vec { label: "Edit Layer Style", menu: &[], shortcut: None, - params: r##"{"layer":id,"effects":[{"kind":"dropShadow|innerShadow|outerGlow|innerGlow|stroke|colorOverlay|gradientOverlay|patternOverlay|satin|bevelEmboss","params":{…layer.layerStyle. params…},"fx":}]} (replaces the layer's whole effect list; an entry with `fx` edits that effect with the params present, one without builds from params)"##, + params: r##"{"layer":id,"blendingOptions":{…layer.layerStyle.blendingOptions params…}?,"globalLight":{"angle":deg?,"altitude":deg?}?,"effects":[{"kind":"dropShadow|innerShadow|outerGlow|innerGlow|stroke|colorOverlay|gradientOverlay|patternOverlay|satin|bevelEmboss","params":{…layer.layerStyle. params…},"fx":}]} (replaces the layer's whole effect list and applies optional blending and light settings in one history state; an entry with `fx` edits that effect with the params present, one without builds from params)"##, enabled: has_layer, run: replace_effects, journal: true, diff --git a/crates/ui-egui/src/dialogs/layer_style.rs b/crates/ui-egui/src/dialogs/layer_style.rs index 65772e1573..80a6d7e8af 100644 --- a/crates/ui-egui/src/dialogs/layer_style.rs +++ b/crates/ui-egui/src/dialogs/layer_style.rs @@ -786,20 +786,17 @@ fn find_light_angle(f: &Map, initial_light_angle: Option) -> light_angle } -/// Runs the dialog's commands through `run`: blending options, then the whole -/// effect list in one `layer.layerStyle.replace` (instances keep their order; -/// their snapshots carry what the dialog doesn't model). -fn apply(f: &Map, mut run: impl FnMut(&str, Value) -> Result) -> Result { +/// Apply the blending options, ordered effect snapshots, and any shared light change together. +fn apply(f: &Map, run: impl FnOnce(&str, Value) -> Result) -> Result { validate_params(f)?; let layer = f.get("layer").cloned().unwrap_or(Value::Null); let initial_light_angle = f.get("globalLight").and_then(Value::as_f64); + let mut style = json!({"layer": layer}); if let Some(bo) = f.get(&format!("p:{BLENDING}")).and_then(Value::as_object) { let mut bo = bo.clone(); // Which Blend If channel the page shows is dialog state, not a parameter. bo.remove("blendIfChannel"); - bo.insert("layer".into(), layer.clone()); - let p = Value::Object(bo); - run("layer.layerStyle.blendingOptions", p)?; + style["blendingOptions"] = Value::Object(bo); } let mut entries = Vec::new(); for e in effects_of(f) { @@ -809,13 +806,14 @@ fn apply(f: &Map, mut run: impl FnMut(&str, Value) -> Result Result { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 16, "height": 16}))?; + app.run("layer.new.layer", json!({}))?; + Ok(app) +} + +fn layer(app: &PhotocraftApp) -> Option<&Layer> { + let state = app.session.active()?; + state.doc.layer(state.active_layer?) +} + +#[test] +fn opacity_only_ok_is_one_undoable_layer_style_edit() { + let mut app = pixel_layer_app().unwrap(); + let before = app.session.active().unwrap().history.entries().to_vec(); + let id = open(&mut app, Some(BLENDING)).unwrap(); + app.ui.dialog_mut(id).unwrap().fields.get_mut("p:blendingOptions").unwrap()["opacity"] = json!(70); + crate::dialogs::confirm(&mut app, id).unwrap(); + + let entries = app.session.active().unwrap().history.entries(); + assert_eq!(&entries[..before.len()], before.as_slice()); + assert_eq!(&entries[before.len()..], ["Layer Style"]); + assert_eq!(layer(&app).unwrap().opacity, 0.7); + assert!(layer(&app).unwrap().effects.items.is_empty()); + + app.run("edit.undo", json!({})).unwrap(); + assert_eq!(layer(&app).unwrap().opacity, 1.0); + app.run("edit.redo", json!({})).unwrap(); + assert_eq!(layer(&app).unwrap().opacity, 0.7); +} + +#[test] +fn fill_stroke_and_global_light_undo_and_redo_together() { + let mut app = pixel_layer_app().unwrap(); + app.run("layer.layerStyle.stroke", json!({"size": 3})).unwrap(); + let initial_effects = layer(&app).unwrap().effects.clone(); + let initial_light = app.session.active().unwrap().doc.global_light; + let before = app.session.active().unwrap().history.entries().len(); + let id = open(&mut app, Some(BLENDING)).unwrap(); + let fields = &mut app.ui.dialog_mut(id).unwrap().fields; + fields.get_mut("p:blendingOptions").unwrap()["fillOpacity"] = json!(40); + fields.insert("pendingGlobalLight".into(), json!(45)); + set_param(fields, "fx1", "size", json!(7)); + crate::dialogs::confirm(&mut app, id).unwrap(); + + assert_eq!(&app.session.active().unwrap().history.entries()[before..], ["Layer Style"]); + assert_eq!(layer(&app).unwrap().fill_opacity, 0.4); + let edited_effects = layer(&app).unwrap().effects.clone(); + assert!(matches!(&edited_effects.items[0], photocraft_doc::Effect::Stroke(stroke) if stroke.size == 7.0)); + assert_eq!(app.session.active().unwrap().doc.global_light.angle, 45.0); + + app.run("edit.undo", json!({})).unwrap(); + assert_eq!(layer(&app).unwrap().fill_opacity, 1.0); + assert_eq!(layer(&app).unwrap().effects, initial_effects); + assert_eq!(app.session.active().unwrap().doc.global_light, initial_light); + + app.run("edit.redo", json!({})).unwrap(); + assert_eq!(layer(&app).unwrap().fill_opacity, 0.4); + assert_eq!(layer(&app).unwrap().effects, edited_effects); + assert_eq!(app.session.active().unwrap().doc.global_light.angle, 45.0); +} + +#[test] +fn cancel_and_escape_leave_history_and_opacity_unchanged() { + let app = pixel_layer_app().unwrap(); + let mut h = Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_eframe(|cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); + app + }); + let before = h.state().session.active().unwrap().history.entries().to_vec(); + for escape in [false, true] { + let app = h.state_mut(); + let id = open(app, Some(BLENDING)).unwrap(); + app.ui.dialog_mut(id).unwrap().fields.get_mut("p:blendingOptions").unwrap()["opacity"] = json!(70); + h.run_steps(3); + if escape { + h.key_press(egui::Key::Escape); + h.run_steps(3); + } else { + crate::dialogs::cancel(h.state_mut(), id).unwrap(); + } + assert!(h.state_mut().ui.dialog_mut(id).is_none()); + assert_eq!(h.state().session.active().unwrap().history.entries(), before.as_slice()); + assert_eq!(layer(h.state()).unwrap().opacity, 1.0); + } +} + +#[test] +fn unchanged_ok_adds_at_most_one_step() { + let mut app = pixel_layer_app().unwrap(); + let before = app.session.active().unwrap().history.entries().len(); + let id = open(&mut app, None).unwrap(); + crate::dialogs::confirm(&mut app, id).unwrap(); + let entries = app.session.active().unwrap().history.entries(); + assert!(entries.len() <= before + 1); + if let Some(label) = entries.get(before) { + assert_eq!(label, "Layer Style"); + } +} + +#[test] +fn rejected_effect_keeps_opacity_and_history_unchanged() { + let mut app = pixel_layer_app().unwrap(); + app.run("layer.layerStyle.stroke", json!({"size": 3})).unwrap(); + let initial_effects = layer(&app).unwrap().effects.clone(); + let before = app.session.active().unwrap().history.entries().to_vec(); + let id = open(&mut app, Some(BLENDING)).unwrap(); + let fields = &mut app.ui.dialog_mut(id).unwrap().fields; + fields.get_mut("p:blendingOptions").unwrap()["opacity"] = json!(70); + fields.get_mut("effects").unwrap()[0]["kind"] = json!("unknown-effect"); + assert!(crate::dialogs::confirm(&mut app, id).is_err()); + + assert_eq!(app.session.active().unwrap().history.entries(), before.as_slice()); + assert_eq!(layer(&app).unwrap().opacity, 1.0); + assert_eq!(layer(&app).unwrap().effects, initial_effects); +} From 9cce22b806d3d8b961f0d0897c5708b02af56cb5 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 15 Nov 2026 16:41:17 +0100 Subject: [PATCH 2/6] Artboards to Files order test: expect the file names (#15170) #13641 renamed the exported files to with spaces as hyphens; the order test from #13670 still expected the old p_0000_Empty Board.png names and failed on main. Assert all three names in top-first order. (cherry picked from commit 42e9467bf9d746140a7ecd7a8b7b6382b1b5769b) --- crates/engine/src/cmd/artboard_cmds/pdf_page_order_tests.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/artboard_cmds/pdf_page_order_tests.rs b/crates/engine/src/cmd/artboard_cmds/pdf_page_order_tests.rs index 49356105a6..0541f8e456 100644 --- a/crates/engine/src/cmd/artboard_cmds/pdf_page_order_tests.rs +++ b/crates/engine/src/cmd/artboard_cmds/pdf_page_order_tests.rs @@ -54,5 +54,7 @@ fn artboards_to_files_keeps_its_top_first_order() { let r = s.execute("file.export.artboardsToFiles", json!({"dir": dir.to_string_lossy(), "prefix": "p"})).unwrap(); let _ = std::fs::remove_dir_all(&dir); let files: Vec<&str> = r["files"].as_array().unwrap().iter().filter_map(Value::as_str).collect(); - assert!(files[0].ends_with("p_0000_Empty Board.png") && files[2].ends_with("p_0002_Red Board.png"), "{files:?}"); + // Files are named , spaces as hyphens (#13633); top artboard first. + let names: Vec<&str> = files.iter().map(|f| f.rsplit(['/', '\\']).next().unwrap_or(f)).collect(); + assert_eq!(names, ["pEmpty-Board.png", "pBlue-Board.png", "pRed-Board.png"], "{files:?}"); } From 9927d15d469d281dc46216d47a9729c2b0fdb0ad Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 15 Nov 2026 16:01:21 +0000 Subject: [PATCH 3/6] Make the JPEG preview revision test independent of worker timing --- .../dialogs/jpeg_options_ui/size_readout/tests.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/dialogs/jpeg_options_ui/size_readout/tests.rs b/crates/ui-egui/src/dialogs/jpeg_options_ui/size_readout/tests.rs index f5cce74ce9..166ff8e29b 100644 --- a/crates/ui-egui/src/dialogs/jpeg_options_ui/size_readout/tests.rs +++ b/crates/ui-egui/src/dialogs/jpeg_options_ui/size_readout/tests.rs @@ -118,7 +118,17 @@ fn unchanged_settings_are_not_encoded_again() { assert_eq!(encodes.load(Ordering::SeqCst), 3); let edited = Key { revision: 4, ..key }; - assert_eq!(readout.update(edited, || || None, || {}), Estimate::Pending(None), "an edited document shows no size of the old one"); + let (ready, wait) = std::sync::mpsc::channel::<()>(); + let estimate = readout.update( + edited, + || move || { + let _ = wait.recv(); + None + }, + || {}, + ); + ready.send(()).unwrap(); + assert_eq!(estimate, Estimate::Pending(None), "an edited document shows no size of the old one"); } /// Going back to settings already worked out shows their size at once, without encoding again; From f191395604a966205ae1bb4c6c5b98c37ffd8f71 Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 15 Nov 2026 16:08:36 +0000 Subject: [PATCH 4/6] Format the JPEG preview regression test --- .../dialogs/jpeg_options_ui/size_readout/tests.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/dialogs/jpeg_options_ui/size_readout/tests.rs b/crates/ui-egui/src/dialogs/jpeg_options_ui/size_readout/tests.rs index 166ff8e29b..94371e0533 100644 --- a/crates/ui-egui/src/dialogs/jpeg_options_ui/size_readout/tests.rs +++ b/crates/ui-egui/src/dialogs/jpeg_options_ui/size_readout/tests.rs @@ -119,14 +119,11 @@ fn unchanged_settings_are_not_encoded_again() { let edited = Key { revision: 4, ..key }; let (ready, wait) = std::sync::mpsc::channel::<()>(); - let estimate = readout.update( - edited, - || move || { - let _ = wait.recv(); - None - }, - || {}, - ); + let encode = move || { + let _ = wait.recv(); + None + }; + let estimate = readout.update(edited, || encode, || {}); ready.send(()).unwrap(); assert_eq!(estimate, Estimate::Pending(None), "an edited document shows no size of the old one"); } From 8f09b5b2616feb767398457d66699b6f2f199354 Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 15 Nov 2026 16:22:47 +0000 Subject: [PATCH 5/6] Use an action row in the shortcut set switch test --- .../src/prefs/prefs_ui/shortcut_set_switch_tests.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/prefs/prefs_ui/shortcut_set_switch_tests.rs b/crates/ui-egui/src/prefs/prefs_ui/shortcut_set_switch_tests.rs index dafa8be560..7512acee99 100644 --- a/crates/ui-egui/src/prefs/prefs_ui/shortcut_set_switch_tests.rs +++ b/crates/ui-egui/src/prefs/prefs_ui/shortcut_set_switch_tests.rs @@ -165,7 +165,13 @@ fn an_unmodified_set_switches_at_once() { fn an_action_function_key_is_no_edit() { let (mut h, first) = dialog(); h.state_mut().ui.dialogs.retain(|d| d.id != first); - crate::actions::assign_shortcut(h.state_mut(), "Sepia", "F2").unwrap(); + let app = h.state_mut(); + app.run("actions.record", json!({"name": "Sepia"})).unwrap(); + app.run("actions.stop", json!({})).unwrap(); + let row = app.session.actions.list.iter().position(|action| action.name == "Sepia").unwrap(); + crate::actions::assign_shortcut(app, row, "F2").unwrap(); + let action_id = app.session.actions.list[row].action_id.as_deref().unwrap(); + let shortcut_id = photocraft_engine::actions_cmds::occurrence_key(action_id); let ctx = h.ctx.clone(); let id = crate::menus::invoke(h.state_mut(), &ctx, "edit.keyboardShortcuts", json!({})).unwrap()["dialog"].as_u64().unwrap(); h.run_steps(4); @@ -173,6 +179,7 @@ fn an_action_function_key_is_no_edit() { pick_set(&mut h, "PhotoCraft Defaults"); assert!(!asking(&h)); assert_eq!(field(&h, id, "activeSet"), json!("")); + assert_eq!(field(&h, id, "overrides")[shortcut_id], "F2"); } #[test] From 9c86fb6826e6cca3167b82ecdf154f2ee93e029d Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 25 Nov 2026 20:01:40 +0000 Subject: [PATCH 6/6] Use the boxed app in Layer Style regression tests --- crates/ui-egui/src/dialogs/layer_style/ok_history_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ui-egui/src/dialogs/layer_style/ok_history_tests.rs b/crates/ui-egui/src/dialogs/layer_style/ok_history_tests.rs index a1a5a76e78..55d7c5aae7 100644 --- a/crates/ui-egui/src/dialogs/layer_style/ok_history_tests.rs +++ b/crates/ui-egui/src/dialogs/layer_style/ok_history_tests.rs @@ -7,7 +7,7 @@ use super::{ use egui_kittest::Harness; use photocraft_doc::Layer; -fn pixel_layer_app() -> Result { +fn pixel_layer_app() -> Result, String> { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); app.run("file.new", json!({"width": 16, "height": 16}))?; app.run("layer.new.layer", json!({}))?;