diff --git a/crates/engine/src/cmd/variables_cmds.rs b/crates/engine/src/cmd/variables_cmds.rs index 9c89c4756ed..c56ee0f6c04 100644 --- a/crates/engine/src/cmd/variables_cmds.rs +++ b/crates/engine/src/cmd/variables_cmds.rs @@ -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) -> 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 { @@ -511,6 +522,7 @@ pub fn specs() -> Vec { "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 { "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 { diff --git a/crates/engine/src/cmd/variables_cmds/tests.rs b/crates/engine/src/cmd/variables_cmds/tests.rs index ec5b728e71d..1f7fa5cb12c 100644 --- a/crates/engine/src/cmd/variables_cmds/tests.rs +++ b/crates/engine/src/cmd/variables_cmds/tests.rs @@ -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")); +} diff --git a/crates/ui-egui/src/browse_field_tests.rs b/crates/ui-egui/src/browse_field_tests.rs index 09debf418b7..c42b296a95b 100644 --- a/crates/ui-egui/src/browse_field_tests.rs +++ b/crates/ui-egui/src/browse_field_tests.rs @@ -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"))); +} diff --git a/crates/ui-egui/src/menus/view_cmds/dialogs.rs b/crates/ui-egui/src/menus/view_cmds/dialogs.rs index f4bcd875420..002b3f4ee9f 100644 --- a/crates/ui-egui/src/menus/view_cmds/dialogs.rs +++ b/crates/ui-egui/src/menus/view_cmds/dialogs.rs @@ -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(); diff --git a/crates/ui-egui/src/menus/view_cmds/dispatch.rs b/crates/ui-egui/src/menus/view_cmds/dispatch.rs index 4a9884c6c0b..7e964997dd5 100644 --- a/crates/ui-egui/src/menus/view_cmds/dispatch.rs +++ b/crates/ui-egui/src/menus/view_cmds/dispatch.rs @@ -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"