mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 03:17:43 +00:00
export-w6: net change of 1 commits
- export-w6: written from its sheet
This commit is contained in:
@@ -11,15 +11,17 @@ pub fn serde_fields<T: Serialize>(value: &T) -> Option<serde_json::Value> {
|
||||
serde_json::to_value(value).ok().filter(Value::is_object)
|
||||
}
|
||||
|
||||
/// Replaces the top-level key `field` of `value` with `json`, by serialising the value, replacing the key and reading
|
||||
/// it back. An unknown key or a value `T` cannot hold gives the refusal's message and leaves `value` unchanged.
|
||||
/// Replaces the key `field` of `value` with `json`, by serialising the value, replacing the key and reading it back.
|
||||
/// `field` is a dotted path (`compression.color.ppi`) through nested objects; a name without a dot is a top-level key.
|
||||
/// An unknown key at any step or a value `T` cannot hold gives the refusal's message and leaves `value` unchanged.
|
||||
pub fn set_serde_field<T: Serialize + DeserializeOwned>(value: &mut T, field: &str, json: serde_json::Value) -> Result<(), String> {
|
||||
let Some(Value::Object(mut object)) = serde_json::to_value(&*value).ok() else {
|
||||
let Some(mut root) = serde_json::to_value(&*value).ok().filter(Value::is_object) else {
|
||||
return Err(FieldRefusal::NoFields.message(field));
|
||||
};
|
||||
let Some(slot) = object.get_mut(field) else { return Err(FieldRefusal::Unknown.message(field)) };
|
||||
let slot = field.split('.').try_fold(&mut root, |at, step| at.as_object_mut().and_then(|object| object.get_mut(step)));
|
||||
let Some(slot) = slot else { return Err(FieldRefusal::Unknown.message(field)) };
|
||||
*slot = json.clone();
|
||||
match serde_json::from_value::<T>(Value::Object(object)) {
|
||||
match serde_json::from_value::<T>(root) {
|
||||
Ok(read) => {
|
||||
*value = read;
|
||||
Ok(())
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
//! `HostedPdfExport`: the Save PDF dialog owned by a `DialogHost`.
|
||||
|
||||
use super::{PdfArtboards, PdfExportMode, PdfExportResult, PdfExportWords, PdfPresetEvent, PresetDraft};
|
||||
use crate::dialog::{DialogId, DialogResult, DialogStack, StackDialog};
|
||||
use super::sections::enabled;
|
||||
use super::{
|
||||
ArtboardChoice, PasswordSwitches, PdfArtboards, PdfExportDialog, PdfExportMode, PdfExportResult, PdfExportWords, PdfPresetEvent, PdfSection,
|
||||
PresetDraft, apply_standard, check_range, summary_text, warning_lines,
|
||||
};
|
||||
use crate::contract::Bind;
|
||||
use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldAnswer, HeldOk, StackDialog, dialog_id, serde_fields, set_serde_field};
|
||||
use crate::patterns::presets::PresetError;
|
||||
use crate::runtime::env::{Label, UnitKind};
|
||||
use craft_pdf::settings::{ColorMode, PdfPreset, PdfSettings, ProfileCatalog};
|
||||
use crate::runtime::overlay::CloseReason;
|
||||
use craft_pdf::settings::{ColorMode, PdfPreset, PdfSettings, ProfileCatalog, Standard, builtin_presets, default_preset_name};
|
||||
use egui::{Context, Ui};
|
||||
use serde_json::Value;
|
||||
use std::sync::Arc;
|
||||
@@ -19,170 +25,298 @@ pub struct PdfExportAnswer {
|
||||
pub draft: Option<PresetDraft>,
|
||||
}
|
||||
|
||||
/// The ids automation names the sections by, in the order of [`PdfSection::ALL`].
|
||||
const SECTION_IDS: [&str; PdfSection::ALL.len()] = ["general", "compression", "marks", "output", "advanced", "security", "summary"];
|
||||
|
||||
/// A check a typed preset name goes through.
|
||||
type NameChecker = Box<dyn Fn(&str) -> Option<PresetError> + Send>;
|
||||
|
||||
/// A [`PdfExportDialog`](super::PdfExportDialog) that the `DialogHost` owns, under the key `"pdf-export"`. OK is held
|
||||
/// until the app has written the file ([`answer_ok`](HostedPdfExport::answer_ok)).
|
||||
pub struct HostedPdfExport {
|
||||
id: DialogId,
|
||||
app: String,
|
||||
settings: PdfSettings,
|
||||
/// The preset the dialog opens on; the app's default preset without one.
|
||||
opening_preset: Option<String>,
|
||||
/// The preset the settings started from or were last filled from.
|
||||
chosen: Option<String>,
|
||||
saved_presets: Vec<PdfPreset>,
|
||||
flattener_presets: Vec<String>,
|
||||
profiles: Vec<String>,
|
||||
catalog: Option<Arc<dyn ProfileCatalog + Send + Sync>>,
|
||||
color_mode: ColorMode,
|
||||
length_unit: UnitKind,
|
||||
document_bleed: [f64; 4],
|
||||
/// The artboards chosen when the dialog opens.
|
||||
opening: PdfArtboards,
|
||||
choice: ArtboardChoice,
|
||||
artboard_count: Option<usize>,
|
||||
mode: PdfExportMode,
|
||||
draft: PresetDraft,
|
||||
check_name: Option<NameChecker>,
|
||||
section: PdfSection,
|
||||
switches: PasswordSwitches,
|
||||
custom_flattener: bool,
|
||||
host_warnings: Vec<String>,
|
||||
/// What the dialog's passes reported and the app has not taken yet.
|
||||
events: Vec<PdfPresetEvent>,
|
||||
custom_pressed: bool,
|
||||
save_summary: Option<String>,
|
||||
held: HeldAnswer<PdfExportAnswer>,
|
||||
}
|
||||
|
||||
/// The artboard choice a dialog that opens with `artboards` starts from.
|
||||
fn choice_of(artboards: &PdfArtboards) -> ArtboardChoice {
|
||||
match artboards {
|
||||
PdfArtboards::Range(range) => ArtboardChoice { range_only: true, range: range.clone() },
|
||||
_ => ArtboardChoice::default(),
|
||||
}
|
||||
}
|
||||
|
||||
impl HostedPdfExport {
|
||||
/// The dialog for the application called `app`, editing `settings`.
|
||||
pub fn new(app: impl Into<String>, settings: PdfSettings) -> Self {
|
||||
let _ = (app, settings);
|
||||
todo!("ex.P17")
|
||||
let app = app.into();
|
||||
HostedPdfExport {
|
||||
id: dialog_id(KEY),
|
||||
chosen: Some(default_preset_name(&app)),
|
||||
app,
|
||||
settings,
|
||||
opening_preset: None,
|
||||
saved_presets: Vec::new(),
|
||||
flattener_presets: Vec::new(),
|
||||
profiles: Vec::new(),
|
||||
catalog: None,
|
||||
color_mode: ColorMode::Rgb,
|
||||
length_unit: UnitKind::Points,
|
||||
document_bleed: [0.0; 4],
|
||||
opening: PdfArtboards::All,
|
||||
choice: ArtboardChoice::default(),
|
||||
artboard_count: None,
|
||||
mode: PdfExportMode::default(),
|
||||
draft: PresetDraft::default(),
|
||||
check_name: None,
|
||||
section: PdfSection::default(),
|
||||
switches: PasswordSwitches::default(),
|
||||
custom_flattener: false,
|
||||
host_warnings: Vec::new(),
|
||||
events: Vec::new(),
|
||||
custom_pressed: false,
|
||||
save_summary: None,
|
||||
held: HeldAnswer::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The preset the settings start from, by name.
|
||||
#[must_use]
|
||||
pub fn preset(self, name: impl Into<String>) -> Self {
|
||||
let _ = (self, name);
|
||||
todo!("ex.P17")
|
||||
pub fn preset(mut self, name: impl Into<String>) -> Self {
|
||||
let name = name.into();
|
||||
self.chosen = Some(name.clone());
|
||||
self.opening_preset = Some(name);
|
||||
self
|
||||
}
|
||||
|
||||
/// The presets the user saved, after the built-in ones in the preset list.
|
||||
#[must_use]
|
||||
pub fn saved_presets(self, presets: Vec<PdfPreset>) -> Self {
|
||||
let _ = (self, presets);
|
||||
todo!("ex.P17")
|
||||
pub fn saved_presets(mut self, presets: Vec<PdfPreset>) -> Self {
|
||||
self.saved_presets = presets;
|
||||
self
|
||||
}
|
||||
|
||||
/// The names of the saved flattener presets.
|
||||
#[must_use]
|
||||
pub fn flattener_presets(self, names: Vec<String>) -> Self {
|
||||
let _ = (self, names);
|
||||
todo!("ex.P17")
|
||||
pub fn flattener_presets(mut self, names: Vec<String>) -> Self {
|
||||
self.flattener_presets = names;
|
||||
self
|
||||
}
|
||||
|
||||
/// The colour profiles the app knows.
|
||||
#[must_use]
|
||||
pub fn profiles(self, names: Vec<String>) -> Self {
|
||||
let _ = (self, names);
|
||||
todo!("ex.P17")
|
||||
pub fn profiles(mut self, names: Vec<String>) -> Self {
|
||||
self.profiles = names;
|
||||
self
|
||||
}
|
||||
|
||||
/// The lookup the checks use for the profiles; it stands for the dialog's `Option<&dyn ProfileCatalog>`.
|
||||
#[must_use]
|
||||
pub fn catalog(self, catalog: Arc<dyn ProfileCatalog + Send + Sync>) -> Self {
|
||||
let _ = (self, catalog);
|
||||
todo!("ex.P17")
|
||||
pub fn catalog(mut self, catalog: Arc<dyn ProfileCatalog + Send + Sync>) -> Self {
|
||||
self.catalog = Some(catalog);
|
||||
self
|
||||
}
|
||||
|
||||
/// The colour mode of the document.
|
||||
#[must_use]
|
||||
pub fn color_mode(self, mode: ColorMode) -> Self {
|
||||
let _ = (self, mode);
|
||||
todo!("ex.P17")
|
||||
pub fn color_mode(mut self, mode: ColorMode) -> Self {
|
||||
self.color_mode = mode;
|
||||
self
|
||||
}
|
||||
|
||||
/// The unit lengths are shown in.
|
||||
#[must_use]
|
||||
pub fn length_unit(self, unit: UnitKind) -> Self {
|
||||
let _ = (self, unit);
|
||||
todo!("ex.P17")
|
||||
pub fn length_unit(mut self, unit: UnitKind) -> Self {
|
||||
self.length_unit = unit;
|
||||
self
|
||||
}
|
||||
|
||||
/// The document's bleed in points (top, bottom, left, right).
|
||||
#[must_use]
|
||||
pub fn document_bleed(self, bleed: [f64; 4]) -> Self {
|
||||
let _ = (self, bleed);
|
||||
todo!("ex.P17")
|
||||
pub fn document_bleed(mut self, bleed: [f64; 4]) -> Self {
|
||||
self.document_bleed = bleed;
|
||||
self
|
||||
}
|
||||
|
||||
/// The artboards chosen when the dialog opens.
|
||||
#[must_use]
|
||||
pub fn artboards(self, artboards: PdfArtboards) -> Self {
|
||||
let _ = (self, artboards);
|
||||
todo!("ex.P17")
|
||||
pub fn artboards(mut self, artboards: PdfArtboards) -> Self {
|
||||
self.choice = choice_of(&artboards);
|
||||
self.opening = artboards;
|
||||
self
|
||||
}
|
||||
|
||||
/// The number of artboards of the document.
|
||||
#[must_use]
|
||||
pub fn artboard_count(self, count: usize) -> Self {
|
||||
let _ = (self, count);
|
||||
todo!("ex.P17")
|
||||
pub fn artboard_count(mut self, count: usize) -> Self {
|
||||
self.artboard_count = Some(count);
|
||||
self
|
||||
}
|
||||
|
||||
/// What the dialog is for.
|
||||
#[must_use]
|
||||
pub fn mode(self, mode: PdfExportMode) -> Self {
|
||||
let _ = (self, mode);
|
||||
todo!("ex.P17")
|
||||
pub fn mode(mut self, mode: PdfExportMode) -> Self {
|
||||
self.mode = mode;
|
||||
self
|
||||
}
|
||||
|
||||
/// The preset editor's name and description it opens with.
|
||||
#[must_use]
|
||||
pub fn draft(self, draft: PresetDraft) -> Self {
|
||||
let _ = (self, draft);
|
||||
todo!("ex.P17")
|
||||
pub fn draft(mut self, draft: PresetDraft) -> Self {
|
||||
self.draft = draft;
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether a typed preset name may be used, in the preset editor.
|
||||
#[must_use]
|
||||
pub fn check_name(self, check: impl Fn(&str) -> Option<PresetError> + Send + 'static) -> Self {
|
||||
let _ = (self, check);
|
||||
todo!("ex.P17")
|
||||
pub fn check_name(mut self, check: impl Fn(&str) -> Option<PresetError> + Send + 'static) -> Self {
|
||||
self.check_name = Some(Box::new(check));
|
||||
self
|
||||
}
|
||||
|
||||
/// The settings the dialog shows now.
|
||||
pub fn settings(&self) -> &PdfSettings {
|
||||
let _ = self;
|
||||
todo!("ex.P18")
|
||||
&self.settings
|
||||
}
|
||||
|
||||
/// Replaces the saved presets of the preset list.
|
||||
pub fn set_saved_presets(&mut self, presets: Vec<PdfPreset>) {
|
||||
let _ = (self, presets);
|
||||
todo!("ex.P18")
|
||||
self.saved_presets = presets;
|
||||
}
|
||||
|
||||
/// Replaces the names of the saved flattener presets.
|
||||
pub fn set_flattener_presets(&mut self, names: Vec<String>) {
|
||||
let _ = (self, names);
|
||||
todo!("ex.P18")
|
||||
self.flattener_presets = names;
|
||||
}
|
||||
|
||||
/// Whether the flattener select shows `[Custom]`.
|
||||
pub fn set_custom_flattener(&mut self, on: bool) {
|
||||
let _ = (self, on);
|
||||
todo!("ex.P18")
|
||||
self.custom_flattener = on;
|
||||
}
|
||||
|
||||
/// The warnings of the host, shown after the settings' own.
|
||||
pub fn set_host_warnings(&mut self, warnings: Vec<String>) {
|
||||
let _ = (self, warnings);
|
||||
todo!("ex.P18")
|
||||
self.host_warnings = warnings;
|
||||
}
|
||||
|
||||
/// What the user did to the app's presets in the last pass, once.
|
||||
pub fn take_preset_events(&mut self) -> Vec<PdfPresetEvent> {
|
||||
let _ = self;
|
||||
todo!("ex.P18")
|
||||
std::mem::take(&mut self.events)
|
||||
}
|
||||
|
||||
/// Whether Custom… was pressed, once.
|
||||
pub fn take_custom_flattener(&mut self) -> bool {
|
||||
let _ = self;
|
||||
todo!("ex.P18")
|
||||
std::mem::take(&mut self.custom_pressed)
|
||||
}
|
||||
|
||||
/// The summary text Save Summary… asked to save, once.
|
||||
pub fn take_save_summary(&mut self) -> Option<String> {
|
||||
let _ = self;
|
||||
todo!("ex.P18")
|
||||
self.save_summary.take()
|
||||
}
|
||||
|
||||
/// The Summary's text for the dialog's settings, as Save Summary… saves it, worded by `words`.
|
||||
pub fn summary_text(&self, words: &PdfExportWords) -> String {
|
||||
let _ = (self, words);
|
||||
todo!("ex.P18")
|
||||
// PLAN: assumed name and signature of type-w6's extracted warning lines (`show` and this call share them)
|
||||
let mut warnings = warning_lines(words, &self.settings, Self::catalog_of(&self.catalog));
|
||||
warnings.extend(self.host_warnings.iter().cloned());
|
||||
summary_text(words, &self.settings, &self.base(), &warnings)
|
||||
}
|
||||
|
||||
/// The app's answer to the held OK: `Ok` closes the dialog with the held answer; `Err` shows its label and keeps
|
||||
/// the dialog open.
|
||||
pub fn answer_ok(&mut self, answer: Result<(), Label>) {
|
||||
let _ = (self, answer);
|
||||
todo!("ex.P18")
|
||||
self.held.answer(answer);
|
||||
}
|
||||
|
||||
/// The profile lookup of the checks; every profile name is unknown without one, as in the dialog.
|
||||
fn catalog_of(catalog: &Option<Arc<dyn ProfileCatalog + Send + Sync>>) -> &dyn ProfileCatalog {
|
||||
match catalog {
|
||||
Some(catalog) => catalog.as_ref(),
|
||||
None => &(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The built-in presets, then the saved ones.
|
||||
fn presets(&self) -> Vec<PdfPreset> {
|
||||
let mut presets = builtin_presets(&self.app);
|
||||
presets.extend(self.saved_presets.iter().cloned());
|
||||
presets
|
||||
}
|
||||
|
||||
/// The settings the Summary compares with: the chosen preset's (the default preset's in the editor).
|
||||
fn base(&self) -> PdfSettings {
|
||||
let name = match self.mode {
|
||||
PdfExportMode::PresetEditor => Some(default_preset_name(&self.app)),
|
||||
_ => self.chosen.clone(),
|
||||
};
|
||||
self.presets().into_iter().find(|preset| Some(&preset.name) == name.as_ref()).map(|preset| preset.settings).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The artboards the answer names.
|
||||
fn chosen_artboards(&self) -> PdfArtboards {
|
||||
let range = self.choice.range.trim();
|
||||
match &self.opening {
|
||||
PdfArtboards::ArtBounds => PdfArtboards::ArtBounds,
|
||||
_ if self.choice.range_only && !range.is_empty() => PdfArtboards::Range(range.to_owned()),
|
||||
_ => PdfArtboards::All,
|
||||
}
|
||||
}
|
||||
|
||||
/// The answer OK files.
|
||||
fn answer(&self, options: PdfSettings, artboards: PdfArtboards) -> PdfExportAnswer {
|
||||
PdfExportAnswer {
|
||||
result: PdfExportResult { options, artboards },
|
||||
draft: (self.mode == PdfExportMode::PresetEditor).then(|| self.draft.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a failing check or a bad artboard range keeps OK off, as the dialog's own footer has it.
|
||||
fn blocked(&self) -> bool {
|
||||
let catalog = Self::catalog_of(&self.catalog);
|
||||
let range = self.choice.range.trim();
|
||||
let range_bad =
|
||||
self.choice.range_only && !range.is_empty() && self.artboard_count.is_some_and(|count| check_range(range, count).is_err());
|
||||
range_bad || self.settings.check(catalog).and_then(|()| self.settings.check_destination(catalog, self.color_mode)).is_err()
|
||||
}
|
||||
|
||||
/// Whether the four bleed sides are equal, which is what the link between them means.
|
||||
fn bleed_linked(&self) -> bool {
|
||||
let [top, rest @ ..] = self.settings.bleed.values();
|
||||
rest.iter().all(|side| *side == top)
|
||||
}
|
||||
}
|
||||
|
||||
/// The section `id` names.
|
||||
fn section_of(id: &str) -> Option<PdfSection> {
|
||||
PdfSection::ALL.iter().zip(SECTION_IDS).find(|(_, name)| *name == id).map(|(section, _)| *section)
|
||||
}
|
||||
|
||||
impl StackDialog for HostedPdfExport {
|
||||
@@ -193,24 +327,160 @@ impl StackDialog for HostedPdfExport {
|
||||
}
|
||||
|
||||
fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option<DialogResult<PdfExportAnswer>> {
|
||||
let _ = (self.id, ui);
|
||||
todo!("ex.P19")
|
||||
let finish = self.held.is_accepted();
|
||||
let refusal = self.held.refusal().map(|(reason, refused)| (reason.clone(), refused.result.options.clone()));
|
||||
let catalog = Self::catalog_of(&self.catalog);
|
||||
let mut dialog = PdfExportDialog::new(self.app.clone(), Bind::bound(&mut self.settings))
|
||||
.saved_presets(&self.saved_presets)
|
||||
.flattener_presets(&self.flattener_presets)
|
||||
.profiles(&self.profiles)
|
||||
.catalog(catalog)
|
||||
.color_mode(self.color_mode)
|
||||
.length_unit(self.length_unit)
|
||||
.document_bleed(self.document_bleed)
|
||||
.artboards(self.opening.clone())
|
||||
.mode(self.mode)
|
||||
.preset_draft(Bind::bound(&mut self.draft))
|
||||
.section(Bind::bound(&mut self.section))
|
||||
.chosen_preset(Bind::bound(&mut self.chosen))
|
||||
.artboard_choice(Bind::bound(&mut self.choice))
|
||||
.password_switches(Bind::bound(&mut self.switches))
|
||||
.custom_flattener(self.custom_flattener)
|
||||
.host_warnings(&self.host_warnings)
|
||||
.hold_ok(true)
|
||||
.finish_held(finish)
|
||||
// OK waits for the app's answer: a second OK cannot file over the held one.
|
||||
.ok_enabled(!self.held.is_waiting());
|
||||
if let Some(name) = &self.opening_preset {
|
||||
dialog = dialog.preset(name.clone());
|
||||
}
|
||||
if let Some(count) = self.artboard_count {
|
||||
dialog = dialog.artboard_count(count);
|
||||
}
|
||||
if let Some(check) = &self.check_name {
|
||||
dialog = dialog.check_name(|typed: &str| check(typed));
|
||||
}
|
||||
if let Some(refusal) = refusal {
|
||||
dialog = dialog.refusal(refusal);
|
||||
}
|
||||
let shown = dialog.show(ui);
|
||||
// A pass reports a press only in the pass that took the input, so a second pass of the frame adds none.
|
||||
self.events.extend(shown.presets);
|
||||
self.custom_pressed |= shown.custom_flattener;
|
||||
if shown.save_summary.is_some() {
|
||||
self.save_summary = shown.save_summary;
|
||||
}
|
||||
if self.held.refusal().is_some_and(|(_, refused)| refused.result.options != self.settings) {
|
||||
self.held.clear_refusal();
|
||||
}
|
||||
if let Some(result) = shown.held {
|
||||
let answer = self.answer(result.options, result.artboards);
|
||||
self.held.file(answer);
|
||||
}
|
||||
match (shown.result, shown.closed) {
|
||||
(Some(result), _) => Some(DialogResult::Ok(self.answer(result.options, result.artboards))),
|
||||
(None, Some(CloseReason::Activated)) if finish => Some(self.held.finish().map_or(DialogResult::Cancel, DialogResult::Ok)),
|
||||
(None, Some(_)) => Some(DialogResult::Cancel),
|
||||
(None, None) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn key(&self) -> Option<&str> {
|
||||
Some(KEY)
|
||||
}
|
||||
|
||||
fn confirm(&mut self, _: &Context) -> Option<PdfExportAnswer> {
|
||||
if self.held.is_accepted() {
|
||||
return self.held.finish();
|
||||
}
|
||||
if self.held.is_waiting() || self.blocked() {
|
||||
return None;
|
||||
}
|
||||
let answer = self.answer(self.settings.clone(), self.chosen_artboards());
|
||||
self.held.file(answer);
|
||||
None
|
||||
}
|
||||
|
||||
fn held_state(&self) -> Option<HeldOk> {
|
||||
self.held.state()
|
||||
}
|
||||
|
||||
/// The settings, `section`, `preset`, `artboards`, `range`, `bleedLinked`, and `name` and `description` in the
|
||||
/// editor.
|
||||
fn json_fields(&self) -> Option<Value> {
|
||||
todo!("ex.P19")
|
||||
let mut fields = serde_fields(&self.settings)?;
|
||||
let map = fields.as_object_mut()?;
|
||||
let artboards = match (&self.opening, self.choice.range_only) {
|
||||
(PdfArtboards::ArtBounds, _) => "artBounds",
|
||||
(_, true) => "range",
|
||||
_ => "all",
|
||||
};
|
||||
let section = PdfSection::ALL.iter().zip(SECTION_IDS).find(|(section, _)| **section == self.section).map(|(_, id)| id);
|
||||
map.insert("section".to_owned(), section.into());
|
||||
map.insert("preset".to_owned(), self.chosen.clone().into());
|
||||
map.insert("artboards".to_owned(), artboards.into());
|
||||
map.insert("range".to_owned(), self.choice.range.clone().into());
|
||||
map.insert("bleedLinked".to_owned(), self.bleed_linked().into());
|
||||
if self.mode == PdfExportMode::PresetEditor {
|
||||
map.insert("name".to_owned(), self.draft.name.clone().into());
|
||||
map.insert("description".to_owned(), self.draft.description.clone().into());
|
||||
}
|
||||
Some(fields)
|
||||
}
|
||||
|
||||
/// Sets one of the fields `json_fields` lists, else a settings field; a write to a disabled setting is refused.
|
||||
fn set_json_field(&mut self, ctx: &Context, field: &str, value: Value) -> Result<(), String> {
|
||||
let _ = (ctx, field, value);
|
||||
todo!("ex.P19")
|
||||
fn set_json_field(&mut self, _: &Context, field: &str, value: Value) -> Result<(), String> {
|
||||
let rejects = || FieldRefusal::Rejects(&value).message(field);
|
||||
match field {
|
||||
"section" => self.section = value.as_str().and_then(section_of).ok_or_else(rejects)?,
|
||||
"preset" => {
|
||||
let name = value.as_str().ok_or_else(rejects)?;
|
||||
let preset = self.presets().into_iter().find(|preset| preset.name == name).ok_or_else(rejects)?;
|
||||
self.settings = preset.settings;
|
||||
self.switches = PasswordSwitches::default();
|
||||
self.chosen = Some(preset.name);
|
||||
}
|
||||
"artboards" => match value.as_str() {
|
||||
Some("all") => self.choice.range_only = false,
|
||||
Some("range") => self.choice.range_only = true,
|
||||
Some("artBounds") if self.opening == PdfArtboards::ArtBounds => {}
|
||||
_ => return Err(rejects()),
|
||||
},
|
||||
"range" => self.choice.range = value.as_str().ok_or_else(rejects)?.to_owned(),
|
||||
"bleedLinked" => {
|
||||
let linked = value.as_bool().ok_or_else(rejects)?;
|
||||
if !enabled(&self.settings, "bleed.top") {
|
||||
return Err(FieldRefusal::Inactive.message(field));
|
||||
}
|
||||
if linked {
|
||||
let bleed = &mut self.settings.bleed;
|
||||
(bleed.bottom, bleed.left, bleed.right) = (bleed.top, bleed.top, bleed.top);
|
||||
}
|
||||
}
|
||||
"name" | "description" if self.mode == PdfExportMode::PresetEditor => {
|
||||
let text = value.as_str().ok_or_else(rejects)?.to_owned();
|
||||
if field == "name" {
|
||||
self.draft.name = text;
|
||||
} else {
|
||||
self.draft.description = text;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if !enabled(&self.settings, field) {
|
||||
return Err(FieldRefusal::Inactive.message(field));
|
||||
}
|
||||
let mut next = self.settings.clone();
|
||||
set_serde_field(&mut next, field, value.clone())?;
|
||||
if field == "standard" {
|
||||
apply_standard(&mut next);
|
||||
if next.standard != Standard::None {
|
||||
self.switches = PasswordSwitches::default();
|
||||
}
|
||||
}
|
||||
self.settings = next;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
//! The hosted Save PDF dialog on a `DialogHost`: the fields automation lists and sets, Alt-Reset, and the Summary text.
|
||||
|
||||
use craft_pdf::settings::{Choice, Compatibility, PdfPreset, PdfSettings, Standard};
|
||||
use craft_ui::dialog::{DialogHost, DialogId};
|
||||
use craft_ui::patterns::form_dialogs::{HostedPdfExport, PdfExportWords, summary_text};
|
||||
use craft_ui::runtime::env::Env;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Context, Modifiers, Vec2};
|
||||
use serde_json::{Value, json};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A saved preset that differs from the defaults in its version and its trim marks.
|
||||
fn mine() -> PdfPreset {
|
||||
let mut settings = PdfSettings::default();
|
||||
settings.compatibility = Compatibility::Pdf15;
|
||||
settings.marks.trim = true;
|
||||
PdfPreset { name: "Mine".to_owned(), description: String::new(), settings }
|
||||
}
|
||||
|
||||
/// A dialog that opens on `Mine` with its settings, over four artboards.
|
||||
fn dialog() -> HostedPdfExport {
|
||||
HostedPdfExport::new("VectorCraft", mine().settings).saved_presets(vec![mine()]).preset("Mine").artboard_count(4)
|
||||
}
|
||||
|
||||
/// The listed value of `field` of the open dialog.
|
||||
fn listed(host: &DialogHost, id: DialogId, field: &str) -> Value {
|
||||
let fields = host.open_dialogs().into_iter().find(|open| open.id == id).and_then(|open| open.fields).unwrap_or(Value::Null);
|
||||
fields.pointer(&format!("/{}", field.replace('.', "/"))).cloned().unwrap_or(Value::Null)
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn the_hosted_dialog_takes_the_automation_fields() {
|
||||
let ctx = Context::default();
|
||||
let mut host = DialogHost::new();
|
||||
let id = host.open(dialog());
|
||||
for name in ["section", "preset", "artboards", "range", "bleedLinked", "marks", "compression"] {
|
||||
assert_ne!(listed(&host, id, name), Value::Null, "{name} is listed");
|
||||
}
|
||||
let set = |host: &mut DialogHost, field: &str, value: Value| host.set_json_field(&ctx, id, field, value);
|
||||
|
||||
// The preset applies a listed name and refuses an unknown one.
|
||||
assert!(set(&mut host, "preset", json!("No such preset")).is_err());
|
||||
assert_eq!(listed(&host, id, "preset"), json!("Mine"));
|
||||
assert_eq!(set(&mut host, "marks.trim", json!(false)), Ok(()));
|
||||
assert_eq!(listed(&host, id, "marks.trim"), json!(false));
|
||||
assert_eq!(set(&mut host, "preset", json!("Mine")), Ok(()));
|
||||
assert_eq!(listed(&host, id, "marks.trim"), json!(true), "the preset's settings are back");
|
||||
|
||||
// A nested write reaches the setting; an unknown step is refused.
|
||||
assert_eq!(set(&mut host, "marks.weight", json!(2.5)), Ok(()));
|
||||
assert_eq!(listed(&host, id, "marks.weight"), json!(2.5));
|
||||
assert!(set(&mut host, "marks.noSuchSetting", json!(1)).is_err());
|
||||
|
||||
// The bleed link is read from the values: a non-bool is refused and `true` evens the four sides.
|
||||
assert_eq!(set(&mut host, "bleed.useDocument", json!(false)), Ok(()));
|
||||
assert_eq!(set(&mut host, "bleed.top", json!(3.0)), Ok(()));
|
||||
assert_eq!(listed(&host, id, "bleedLinked"), json!(false));
|
||||
assert!(set(&mut host, "bleedLinked", json!("yes")).is_err());
|
||||
assert_eq!(set(&mut host, "bleedLinked", json!(true)), Ok(()));
|
||||
assert_eq!(listed(&host, id, "bleed.right"), json!(3.0));
|
||||
assert_eq!(listed(&host, id, "bleedLinked"), json!(true));
|
||||
|
||||
// The artboards and the range.
|
||||
assert!(set(&mut host, "artboards", json!("everything")).is_err());
|
||||
assert_eq!(set(&mut host, "artboards", json!("range")), Ok(()));
|
||||
assert_eq!(set(&mut host, "range", json!("1-2")), Ok(()));
|
||||
assert_eq!(listed(&host, id, "artboards"), json!("range"));
|
||||
assert_eq!(listed(&host, id, "range"), json!("1-2"));
|
||||
assert_eq!(set(&mut host, "section", json!("security")), Ok(()));
|
||||
assert_eq!(listed(&host, id, "section"), json!("security"));
|
||||
|
||||
// A write to a setting a standard disables is inactive and changes nothing; the standard moves the version.
|
||||
assert_eq!(set(&mut host, "standard", json!("pdfX3")), Ok(()));
|
||||
assert_eq!(listed(&host, id, "compatibility"), json!(Standard::PdfX3.version().id()));
|
||||
for field in ["longPagesInPoints", "output.profiles"] {
|
||||
let before = listed(&host, id, field);
|
||||
let refused = set(&mut host, field, json!(false)).expect_err(field);
|
||||
assert!(refused.contains("takes no input"), "{field}: {refused}");
|
||||
assert_eq!(listed(&host, id, field), before, "{field} is unchanged");
|
||||
}
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn reset_restores_the_chosen_preset_and_keeps_the_artboards() {
|
||||
let host = Rc::new(RefCell::new(DialogHost::new()));
|
||||
let id = host.borrow_mut().open(dialog());
|
||||
let shown = Rc::clone(&host);
|
||||
let mut fixture = Fixture::new(move |ui| shown.borrow_mut().run(ui));
|
||||
fixture.room(Vec2::new(1400.0, 1000.0));
|
||||
fixture.idle(10);
|
||||
let edit = |host: &Rc<RefCell<DialogHost>>, ctx: &Context| {
|
||||
let mut host = host.borrow_mut();
|
||||
for (field, value) in [("marks.trim", json!(false)), ("range", json!("2")), ("artboards", json!("range")), ("section", json!("security"))] {
|
||||
assert_eq!(host.set_json_field(ctx, id, field, value), Ok(()), "{field}");
|
||||
}
|
||||
};
|
||||
edit(&host, fixture.ctx());
|
||||
fixture.idle(4);
|
||||
assert!(!host.borrow_mut().get_mut::<HostedPdfExport>().expect("open").settings().marks.trim);
|
||||
|
||||
fixture.hold(Modifiers::ALT).idle(2);
|
||||
let reset = fixture.by_label("Reset").expect("Alt turns Cancel into Reset");
|
||||
fixture.click(&reset).idle(4);
|
||||
{
|
||||
let mut host = host.borrow_mut();
|
||||
let dialog = host.get_mut::<HostedPdfExport>().expect("Reset keeps the dialog open");
|
||||
assert_eq!(dialog.settings(), &mine().settings, "the chosen preset's settings are back");
|
||||
}
|
||||
assert_eq!(listed(&host.borrow(), id, "artboards"), json!("range"));
|
||||
assert_eq!(listed(&host.borrow(), id, "range"), json!("2"));
|
||||
assert_eq!(listed(&host.borrow(), id, "section"), json!("security"));
|
||||
|
||||
// With the chosen preset gone from the list, Reset leaves the settings as they are.
|
||||
host.borrow_mut().get_mut::<HostedPdfExport>().expect("open").set_saved_presets(Vec::new());
|
||||
edit(&host, fixture.ctx());
|
||||
fixture.idle(4);
|
||||
let reset = fixture.by_label("Reset").expect("Reset while Alt is held");
|
||||
fixture.click(&reset).idle(4);
|
||||
let mut host = host.borrow_mut();
|
||||
assert!(!host.get_mut::<HostedPdfExport>().expect("open").settings().marks.trim, "an unlisted preset restores nothing");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn summary_text_writes_lf_whatever_the_values_hold() {
|
||||
let words = Rc::new(RefCell::new(None));
|
||||
let slot = Rc::clone(&words);
|
||||
let mut fixture = Fixture::new(move |ui| *slot.borrow_mut() = Some(Env::of(ui).words::<PdfExportWords>()));
|
||||
fixture.idle(1);
|
||||
let words = words.borrow().clone().expect("a pass ran");
|
||||
let mut settings = PdfSettings::default();
|
||||
settings.output.output_condition = "first\r\nsecond\rthird".to_owned();
|
||||
let text = summary_text(&words, &settings, &PdfSettings::default(), &[]);
|
||||
assert!(!text.contains('\r'), "no CR in {text:?}");
|
||||
assert!(text.ends_with('\n') && !text.ends_with("\n\n"), "one final LF in {text:?}");
|
||||
assert!(text.contains("first\nsecond\nthird"), "{text:?}");
|
||||
}
|
||||
Reference in New Issue
Block a user