Skip to content

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

New Document: presets keep the Color Profile; a stale profile is cleared

Higgs325844527b6a7e282
diff --git a/crates/engine/src/document_preset_cmds.rs b/crates/engine/src/document_preset_cmds.rs
index f0847e4b8..52c637fdc 100644
--- a/crates/engine/src/document_preset_cmds.rs
+++ b/crates/engine/src/document_preset_cmds.rs
@@ -102,6 +102,10 @@ pub struct DocumentSettings {
pub background: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub background_color: Option<[f32; 3]>,
+ /// `file.new`'s Color Profile (#321): a built-in profile id or an .icc path; absent is the
+ /// working space, as in presets saved before it existed.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub profile: Option<String>,
/// Display units only; width/height are always pixels and resolution is always ppi.
pub unit: String,
pub resolution_unit: String,
@@ -128,6 +132,9 @@ impl DocumentSettings {
if self.background_color.is_some_and(|c| !c.iter().all(|v| v.is_finite() && (0.0..=1.0).contains(v))) {
return Err("Background colour components must be within 0..1".into());
}
+ if self.profile.as_deref().is_some_and(|p| p.trim().is_empty()) {
+ return Err("A saved Color Profile needs a profile id or path".into());
+ }
if !matches!(self.unit.as_str(), "px" | "in" | "cm" | "mm" | "pt" | "pica") || !matches!(self.resolution_unit.as_str(), "in" | "cm") {
return Err("Unknown dimension or resolution display unit".into());
}
@@ -171,6 +178,9 @@ impl DocumentSettings {
if let Some(c) = self.background_color {
p["backgroundColor"] = json!(c);
}
+ if let Some(profile) = &self.profile {
+ p["profile"] = json!(profile);
+ }
p
}
}
@@ -359,6 +369,29 @@ mod tests {
next
}
+ /// #321: a preset keeps New Document's Color Profile through a reload and back into
+ /// `file.new`; one saved without it (or before it existed) stays on the working space.
+ #[test]
+ fn the_color_profile_survives_save_reload_and_file_new() {
+ let mut s = Session::new();
+ let mut config = settings();
+ config["mode"] = json!("cmyk");
+ config["profile"] = json!("coated-cmyk");
+ let preset = saved(&mut s, "Press", config.clone());
+ assert_eq!(preset["settings"]["profile"], "coated-cmyk");
+ let next = reload(&s);
+ let params = next.presets.documents[0].settings.command_params();
+ assert_eq!(params["profile"], "coated-cmyk");
+ let mut s2 = Session::new();
+ s2.execute("file.new", params).unwrap();
+ assert!(s2.active().unwrap().doc.icc_profile.is_some());
+ let plain = saved(&mut s, "Plain", settings());
+ assert!(plain["settings"].get("profile").is_none());
+ assert!(s.presets.documents[1].settings.command_params().get("profile").is_none());
+ config["profile"] = json!(" ");
+ assert!(s.execute("document.presets.save", json!({"name":"Blank","settings":config})).is_err());
+ }
+
#[test]
fn every_field_survives_save_reload_and_file_new() {
for mode in ["rgb", "gray", "cmyk", "lab"] {
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 9c105158c..9bb00b30b 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -177,10 +177,12 @@ fn file_new_tags_the_document_with_the_chosen_profile() {
}
// A profile that doesn't suit the mode, an unknown one or a wrong type opens nothing.
let open = s.documents().len();
+ let (active, steps) = (s.active().unwrap().doc.id, s.active().unwrap().history.past_len());
for bad in [json!("coated-cmyk"), json!("no-such-profile"), json!(""), json!(7), json!(null)] {
assert!(s.execute("file.new", json!({"width": 4, "height": 4, "profile": bad})).is_err(), "{bad}");
+ assert_eq!(s.documents().len(), open, "{bad}");
+ assert_eq!((s.active().unwrap().doc.id, s.active().unwrap().history.past_len()), (active, steps), "{bad}");
}
- assert_eq!(s.documents().len(), open);
}
#[test]
diff --git a/crates/ui-egui/src/new_doc_ui.rs b/crates/ui-egui/src/new_doc_ui.rs
index 761b1fd85..ca836b61f 100644
--- a/crates/ui-egui/src/new_doc_ui.rs
+++ b/crates/ui-egui/src/new_doc_ui.rs
@@ -346,6 +346,8 @@ pub fn apply_saved_preset(f: &mut Map<String, Value>, preset: &photocraft_engine
/// Restore every setting of a saved or recent document (all but its name).
pub fn apply_settings(f: &mut Map<String, Value>, settings: &DocumentSettings) {
f.remove("backgroundColor");
+ // A preset without a Color Profile is on the working space, whatever was picked before.
+ f.remove("profile");
if let Some(params) = settings.command_params().as_object() {
f.extend(params.clone());
}
@@ -745,7 +747,9 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strin
let opts = profile_options(space, &working, &installed);
let mut profile = get_s(f, "profile", "working");
if !opts.iter().any(|(v, _)| *v == profile) {
+ // Not a profile of this mode (or no longer installed): Create must not send it.
profile = "working".into();
+ f.remove("profile");
}
let shown: Vec<(String, &str)> = opts.iter().map(|(v, l)| (v.clone(), l.as_str())).collect();
if widgets::dropdown(ui, "nd-profile", &mut profile, &shown, 240.0) {
@@ -852,6 +856,24 @@ mod tests {
}
}
+ /// A saved preset replaces the Color Profile picked before: its own, or the working space.
+ #[test]
+ fn applying_a_preset_replaces_the_picked_profile() {
+ let mut s = photocraft_engine::Session::new();
+ let base = json!({"width": 8, "height": 8, "mode": "cmyk", "unit": "px"});
+ s.execute("document.presets.save", json!({"name": "Plain", "settings": base})).unwrap();
+ let mut press = base.clone();
+ press["profile"] = json!("coated-cmyk");
+ s.execute("document.presets.save", json!({"name": "Press", "settings": press})).unwrap();
+ let mut f = Map::new();
+ f.insert("profile".into(), json!(photocraft_cms::Builtin::AdobeRgbCompat.id()));
+ apply_saved_preset(&mut f, &s.presets.documents[0]);
+ assert!(!f.contains_key("profile"), "{f:?}");
+ assert!(s.execute("file.new", command_params(&f)).is_ok());
+ apply_saved_preset(&mut f, &s.presets.documents[1]);
+ assert_eq!(f["profile"], "coated-cmyk");
+ }
+
#[test]
fn units_round_trip_through_pixels() {
assert_eq!(to_unit(2100.0, "in", 300.0), 7.0);
@@ -1028,6 +1050,7 @@ mod tests {
depth: 8,
background: "white".into(),
background_color: None,
+ profile: None,
unit: "px".into(),
resolution_unit: "in".into(),
};