mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge #14788 into the merge queue batch
fix(ui-egui): require at least two files in Load Files into Stack dialog (#14000)
This commit is contained in:
commit
904683cf40
2 files changed
+141
-7
No files matched your search
@@ -46,19 +46,34 @@ fn open_for(app: &mut PhotocraftApp, command: &'static str, title: &str, statist
|
||||
}
|
||||
let title = Label::from(title.to_string());
|
||||
params::open_param(app, command, DialogFields::new(), Value::Null, move |fields: &mut DialogFields| {
|
||||
let ready = paths(fields).is_some();
|
||||
ParamDialog::command(command, title.clone(), rows.clone()).values(Bind::bound(fields)).ok_enabled(ready)
|
||||
let ready = paths(fields, command).is_some();
|
||||
let mut rows = rows.clone();
|
||||
// Load Files into Stack's Align needs files to align: greyed (its tick kept) below two in Files mode (#14000).
|
||||
if command == COMMAND
|
||||
&& !ready
|
||||
&& !fields.bool("files.useFolder").unwrap_or(false)
|
||||
&& let Some(align) = rows.iter_mut().find(|row| row.key == "align")
|
||||
{
|
||||
*align = align.clone().disabled();
|
||||
}
|
||||
ParamDialog::command(command, title.clone(), rows).values(Bind::bound(fields)).ok_enabled(ready)
|
||||
})
|
||||
}
|
||||
|
||||
/// How many files a Files-mode list needs before OK takes it: Load Files into Stack stacks at least two (#14000),
|
||||
/// Statistics takes one.
|
||||
fn min_files(command: &str) -> usize {
|
||||
if command == STATISTICS { 1 } else { 2 }
|
||||
}
|
||||
|
||||
/// The open documents that have a file, in tab order (what Add Open Files adds).
|
||||
fn open_files(app: &PhotocraftApp) -> Vec<PathBuf> {
|
||||
app.session.documents().iter().filter_map(|d| d.path.clone()).map(PathBuf::from).collect()
|
||||
}
|
||||
|
||||
/// What OK loads: the folder in Folder mode, else the listed files. `None` while there is nothing to load, which is
|
||||
/// also when OK stays disabled.
|
||||
fn paths(fields: &DialogFields) -> Option<Value> {
|
||||
/// What OK of `command` loads: the folder in Folder mode, else the listed files. `None` while there is nothing to load
|
||||
/// (or too few files, [`min_files`]), which is also when OK stays disabled.
|
||||
fn paths(fields: &DialogFields, command: &str) -> Option<Value> {
|
||||
if fields.bool("files.useFolder").unwrap_or(false) {
|
||||
let folder = fields.text("files.folder").filter(|f| !f.is_empty())?;
|
||||
return Some(json!(folder));
|
||||
@@ -72,12 +87,12 @@ fn paths(fields: &DialogFields) -> Option<Value> {
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
(!list.is_empty()).then(|| json!(list))
|
||||
(list.len() >= min_files(command)).then(|| json!(list))
|
||||
}
|
||||
|
||||
/// OK: loads the list (or the folder) into a new document.
|
||||
pub(crate) fn run(app: &mut PhotocraftApp, _ctx: &egui::Context, result: ParamResult) {
|
||||
let Some(paths) = paths(&result.fields) else {
|
||||
let Some(paths) = paths(&result.fields, &result.key) else {
|
||||
app.ui.status = tl!("Add the files to load").to_string();
|
||||
app.ui.status_error = true;
|
||||
return;
|
||||
@@ -106,3 +121,5 @@ pub(crate) fn listing(_app: &PhotocraftApp, folder: &Path) -> Vec<PathBuf> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
#[cfg(test)]
|
||||
mod two_files_tests;
|
||||
@@ -0,0 +1,117 @@
|
||||
//! #14000: in Load Files into Stack (Files mode), OK and Attempt to Automatically Align Source
|
||||
//! Images require at least two files in the list. With 0 or 1 file both are greyed; from 2 on both
|
||||
//! are enabled. The align checkbox keeps its ticked state while greyed and shows again once a second
|
||||
//! file is added. Create Smart Object stays enabled at every row count. Use: Folder and Statistics
|
||||
//! keep their existing rules.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft::host::ScriptedFiles;
|
||||
use craft::ui::dialog::DialogId;
|
||||
use craft::ui::patterns::form_dialogs::HostedParamDialog;
|
||||
use egui::vec2;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
|
||||
const ALIGN: &str = "Attempt to Automatically Align Source Images";
|
||||
const SMART: &str = "Create Smart Object after Loading Layers";
|
||||
|
||||
fn pngs(tag: &str, names: &[&str]) -> (String, Vec<String>) {
|
||||
let dir = std::env::temp_dir().join(format!("photocraft-two-files-{tag}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let mut s = photocraft_engine::Session::new();
|
||||
s.execute("file.new", json!({"width": 8, "height": 6, "background": "#406080"})).unwrap();
|
||||
let paths = names
|
||||
.iter()
|
||||
.map(|n| {
|
||||
let p = dir.join(n).to_string_lossy().into_owned();
|
||||
s.execute("file.saveACopy", json!({"path": p})).unwrap();
|
||||
p
|
||||
})
|
||||
.collect();
|
||||
(dir.to_string_lossy().into_owned(), paths)
|
||||
}
|
||||
|
||||
fn harness() -> Harness<'static, Box<PhotocraftApp>> {
|
||||
let files: Arc<dyn craft::host::Files> = Arc::new(ScriptedFiles::answering(vec![]));
|
||||
Harness::builder().with_size(vec2(1440.0, 900.0)).with_max_steps(64).build_eframe(move |cc| {
|
||||
PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
|
||||
PhotocraftApp::with_context(cc.egui_ctx.clone(), photocraft_engine::Session::new(), crate::Services { files: Some(files), ..Default::default() })
|
||||
})
|
||||
}
|
||||
|
||||
fn fields<'a>(h: &'a Harness<'_, Box<PhotocraftApp>>, id: DialogId) -> &'a DialogFields {
|
||||
h.state().framework.dialogs.get_by_id::<HostedParamDialog>(id).expect("the dialog is open").fields()
|
||||
}
|
||||
|
||||
/// Puts `files` in the Files row (with Use: Files or Folder and `folder`) and lets the dialog redraw.
|
||||
fn source(h: &mut Harness<'_, Box<PhotocraftApp>>, id: DialogId, files: &[String], folder: Option<&str>) {
|
||||
let ctx = h.ctx.clone();
|
||||
let value = json!({"files": files, "folder": folder.unwrap_or(""), "useFolder": folder.is_some()});
|
||||
h.state_mut().framework.dialogs.set_json_field(&ctx, id, "files", value).unwrap();
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
fn disabled(h: &Harness<'_, Box<PhotocraftApp>>, label: &str) -> bool {
|
||||
h.get_by_label(label).accesskit_node().is_disabled()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_files_into_stack_greys_ok_and_align_until_two_files() {
|
||||
let (_, paths) = pngs("ui", &["a.png", "b.png", "c.png"]);
|
||||
let mut h = harness();
|
||||
let id = open(h.state_mut());
|
||||
h.run_steps(3);
|
||||
for n in [0, 1] {
|
||||
source(&mut h, id, &paths[..n], None);
|
||||
assert!(disabled(&h, "OK"), "OK disabled with {n} files");
|
||||
assert!(disabled(&h, ALIGN), "Align disabled with {n} files");
|
||||
assert!(!disabled(&h, SMART), "Smart Object enabled with {n} files");
|
||||
}
|
||||
for n in [2, 3] {
|
||||
source(&mut h, id, &paths[..n], None);
|
||||
assert!(!disabled(&h, "OK"), "OK enabled with {n} files");
|
||||
assert!(!disabled(&h, ALIGN), "Align enabled with {n} files");
|
||||
assert!(!disabled(&h, SMART), "Smart Object enabled with {n} files");
|
||||
}
|
||||
h.get_by_label(ALIGN).click();
|
||||
h.run_steps(2);
|
||||
assert_eq!(fields(&h, id).bool("align"), Some(true), "Align ticked");
|
||||
// Back to one file: greyed, the tick kept; a second file brings it back ticked.
|
||||
source(&mut h, id, &paths[..1], None);
|
||||
assert!(disabled(&h, "OK") && disabled(&h, ALIGN));
|
||||
assert_eq!(fields(&h, id).bool("align"), Some(true), "the tick is kept while greyed");
|
||||
source(&mut h, id, &paths[..2], None);
|
||||
assert!(!disabled(&h, ALIGN));
|
||||
assert_eq!(fields(&h, id).bool("align"), Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folder_mode_and_statistics_keep_their_rules() {
|
||||
let (dir, paths) = pngs("rules", &["a.png"]);
|
||||
let mut h = harness();
|
||||
let id = open(h.state_mut());
|
||||
h.run_steps(3);
|
||||
// Use: Folder needs a chosen folder; Align takes input either way.
|
||||
source(&mut h, id, &[], Some(""));
|
||||
assert!(disabled(&h, "OK"), "no folder chosen");
|
||||
assert!(!disabled(&h, ALIGN));
|
||||
source(&mut h, id, &[], Some(&dir));
|
||||
assert!(!disabled(&h, "OK"), "a chosen folder confirms");
|
||||
assert!(!disabled(&h, ALIGN));
|
||||
|
||||
// Statistics confirms one file, and the file count never greys its Align.
|
||||
let mut h = harness();
|
||||
let id = open_statistics(h.state_mut());
|
||||
h.run_steps(3);
|
||||
source(&mut h, id, &[], None);
|
||||
assert!(disabled(&h, "OK"), "Statistics with no file");
|
||||
assert!(!disabled(&h, ALIGN));
|
||||
source(&mut h, id, &paths[..1], None);
|
||||
assert!(!disabled(&h, "OK"), "Statistics with one file");
|
||||
assert!(!disabled(&h, ALIGN));
|
||||
}
|
||||
Reference in new issue
Block a user