Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Layer Comps: the warning icon clears only its own comp (#4197) (#4198)

David Mann56869d12e380
diff --git a/crates/engine/src/comps_cmds.rs b/crates/engine/src/comps_cmds.rs
index 58f92f84e..fe5708387 100644
--- a/crates/engine/src/comps_cmds.rs
+++ b/crates/engine/src/comps_cmds.rs
@@ -319,13 +319,21 @@ fn restore_last(s: &mut Session) -> Result<Value> {
}
/// Comps that refer to deleted layers (Photoshop's warning triangle). `"clear": true` drops
-/// those stale states (Clear Layer Comp Warning) as one history step.
+/// those stale states as one history step: from `"comp"` only when given (Clear Layer Comp
+/// Warning), else from every comp (Clear All Layer Comp Warnings). `"comp"` also limits the
+/// report to that comp; an unknown comp is an error and changes nothing.
fn update_warnings(s: &mut Session, p: &Value) -> Result<Value> {
+ let cmd = "layerComp.updateWarnings";
let d = s.active().ok_or(EngineError::NoDocument)?;
+ let only = match p.get("comp") {
+ None | Some(Value::Null) => None,
+ Some(_) => Some(comp_param(&d.doc, p, cmd)?),
+ };
let warnings: Vec<Value> = d
.doc
.layer_comps
.iter()
+ .filter(|c| only.is_none_or(|id| c.id == id))
.filter_map(|c| {
let missing = c.missing_layers(&d.doc);
(!missing.is_empty()).then(|| json!({"comp": c.id, "name": c.name, "missingLayers": missing.iter().map(|l| l.0).collect::<Vec<_>>()}))
@@ -333,9 +341,10 @@ fn update_warnings(s: &mut Session, p: &Value) -> Result<Value> {
.collect();
let n = warnings.len();
if flag(p, "clear", false) && n > 0 {
- s.edit("Clear Layer Comp Warnings", |doc, _| {
+ let label = if only.is_some() { "Clear Layer Comp Warning" } else { "Clear All Layer Comp Warnings" };
+ s.edit(label, |doc, _| {
let live: std::collections::HashSet<LayerId> = doc.walk().into_iter().map(|(_, _, l)| l.id).collect();
- for c in &mut doc.layer_comps {
+ for c in doc.layer_comps.iter_mut().filter(|c| only.is_none_or(|id| c.id == id)) {
c.states.retain(|st| live.contains(&st.layer));
}
Ok(())
@@ -434,7 +443,7 @@ pub fn specs() -> Vec<CommandSpec> {
"layerComp.updateWarnings",
"Layer Comp Warnings",
&[],
- r##"{"clear":bool=false (drop states of deleted layers)} → {warnings:[{comp,name,missingLayers}],count}"##,
+ r##"{"comp":id|name? (default: every comp; limits the report and the clear to that comp),"clear":bool=false (drop states of deleted layers)} → {warnings:[{comp,name,missingLayers}],count}"##,
has_doc,
update_warnings
),
diff --git a/crates/engine/src/comps_cmds/tests.rs b/crates/engine/src/comps_cmds/tests.rs
index 22ef1a469..5458523bc 100644
--- a/crates/engine/src/comps_cmds/tests.rs
+++ b/crates/engine/src/comps_cmds/tests.rs
@@ -304,3 +304,46 @@ fn unreachable_comp_positions_leave_the_layer_in_place() {
assert_eq!(pos(&s, b), Some((31, 22)));
}
}
+
+#[test]
+fn clearing_one_comps_warning_leaves_the_others() {
+ // Regression for #4197: the per-comp warning icon cleared every comp's warnings.
+ let (mut s, a, _) = session(8);
+ let c1 = s.execute("layerComp.new", json!({"name": "One"})).unwrap()["comp"].as_u64().unwrap();
+ let c2 = s.execute("layerComp.new", json!({"name": "Two"})).unwrap()["comp"].as_u64().unwrap();
+ s.edit("del", |d, _| {
+ d.remove(a);
+ Ok(())
+ })
+ .unwrap();
+ // Reporting can be limited to one comp too.
+ let w = s.execute("layerComp.updateWarnings", json!({"comp": c2})).unwrap();
+ assert_eq!(w["count"], 1);
+ assert_eq!(w["warnings"][0]["comp"], c2);
+ s.execute("layerComp.updateWarnings", json!({"clear": true, "comp": c1})).unwrap();
+ assert!(doc(&s).comp(c1 as u32).unwrap().missing_layers(doc(&s)).is_empty());
+ assert_eq!(doc(&s).comp(c2 as u32).unwrap().missing_layers(doc(&s)), vec![a]);
+ let w = s.execute("layerComp.updateWarnings", json!({})).unwrap();
+ assert_eq!(w["count"], 1);
+ assert_eq!(w["warnings"][0]["comp"], c2);
+ // Without "comp" every comp is cleared (Clear All Layer Comp Warnings).
+ s.execute("layerComp.updateWarnings", json!({"clear": true})).unwrap();
+ assert_eq!(s.execute("layerComp.updateWarnings", json!({})).unwrap()["count"], 0);
+}
+
+#[test]
+fn clearing_warnings_of_an_unknown_comp_is_an_error() {
+ let (mut s, a, _) = session(8);
+ let c1 = s.execute("layerComp.new", json!({})).unwrap()["comp"].as_u64().unwrap();
+ s.edit("del", |d, _| {
+ d.remove(a);
+ Ok(())
+ })
+ .unwrap();
+ let before = doc(&s).layer_comps.clone();
+ for bad in [json!(c1 + 99), json!("nope"), json!(-1), json!(1.5), json!(true), json!([c1])] {
+ assert!(s.execute("layerComp.updateWarnings", json!({"clear": true, "comp": bad})).is_err(), "{bad}");
+ }
+ assert_eq!(doc(&s).layer_comps, before);
+ assert_eq!(s.execute("layerComp.updateWarnings", json!({})).unwrap()["count"], 1);
+}
diff --git a/crates/ui-egui/src/comps_ui.rs b/crates/ui-egui/src/comps_ui.rs
index d6dc3e8d8..30304f6c8 100644
--- a/crates/ui-egui/src/comps_ui.rs
+++ b/crates/ui-egui/src/comps_ui.rs
@@ -18,6 +18,12 @@ pub fn target(app: &PhotocraftApp) -> Option<u32> {
app.ui.layer_comp_selected.filter(|id| doc.comp(*id).is_some()).or(doc.last_applied_comp.filter(|id| doc.comp(*id).is_some()))
}
+/// The row's warning triangle: Clear Layer Comp Warning for that comp only (Photoshop keeps
+/// Clear All Layer Comp Warnings as a separate command).
+fn clear_warning_action(comp: u32) -> (&'static str, Value) {
+ ("layerComp.updateWarnings", json!({"clear": true, "comp": comp}))
+}
+
fn run(app: &mut PhotocraftApp, id: &str, p: Value) {
if let Err(e) = app.run(id, p) {
app.ui.status = e;
@@ -124,7 +130,7 @@ pub fn panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
));
if resp.clicked() {
toggle_hit = true;
- action = Some(("layerComp.updateWarnings", json!({"clear": true})));
+ action = Some(clear_warning_action(c.id));
}
}
name_x += 18.0;
@@ -185,3 +191,26 @@ pub fn panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use photocraft_engine::Session;
+
+ #[test]
+ fn warning_icon_clears_only_its_own_comp() {
+ // Regression for #4197.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16})).unwrap();
+ let layer = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();
+ let c1 = s.execute("layerComp.new", json!({"name": "One"})).unwrap()["comp"].as_u64().unwrap() as u32;
+ let c2 = s.execute("layerComp.new", json!({"name": "Two"})).unwrap()["comp"].as_u64().unwrap() as u32;
+ s.execute("layer.delete", json!({"layer": layer})).unwrap();
+ let (id, p) = clear_warning_action(c1);
+ assert_eq!(p["comp"], c1);
+ s.execute(id, p).unwrap();
+ let doc = &s.active().unwrap().doc;
+ assert!(doc.comp(c1).unwrap().missing_layers(doc).is_empty());
+ assert_eq!(doc.comp(c2).unwrap().missing_layers(doc).iter().map(|l| l.0).collect::<Vec<_>>(), vec![layer]);
+ }
+}