GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! Window → Asset Export and Object → Collect for Export: art collected as assets
//! ([`vectorcraft_doc::ExportAsset`]), exported each on its own, cropped to its art, at the export
//! settings Export for Screens shares (`document.exportSettings`). File → Export Selection collects
//! the selection this way and opens Export for Screens on its Assets tab (a UI command).

use std::collections::HashSet;

use serde_json::{Map, Value, json};
use vectorcraft_doc::{ExportAsset, NodeId};

use super::edit::{refuse_symbol_part, roots_of, selected_roots};
use super::fileio::{
    DEFAULT_FORMAT_3D, FORMATS_3D, SCREEN_SETTINGS_KEYS, TEXTURE_SIZE, TEXTURE_SIZES, check_screen_settings, create_dir, export_crop, export_screens,
    image_maps, mesh_file_names, mesh_files, safe_file_name, store_screen_settings, textures, with_screen_export_preset, write_file,
};
use super::{CommandSpec, EngineError, Result, Session, bad, bool_or, checked_ids_param, cmd, has_doc, ok, str_param, switch_or, text_param};

/// The longest asset name kept (characters).
const MAX_NAME: usize = 255;

pub fn specs() -> Vec<CommandSpec> {
    vec![
        cmd!(
            "assets.add",
            "Collect for Export",
            ["Object", "Collect for Export"],
            None,
            "{ids?: [id…] (default: the selected objects; a layer id collects the art under it, including in its sublayers), multiple?: true (an asset per object; false: one asset of them all), name?: (the asset's name, when it makes one)} collect art as assets for Window › Asset Export and Export for Screens' Assets tab, named after the object (else Asset 1, Asset 2…); art already collected as such an asset keeps it → {assets: [asset id…] (in order), added: n}. Assets follow their art: moving it moves the crop, deleting it removes it from the asset",
            has_doc,
            add
        ),
        cmd!(
            "assets.remove",
            "Remove Asset",
            [],
            None,
            "{assets: [asset id…]} take assets out of the Asset Export panel (their art stays) → {removed: n}",
            has_doc,
            remove
        ),
        cmd!("assets.rename", "Rename Asset", [], None, "{asset: id, name} rename an asset (its files' names)", has_doc, rename),
        cmd!(
            query "assets.list",
            "Assets",
            [],
            None,
            "{} → {assets: [{id, name, nodes: [id…], bounds: [x, y, width, height] | null (the crop: the bounds the renderer measures for what its art draws, effects included, with the clips of the groups and layers that hold it), format3D: usda|usdz|gltf|obj | null (a 3D asset's mesh format; null for art exported as pictures)}]} the Asset Export panel's assets, in order",
            has_doc,
            list
        ),
        cmd!(
            "assets.export",
            "Export Assets",
            [],
            None,
            "{assets?: [asset id…] (default all but the 3D assets, which assets.export3D writes), folder?, zip?, openLocation?, formats?, preset?, settings?, prefix?, subfolders?, antiAlias?} each asset's art alone, cropped to it, in every format row: document.exportForScreens {assets} at the document's export settings (document.exportSettings) under the ones given; with subfolders false every file goes into the folder itself, mobile and density rows too → what document.exportForScreens returns (no folder: the files, or one zip, as dataBase64; warnings name a key in the rows or settings given that only another format takes, not one in the remembered settings). Leaves the remembered settings as they are (assets.settings.set changes them)",
            has_doc,
            export
        ),
        cmd!(
            "assets.add3D",
            "Export 3D Object",
            [],
            None,
            "{ids?: [id…] (default: the selected objects), format?: usda|usdz|gltf|obj (default usda)} collect each object with an Extrude & Bevel, Revolve, Inflate or Rotate effect as a 3D asset of Window › Asset Export, named after the object (else Asset 1, Asset 2…): assets.export3D writes it as a mesh file. An object already collected as a 3D asset keeps it; an object in ids without such an effect is refused. Without ids, the selected objects without one (on a group or layer, one on its fill or stroke draws nothing and does not count), and a selected object inside another selected object that has one, are left out; refused when none is left → {assets: [asset id…] (in order), added: n}",
            has_doc,
            add_3d
        ),
        cmd!(
            "assets.set3DFormat",
            "3D Export Format",
            [],
            None,
            "{assets: [asset id…], format: usda|usdz|gltf|obj} the mesh format 3D assets export to (Asset Export's Format for a 3D asset) → {changed: n}",
            has_doc,
            set_3d_format
        ),
        cmd!(
            "assets.export3D",
            "Export 3D Assets",
            [],
            None,
            "{assets?: [asset id…] (default every 3D asset), folder?, zip?, textureSize?: 128…4096 (default 1024: the pixels along the longer side of each face's texture)} each 3D asset's object as a mesh file in its format (assets.set3DFormat), named after the asset: USDA or USDZ, glTF 2.0 (one .gltf, its buffer embedded) or OBJ with its .mtl (whitespace and # in an OBJ's name become _). Every solid of the object (the fill's, each stroke's, a group's members') is a mesh with a material in the color it is painted; faces turned away from the viewer are included, at the object's 3D rotation, y up, about the point it turns about, in points (glTF: meters). The graphics mapped onto a plane or onto Extrude & Bevel's front or back cap (threeD.mapGraphic) are a PNG texture of that face: its color with the graphics drawn over it, flat and unlit, on a mesh of the face's own with texture coordinates; glTF embeds it, USDZ stores it after the layer, and OBJ and USDA write it beside them as <name>-texture-1.png… (whitespace, # and @ become _); a face that is not opaque keeps its opacity in the texture's alpha, and an OBJ also gets <name>-texture-1-opacity.png…, the alpha as gray levels for its map_d. Graphics on Revolve's side (in a group, the side of the member with the longest profile) are a texture of the side unrolled as the canvas lays it out, on a mesh of the side's triangles; its end caps and cut sides stay plain. Files that would share a name get -2, -3…; an object with a hole that crosses its face's outline or another hole, or with more than 2,000,000 triangles, or textures of more than 67,108,864 pixels in one export, is refused → {files: [path…]} (no folder: {files: [{name, dataBase64}]}, or with zip one zip as {name, dataBase64, files})",
            has_doc,
            export_3d
        ),
        cmd!(
            "assets.settings.set",
            "Asset Export Settings",
            [],
            None,
            "{exportPreset?: (a preset of document.exportPresets.list: its formats or preset, its format settings over the saved ones, its prefix (none when it has none), its folders (or subfolders: rows or none) with subfolders on for rows, and antiAlias; the other keys go over it), formats?, preset?: mobile|density|\"\" (none), settings?, prefix?, subfolders?: true|false (Create Sub-folders: true sorts the files into a folder per row; false puts assets.export's files in the folder itself and, given without exportPreset, sets a remembered folder per format or item to none as well), antiAlias?, openLocation?: true|false (Open Location after Export: remembered in document.exportSettings, not in presets or the reply; given alone to a document without settings, Create Sub-folders stays on)} change the export settings the Asset Export panel and Export for Screens share (document.exportSettings; the values of document.exportForScreens; null removes one): formats replace a preset, a preset replaces the formats → {settings}. Not an undo step",
            has_doc,
            settings_set
        ),
    ]
}

/// `e` as an error of command `cmd` (for errors of the export underneath).
pub(crate) fn of_cmd(cmd: &str, e: EngineError) -> EngineError {
    match e {
        EngineError::BadParams { msg, .. } => bad(cmd, msg),
        e => e,
    }
}

/// `name` trimmed and at most [`MAX_NAME`] characters long (`None` when empty).
fn clean_name(name: &str) -> Option<String> {
    let n: String = name.trim().chars().take(MAX_NAME).collect();
    (!n.is_empty()).then_some(n)
}

/// Ids of art under `id`: the id itself when it is not a layer, else every non-layer under it
/// (sublayers unfolded). Layers alone have nothing to collect.
pub(crate) fn art_under(doc: &vectorcraft_doc::Document, id: NodeId, out: &mut Vec<NodeId>) {
    let Some(n) = doc.node(id) else { return };
    if n.is_layer() {
        if let Some(kids) = n.children() {
            for c in kids.iter().filter(|c| !c.is_ruler_guide()) {
                art_under(doc, c.id, out);
            }
        }
        return;
    }
    if !n.is_ruler_guide() {
        out.push(id);
    }
}

fn add(s: &mut Session, p: &Value) -> Result<Value> {
    const C: &str = "assets.add";
    let ids = if p.get("ids").is_some_and(|v| !v.is_null()) {
        // An explicit list is authoritative: never silently drop malformed or stale ids.
        let ids = checked_ids_param(s, p, "ids", C)?;
        let d = &s.doc()?.doc;
        let mut art = Vec::new();
        for id in &ids {
            art_under(d, *id, &mut art);
        }
        roots_of(d, d.paint_order(art))
    } else {
        selected_roots(s)?
    };
    if ids.is_empty() {
        return Err(bad(C, "select the art to collect (or give its ids)"));
    }
    let name = str_param(p, "name").and_then(clean_name);
    let sets: Vec<Vec<NodeId>> = if bool_or(p, "multiple", true) { ids.iter().map(|id| vec![*id]).collect() } else { vec![ids] };
    if name.is_some() && sets.len() > 1 {
        return Err(bad(C, "a name names one asset: give multiple: false, or one object"));
    }
    // A 3D asset of the same art is another asset.
    let existing =
        |d: &vectorcraft_doc::Document, nodes: &[NodeId]| d.assets.iter().find(|a| a.nodes == nodes && a.format_3d.is_none()).map(|a| a.id);
    let d = &s.doc()?.doc;
    // Nothing new: no undo step.
    if sets.iter().all(|set| existing(d, set).is_some()) {
        return Ok(json!({ "assets": sets.iter().filter_map(|set| existing(d, set)).collect::<Vec<_>>(), "added": 0 }));
    }
    s.edit("Collect for Export", |d, _| {
        let (mut out, mut added) = (vec![], 0);
        for nodes in sets {
            if let Some(id) = existing(d, &nodes) {
                out.push(id);
                continue;
            }
            let own = match nodes.as_slice() {
                [one] => d.node(*one).and_then(|n| n.name.as_deref()).and_then(clean_name),
                _ => None,
            };
            let name = name.clone().or(own).unwrap_or_else(|| d.next_asset_name());
            let id = d.alloc_id().0;
            d.assets.push(ExportAsset { id, name, nodes, format_3d: None });
            out.push(id);
            added += 1;
        }
        Ok(json!({ "assets": out, "added": added }))
    })
}

/// The format of param `format` of command `cmd` ([`FORMATS_3D`]), `default` when it is not given.
fn format_3d_param(p: &Value, cmd: &str, default: Option<&str>) -> Result<String> {
    match (p.get("format"), default) {
        (None, Some(d)) => Ok(d.to_string()),
        (Some(Value::String(f)), _) if FORMATS_3D.contains(&f.as_str()) => Ok(f.clone()),
        _ => {
            let key = "format";
            Err(bad(cmd, format!("`{key}` must be one of {}", FORMATS_3D.join(", "))))
        }
    }
}

/// The asset ids of param `key` of command `cmd`, each a 3D asset of `d`.
fn three_d_asset_ids(d: &vectorcraft_doc::Document, p: &Value, key: &str, cmd: &str) -> Result<Vec<u64>> {
    let ids = asset_ids(p, key, cmd)?;
    for id in &ids {
        match d.asset(*id) {
            None => return Err(bad(cmd, "no such assets (see assets.list)")),
            Some(a) if a.format_3d.is_none() => return Err(bad(cmd, format!("asset {id} isn't a 3D asset"))),
            _ => {}
        }
    }
    Ok(ids)
}

/// The selected objects `assets.add3D` collects without `ids`: those with a 3D effect it exports
/// ([`vectorcraft_render::effects::exports_three_d`]; a path of a compound path counts as the
/// compound path), layers included, in tree order, dropping
/// each one inside another of them (that one's asset holds it). Refused while an object of a
/// dynamic symbol instance's art is selected, as [`selected_roots`] is.
fn selected_3d_roots(s: &Session) -> Result<Vec<NodeId>> {
    refuse_symbol_part(s)?;
    let st = s.doc()?;
    // A path of a compound path stands for the compound path, which holds the paint and effects.
    let found = st
        .selection
        .objects
        .iter()
        .map(|id| super::path::compound_or_self(&st.doc, *id))
        .filter(|id| st.doc.node(*id).is_some_and(vectorcraft_render::effects::exports_three_d));
    let mut paths: Vec<(Vec<usize>, NodeId)> = st.doc.index_paths(found).into_iter().map(|(id, path)| (path, id)).collect();
    paths.sort_by(|a, b| a.0.cmp(&b.0));
    let mut kept: Vec<(Vec<usize>, NodeId)> = Vec::with_capacity(paths.len());
    for (path, id) in paths {
        if !kept.last().is_some_and(|(outer, _)| path.starts_with(outer)) {
            kept.push((path, id));
        }
    }
    Ok(kept.into_iter().map(|(_, id)| id).collect())
}

fn add_3d(s: &mut Session, p: &Value) -> Result<Value> {
    const C: &str = "assets.add3D";
    let format = format_3d_param(p, C, Some(DEFAULT_FORMAT_3D))?;
    let ids = if p.get("ids").is_some_and(|v| !v.is_null()) { checked_ids_param(s, p, "ids", C)? } else { selected_3d_roots(s)? };
    if ids.is_empty() {
        return Err(bad(C, "select an object with a 3D effect (or give its ids)"));
    }
    let d = &s.doc()?.doc;
    let mut ids = d.paint_order(ids);
    ids.dedup();
    for id in &ids {
        let n = d.node(*id).ok_or_else(|| bad(C, format!("no such object {0}", id.0)))?;
        if let Err(e) = vectorcraft_render::effects::three_d_meshes_in(Some(d), n) {
            let what = n.name.clone().unwrap_or_else(|| format!("#{}", id.0));
            return Err(bad(C, format!("{what} can't be exported in 3D: {e}")));
        }
    }
    let existing = |d: &vectorcraft_doc::Document, id: NodeId| d.assets.iter().find(|a| a.nodes == [id] && a.format_3d.is_some()).map(|a| a.id);
    // Nothing new: no undo step.
    if ids.iter().all(|id| existing(d, *id).is_some()) {
        return Ok(json!({ "assets": ids.iter().filter_map(|id| existing(d, *id)).collect::<Vec<_>>(), "added": 0 }));
    }
    s.edit("Export 3D Object", |d, _| {
        let (mut out, mut added) = (vec![], 0);
        for node in ids {
            if let Some(id) = existing(d, node) {
                out.push(id);
                continue;
            }
            let name = d.node(node).and_then(|n| n.name.as_deref()).and_then(clean_name).unwrap_or_else(|| d.next_asset_name());
            let id = d.alloc_id().0;
            d.assets.push(ExportAsset { id, name, nodes: vec![node], format_3d: Some(format.clone()) });
            out.push(id);
            added += 1;
        }
        Ok(json!({ "assets": out, "added": added }))
    })
}

fn set_3d_format(s: &mut Session, p: &Value) -> Result<Value> {
    const C: &str = "assets.set3DFormat";
    let d = &s.doc()?.doc;
    let ids = three_d_asset_ids(d, p, "assets", C)?;
    let format = format_3d_param(p, C, None)?;
    let changed = d.assets.iter().filter(|a| ids.contains(&a.id) && a.format_3d.as_deref() != Some(format.as_str())).count();
    // Nothing changes: no undo step.
    if changed == 0 {
        return Ok(json!({ "changed": 0 }));
    }
    s.edit("3D Export Format", |d, _| {
        for a in d.assets.iter_mut().filter(|a| ids.contains(&a.id)) {
            a.format_3d = Some(format.clone());
        }
        Ok(json!({ "changed": changed }))
    })
}

fn export_3d(s: &mut Session, p: &Value) -> Result<Value> {
    const C: &str = "assets.export3D";
    let d = &s.doc()?.doc;
    let ids = if p.get("assets").is_some_and(|v| !v.is_null()) {
        three_d_asset_ids(d, p, "assets", C)?
    } else {
        d.assets.iter().filter(|a| a.format_3d.is_some()).map(|a| a.id).collect()
    };
    if ids.is_empty() {
        return Err(bad(C, "no 3D assets yet: collect an object with a 3D effect first (assets.add3D)"));
    }
    let folder = text_param(p, "folder", C)?.filter(|f| !f.is_empty());
    let zip = switch_or(p, "zip", false, C)?;
    let size = texture_size_param(p, C)?;
    let mut pixels = 0u64;
    // Every file's name, compared without case: a later file that would take one gets -2, -3…
    let mut taken: HashSet<String> = HashSet::new();
    let mut files: Vec<(String, Vec<u8>)> = vec![];
    for id in ids {
        let Some(a) = d.asset(id) else { continue };
        let format = a.format_3d.as_deref().unwrap_or(DEFAULT_FORMAT_3D);
        let nodes =
            a.nodes.iter().map(|node| d.node(*node).ok_or_else(|| bad(C, format!("no such object {0}", node.0)))).collect::<Result<Vec<_>>>()?;
        // The asset's objects share one budget of triangles.
        let what = &a.name;
        let parts =
            vectorcraft_render::effects::three_d_meshes_of_in(Some(d), nodes).map_err(|e| bad(C, format!("{what} can't be exported in 3D: {e}")))?;
        let pictures = textures(d, &parts, size, &mut pixels).map_err(|e| bad(C, format!("{what} can't be exported in 3D: {e}")))?;
        let images = image_maps(&pictures);
        let base = Some(safe_file_name(&a.name)).filter(|b| !b.is_empty()).unwrap_or_else(|| "Asset".into());
        let mut stem = base.clone();
        let mut k = 1;
        loop {
            let names = mesh_file_names(&stem, format, &images).map_err(|e| bad(C, e))?;
            if names.iter().all(|n| !taken.contains(&n.to_lowercase())) {
                break;
            }
            k += 1;
            stem = format!("{base}-{k}");
        }
        let made = mesh_files(&stem, format, &parts, &pictures).map_err(|e| bad(C, e))?;
        taken.extend(made.iter().map(|(n, _)| n.to_lowercase()));
        files.extend(made);
    }
    let names: Vec<&str> = files.iter().map(|(n, _)| n.as_str()).collect();
    if zip {
        let archive = super::fileio::zip::store(&files).map_err(|e| bad(C, e))?;
        // "-3D" keeps it apart from the picture assets' ZIP, which is named after the document.
        let name = Some(safe_file_name(&d.title)).filter(|t| !t.is_empty()).map_or_else(|| "3D Assets.zip".into(), |t| format!("{t}-3D.zip"));
        return match folder {
            Some(dir) => {
                create_dir(dir)?;
                let path = format!("{dir}/{name}");
                write_file(&path, &archive)?;
                Ok(json!({ "path": path, "bytes": archive.len(), "files": names }))
            }
            None => Ok(json!({ "name": name, "dataBase64": vectorcraft_format::base64_encode(&archive), "bytes": archive.len(), "files": names })),
        };
    }
    match folder {
        Some(dir) => {
            create_dir(dir)?;
            let mut written = vec![];
            for (name, bytes) in &files {
                let path = format!("{dir}/{name}");
                write_file(&path, bytes)?;
                written.push(path);
            }
            Ok(json!({ "files": written }))
        }
        None => {
            let files: Vec<Value> =
                files.iter().map(|(name, bytes)| json!({ "name": name, "dataBase64": vectorcraft_format::base64_encode(bytes) })).collect();
            Ok(json!({ "files": files }))
        }
    }
}

/// Param `textureSize` of command `cmd`: the pixels along the longer side of each face's texture,
/// [`TEXTURE_SIZE`] when it is not given.
fn texture_size_param(p: &Value, cmd: &str) -> Result<u32> {
    let key = "textureSize";
    let (lo, hi) = TEXTURE_SIZES;
    match p.get(key) {
        None | Some(Value::Null) => Ok(TEXTURE_SIZE),
        Some(v) => v
            .as_u64()
            .and_then(|n| u32::try_from(n).ok())
            .filter(|n| (lo..=hi).contains(n))
            .ok_or_else(|| bad(cmd, format!("`{key}` must be a whole number of pixels from {lo} to {hi}"))),
    }
}

/// The asset ids of param `key`.
fn asset_ids(p: &Value, key: &str, cmd: &str) -> Result<Vec<u64>> {
    let ids = p.get(key).and_then(Value::as_array).ok_or_else(|| bad(cmd, format!("`{key}` must be an array of asset ids (see assets.list)")))?;
    ids.iter().map(|v| v.as_u64().ok_or_else(|| bad(cmd, format!("{v} is not an asset id")))).collect()
}

fn remove(s: &mut Session, p: &Value) -> Result<Value> {
    const C: &str = "assets.remove";
    let ids = asset_ids(p, "assets", C)?;
    let n = s.doc()?.doc.assets.iter().filter(|a| ids.contains(&a.id)).count();
    if n == 0 {
        return Err(bad(C, "no such assets (see assets.list)"));
    }
    s.edit("Remove Asset", |d, _| {
        d.assets.retain(|a| !ids.contains(&a.id));
        Ok(json!({ "removed": n }))
    })
}

fn rename(s: &mut Session, p: &Value) -> Result<Value> {
    const C: &str = "assets.rename";
    let id = p.get("asset").and_then(Value::as_u64).ok_or_else(|| bad(C, "`asset` is the asset id (see assets.list)"))?;
    let name = str_param(p, "name").and_then(clean_name).ok_or_else(|| bad(C, "give the asset a name"))?;
    let at = s.doc()?.doc.assets.iter().position(|a| a.id == id).ok_or_else(|| bad(C, format!("no asset {id}")))?;
    s.edit("Rename Asset", |d, _| {
        if let Some(a) = d.assets.get_mut(at) {
            a.name = name;
        }
        ok()
    })
}

fn list(s: &mut Session, _: &Value) -> Result<Value> {
    let d = &s.doc()?.doc;
    let rows: Vec<Value> = d
        .assets
        .iter()
        .map(|a| {
            let bounds = export_crop(d, &a.nodes);
            json!({
                "id": a.id,
                "name": a.name,
                "nodes": a.nodes.iter().map(|n| n.0).collect::<Vec<_>>(),
                "bounds": bounds.map(|b| [b.x0, b.y0, b.width(), b.height()]),
                "format3D": a.format_3d,
            })
        })
        .collect();
    Ok(json!({ "assets": rows }))
}

/// The shared export settings of the document (the [`SCREEN_SETTINGS_KEYS`] it has).
fn shared_settings(s: &Session) -> Result<Map<String, Value>> {
    let saved = &s.doc()?.doc.export_settings;
    Ok(SCREEN_SETTINGS_KEYS.iter().filter_map(|k| Some((k.to_string(), saved.get(*k).filter(|v| !v.is_null())?.clone()))).collect())
}

/// `base` with the settings of `p` over it: a given value replaces (null removes) the saved one;
/// formats drop a saved preset and a preset (`""`: none) saved formats.
fn overlay(base: &mut Map<String, Value>, p: &Map<String, Value>) {
    let given = |k: &str| p.get(k).is_some_and(|v| !v.is_null());
    if given("formats") && !p.contains_key("preset") {
        base.remove("preset");
    }
    if given("preset") && !p.contains_key("formats") {
        base.remove("formats");
    }
    for (k, v) in p {
        match v {
            Value::Null => _ = base.remove(k),
            Value::String(id) if k == "preset" && id.is_empty() => _ = base.remove(k),
            v => _ = base.insert(k.clone(), v.clone()),
        }
    }
}

fn export(s: &mut Session, p: &Value) -> Result<Value> {
    const C: &str = "assets.export";
    let mut q = shared_settings(s)?;
    if let Some(o) = p.as_object() {
        overlay(&mut q, o);
    }
    // Create Sub-folders off puts every file in the folder itself, mobile and density rows too.
    if q.get("subfolders").and_then(Value::as_bool) == Some(false) && p.get("folders").is_none() {
        q.insert("folders".into(), json!("none"));
    }
    let d = &s.doc()?.doc;
    // 3D assets are written as mesh files, by assets.export3D: the export refuses one it is given.
    if !q.contains_key("assets") {
        let all: Vec<u64> = d.assets.iter().filter(|a| a.format_3d.is_none()).map(|a| a.id).collect();
        if all.is_empty() {
            return Err(bad(C, "no assets yet: collect art for export first (assets.add)"));
        }
        q.insert("assets".into(), json!(all));
    }
    export_screens(s, &Value::Object(q), p).map_err(|e| of_cmd(C, e))
}

fn settings_set(s: &mut Session, p: &Value) -> Result<Value> {
    const C: &str = "assets.settings.set";
    let o = p.as_object().ok_or_else(|| bad(C, "give the settings to change, e.g. {\"formats\": [{\"format\": \"png\", \"scale\": \"2x\"}]}"))?;
    if let Some(k) = o.keys().find(|k| !SCREEN_SETTINGS_KEYS.contains(&k.as_str()) && !matches!(k.as_str(), "exportPreset" | "openLocation")) {
        return Err(bad(C, format!("`{k}` isn't an export setting: {}, openLocation", SCREEN_SETTINGS_KEYS.join(", "))));
    }
    for k in ["subfolders", "openLocation"] {
        if o.get(k).is_some_and(|v| !v.is_boolean() && !v.is_null()) {
            return Err(bad(C, format!("`{k}` is true or false")));
        }
    }
    let mut settings = s.doc()?.doc.export_settings.clone();
    let mut given = o.clone();
    // Create Sub-folders is on for a document without settings (Export for Screens opens so), and
    // Open Location after Export set on its own leaves it on.
    if settings.is_empty() && o.len() == 1 && o.get("openLocation").is_some_and(Value::is_boolean) {
        given.insert("subfolders".into(), json!(true));
    }
    if let Some(name) = given.remove("exportPreset") {
        let name = name.as_str().ok_or_else(|| bad(C, "`exportPreset` is the name of a preset of document.exportPresets.list"))?;
        with_screen_export_preset(s, C, &mut settings, name)?;
    }
    // Create Sub-folders is Export for Screens' folder per row: the dialog opens on the choice.
    // Turned off on its own (the panel menu), it also ends a remembered folder per format or item;
    // with an export preset, that preset's folder per format or item stays.
    let folders = settings.get("folders").and_then(Value::as_str).map(str::to_string);
    let sorted = folders.as_deref().is_some_and(|f| f != "none");
    match given.get("subfolders").and_then(Value::as_bool) {
        Some(true) => _ = settings.insert("folders".into(), json!("rows")),
        Some(false) if folders.as_deref() == Some("rows") || (sorted && !o.contains_key("exportPreset")) => {
            _ = settings.insert("folders".into(), json!("none"))
        }
        _ => {}
    }
    overlay(&mut settings, &given);
    check_screen_settings(s, &Value::Object(settings.clone())).map_err(|e| of_cmd(C, e))?;
    store_screen_settings(s, settings)?;
    Ok(json!({ "settings": shared_settings(s)? }))
}