mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
fix(ui): open export dialog for Data Sets as Files and gate on data sets
This commit is contained in:
5 files changed
+53
No files matched your search
@@ -19,6 +19,14 @@ fn has_doc(s: &Session) -> std::result::Result<(), String> {
|
||||
s.active().map(|_| ()).ok_or_else(|| "no document".into())
|
||||
}
|
||||
|
||||
fn has_data_sets(s: &Session) -> std::result::Result<(), String> {
|
||||
let st = s.active().ok_or_else(|| "no document".to_string())?;
|
||||
if st.doc.variables.data_sets.is_empty() {
|
||||
return Err("no data sets in document".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {
|
||||
EngineError::BadParams { cmd: cmd.into(), msg: msg.into() }
|
||||
}
|
||||
@@ -488,6 +496,9 @@ macro_rules! spec {
|
||||
($id:expr, $label:expr, $menu:expr, $params:expr, $run:expr) => {
|
||||
CommandSpec { id: $id, label: $label, menu: $menu, shortcut: None, params: $params, enabled: has_doc, journal: true, run: $run }
|
||||
};
|
||||
($id:expr, $label:expr, $menu:expr, $params:expr, $enabled:expr, $run:expr) => {
|
||||
CommandSpec { id: $id, label: $label, menu: $menu, shortcut: None, params: $params, enabled: $enabled, journal: true, run: $run }
|
||||
};
|
||||
}
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
@@ -511,6 +522,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Apply Data Set…",
|
||||
&["Image"],
|
||||
"{name|index} → {applied, index}: sets layer visibility/text/pixels from the data set (one history step)",
|
||||
has_data_sets,
|
||||
|s, p| apply_data_set(s, p)
|
||||
),
|
||||
spec!(
|
||||
@@ -525,6 +537,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Data Sets as Files…",
|
||||
&["File", "Export"],
|
||||
"{dir, format?:png, dataSets?[names], naming?:\"{name}|{index}|{document}\"} → {files,count}: apply each data set and export the flattened document; colliding filenames are rejected before writing (include {index} to disambiguate)",
|
||||
has_data_sets,
|
||||
|s, p| export_as_files(s, p)
|
||||
),
|
||||
CommandSpec {
|
||||
|
||||
@@ -652,3 +652,20 @@ fn exported_data_sets_keep_the_character_style() {
|
||||
assert_eq!((got.weight, got.italic, got.underline), (style.weight, style.italic, style.underline));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_set_commands_require_data_sets_to_be_enabled() {
|
||||
let mut s = Session::new();
|
||||
assert!(!s.is_enabled("file.export.dataSetsAsFiles"));
|
||||
assert!(!s.is_enabled("image.applyDataSet"));
|
||||
|
||||
s.execute("file.new", json!({"width": 64, "height": 64})).unwrap();
|
||||
// Document exists but has no data sets: commands remain disabled
|
||||
assert!(!s.is_enabled("file.export.dataSetsAsFiles"));
|
||||
assert!(!s.is_enabled("image.applyDataSet"));
|
||||
|
||||
// Once data sets are defined, commands become enabled
|
||||
s.execute("image.variables.dataSets", json!({"dataSets": [{"name": "Set 1", "values": []}]})).unwrap();
|
||||
assert!(s.is_enabled("file.export.dataSetsAsFiles"));
|
||||
assert!(s.is_enabled("image.applyDataSet"));
|
||||
}
|
||||
@@ -81,3 +81,21 @@ fn image_processor_folders_are_chosen_with_browse() {
|
||||
assert_eq!(field(&h, "output"), Some(json!("/tmp/web")));
|
||||
assert!(matches!(asked.borrow().as_slice(), [FileDialogRequest::Folder { .. }, FileDialogRequest::Folder { .. }]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_sets_as_files_folder_is_chosen_with_browse() {
|
||||
let (mut h, asked) = harness(vec![Some(FileDialogAnswer::FolderTo("/tmp/dataset-exports".into()))]);
|
||||
h.state_mut().run("file.new", json!({"width": 64, "height": 64})).unwrap();
|
||||
h.state_mut().run("image.variables.dataSets", json!({"dataSets": [{"name": "Set 1", "values": []}]})).unwrap();
|
||||
h.state_mut().sync_views();
|
||||
let ctx = h.ctx.clone();
|
||||
crate::menus::invoke(h.state_mut(), &ctx, "file.export.dataSetsAsFiles", json!({})).unwrap();
|
||||
h.run_steps(4);
|
||||
assert!(field(&h, "dir").is_some(), "the dialog has a Dir field");
|
||||
assert_eq!(field(&h, "naming"), Some(json!("{name}")));
|
||||
assert_eq!(field(&h, "format"), Some(json!("png")));
|
||||
h.get_by_label("Browse…").click();
|
||||
h.run_steps(4);
|
||||
assert!(matches!(asked.borrow().as_slice(), [FileDialogRequest::Folder { .. }]));
|
||||
assert_eq!(field(&h, "dir"), Some(json!("/tmp/dataset-exports")));
|
||||
}
|
||||
@@ -415,6 +415,10 @@ pub(super) fn front(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, para
|
||||
let stem = name.rsplit_once('.').map_or(name.as_str(), |(a, _)| a).to_string();
|
||||
dialog(app, json!({"path": format!("{dir}/{stem}.pdf"), "quality": 10}), json!({}))
|
||||
}
|
||||
"file.export.dataSetsAsFiles" => {
|
||||
let (_, _, _) = doc?;
|
||||
dialog(app, json!({"dir": dir, "naming": "{name}", "format": "png"}), json!({"format": ["png", "jpg", "psd", "tiff", "webp", "bmp"]}))
|
||||
}
|
||||
"layer.new.artboard" => {
|
||||
let (w, h, _) = doc?;
|
||||
let presets: Vec<&str> = std::iter::once("").chain(photocraft_engine::artboard_cmds::PRESETS.iter().map(|p| p.0)).collect();
|
||||
|
||||
@@ -218,6 +218,7 @@ fn wraps(id: &str) -> bool {
|
||||
| "file.export.layerCompsToFiles"
|
||||
| "file.export.artboardsToFiles"
|
||||
| "file.export.artboardsToPdf"
|
||||
| "file.export.dataSetsAsFiles"
|
||||
| "layer.new.artboard"
|
||||
| "file.export.colorLookupTables"
|
||||
| "view.newGuideLayout"
|
||||
|
||||
Reference in new issue
Block a user