+//! ………
+//!
+//! ```
+//!
+//! 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