Open and save Proof Setup files in the browser

This commit is contained in:
dotCooCoo committed 2026-11-26 16:41:08 -08:00
1 parent 5c7bbeecaa
commit 75fa147553
2 files changed
+119 -32

No files matched your search

+34 -13
View File
@@ -157,14 +157,7 @@ pub fn encode(setup: &ProofSetup) -> Vec<u8> {
pub(super) fn save(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "view.proofSetup.save";
let (path, named) = file_path(s, p, C)?;
let mut setup = proof::view().setup;
setup_params(p, &mut setup, C)?;
check_device(&setup.target).map_err(|e| bad(C, e))?;
// A file `view.proofSetup.load` would refuse is not written (a device with a very long name).
let bytes = encode(&setup);
if bytes.len() > MAX_FILE {
return Err(bad(C, format!("{path}: larger than {MAX_FILE} bytes")));
}
let (setup, bytes) = encode_options(p, &path)?;
if named
&& let Some(dir) = std::path::Path::new(&path).parent()
&& !dir.is_dir()
@@ -179,19 +172,47 @@ pub(super) fn save(s: &mut Session, p: &Value) -> Result<Value> {
Ok(Value::Object(r))
}
/// Serialize the Customize dialog's draft without committing it or writing a path.
pub fn draft_bytes(p: &Value) -> Result<Vec<u8>> {
encode_options(p, "Proof Setup").map(|(_, bytes)| bytes)
}
fn encode_options(p: &Value, file: &str) -> Result<(ProofSetup, Vec<u8>)> {
const C: &str = "view.proofSetup.save";
let mut setup = proof::view().setup;
setup_params(p, &mut setup, C)?;
check_device(&setup.target).map_err(|e| bad(C, e))?;
let bytes = encode(&setup);
if bytes.len() > MAX_FILE {
return Err(bad(C, format!("{file}: larger than {MAX_FILE} bytes")));
}
Ok((setup, bytes))
}
/// The file `p` names for `cmd` and the usable proof setup in it.
fn read_setup(s: &Session, p: &Value, cmd: &str) -> Result<(String, ProofSetup)> {
let (path, _) = file_path(s, p, cmd)?;
// One byte past the limit shows a file over it, which is not read further.
let bytes = fileio::read_file_head(&path, MAX_FILE + 1)?;
if bytes.len() > MAX_FILE {
return Err(bad(cmd, format!("{path}: larger than {MAX_FILE} bytes")));
}
let setup = parse(&bytes).map_err(|e| bad(cmd, format!("{path}: {e}")))?;
check_device(&setup.target).map_err(|e| bad(cmd, format!("{path}: {e}")))?;
let setup = checked_setup(&bytes, &path, cmd)?;
Ok((path, setup))
}
/// Read a picked proof setup's validated settings without applying them to the view.
pub fn draft_fields(bytes: &[u8], name: &str) -> Result<Value> {
let setup = checked_setup(bytes, name, "view.proofSetup.read")?;
Ok(Value::Object(setup_json(&setup)))
}
fn checked_setup(bytes: &[u8], name: &str, cmd: &str) -> Result<ProofSetup> {
if bytes.len() > MAX_FILE {
return Err(bad(cmd, format!("{name}: larger than {MAX_FILE} bytes")));
}
let setup = parse(bytes).map_err(|e| bad(cmd, format!("{name}: {e}")))?;
check_device(&setup.target).map_err(|e| bad(cmd, format!("{name}: {e}")))?;
Ok(setup)
}
pub(super) fn read(s: &mut Session, p: &Value) -> Result<Value> {
let (path, setup) = read_setup(s, p, "view.proofSetup.read")?;
let mut r = setup_json(&setup);
+85 -19
View File
@@ -4,8 +4,8 @@
//! Preview shows the proof on the canvas while the dialog is open; OK sets the proof setup and
//! turns Proof Colors on (`view.proofSetup`), and Cancel puts the earlier proof view back. Save…
//! writes the dialog's settings to a proof setup file (`view.proofSetup.save`) and Load… reads one
//! into the dialog (`view.proofSetup.read`). Both are desktop only. Their pickers start in the
//! Proof Setups folder next to the preferences, whose setups View → Proof Setup lists below
//! into the dialog (`view.proofSetup.read`). The browser reads picked bytes and downloads a copy
//! when saving. Desktop pickers start in the Proof Setups folder next to the preferences, whose setups View → Proof Setup lists below
//! 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.
//!
@@ -116,15 +116,10 @@ fn rgb_device(device: &str) -> bool {
/// 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)),
]
};
let footer = 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)
}
@@ -297,6 +292,29 @@ fn act(app: &mut VectorcraftApp, id: DialogId, fields: &DialogFields, action: &s
/// Save…: the dialog's settings go to the picked file, and the menu lists the saved setups again.
fn save(app: &mut VectorcraftApp, fields: &DialogFields) {
let mut request =
PickRequest::save("proof-setups").file_name(format!("{}.{EXT}", tl!("Proof Setup"))).filter("Proof setups", &[EXT]).suggested_extension(EXT);
let mut p = params(fields);
if let Some(o) = p.as_object_mut() {
o.remove("proof");
}
if app.host.files().browser_saves() {
match proof_file::draft_bytes(&p) {
Ok(bytes) => {
crate::file_asks::save(app, request, bytes, Owner::Free, |app, picked| {
match picked {
Picked::Saved { path, .. } => app.status(format!("Saved {}", path.display())),
Picked::Downloaded(name) => app.status(format!("Saved {name}")),
Picked::Failed(error) => return Err(error),
_ => {}
}
Ok(Value::Null)
});
}
Err(e) => app.status(e.to_string()),
}
return;
}
// 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);
@@ -307,15 +325,9 @@ fn save(app: &mut VectorcraftApp, fields: &DialogFields) {
if made.is_some() {
saved().made = made;
}
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");
}
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();
@@ -343,8 +355,16 @@ fn load(app: &mut VectorcraftApp, id: DialogId) {
&& app.dialogs().is_some_and(|dialogs| dialogs.with(|host| host.get_by_id::<HostedParamDialog>(id).is_some()))
};
ask_while(app, request, Owner::Free, is_live, 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 r = match picked {
Picked::Contents(files) => {
let Some((name, bytes)) = files.into_iter().next() else { return Ok(Value::Null) };
proof_file::draft_fields(&bytes, &name).map_err(|e| e.to_string())?
}
other => {
let Some(path) = other.first_path() else { return Ok(Value::Null) };
app.run("view.proofSetup.read", json!({ "path": path.to_string_lossy() }))?
}
};
let fields = [
("device", "target"),
("intent", "intent"),
@@ -817,6 +837,52 @@ mod tests {
assert_eq!((v.setup.target, v.setup.intent), (ProofTarget::Rgb(cms::DISPLAY_P3.into()), Intent::Saturation));
}
#[test]
fn a_browser_pick_loads_a_proof_setup_into_the_draft() {
let _view = DefaultProofView::new();
let (mut app, ctx) = app();
let files = scripted(&mut app, ScriptedFiles::held().in_browser());
app.run("ui.proofSetupDialog", json!({})).unwrap();
set(&mut app, &ctx, "preview", json!(false));
let before = proof::view();
let mut source = before.setup.clone();
source.intent = Intent::Saturation;
press(&mut app, &ctx, LOAD_BUTTON);
files.answer(Picked::Contents(vec![("browser.vcproof".into(), proof_file::encode(&source))]));
crate::file_asks::poll(&mut app);
assert_eq!(values(&app)["intent"], "saturation");
assert_eq!(proof::view(), before);
press(&mut app, &ctx, LOAD_BUTTON);
files.answer(Picked::Contents(vec![("oversized.vcproof".into(), vec![b' '; proof_file::MAX_FILE + 1])]));
crate::file_asks::poll(&mut app);
assert!(app.ui.status.contains("larger than"));
assert_eq!(values(&app)["intent"], "saturation");
assert_eq!(proof::view(), before);
}
#[test]
fn saving_a_browser_proof_setup_gives_the_file_service_the_draft_bytes() {
let _view = DefaultProofView::new();
let (mut app, ctx) = app();
let files = scripted(&mut app, ScriptedFiles::held().in_browser());
app.run("ui.proofSetupDialog", json!({})).unwrap();
for (k, v) in [("intent", json!("saturation")), ("preview", json!(false))] {
set(&mut app, &ctx, k, v);
}
let before = proof::view();
let dir = vectorcraft_testkit::TempDir::new("proof-setup-browser-save");
let path = dir.join("draft.vcproof");
press(&mut app, &ctx, SAVE);
files.answer(Picked::Saved { path: path.clone(), file_type: None });
crate::file_asks::poll(&mut app);
let bytes = files.file(&path).expect("the browser file service receives the draft bytes");
assert_eq!(proof_file::parse(&bytes).unwrap().intent, Intent::Saturation);
assert_eq!(proof::view(), before);
assert_eq!(values(&app)["intent"], "saturation");
assert!(!path.exists(), "the browser save does not write through the native path");
}
#[test]
fn a_delayed_load_only_changes_the_proof_setup_that_requested_it() {
let _view = DefaultProofView::new();