From c9f664acee5605136f733add66fe210fda2adcb0 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sat, 21 Nov 2026 23:30:37 +0700 Subject: [PATCH] color-w23: net change of 1 commits - color-w23: written by an agent --- crates/ui-egui/src/dialogs/proof_setup.rs | 1071 +++++++++------------ 1 file changed, 475 insertions(+), 596 deletions(-) diff --git a/crates/ui-egui/src/dialogs/proof_setup.rs b/crates/ui-egui/src/dialogs/proof_setup.rs index 44350ac659..20562a27e6 100644 --- a/crates/ui-egui/src/dialogs/proof_setup.rs +++ b/crates/ui-egui/src/dialogs/proof_setup.rs @@ -9,81 +9,89 @@ //! Customize… ([`saved_items`]): Save… makes the folder when it may be written, and Load…'s picker //! starts in its own default folder until the folder exists. //! +//! The dialog is a held [`HostedParamDialog`]: [`pump`] runs once per frame after the shell pass, +//! previews the dialog's values, puts the view back when the dialog stops previewing or is covered, +//! answers the Save…/Load… buttons and keeps the saved-setups listing. +//! //! Fields: `device` (`workingCmyk`, `cmyk:` or `rgb:`), `preserveNumbers`, //! `intent`, `simulatePaper`, `simulateBlack` and `preview`. -use std::sync::mpsc::{Receiver, TryRecvError}; +use std::path::{Path, PathBuf}; use std::sync::{Mutex, MutexGuard}; +use craft::host::queue::{JobQueue, Priority, QueueOptions}; +use craft::host::{PickRequest, Picked, Wake}; +use craft::ui::contract::Bind; +use craft::ui::dialog::DialogId; +use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamKind, ParamResult}; +use craft::ui::sections::{DialogRole, FooterButton}; use serde_json::{Value, json}; use vectorcraft_color::cms::{self, Intent, ProfileKind, ProofTarget}; use vectorcraft_engine::cmd::colormgmt::proof_file::{self, EXT}; use vectorcraft_render::proof; -use super::{DialogSpec, form}; +use super::host::{HeldOptions, open_held_with}; +use crate::VectorcraftApp; +use crate::file_asks::{Owner, ask}; use crate::menus::Item; -use crate::state::Dialog; -use crate::widgets; -use crate::{FilePick, VectorcraftApp}; /// The dialog kind of Proof Setup › Customize. pub const KIND: &str = "proofSetup"; const CMD: &str = "view.proofSetup"; -/// The proof view as the dialog found it, which Cancel puts back after a preview. -const BEFORE: &str = "__before"; -/// The settings the canvas shows while Preview is on, as last sent. -const PREVIEWED: &str = "__previewed"; - /// The device a proof target that is not a device (an RGB display or a color blindness type) /// starts the dialog on. const WORKING: &str = "workingCmyk"; -const LABEL_W: f32 = 140.0; +/// The footer button ids. +const SAVE: &str = "save"; +const LOAD_BUTTON: &str = "load"; -pub(super) const SPEC: DialogSpec = DialogSpec { heading: |_| tl!("Proof Setup").into(), body, confirm, min_width: 420.0, ..DialogSpec::FORM }; +/// The open Proof Setup dialog and what its preview did to the proof view (`UiState::proof_setup`). +#[derive(Clone, Debug)] +pub(crate) struct Open { + /// The dialog on the window's stack. + id: DialogId, + /// The proof view the dialog found, which Cancel puts back after a preview. + before: proof::ProofView, + /// The proof view the preview last put on the canvas; `None` while no preview of this dialog is shown. + shown: Option, + /// The `view.proofSetup` parameters the preview last sent, so an unchanged dialog does not send them + /// again (the engine may normalize the view, so `shown` cannot say it). + sent: Option, +} /// Open the dialog on the current proof setup. While it is open, it stays as it is, so Cancel /// still puts back the view it found first. pub fn open(app: &mut VectorcraftApp) { - if app.ui.dialog.as_ref().is_some_and(|d| d.kind == KIND) { + let held = app.ui.proof_setup.as_ref().is_some_and(|o| app.dialogs().is_some_and(|d| d.with(|host| host.stack().contains(o.id)))); + if held { return; } - // A preview whose dialog another one replaced is undone before this dialog takes the view. - restore_replaced(app); - let v = proof::view(); - let s = &v.setup; + let Some(ctx) = app.ui_ctx.clone() else { + app.status(tl!("There is no window to show Proof Setup in.")); + return; + }; + let before = proof::view(); + let s = &before.setup; let device = match &s.target { ProofTarget::WorkingCmyk | ProofTarget::Cmyk(_) | ProofTarget::Rgb(_) => s.target.id(), _ => WORKING.to_string(), }; - let before = snapshot(&v); - app.ui.dialog = Some(Dialog::new( - KIND, - json!({ - "device": device, - "preserveNumbers": s.preserve_numbers, - "intent": s.intent.id(), - "simulatePaper": s.simulate_paper, - "simulateBlack": s.simulate_black, - "preview": true, - BEFORE: before, - }), - )); -} - -/// The parts of the proof view `v` that Cancel puts back, as the dialog keeps them in `BEFORE`. -fn snapshot(v: &proof::ProofView) -> Value { - let s = &v.setup; - json!({ - "target": s.target.id(), - "intent": s.intent.id(), - "simulatePaper": s.simulate_paper, - "preserveNumbers": s.preserve_numbers, - "simulateBlack": s.simulate_black, - "proof": v.proof_colors, - }) + let fields = DialogFields::from_pairs([ + ("device", FieldValue::Text(device)), + ("preserveNumbers", FieldValue::Bool(s.preserve_numbers)), + ("intent", FieldValue::Text(s.intent.id().to_string())), + ("simulatePaper", FieldValue::Bool(s.simulate_paper)), + ("simulateBlack", FieldValue::Bool(s.simulate_black)), + ]); + let dialog = HostedParamDialog::new(&ctx, KIND, fields, build); + let options = HeldOptions { cancel: Some(Box::new(cancel)), ..Default::default() }; + match open_held_with(app, dialog, |app, result: &ParamResult| confirm(app, &result.fields), options) { + Some(id) => app.ui.proof_setup = Some(Open { id, before, shown: None, sent: None }), + None => app.status(tl!("There is no window to show Proof Setup in.")), + } } /// The devices to simulate, as (`device` value, name shown): the working CMYK space, then every @@ -104,127 +112,133 @@ fn rgb_device(device: &str) -> bool { device.starts_with("rgb:") } -/// What OK sends to `view.proofSetup`. -fn params(d: &Dialog) -> Value { +/// The dialog over the host's values, made again every pass. +fn build(fields: &mut DialogFields) -> ParamDialog<'_> { + // The web has no folder to keep setups in. + let footer = if cfg!(target_arch = "wasm32") { + Vec::new() + } else { + vec![ + (SAVE.to_string(), FooterButton::new(tl!("Save…").to_string(), DialogRole::Other)), + (LOAD_BUTTON.to_string(), FooterButton::new(tl!("Load…").to_string(), DialogRole::Other)), + ] + }; + ParamDialog::new(KIND, tl!("Proof Setup").to_string(), rows(fields)).values(Bind::bound(fields)).previews(true).extra_buttons(footer) +} + +/// The rows for the values `fields` hold now. +fn rows(fields: &DialogFields) -> Vec { + let mut devices = devices(); + let device = fields.text("device").unwrap_or(WORKING); + if !devices.iter().any(|(id, _)| id == device) { + // A profile that is no longer loaded shows its name as it is. + devices.push((device.to_string(), device.split_once(':').map_or(device, |(_, name)| name).to_string())); + } + let rgb = rgb_device(device); + let paper = fields.bool("simulatePaper").unwrap_or(false); + // Paper and black ink are a printing press's: an RGB device has neither. + let cmyk: Vec = devices.iter().filter(|(id, _)| !rgb_device(id)).map(|(id, _)| id.clone()).collect(); + let preserve = if rgb { tl!("Preserve RGB Numbers") } else { tl!("Preserve CMYK Numbers") }; + let intents = Intent::ALL.iter().map(|i| (i.id().to_string(), i.label().to_string().into())).collect(); + // Simulate Paper Color shows the press black too, so its box shows checked while paper is on. + let black = Param::new("simulateBlack", tl!("Simulate Black Ink").to_string(), ParamKind::bool(false)); + let black = if paper && !rgb { black.enabled_when("simulatePaper", false).disabled_value(true) } else { black.enabled_when_in("device", cmyk.clone()) }; + vec![ + Param::new( + "device", + tl!("Device to Simulate:").to_string(), + ParamKind::choice(devices.into_iter().map(|(id, name)| (id, name.into())).collect(), Some(WORKING.to_string())), + ), + Param::new("preserveNumbers", preserve.to_string(), ParamKind::bool(false)), + Param::new("intent", tl!("Rendering intent:").to_string(), ParamKind::choice(intents, None)).enabled_when("preserveNumbers", false), + Param::new("simulatePaper", tl!("Simulate Paper Color").to_string(), ParamKind::bool(false)).enabled_when_in("device", cmyk), + black, + ] +} + +/// What the dialog's values send to `view.proofSetup`. +fn params(fields: &DialogFields) -> Value { json!({ - "target": d.str("device"), - "preserveNumbers": d.bool("preserveNumbers"), - "intent": d.str("intent"), - "simulatePaper": d.bool("simulatePaper"), - "simulateBlack": d.bool("simulateBlack"), + "target": fields.text("device").unwrap_or(WORKING), + "preserveNumbers": fields.bool("preserveNumbers").unwrap_or(false), + "intent": fields.text("intent").unwrap_or_default(), + "simulatePaper": fields.bool("simulatePaper").unwrap_or(false), + "simulateBlack": fields.bool("simulateBlack").unwrap_or(false), "proof": true, }) } -fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { - let devices = devices(); - let device = d.str("device"); - // A profile that is no longer loaded shows its name as it is. - let shown = - devices.iter().find(|(id, _)| *id == device).map_or_else(|| device.split_once(':').map_or(device.as_str(), |(_, n)| n), |(_, n)| n.as_str()); - let names: Vec<&str> = devices.iter().map(|(_, n)| n.as_str()).collect(); - widgets::label_row(ui, tl!("Device to Simulate:"), LABEL_W, |ui| { - if let Some((id, _)) = widgets::dropdown_names(ui, "proof-setup-device", shown, &names, 260.0).and_then(|i| devices.get(i)) { - d.fields.insert("device".into(), json!(id)); - } - }); - ui.add_space(4.0); - let rgb = rgb_device(&d.str("device")); - let preserve = if rgb { tl!("Preserve RGB Numbers") } else { tl!("Preserve CMYK Numbers") }; - form::check(ui, d, "preserveNumbers", preserve); - let intents: Vec<(&str, &str)> = Intent::ALL.iter().map(|i| (i.id(), i.label())).collect(); - ui.add_enabled_ui(!d.bool("preserveNumbers"), |ui| form::choice(ui, d, "intent", tl!("Rendering intent:"), (LABEL_W, 200.0), &intents)); - ui.add_space(4.0); - // Paper and black ink are a printing press's: an RGB device has neither. - let paper = d.bool("simulatePaper"); - if widgets::check(ui, tl!("Simulate Paper Color"), paper && !rgb, !rgb) { - d.fields.insert("simulatePaper".into(), json!(!paper)); - } - // Simulate Paper Color shows the press black too, so the box shows checked while it is on. - let black = d.bool("simulateBlack"); - if widgets::check(ui, tl!("Simulate Black Ink"), (black || paper) && !rgb, !rgb && !paper) { - d.fields.insert("simulateBlack".into(), json!(!black)); - } - // The web has no folder to keep setups in. - if !cfg!(target_arch = "wasm32") { - ui.add_space(6.0); - let (mut save_clicked, mut load_clicked) = (false, false); - ui.horizontal(|ui| { - save_clicked = widgets::flat_button(ui, tl!("Save…"), 64.0).clicked(); - load_clicked = widgets::flat_button(ui, tl!("Load…"), 64.0).clicked(); - }); - // Their file dialogs, shown off the UI thread, save from or load into the dialog as it is - // then. - let r = if save_clicked { - crate::picks::in_app_dialog(app, d, save) - } else if load_clicked { - crate::picks::in_app_dialog(app, d, load) - } else { - Ok(()) - }; - if let Err(e) = r { - app.status(e); - } - } - ui.add_space(8.0); - form::preview_check(ui, d); - preview(app, d); - false +/// What [`pump`] reads from the open dialog in one visit to the host. +struct Seen { + /// The dialog is the top one: nothing covers it. + top: bool, + /// Its Preview box is on. + previewing: bool, + fields: DialogFields, + /// The footer button pressed since the last frame. + action: Option, } -/// While Preview is on, the canvas shows the dialog's settings, sent again whenever they change; -/// turned off, the earlier proof view comes back. -fn preview(app: &mut VectorcraftApp, d: &mut Dialog) { - // Back from under another dialog that undid its preview, the dialog starts from the view the - // canvas shows now and previews again. - if d.fields.contains_key(PREVIEWED) && app.ui.proof_preview.is_none() { - d.fields.remove(PREVIEWED); +/// The dialog `id` as the window's host holds it; `None` once it has closed. +fn seen(app: &VectorcraftApp, id: DialogId) -> Option { + app.dialogs()?.with(|host| { + let top = host.stack().is_top(id); + let dialog = host.get_mut_by_id::(id)?; + Some(Seen { top, previewing: dialog.is_previewing(), fields: dialog.fields().clone(), action: dialog.take_footer_action() }) + }) +} + +/// Once per frame after the shell pass ([`super::host::pump`]): the saved setups' listing, then the +/// open dialog. A dialog on top that previews shows its values on the canvas, sent again whenever +/// they change; covered by another dialog, or with Preview off, the earlier proof view comes back. +/// Cancel is not read here: [`cancel`] runs when the host reports the dialog closed. +pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { + list_saved(app, ctx); + let Some(id) = app.ui.proof_setup.as_ref().map(|o| o.id) else { return }; + let Some(seen) = seen(app, id) else { return }; + if seen.top && seen.previewing { + preview(app, &seen.fields); + } else { + restore(app); } - if d.bool("preview") { - let p = params(d); - if d.fields.get(PREVIEWED) != Some(&p) { - // A preview that starts while the canvas shows no preview of this dialog (none yet, or a - // proof setup a command set since) takes the view the canvas shows now as the one to - // put back. - let v = proof::view(); - let start = match &app.ui.proof_preview { - Some((_, previewed)) if dialog_part(&v) == dialog_part(previewed) => None, - _ => Some(snapshot(&v)), - }; - match app.run(CMD, p.clone()) { - Ok(_) => { - if let Some(before) = start { - d.fields.insert(BEFORE.into(), before); - } - d.fields.insert(PREVIEWED.into(), p); - app.ui.proof_preview = d.fields.get(BEFORE).map(|b| (b.clone(), proof::view())); - } - Err(e) => app.ui.status = e, + if let Some(action) = seen.action { + act(app, id, &seen.fields, &action); + } +} + +/// Show the dialog's values on the canvas unless they are the ones already shown. +fn preview(app: &mut VectorcraftApp, fields: &DialogFields) { + let p = params(fields); + let Some(open) = app.ui.proof_setup.as_ref() else { return }; + if open.sent.as_ref() == Some(&p) { + return; + } + // A preview that starts while the canvas shows no preview of this dialog (none yet, or a proof + // setup a command set since) takes the view the canvas shows now as the one to put back. + let now = proof::view(); + let before = if open.shown.as_ref().is_some_and(|shown| dialog_part(&now) == dialog_part(shown)) { open.before.clone() } else { now }; + match app.run(CMD, p.clone()) { + Ok(_) => { + if let Some(open) = app.ui.proof_setup.as_mut() { + (open.before, open.sent, open.shown) = (before, Some(p), Some(proof::view())); } } - } else if d.fields.remove(PREVIEWED).is_some() { - restore(app); + Err(e) => app.status(e), } } -/// Put back the proof view the dialog found before its preview. A proof setup set since the -/// preview (by `view.proofSetup`, for example) stays. +/// Put back the proof view the dialog found before its preview, and forget the preview. A proof +/// setup set since the preview (by `view.proofSetup`, for example) stays. fn restore(app: &mut VectorcraftApp) { - if let Some((before, previewed)) = app.ui.proof_preview.take() - && dialog_part(&proof::view()) == dialog_part(&previewed) - { + let Some(open) = app.ui.proof_setup.as_mut() else { return }; + open.sent = None; + let Some(shown) = open.shown.take() else { return }; + let before = open.before.clone(); + if dialog_part(&proof::view()) == dialog_part(&shown) { set_view(app, &before); } } -/// When another dialog has taken the place of a previewing Proof Setup dialog, put back the proof -/// view that dialog found. -pub(super) fn restore_replaced(app: &mut VectorcraftApp) { - if !app.ui.dialog.as_ref().is_some_and(|d| d.kind == KIND) { - restore(app); - } -} - /// The parts of a proof view the dialog sets: the device, the intent, the three options and Proof /// Colors. fn dialog_part(v: &proof::ProofView) -> (&ProofTarget, Intent, bool, bool, bool, bool) { @@ -232,20 +246,18 @@ fn dialog_part(v: &proof::ProofView) -> (&ProofTarget, Intent, bool, bool, bool, (&s.target, s.intent, s.simulate_paper, s.preserve_numbers, s.simulate_black, v.proof_colors) } -/// Set the proof view `before` holds and redraw the documents. The view is set directly, not -/// through `view.proofSetup`, which refuses a device profile removed since it was set. -fn set_view(app: &mut VectorcraftApp, before: &Value) { - let text = |k: &str| before.get(k).and_then(Value::as_str).unwrap_or_default(); - let on = |k: &str| before.get(k).and_then(Value::as_bool).unwrap_or_default(); - let (Some(target), Some(intent)) = (ProofTarget::parse(text("target")), Intent::parse(text("intent"))) else { return }; +/// Set the dialog's parts of the proof view to those of `before` and redraw the documents. The view +/// is set directly, not through `view.proofSetup`, which refuses a device profile removed since it +/// was set; Overprint Preview stays as the user set it. +fn set_view(app: &mut VectorcraftApp, before: &proof::ProofView) { let mut v = proof::view(); let was = v.clone(); - v.setup.target = target; - v.setup.intent = intent; - v.setup.simulate_paper = on("simulatePaper"); - v.setup.preserve_numbers = on("preserveNumbers"); - v.setup.simulate_black = on("simulateBlack"); - v.proof_colors = on("proof"); + v.setup.target = before.setup.target.clone(); + v.setup.intent = before.setup.intent; + v.setup.simulate_paper = before.setup.simulate_paper; + v.setup.preserve_numbers = before.setup.preserve_numbers; + v.setup.simulate_black = before.setup.simulate_black; + v.proof_colors = before.proof_colors; if v == was { return; } @@ -258,87 +270,100 @@ fn set_view(app: &mut VectorcraftApp, before: &Value) { } } -fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { - let r = app.run(CMD, params(d))?; - app.ui.proof_preview = None; - app.ui.dialog = None; - Ok(r) +/// OK: set the proof setup and turn Proof Colors on. An `Err` keeps the dialog open. +fn confirm(app: &mut VectorcraftApp, fields: &DialogFields) -> Result<(), String> { + app.run(CMD, params(fields))?; + app.ui.proof_setup = None; + Ok(()) } /// Cancel: after a preview, the earlier proof view comes back. -pub(super) fn cancel(app: &mut VectorcraftApp, d: &Dialog) { - if d.fields.contains_key(PREVIEWED) { - restore(app); - } +fn cancel(app: &mut VectorcraftApp) { + restore(app); + app.ui.proof_setup = None; } -/// A file dialog for proof setup files, starting in the Proof Setups folder. -fn pick(app: &VectorcraftApp, name: String) -> FilePick { - let folder = app.session.proof_setup_dir().map(|f| f.to_string_lossy().into_owned()); - FilePick { name, folder, filters: vec![FilePick::filter("Proof setups", &[EXT])] } +/// A footer button was pressed in the dialog `id` holding `fields`. +fn act(app: &mut VectorcraftApp, id: DialogId, fields: &DialogFields, action: &str) { + match action { + SAVE => save(app, fields), + LOAD_BUTTON => load(app, id), + _ => {} + } } /// Save…: the dialog's settings go to the picked file, and the menu lists the saved setups again. -fn save(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { - // The picker opens in the folder, made for it when it may be written. When the picker is - // canceled, the folder it made is removed again while it is still empty: here for a picker - // shown in line, and by [`poll`] once a picker shown off the UI thread has closed. - let made = app - .session - .proof_setup_dir() +fn save(app: &mut VectorcraftApp, fields: &DialogFields) { + // The picker opens in the folder, made for it when it may be written. [`list_saved`] removes + // it again when the picker closes without a file in it. + let dir = app.session.proof_setup_dir().map(Path::to_path_buf); + let made = dir + .clone() .filter(|dir| !dir.is_dir() && vectorcraft_engine::file_access::check_write(&dir.to_string_lossy()).is_ok()) - .map(std::path::Path::to_path_buf) .filter(|dir| std::fs::create_dir_all(dir).is_ok()); - let pick = pick(app, format!("{}.{EXT}", tl!("Proof Setup"))); - let Some((picked, file_type)) = crate::picks::save_with_type(app, &pick) else { - if let Some(dir) = made { - if app.picks.is_waiting() { - saved().made = Some(dir); - } else { - let _ = std::fs::remove_dir(dir); - } - } - return Ok(()); - }; - let path = if std::path::Path::new(&picked).extension().is_none() { format!("{picked}.{EXT}") } else { picked.clone() }; - // The picker asked only about the name as typed: a file at the name with the extension added - // is replaced after a question, whose OK runs Save… again. - if path != picked && crate::picks::ask_to_replace(app, crate::picks::PickRequest::Save(pick.clone()), &picked, file_type.as_deref(), &path) { - return Ok(()); + if made.is_some() { + saved().made = made; } - let mut p = params(d); + let mut request = PickRequest::save("proof-setups") + .file_name(format!("{}.{EXT}", tl!("Proof Setup"))) + .filter("Proof setups", &[EXT]) + .suggested_extension(EXT); + if let Some(dir) = dir { + request = request.directory(dir); + } + let mut p = params(fields); if let Some(o) = p.as_object_mut() { o.remove("proof"); - o.insert("path".into(), json!(path)); } - app.run("view.proofSetup.save", p)?; - // The read asks for a frame when it ends, so an idle window lists the setup. - refresh_saved(app, app.ui_ctx.as_ref()); - app.status(format!("Saved {path}")); - Ok(()) + ask(app, request, Owner::Free, move |app, picked| { + let Picked::Saved { path, .. } = picked else { return Ok(Value::Null) }; + let path = path.to_string_lossy().into_owned(); + let mut p = p; + if let Some(o) = p.as_object_mut() { + o.insert("path".into(), json!(path)); + } + app.run("view.proofSetup.save", p)?; + refresh_saved(app); + app.status(format!("Saved {path}")); + Ok(Value::Null) + }); } -/// Load…: the picked file's settings fill the dialog (Preview then shows them). -fn load(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { - let mut pick = pick(app, String::new()); +/// Load…: the picked file's settings fill the dialog `id` (Preview then shows them). +fn load(app: &mut VectorcraftApp, id: DialogId) { + let mut request = PickRequest::open("proof-setups").filter("Proof setups", &[EXT]); // Before a setup is saved there is no folder, and the picker starts in its own default folder. - if pick.folder.as_deref().is_some_and(|f| !std::path::Path::new(f).is_dir()) { - pick.folder = None; + if let Some(dir) = app.session.proof_setup_dir().filter(|dir| dir.is_dir()) { + request = request.directory(dir); } - // A canceled picker leaves the dialog as it is. - let Some(path) = crate::picks::open(app, &pick) else { return Ok(()) }; - let r = app.run("view.proofSetup.read", json!({ "path": path }))?; - let fields = [ - ("device", "target"), - ("intent", "intent"), - ("preserveNumbers", "preserveNumbers"), - ("simulatePaper", "simulatePaper"), - ("simulateBlack", "simulateBlack"), - ]; - for (field, key) in fields { - d.fields.insert(field.into(), r[key].clone()); - } - Ok(()) + ask(app, request, Owner::Free, move |app, picked| { + let Some(path) = picked.first_path() else { return Ok(Value::Null) }; + let r = app.run("view.proofSetup.read", json!({ "path": path.to_string_lossy() }))?; + let fields = [ + ("device", "target"), + ("intent", "intent"), + ("preserveNumbers", "preserveNumbers"), + ("simulatePaper", "simulatePaper"), + ("simulateBlack", "simulateBlack"), + ]; + // The dialog may have closed while the picker was open: then there is nothing to fill. + // PLAN: sh.K23's `set_json_fields` refuses a row that is disabled by the file's own device (an RGB + // device disables paper and black ink), so the whole load would fail; the values are set unchecked. + if let Some(dialogs) = app.dialogs() { + dialogs.with(|host| { + if let Some(dialog) = host.get_mut_by_id::(id) { + for (field, key) in fields { + match &r[key] { + Value::Bool(b) => dialog.set_field(field, FieldValue::Bool(*b)), + Value::String(s) => dialog.set_field(field, FieldValue::Text(s.clone())), + _ => {} + } + } + } + }); + } + Ok(Value::Null) + }); } // ---------- the saved setups in View › Proof Setup ---------- @@ -352,19 +377,15 @@ struct Saved { setups: Vec<(&'static str, String)>, /// How many listings differed from the one before (the macOS menu bar follows it). revision: u64, - /// A first listing was read. + /// A first listing was asked for. started: bool, - /// The window was the active one at the last [`poll`]. - focused: bool, - /// The session's count of saved setups at the last [`poll`] (`Session::proof_setup_saves`). + /// The session's count of saved setups at the last [`pump`] (`Session::proof_setup_saves`). saves: u64, - /// The Proof Setups folder that Save… made for a picker shown off the UI thread, removed by - /// [`poll`] once the picker has closed if it is still empty. - made: Option, - /// The listing being read on another thread, as (name, path) of each setup. - reading: Option>>, - /// Another read was asked for while one was under way; [`poll`] starts it when that one ends. - pending: bool, + /// The Proof Setups folder that Save… made for its picker, removed by [`pump`] once no picker + /// is open if it is still empty. + made: Option, + /// The listings on the host's jobs, as (name, path) of each setup; made by the first listing. + queue: Option>>, } /// The listing the menus show. One per process in the app; one per thread in unit tests, which run @@ -383,102 +404,80 @@ fn saved() -> MutexGuard<'static, Saved> { saved.lock().unwrap_or_else(|e| e.into_inner()) } -/// Read the Proof Setups folder's listing again on another thread (`view.proofSetup.list`'s -/// listing: file names only, at most 100), unless the session has no folder. While a read is under -/// way, another read starts when it ends ([`poll`]). `ctx` is asked to draw when the read ends. -pub fn refresh_saved(app: &VectorcraftApp, ctx: Option<&egui::Context>) { - let Some(dir) = app.session.proof_setup_dir().map(std::path::Path::to_path_buf) else { return }; +/// The setups in `dir` as `view.proofSetup.list` lists them: file names only, at most 100 (the +/// engine's cap). +fn listing(dir: &Path) -> Vec<(String, String)> { + // The app's own folder, read for the person at the keyboard. + let listed = vectorcraft_engine::file_access::unconfined(|| proof_file::listed(dir)); + listed + .ok() + .flatten() + .and_then(|r| r["setups"].as_array().cloned()) + .unwrap_or_default() + .iter() + .filter_map(|e| Some((e["name"].as_str()?.to_string(), e["path"].as_str()?.to_string()))) + .collect() +} + +/// Read the Proof Setups folder's listing again on the host's jobs, unless the session has no +/// folder. A listing asked for while one runs waits for it; one still waiting is replaced. The +/// window is asked for a frame when a listing ends. +pub fn refresh_saved(app: &VectorcraftApp) { + let Some(dir) = app.session.proof_setup_dir().map(Path::to_path_buf) else { return }; let mut saved = saved(); - if saved.reading.is_some() { - saved.pending = true; - return; - } - let (tx, rx) = std::sync::mpsc::channel(); - let ctx = ctx.cloned(); - let spawned = std::thread::Builder::new().name("vectorcraft-proof-setups".into()).spawn(move || { - // The app's own folder, read for the person at the keyboard. - let listed = vectorcraft_engine::file_access::unconfined(|| proof_file::listed(&dir)); - let setups = listed - .ok() - .flatten() - .and_then(|r| r["setups"].as_array().cloned()) - .unwrap_or_default() - .iter() - .filter_map(|e| Some((e["name"].as_str()?.to_string(), e["path"].as_str()?.to_string()))) - .collect(); - if tx.send(setups).is_ok() - && let Some(ctx) = ctx - { - ctx.request_repaint(); - } + let queue = saved.queue.get_or_insert_with(|| { + let wake = app.ui_ctx.clone().map_or_else(Wake::none, |ctx| Wake::new(move || ctx.request_repaint())); + JobQueue::new(app.host.jobs_shared().clone(), wake, QueueOptions { lanes: 1, name: "vectorcraft-proof-setups" }) }); - if spawned.is_ok() { - saved.reading = Some(rx); - } + queue.submit((), Priority::BACKGROUND, move |_| listing(&dir)); } -/// Take in the read under way: when it has ended, or with `wait` once it ends. A name past the -/// menu's name limit is left out. -fn take(wait: bool) { +/// Take in the listings that ended, keeping the last. A name past the menu's name limit is left out. +fn take() { let mut saved = saved(); - let Some(reading) = &saved.reading else { return }; - let listed = if wait { reading.recv().map_err(|_| TryRecvError::Disconnected) } else { reading.try_recv() }; - match listed { - Ok(listed) => { - let setups: Vec<(&'static str, String)> = - listed.into_iter().filter_map(|(name, path)| Some((crate::scripts_menu::intern(&name)?, path))).collect(); - if saved.setups != setups { - saved.setups = setups; - saved.revision += 1; - } - saved.reading = None; - } - Err(TryRecvError::Empty) => {} - Err(TryRecvError::Disconnected) => saved.reading = None, + let Some(listed) = saved.queue.as_ref().and_then(|queue| std::iter::from_fn(|| queue.try_recv()).last()) else { return }; + let setups: Vec<(&'static str, String)> = + listed.result.into_iter().filter_map(|(name, path)| Some((crate::scripts_menu::intern(&name)?, path))).collect(); + if saved.setups != setups { + saved.setups = setups; + saved.revision += 1; } } -/// Wait for the read under way and take it in. +/// Wait for the listing under way and take it in. #[cfg(test)] pub(crate) fn wait() { - take(true); + if let Some(queue) = &saved().queue { + queue.finish(std::time::Duration::from_secs(10)); + } + take(); } -/// Once per iteration: list the folder on the first one, after `view.proofSetup.save` wrote a -/// setup (from the dialog, a control client or the command palette), and when the window becomes -/// the active one, so a setup copied into the folder is listed without a restart; take in a read -/// that ended, and then start a read asked for while it was under way. Once Save…'s picker shown -/// off the UI thread has closed, the folder it made is removed if it is still empty. -pub fn poll(app: &mut VectorcraftApp, ctx: &egui::Context) { - let focused = ctx.input(|i| i.focused); +/// List the folder on the first frame, after `view.proofSetup.save` wrote a setup (from the dialog, +/// a control client or the command palette), and when the window becomes the active one, so a setup +/// copied into the folder is listed without a restart; take in the listings that ended. Once no +/// picker is open, the folder Save… made is removed if it is still empty. +fn list_saved(app: &mut VectorcraftApp, ctx: &egui::Context) { + // A pass applies only its own input: the focus event is in the frame's first pass, so a second + // pass of the frame does not list again. + let focused = ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::WindowFocused(true)))); let saves = app.session.proof_setup_saves(); let start = { let mut saved = saved(); - if !app.picks.is_waiting() + if !app.file_dialog_open() && let Some(dir) = saved.made.take() { // A folder that a setup was saved in is not empty, and stays. let _ = std::fs::remove_dir(dir); } - let start = !saved.started || (focused && !saved.focused) || saves != saved.saves; - (saved.started, saved.focused, saved.saves) = (true, focused, saves); + let start = !saved.started || focused || saves != saved.saves; + (saved.started, saved.saves) = (true, saves); start }; if start { - refresh_saved(app, Some(ctx)); - } - take(false); - let again = { - let mut saved = saved(); - let again = saved.pending && saved.reading.is_none(); - if again { - saved.pending = false; - } - again - }; - if again { - refresh_saved(app, Some(ctx)); + refresh_saved(app); } + take(); } /// Changes whenever the menus' listing changes. @@ -494,6 +493,9 @@ pub fn saved_items() -> Vec { #[cfg(test)] mod tests { use super::*; + use craft::host::ScriptedFiles; + use craft::ui::dialog::DialogHost; + use std::sync::Arc; use vectorcraft_engine::Session; /// Proof views are process-wide: these tests take turns, and each leaves the default view. @@ -515,19 +517,76 @@ mod tests { } } - fn frame(app: &mut VectorcraftApp, ctx: &egui::Context) { - let mut out = ctx.run_ui(egui::RawInput::default(), |ui| super::super::show(app, ui.ctx())); - out.textures_delta.clear(); - } - fn app() -> (VectorcraftApp, egui::Context) { let mut app = VectorcraftApp::new(Session::new(), Default::default()); app.run("file.new", json!({})).unwrap(); let ctx = egui::Context::default(); crate::theme::install_fonts(&ctx); + app.ui_ctx = Some(ctx.clone()); (app, ctx) } + fn rgb() -> String { + format!("rgb:{}", cms::DISPLAY_P3) + } + + fn id(app: &VectorcraftApp) -> DialogId { + app.ui.proof_setup.as_ref().map(|o| o.id).unwrap() + } + + fn on_host(app: &VectorcraftApp, f: impl FnOnce(&mut DialogHost) -> R) -> R { + app.dialogs().unwrap().with(f) + } + + fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { + super::super::host::pump(app, ctx); + } + + /// Sets a field of the open dialog as automation does, then runs the frame. + fn set(app: &mut VectorcraftApp, ctx: &egui::Context, field: &str, value: Value) { + let id = id(app); + on_host(app, |host| host.set_json_field(ctx, id, field, value)).unwrap(); + pump(app, ctx); + } + + /// The open dialog's values. + fn values(app: &VectorcraftApp) -> Value { + let id = id(app); + on_host(app, |host| host.open_dialogs().into_iter().find(|d| d.id == id).and_then(|d| d.fields)).unwrap() + } + + /// OK: the held dialog files its answer, and the frame's pump runs `confirm` on it. + fn confirm_dialog(app: &mut VectorcraftApp, ctx: &egui::Context) { + let id = id(app); + on_host(app, |host| host.confirm_by_id(ctx, id)); + pump(app, ctx); + } + + fn cancel_dialog(app: &mut VectorcraftApp, ctx: &egui::Context) { + let id = id(app); + assert!(on_host(app, |host| host.cancel_by_id(ctx, id))); + pump(app, ctx); + } + + /// Presses a footer button of the open dialog and runs the frame. + // PLAN: the press goes straight to `act`; the button-to-id step is the framework's `ParamDialog::extra_buttons` contract. + fn press(app: &mut VectorcraftApp, ctx: &egui::Context, action: &str) { + let id = id(app); + let fields = on_host(app, |host| host.get_mut_by_id::(id).map(|d| d.fields().clone())).unwrap(); + act(app, id, &fields, action); + pump(app, ctx); + } + + fn scripted(app: &mut VectorcraftApp, files: ScriptedFiles) -> Arc { + let files = Arc::new(files); + app.host = app.host.with_files(files.clone()); + files + } + + fn rectangle(app: &mut VectorcraftApp) { + app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50})).unwrap(); + } + /// View › Proof Setup › Customize… opens the dialog on the current proof setup; OK sets the /// device, Preserve Numbers, the intent and Simulate Black Ink, and turns Proof Colors on. #[test] @@ -536,17 +595,14 @@ mod tests { let (mut app, ctx) = app(); let item = crate::menus::menu_entries(&app).into_iter().find(|e| e.path == ["View", "Proof Setup"] && e.label == "Customize…").unwrap(); app.run(item.command.as_deref().unwrap(), item.params.clone()).unwrap(); - let d = app.ui.dialog.as_ref().unwrap(); - assert_eq!(d.kind, KIND); - assert_eq!((d.str("device"), d.str("intent"), d.bool("preserveNumbers")), (WORKING.to_string(), "relative".to_string(), false)); - frame(&mut app, &ctx); + let v = values(&app); + assert_eq!((&v["device"], &v["intent"], &v["preserveNumbers"]), (&json!(WORKING), &json!("relative"), &json!(false))); let device = format!("cmyk:{}", cms::DEVICE_CMYK); - let d = app.ui.dialog.as_mut().unwrap(); - for (k, v) in [("device", json!(device)), ("intent", json!("perceptual")), ("simulateBlack", json!(true)), ("preview", json!(false))] { - d.fields.insert(k.into(), v); + for (k, val) in [("device", json!(device)), ("intent", json!("perceptual")), ("simulateBlack", json!(true)), ("preview", json!(false))] { + set(&mut app, &ctx, k, val); } - super::super::confirm(&mut app).unwrap(); - assert!(app.ui.dialog.is_none()); + confirm_dialog(&mut app, &ctx); + assert!(app.ui.proof_setup.is_none()); let v = proof::view(); assert!(v.proof_colors); assert_eq!(v.setup.target, ProofTarget::Cmyk(cms::DEVICE_CMYK.into())); @@ -554,10 +610,10 @@ mod tests { assert!(v.setup.simulate_black && !v.setup.preserve_numbers && !v.setup.simulate_paper); // Opened again, it shows those settings, and the menu checks Customize…. app.run("ui.proofSetupDialog", json!({})).unwrap(); - let d = app.ui.dialog.as_ref().unwrap(); - assert_eq!((d.str("device"), d.str("intent"), d.bool("simulateBlack")), (device, "perceptual".to_string(), true)); + let v = values(&app); + assert_eq!((&v["device"], &v["intent"], &v["simulateBlack"]), (&json!(device), &json!("perceptual"), &json!(true))); assert_eq!(crate::menus::checked(&app, "ui.proofSetupDialog", &json!({})), Some(true)); - super::super::cancel(&mut app); + cancel_dialog(&mut app, &ctx); } /// Preview shows the dialog's settings on the canvas at once, follows their changes, and @@ -569,24 +625,19 @@ mod tests { app.run("view.proofSetup", json!({"target": "protanopia"})).unwrap(); let before = proof::view(); app.run("ui.proofSetupDialog", json!({})).unwrap(); - assert_eq!(app.ui.dialog.as_ref().unwrap().str("device"), WORKING, "a color blindness proof starts on the working CMYK"); - let rgb = format!("rgb:{}", cms::DISPLAY_P3); - let d = app.ui.dialog.as_mut().unwrap(); - d.fields.insert("device".into(), json!(rgb)); - d.fields.insert("preserveNumbers".into(), json!(true)); - frame(&mut app, &ctx); + assert_eq!(values(&app)["device"], json!(WORKING), "a color blindness proof starts on the working CMYK"); + set(&mut app, &ctx, "device", json!(rgb())); + set(&mut app, &ctx, "preserveNumbers", json!(true)); let v = proof::view(); assert!(v.proof_colors, "the preview turns Proof Colors on"); assert_eq!(v.setup.target, ProofTarget::Rgb(cms::DISPLAY_P3.into())); assert!(v.setup.preserve_numbers); - app.ui.dialog.as_mut().unwrap().fields.insert("preview".into(), json!(false)); - frame(&mut app, &ctx); + set(&mut app, &ctx, "preview", json!(false)); assert_eq!(proof::view(), before, "Preview off puts the view back"); - app.ui.dialog.as_mut().unwrap().fields.insert("preview".into(), json!(true)); - frame(&mut app, &ctx); + set(&mut app, &ctx, "preview", json!(true)); assert_eq!(proof::view().setup.target, ProofTarget::Rgb(cms::DISPLAY_P3.into())); - super::super::cancel(&mut app); - assert!(app.ui.dialog.is_none()); + cancel_dialog(&mut app, &ctx); + assert!(app.ui.proof_setup.is_none()); assert_eq!(proof::view(), before, "Cancel puts the view back"); } @@ -603,17 +654,13 @@ mod tests { let before = proof::view(); assert_eq!(before.setup.target, ProofTarget::Rgb(name.into()), "removing the profile leaves it the device"); app.run("ui.proofSetupDialog", json!({})).unwrap(); - app.ui.dialog.as_mut().unwrap().fields.insert("device".into(), json!(WORKING)); - frame(&mut app, &ctx); + set(&mut app, &ctx, "device", json!(WORKING)); assert_eq!(proof::view().setup.target, ProofTarget::WorkingCmyk, "the preview shows the working CMYK"); - app.ui.dialog.as_mut().unwrap().fields.insert("preview".into(), json!(false)); - frame(&mut app, &ctx); + set(&mut app, &ctx, "preview", json!(false)); assert_eq!(proof::view(), before, "Preview off puts the view back"); - app.ui.dialog.as_mut().unwrap().fields.insert("preview".into(), json!(true)); - frame(&mut app, &ctx); + set(&mut app, &ctx, "preview", json!(true)); let revision = app.session.doc().unwrap().revision; - super::super::cancel(&mut app); - assert!(app.ui.dialog.is_none()); + cancel_dialog(&mut app, &ctx); assert_eq!(proof::view(), before, "Cancel puts the view back"); assert!(app.session.doc().unwrap().revision > revision, "the document redraws"); } @@ -626,100 +673,39 @@ mod tests { let (mut app, ctx) = app(); let before = proof::view(); app.run("ui.proofSetupDialog", json!({})).unwrap(); - let rgb = format!("rgb:{}", cms::DISPLAY_P3); - app.ui.dialog.as_mut().unwrap().fields.insert("device".into(), json!(rgb)); - frame(&mut app, &ctx); + set(&mut app, &ctx, "device", json!(rgb())); assert_eq!(proof::view().setup.target, ProofTarget::Rgb(cms::DISPLAY_P3.into())); app.run("ui.proofSetupDialog", json!({})).unwrap(); - assert_eq!(app.ui.dialog.as_ref().unwrap().str("device"), rgb, "the open dialog stays as it is"); - super::super::cancel(&mut app); + assert_eq!(values(&app)["device"], json!(rgb()), "the open dialog stays as it is"); + cancel_dialog(&mut app, &ctx); assert_eq!(proof::view(), before, "Cancel puts back the view the first run found"); } - /// When another dialog (Saturate) takes the place of a previewing Proof Setup dialog, the proof - /// view the dialog found comes back, and a later Proof Setup starts from that view. + /// When another dialog (Saturate) opens over a previewing Proof Setup dialog, the proof setup + /// the dialog found comes back, and Overprint Preview turned on during the preview stays on. #[test] fn another_dialog_in_its_place_puts_the_view_back() { let _v = DefaultView::new(); let (mut app, ctx) = app(); let before = proof::view(); app.run("ui.proofSetupDialog", json!({})).unwrap(); - let rgb = format!("rgb:{}", cms::DISPLAY_P3); - app.ui.dialog.as_mut().unwrap().fields.insert("device".into(), json!(rgb)); - frame(&mut app, &ctx); + set(&mut app, &ctx, "device", json!(rgb())); assert_eq!(proof::view().setup.target, ProofTarget::Rgb(cms::DISPLAY_P3.into()), "the preview shows"); - app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50})).unwrap(); - app.run("ui.saturateDialog", json!({})).unwrap(); - frame(&mut app, &ctx); - assert_eq!(app.ui.dialog.as_ref().unwrap().kind, super::super::saturate::KIND); - assert_eq!(proof::view(), before, "the preview is undone"); - super::super::cancel(&mut app); - app.run("ui.proofSetupDialog", json!({})).unwrap(); - assert_eq!(app.ui.dialog.as_ref().unwrap().str("device"), WORKING, "the next Proof Setup starts from the view it found"); - super::super::cancel(&mut app); - assert_eq!(proof::view(), before); - } - - /// Opened again before a frame has drawn the dialog that replaced a previewing Proof Setup - /// dialog, Proof Setup starts from the view the first dialog found, and Cancel keeps it. - #[test] - fn reopened_before_a_frame_starts_from_the_view_it_found() { - let _v = DefaultView::new(); - let (mut app, ctx) = app(); - let before = proof::view(); - app.run("ui.proofSetupDialog", json!({})).unwrap(); - let rgb = format!("rgb:{}", cms::DISPLAY_P3); - app.ui.dialog.as_mut().unwrap().fields.insert("device".into(), json!(rgb)); - frame(&mut app, &ctx); - assert_eq!(proof::view().setup.target, ProofTarget::Rgb(cms::DISPLAY_P3.into()), "the preview shows"); - app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50})).unwrap(); - app.run("ui.saturateDialog", json!({})).unwrap(); - app.run("ui.proofSetupDialog", json!({})).unwrap(); - assert_eq!(proof::view(), before, "the preview is undone"); - assert_eq!(app.ui.dialog.as_ref().unwrap().str("device"), WORKING); - super::super::cancel(&mut app); - assert_eq!(proof::view(), before, "Cancel keeps the view the first dialog found"); - } - - /// A proof view set after another dialog replaced a previewing Proof Setup dialog, before a - /// frame, stays: the preview is not put back over it. - #[test] - fn a_view_set_after_the_replacement_stays() { - let _v = DefaultView::new(); - let (mut app, ctx) = app(); - app.run("ui.proofSetupDialog", json!({})).unwrap(); - let rgb = format!("rgb:{}", cms::DISPLAY_P3); - app.ui.dialog.as_mut().unwrap().fields.insert("device".into(), json!(rgb)); - frame(&mut app, &ctx); - app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50})).unwrap(); - app.run("ui.saturateDialog", json!({})).unwrap(); - app.run("view.proofSetup", json!({"target": "protanopia", "proof": true})).unwrap(); - let set = proof::view(); - frame(&mut app, &ctx); - assert_eq!(proof::view(), set, "the view set after the replacement stays"); - super::super::cancel(&mut app); - assert_eq!(proof::view(), set); - } - - /// Overprint Preview turned on during the preview stays on when another dialog replaces the - /// dialog, and the proof setup the dialog found comes back. - #[test] - fn overprint_set_during_the_preview_stays_after_the_replacement() { - let _v = DefaultView::new(); - let (mut app, ctx) = app(); - let before = proof::view(); - app.run("ui.proofSetupDialog", json!({})).unwrap(); - let rgb = format!("rgb:{}", cms::DISPLAY_P3); - app.ui.dialog.as_mut().unwrap().fields.insert("device".into(), json!(rgb)); - frame(&mut app, &ctx); app.run("view.overprintPreview", json!({"on": true})).unwrap(); - app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50})).unwrap(); + rectangle(&mut app); app.run("ui.saturateDialog", json!({})).unwrap(); - frame(&mut app, &ctx); + pump(&mut app, &ctx); let v = proof::view(); assert!(v.overprint, "Overprint Preview stays on"); - assert_eq!((v.setup, v.proof_colors), (before.setup, before.proof_colors), "the proof setup comes back"); - super::super::cancel(&mut app); + assert_eq!((v.setup, v.proof_colors), (before.setup.clone(), before.proof_colors), "the proof setup comes back"); + let proof_id = id(&app); + assert!(!on_host(&app, |host| host.stack().is_top(proof_id))); + // Closing Saturate brings the dialog back; the final comparison is on the whole view. + app.run("view.overprintPreview", json!({"on": false})).unwrap(); + on_host(&app, |host| host.cancel_by_id(&ctx, host.stack().top().unwrap())); + pump(&mut app, &ctx); + cancel_dialog(&mut app, &ctx); + assert_eq!(proof::view(), before); } /// Back from under a dialog opened over it (here Saturate, as a file-replacement question is), @@ -731,62 +717,34 @@ mod tests { let (mut app, ctx) = app(); let before = proof::view(); app.run("ui.proofSetupDialog", json!({})).unwrap(); - let rgb = format!("rgb:{}", cms::DISPLAY_P3); - app.ui.dialog.as_mut().unwrap().fields.insert("device".into(), json!(rgb)); - frame(&mut app, &ctx); - let under = app.ui.dialog.clone().unwrap(); - app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50})).unwrap(); + set(&mut app, &ctx, "device", json!(rgb())); + rectangle(&mut app); app.run("ui.saturateDialog", json!({})).unwrap(); - app.ui.dialog.as_mut().unwrap().fields.insert(super::super::PARENT.into(), serde_json::to_value(under).unwrap()); - frame(&mut app, &ctx); + pump(&mut app, &ctx); assert_eq!(proof::view(), before, "the preview is undone while the other dialog shows"); - super::super::cancel(&mut app); - assert_eq!(app.ui.dialog.as_ref().unwrap().kind, KIND, "Proof Setup comes back"); - frame(&mut app, &ctx); + on_host(&app, |host| host.cancel_by_id(&ctx, host.stack().top().unwrap())); + pump(&mut app, &ctx); assert_eq!(proof::view().setup.target, ProofTarget::Rgb(cms::DISPLAY_P3.into()), "the preview shows again"); - super::super::cancel(&mut app); + cancel_dialog(&mut app, &ctx); assert_eq!(proof::view(), before, "Cancel puts back the view it found"); } - /// A proof setup set by a command while the dialog previews stays when Preview is turned off - /// and when the dialog is canceled. + /// A proof setup a command set while the dialog previews stays when Preview is turned off; + /// Preview on shows the dialog's values again, and Cancel puts back the command's view. #[test] fn a_view_set_while_the_dialog_previews_stays() { let _v = DefaultView::new(); let (mut app, ctx) = app(); app.run("ui.proofSetupDialog", json!({})).unwrap(); - let rgb = format!("rgb:{}", cms::DISPLAY_P3); - app.ui.dialog.as_mut().unwrap().fields.insert("device".into(), json!(rgb)); - frame(&mut app, &ctx); + set(&mut app, &ctx, "device", json!(rgb())); app.run("view.proofSetup", json!({"target": "protanopia", "proof": true})).unwrap(); - let set = proof::view(); - app.ui.dialog.as_mut().unwrap().fields.insert("preview".into(), json!(false)); - frame(&mut app, &ctx); - assert_eq!(proof::view(), set, "Preview off keeps the view the command set"); - app.ui.dialog.as_mut().unwrap().fields.insert("preview".into(), json!(true)); - frame(&mut app, &ctx); + let set_by_command = proof::view(); + set(&mut app, &ctx, "preview", json!(false)); + assert_eq!(proof::view(), set_by_command, "Preview off keeps the view the command set"); + set(&mut app, &ctx, "preview", json!(true)); assert_eq!(proof::view().setup.target, ProofTarget::Rgb(cms::DISPLAY_P3.into()), "the preview shows again"); - super::super::cancel(&mut app); - assert_eq!(proof::view(), set, "Cancel puts back the view the command set"); - } - - /// After a command sets a proof setup while the dialog previews, a changed setting previews - /// again, and Cancel puts back the proof setup the command set. - #[test] - fn a_changed_setting_after_a_command_keeps_the_commands_view_for_cancel() { - let _v = DefaultView::new(); - let (mut app, ctx) = app(); - app.run("ui.proofSetupDialog", json!({})).unwrap(); - let rgb = format!("rgb:{}", cms::DISPLAY_P3); - app.ui.dialog.as_mut().unwrap().fields.insert("device".into(), json!(rgb)); - frame(&mut app, &ctx); - app.run("view.proofSetup", json!({"target": "protanopia", "proof": true})).unwrap(); - let set = proof::view(); - app.ui.dialog.as_mut().unwrap().fields.insert("intent".into(), json!("perceptual")); - frame(&mut app, &ctx); - assert_eq!(proof::view().setup.intent, Intent::Perceptual, "the changed setting previews"); - super::super::cancel(&mut app); - assert_eq!(proof::view(), set, "Cancel puts back the view the command set"); + cancel_dialog(&mut app, &ctx); + assert_eq!(proof::view(), set_by_command, "Cancel puts back the view the command set"); } /// A device the dialog can't use (a profile that is not loaded) is refused, and the dialog @@ -794,14 +752,20 @@ mod tests { #[test] fn ok_with_an_unknown_device_keeps_the_dialog_open() { let _v = DefaultView::new(); - let (mut app, _) = app(); + let (mut app, ctx) = app(); + let name = "Proof Setup Gone RGB"; + cms::register_icc(&cms::icc_bytes(cms::DISPLAY_P3).unwrap(), Some(name.into())).unwrap(); + app.run("view.proofSetup", json!({"target": format!("rgb:{name}")})).unwrap(); + app.run("color.removeProfile", json!({"name": name})).unwrap(); + let before = proof::view(); app.run("ui.proofSetupDialog", json!({})).unwrap(); - let d = app.ui.dialog.as_mut().unwrap(); - d.fields.insert("device".into(), json!("cmyk:Nope")); - d.fields.insert("preview".into(), json!(false)); - assert!(super::super::confirm(&mut app).unwrap_err().contains("unknown CMYK profile")); - assert!(app.ui.dialog.is_some()); - assert_eq!(proof::view(), Default::default()); + set(&mut app, &ctx, "preview", json!(false)); + let dialog = id(&app); + confirm_dialog(&mut app, &ctx); + assert!(on_host(&app, |host| host.stack().contains(dialog)), "the dialog stays open"); + assert!(app.ui.proof_setup.is_some(), "the preview record stays"); + assert_eq!(proof::view(), before); + cancel_dialog(&mut app, &ctx); } /// Save… writes the dialog's settings to the picked file in the Proof Setups folder, which View › @@ -813,136 +777,83 @@ mod tests { let (mut app, ctx) = app(); let dir = vectorcraft_testkit::TempDir::new("proof-setup-dialog"); let folder = dir.join("Proof Setups"); + let file = folder.join(format!("Screen.{EXT}")); + let files = scripted(&mut app, ScriptedFiles::answering([Picked::Saved { path: file.clone(), file_type: None }])); app.session.attach_proof_setups(folder.clone()); - refresh_saved(&app, None); + refresh_saved(&app); wait(); assert!(saved_items().is_empty()); app.run("ui.proofSetupDialog", json!({})).unwrap(); - let rgb = format!("rgb:{}", cms::DISPLAY_P3); - let d = app.ui.dialog.as_mut().unwrap(); - for (k, v) in [("device", json!(rgb)), ("intent", json!("saturation")), ("preserveNumbers", json!(true)), ("preview", json!(false))] { - d.fields.insert(k.into(), v); + for (k, v) in [("device", json!(rgb())), ("intent", json!("saturation")), ("preserveNumbers", json!(true)), ("preview", json!(false))] { + set(&mut app, &ctx, k, v); } - let picked = std::rc::Rc::new(std::cell::RefCell::new(None::)); - let seen = picked.clone(); - let path = folder.join("Screen").to_string_lossy().to_string(); - let answer = path.clone(); - app.services.pick_save = Some(Box::new(move |p: &FilePick| { - *seen.borrow_mut() = Some(p.clone()); - Some(answer.clone()) - })); - let mut d = app.ui.dialog.take().unwrap(); - save(&mut app, &mut d).unwrap(); - let pick = picked.borrow().clone().unwrap(); - assert_eq!(pick.folder.as_deref(), Some(folder.to_string_lossy().as_ref()), "the picker starts in the Proof Setups folder"); - let file = format!("{path}.{EXT}"); - assert!(std::path::Path::new(&file).is_file(), "the extension is added"); + press(&mut app, &ctx, SAVE); + crate::file_asks::poll(&mut app); + assert_eq!(files.requests()[0].directory.as_deref(), Some(folder.as_path()), "the picker starts in the Proof Setups folder"); + assert!(file.is_file(), "the picked file is saved"); assert_eq!(proof::view(), Default::default(), "saving leaves the proof view alone"); wait(); let items = crate::menus::menu_entries(&app).into_iter().filter(|e| e.path == ["View", "Proof Setup"] && e.command.as_deref() == Some(LOAD)); let items: Vec<_> = items.map(|e| (e.label.to_string(), e.params.clone())).collect(); - assert_eq!(items, [("Screen".to_string(), json!({ "path": file }))]); + assert_eq!(items, [("Screen".to_string(), json!({ "path": file.to_string_lossy() }))]); // A control client that may not read the folder is not shown the saved setup. std::fs::create_dir_all(dir.join("out")).unwrap(); let write_only = vectorcraft_engine::file_access::AutomationRoots::new(&[], &[&dir.join("out")]).unwrap(); let listed = vectorcraft_engine::file_access::confine(write_only.as_ref(), || crate::menus::menu_entries(&app)); assert!(!listed.iter().any(|e| e.command.as_deref() == Some(LOAD)), "a setup outside the read roots is left out"); - // A setup the command saves, outside the dialog, is listed at the next iteration. - poll(&mut app, &ctx); - wait(); + // A setup the command saves, outside the dialog, is listed at the next frame. app.run("view.proofSetup.save", json!({ "name": "Direct" })).unwrap(); - poll(&mut app, &ctx); + pump(&mut app, &ctx); wait(); assert_eq!(saved_items().len(), 2, "the menu lists the new setup"); std::fs::remove_file(folder.join(format!("Direct.{EXT}"))).unwrap(); - refresh_saved(&app, None); + refresh_saved(&app); wait(); // Load… puts the file's settings back in the dialog. for (k, v) in [("device", json!(WORKING)), ("intent", json!("perceptual")), ("preserveNumbers", json!(false))] { - d.fields.insert(k.into(), v); + set(&mut app, &ctx, k, v); } - let answer = file.clone(); - app.services.pick_open = Some(Box::new(move |_: &FilePick| Some(answer.clone()))); - load(&mut app, &mut d).unwrap(); - assert_eq!((d.str("device"), d.str("intent"), d.bool("preserveNumbers")), (rgb.clone(), "saturation".to_string(), true)); + files.queue(Picked::Paths(vec![file.clone()])); + press(&mut app, &ctx, LOAD_BUTTON); + crate::file_asks::poll(&mut app); + let v = values(&app); + assert_eq!((&v["device"], &v["intent"], &v["preserveNumbers"]), (&json!(rgb()), &json!("saturation"), &json!(true))); assert_eq!(proof::view(), Default::default(), "loading into the dialog leaves the proof view alone"); - app.ui.dialog = Some(d); - frame(&mut app, &ctx); - super::super::cancel(&mut app); + cancel_dialog(&mut app, &ctx); // The menu item applies the saved setup and turns Proof Colors on. - app.run(LOAD, json!({ "path": file })).unwrap(); + app.run(LOAD, json!({ "path": file.to_string_lossy() })).unwrap(); let v = proof::view(); assert!(v.proof_colors && v.setup.preserve_numbers); assert_eq!((v.setup.target, v.setup.intent), (ProofTarget::Rgb(cms::DISPLAY_P3.into()), Intent::Saturation)); } - /// Save… whose picker is canceled saves nothing and leaves no Proof Setups folder behind. + /// Save… whose picker is canceled saves nothing and leaves no Proof Setups folder behind; the + /// folder stays while the picker is open and when a file is saved in it. #[test] fn a_canceled_save_leaves_no_folder() { let _v = DefaultView::new(); let (mut app, ctx) = app(); let dir = vectorcraft_testkit::TempDir::new("proof-setup-dialog-cancel"); let folder = dir.join("Proof Setups"); + let files = scripted(&mut app, ScriptedFiles::held()); app.session.attach_proof_setups(folder.clone()); app.run("ui.proofSetupDialog", json!({})).unwrap(); - let mut d = app.ui.dialog.take().unwrap(); - let opened = std::rc::Rc::new(std::cell::Cell::new(false)); - let seen = opened.clone(); - let f = folder.clone(); - app.services.pick_save = Some(Box::new(move |_: &FilePick| { - seen.set(f.is_dir()); - None - })); - save(&mut app, &mut d).unwrap(); - assert!(opened.get(), "the picker opens in the folder"); - assert!(!folder.exists(), "the canceled save removes the folder it made"); - // A picker shown off the UI thread is still open when Save… returns: its folder stays. - let (shown, _) = crate::tests::picks::off_the_ui_thread(&mut app.services); - app.ui.dialog = Some(d.clone()); - crate::picks::in_app_dialog(&mut app, &mut d, save).unwrap(); - assert_eq!(shown.requests().len(), 1, "the picker is shown off the UI thread"); - poll(&mut app, &ctx); + press(&mut app, &ctx, SAVE); + assert!(app.file_dialog_open(), "the picker is open"); assert!(folder.is_dir(), "the open picker's folder stays"); - // Canceled, the picker leaves no folder once the next iteration runs. - shown.answer(&[]); - crate::picks::poll(&mut app, &ctx); - poll(&mut app, &ctx); + files.answer(Picked::Cancelled); + crate::file_asks::poll(&mut app); + pump(&mut app, &ctx); assert!(!folder.exists(), "the canceled picker's folder is removed"); // A picker that answers a file there saves it, and the folder stays. - crate::picks::in_app_dialog(&mut app, &mut d, save).unwrap(); + press(&mut app, &ctx, SAVE); let file = folder.join(format!("Kept.{EXT}")); - shown.answer(&[&file.to_string_lossy()]); - crate::picks::poll(&mut app, &ctx); - poll(&mut app, &ctx); + files.answer(Picked::Saved { path: file.clone(), file_type: None }); + crate::file_asks::poll(&mut app); + pump(&mut app, &ctx); assert!(file.is_file(), "the picked file is saved"); - } - - /// The listing Save… starts asks for a frame when it ends, so an idle window takes it in. - #[test] - fn the_listing_after_a_save_asks_for_a_frame() { - let _v = DefaultView::new(); - let (mut app, _) = app(); - let dir = vectorcraft_testkit::TempDir::new("proof-setup-dialog-frame"); - let folder = dir.join("Proof Setups"); - app.session.attach_proof_setups(folder.clone()); - app.run("ui.proofSetupDialog", json!({})).unwrap(); - let mut d = app.ui.dialog.take().unwrap(); - d.fields.insert("preview".into(), json!(false)); - let path = folder.join("Screen").to_string_lossy().to_string(); - app.services.pick_save = Some(Box::new(move |_: &FilePick| Some(path.clone()))); - let ctx = egui::Context::default(); - let (tx, frames) = std::sync::mpsc::channel(); - ctx.set_request_repaint_callback(move |_| { - let _ = tx.send(()); - }); - app.ui_ctx = Some(ctx); - save(&mut app, &mut d).unwrap(); - // With the app's copy of the context dropped, only the listing's thread can still ask for - // a frame; the channel closes without one when that thread ends without asking. - app.ui_ctx = None; - assert_eq!(frames.recv(), Ok(()), "the finished listing asks for a frame"); - wait(); - assert_eq!(saved_items().len(), 1); + assert!(folder.is_dir()); + cancel_dialog(&mut app, &ctx); } /// A saved setup whose name ends in an ellipsis still loads when its menu item is chosen. @@ -952,56 +863,24 @@ mod tests { assert_eq!(crate::menus::click_target("Press…", LOAD, &p), (LOAD.to_string(), p.clone())); } - /// A saved setup keeps its file's name in every UI language, also a name the catalogs translate. - #[test] - fn a_saved_setup_keeps_its_name_in_other_languages() { - let (app, _) = app(); - crate::i18n::set_current(crate::i18n::Lang::from_code("de").expect("de registered")); - let shown = crate::menus::display_label(&app, LOAD, "Save…"); - crate::i18n::set_current(crate::i18n::Lang::EN); - assert_eq!(shown, "Save…"); - } - /// Load…'s picker starts in the Proof Setups folder once it exists, and in its own default /// folder before; Load… does not make the folder. #[test] fn load_starts_in_the_folder_once_it_exists() { let _v = DefaultView::new(); - let (mut app, _) = app(); + let (mut app, ctx) = app(); let dir = vectorcraft_testkit::TempDir::new("proof-setup-dialog-load-folder"); let folder = dir.join("Proof Setups"); + let files = scripted(&mut app, ScriptedFiles::new()); app.session.attach_proof_setups(folder.clone()); app.run("ui.proofSetupDialog", json!({})).unwrap(); - let mut d = app.ui.dialog.take().unwrap(); - let picked = std::rc::Rc::new(std::cell::RefCell::new(Vec::new())); - let seen = picked.clone(); - app.services.pick_open = Some(Box::new(move |p: &FilePick| { - seen.borrow_mut().push(p.folder.clone()); - None - })); - load(&mut app, &mut d).unwrap(); + press(&mut app, &ctx, LOAD_BUTTON); assert!(!folder.exists(), "Load… does not make the folder"); std::fs::create_dir_all(&folder).unwrap(); - load(&mut app, &mut d).unwrap(); - assert_eq!(*picked.borrow(), [None, Some(folder.to_string_lossy().into_owned())]); - } - - /// Load… of a file whose device is not loaded leaves the dialog's settings as they were. - #[test] - fn load_of_an_unusable_file_keeps_the_dialog() { - let _v = DefaultView::new(); - let (mut app, _) = app(); - let dir = vectorcraft_testkit::TempDir::new("proof-setup-dialog-bad"); - let file = dir.join("gone.vcproof").to_string_lossy().to_string(); - let text = r#"{"format": "vectorcraft-proof-setup", "version": 1, "target": "cmyk:Not Loaded", "intent": "relative"}"#; - std::fs::write(&file, text).unwrap(); - app.run("ui.proofSetupDialog", json!({})).unwrap(); - let mut d = app.ui.dialog.take().unwrap(); - let before = d.fields.clone(); - app.services.pick_open = Some(Box::new(move |_: &FilePick| Some(file.clone()))); - let e = load(&mut app, &mut d).unwrap_err(); - assert!(e.contains("`Not Loaded`, which is not loaded"), "{e}"); - assert_eq!(d.fields, before); + press(&mut app, &ctx, LOAD_BUTTON); + let started: Vec<_> = files.requests().into_iter().map(|r| r.directory).collect(); + assert_eq!(started, [None, Some(folder)]); + cancel_dialog(&mut app, &ctx); } /// The device list holds the working CMYK space and every CMYK and RGB profile, each a target