From 5199cc955698cb43722b286e99f4871d7ac32b5f Mon Sep 17 00:00:00 2001
From: rmsff <17902130+rmsff@users.noreply.github.com>
Date: Sat, 14 Nov 2026 03:35:27 +0300
Subject: [PATCH] Contact Sheet II: Load..., Save... and Reset... for its
settings
Save... writes the dialog's values as the reference application's
settings XML (entry labels, dot decimals, the caption font's PostScript
name, spacing in inches at 72 px/in); Load... reads such a file back
through the remembered-settings check, keeping the current value for
anything missing or broken, and shows an alert for a file that is not
one. Reset... asks "Restore default settings?" and Yes restores the
defaults with no folder chosen.
---
Cargo.lock | 1 +
crates/ui-egui/Cargo.toml | 2 +
crates/ui-egui/src/dialogs.rs | 2 +
.../dialogs/contact_sheet_settings_file.rs | 293 ++++++++++++++++++
.../ui-egui/src/dialogs/contact_sheet_ui.rs | 147 ++++++++-
crates/ui-egui/src/dialogs/file_forms.rs | 4 +-
6 files changed, 446 insertions(+), 3 deletions(-)
create mode 100644 crates/ui-egui/src/dialogs/contact_sheet_settings_file.rs
diff --git a/Cargo.lock b/Cargo.lock
index 3a0a5525c5d..37de8f13518 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7009,6 +7009,7 @@ dependencies = [
"photocraft-text",
"photocraft-vector",
"rayon",
+ "roxmltree 0.21.2",
"rustc-hash 2.1.3",
"serde",
"serde_json",
diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml
index e91a63dc85c..647e7decb93 100644
--- a/crates/ui-egui/Cargo.toml
+++ b/crates/ui-egui/Cargo.toml
@@ -53,6 +53,8 @@ serde_json = { workspace = true }
toml = { version = "1.1", default-features = false, features = ["std", "serde"] }
log = { workspace = true }
base64 = "0.23"
+# Reading Contact Sheet II settings files (`contact_sheet_settings_file.rs`); already linked through photocraft-io.
+roxmltree = "0.21"
web-time = { workspace = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index e479c0da5f9..c30bcb0d215 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -93,6 +93,7 @@ pub(crate) mod params {
},
ParamFamily {
check: Some(contact_sheet_ui::check),
+ footer: Some(contact_sheet_ui::footer),
cancel: Some(filter_dialog::cancel),
..ParamFamily::new(|key| key == contact_sheet_ui::COMMAND, contact_sheet_ui::run)
},
@@ -423,6 +424,7 @@ pub(crate) const PROMPTS: &[PromptRow] = &[
preset_group_prompt::answer,
open_prompt::answer_prompt,
alerts::answer,
+ contact_sheet_ui::answer,
crate::jobs_ui::answer,
crate::gpu_status::answer,
crate::actions::library::answer,
diff --git a/crates/ui-egui/src/dialogs/contact_sheet_settings_file.rs b/crates/ui-egui/src/dialogs/contact_sheet_settings_file.rs
new file mode 100644
index 00000000000..cc575e17e16
--- /dev/null
+++ b/crates/ui-egui/src/dialogs/contact_sheet_settings_file.rs
@@ -0,0 +1,293 @@
+//! The settings file of Contact Sheet II's Load… and Save… (#13698), in the reference
+//! application's layout:
+//!
+//! ```xml
+//!
+//!
+//!
+//!
+//!
+//! ………
+//!
+//! ```
+//!
+//! Values are the dialog's entry labels, numbers have a dot whatever the locale, the font is its
+//! PostScript name and the resolution is in the shown unit. What the dialog has no control for
+//! yet is written as today's run has it and is not taken when read: the Files list and the Use
+//! dropdown (#13686; `imageSource` is always Folder), the colour profile (#13688; a fixed name
+//! per mode) and the spacing (#13689; the field's default, 1 px, which today's run does not use).
+
+use serde_json::{Map, Value, json};
+
+use photocraft_engine::prefs::Unit;
+
+use super::{MODES, UNITS, unit_of};
+
+/// The values here are in the command's shape (`depth` a number, `placeAcrossFirst`), with the resolution's unit
+/// under [`RES_UNIT`]; `contact_sheet_ui` turns the dialog's values into it and back.
+pub(super) const RES_UNIT: &str = "resolution.unit";
+const RES_UNITS: [(&str, &str); 2] = [("in", "pixels/inch"), ("cm", "pixels/cm")];
+const DEPTHS: [(u64, &str); 2] = [(8, "8-bit"), (16, "16-bit")];
+const PLACES: [(bool, &str); 2] = [(true, "across first"), (false, "down first")];
+/// Centimetres per inch.
+const CM: f64 = 2.54;
+/// Resolution in pixels/inch.
+const RES_MIN: f64 = 1.0;
+const RES_MAX: f64 = 10_000.0;
+
+/// The dialog's defaults in the command's shape, under the values Save… writes.
+fn defaults() -> Value {
+ json!({"input": "", "units": "inches", "width": 8.0, "height": 10.0, "resolution": 300.0, "mode": "rgb", "depth": 8, "columns": 5, "rows": 6, "placeAcrossFirst": true, "autoSpacing": true, "rotateForBestFit": false, "caption": true, "font": photocraft_text::fonts::DEFAULT_FAMILY, "fontSize": 12.0, "flatten": false, "includeSubfolders": true, "groupByFolder": false})
+}
+
+fn text(f: &Map, key: &str, d: &str) -> String {
+ f.get(key).and_then(Value::as_str).unwrap_or(d).to_string()
+}
+
+fn number(f: &Map, key: &str, d: f64) -> f64 {
+ f.get(key).and_then(Value::as_f64).unwrap_or(d)
+}
+
+fn flag(f: &Map, key: &str, d: bool) -> bool {
+ f.get(key).and_then(Value::as_bool).unwrap_or(d)
+}
+
+const ROOT: &str = "ContactSheetIISettings";
+
+/// The file name Save… suggests.
+pub(super) const FILE_NAME: &str = "Contact Sheet II.xml";
+
+/// Spacing is stored in inches at 72 px/in, whatever the sheet's resolution.
+const SPACING_PPI: f64 = 72.0;
+
+/// What Save… writes after the values.
+pub(super) struct Stamp {
+ pub(super) date: String,
+ pub(super) version: String,
+ pub(super) locale: String,
+}
+
+impl Stamp {
+ /// The time (UTC: the crate has no local clock), this build's version and the UI language
+ /// code with `_` (`en`, `zh_hans`: the language setting has no region).
+ pub(super) fn now() -> Self {
+ let secs = web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH).map_or(0, |d| d.as_secs());
+ let date = i64::try_from(secs).ok().and_then(|s| chrono::DateTime::from_timestamp(s, 0)).unwrap_or_default();
+ Self {
+ date: date.format("%Y-%m-%dT%H:%M:%S").to_string(),
+ version: env!("CARGO_PKG_VERSION").into(),
+ locale: crate::i18n::current().code().replace('-', "_"),
+ }
+ }
+}
+
+/// The settings file for the dialog's fields `f`, over the dialog's defaults.
+pub(super) fn write(f: &Map, stamp: &Stamp) -> String {
+ let mut all = defaults().as_object().cloned().unwrap_or_default();
+ all.extend(f.iter().map(|(k, v)| (k.clone(), v.clone())));
+ let f = &all;
+ let res_unit = text(f, RES_UNIT, "in");
+ let per = if res_unit == "cm" { CM } else { 1.0 };
+ let mode = text(f, "mode", "");
+ let depth = f.get("depth").and_then(Value::as_f64).map_or(0, |d| d as u64);
+ let (units, width, height) = size_in_entry_units(f);
+ let spacing = format!("{:.3} in", 1.0 / SPACING_PPI);
+ let source = [
+ ("imageSource", "Folder".into()),
+ ("path", text(f, "input", "")),
+ ("includeSubfolders", flag(f, "includeSubfolders", true).to_string()),
+ ("groupImages", flag(f, "groupByFolder", false).to_string()),
+ ];
+ let document = [
+ ("units", units.into()),
+ ("width", num(width)),
+ ("height", num(height)),
+ ("resolution", num(number(f, "resolution", 0.0) / per)),
+ ("resUnits", label(&RES_UNITS, res_unit.as_str(), "in").into()),
+ ("mode", label(&MODES, mode.as_str(), "rgb").into()),
+ ("bitDepth", label(&DEPTHS, depth, 8).into()),
+ ("colorProfile", profile(&mode).into()),
+ ("flattenLayers", flag(f, "flatten", true).to_string()),
+ ];
+ let thumbnail = [
+ ("place", label(&PLACES, flag(f, "placeAcrossFirst", true), true).into()),
+ ("cols", num(number(f, "columns", 0.0))),
+ ("rows", num(number(f, "rows", 0.0))),
+ ("bestFit", flag(f, "rotateForBestFit", false).to_string()),
+ ("useAutospacing", flag(f, "autoSpacing", true).to_string()),
+ ("horz", spacing.clone()),
+ ("vert", spacing),
+ ];
+ let font = [("name", postscript_name(&text(f, "font", ""))), ("size", num(number(f, "fontSize", 0.0)))];
+ let caption = flag(f, "caption", true);
+ format!(
+ "\n<{ROOT}>\n \n \n \n
\n \n
\n {}\n {}\n {}\n{ROOT}>\n",
+ attrs(&source),
+ attrs(&document),
+ attrs(&thumbnail),
+ attrs(&font),
+ escape(&stamp.date),
+ escape(&stamp.version),
+ escape(&stamp.locale),
+ )
+}
+
+/// Units, Width and Height as an entry of the Units dropdown: units it does not list (`mm` set
+/// over the control channel) are written as inches, converted at the dialog's resolution.
+fn size_in_entry_units(f: &Map) -> (&'static str, f64, f64) {
+ let (units, width, height) = (text(f, "units", ""), number(f, "width", 0.0), number(f, "height", 0.0));
+ if let Some((id, _)) = UNITS.iter().find(|(id, _)| *id == units) {
+ return (id, width, height);
+ }
+ let (from, ppi) = (unit_of(&units), number(f, "resolution", 0.0));
+ let inches = |v: f64| Unit::Inches.from_px(from.to_px(v, ppi, 0.0, 72.0), ppi, 0.0, 72.0);
+ ("inches", inches(width), inches(height))
+}
+
+/// The dialog's fields `current` with the values of the settings file `xml` over them. A value
+/// missing from the file, or one that is not an entry label, a number or `true`/`false`, leaves
+/// the current one; the dialog refuses what its rows cannot show. `Err` when `xml` is not XML or
+/// not a Contact Sheet II settings file.
+pub(super) fn read(xml: &str, current: &Map) -> Result