diff --git a/crates/ui-egui/src/conditional_mode_change_controls_tests.rs b/crates/ui-egui/src/conditional_mode_change_controls_tests.rs new file mode 100644 index 00000000000..afabd3064fa --- /dev/null +++ b/crates/ui-egui/src/conditional_mode_change_controls_tests.rs @@ -0,0 +1,186 @@ +//! #13676: File › Automate › Conditional Mode Change… ticks any set of source modes, with All and +//! None, and names the modes in full. Drawn with the real dialog window; OK runs the command. + +use craft::ui::dialog::DialogId; +use craft::ui::patterns::form_dialogs::HostedParamDialog; +use egui::accesskit::{Role, Toggled}; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use photocraft_color::ColorMode; +use serde_json::{Value, json}; + +use super::COMMAND; +use crate::PhotocraftApp; +use crate::warp_text_dialog_tests::harness; + +type H = Harness<'static, Box>; + +/// The checkboxes as Photoshop's two columns show them, row by row. +const LEFT: [&str; 4] = ["Bitmap", "Grayscale", "Duotone", "Indexed Color"]; +const RIGHT: [&str; 4] = ["RGB Color", "CMYK Color", "Lab Color", "Multichannel"]; +const ALL_IDS: [&str; 8] = ["bitmap", "grayscale", "duotone", "indexed", "rgb", "cmyk", "lab", "multichannel"]; + +/// An RGB document with the dialog opened from the File menu. +fn opened() -> (H, DialogId) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 40, "height": 20})).unwrap(); + let ctx = app.ctx().clone(); + let reply = crate::menus::invoke(&mut app, &ctx, COMMAND, json!({})).unwrap(); + let want = reply["dialog"].as_str().expect("the reply names a dialog").to_owned(); + let id = app.framework.dialogs.open_dialogs().into_iter().map(|d| d.id).find(|id| id.value().to_string() == want).expect("the dialog is open"); + (harness(app), id) +} + +fn is_open(h: &H, id: DialogId) -> bool { + h.state().framework.dialogs.open_dialogs().iter().any(|d| d.id == id) +} + +fn field(h: &H, id: DialogId, key: &str) -> Value { + h.state() + .framework + .dialogs + .get_by_id::(id) + .and_then(|d| d.fields().get(key).and_then(|v| serde_json::to_value(v).ok())) + .unwrap_or(Value::Null) +} + +fn checked(h: &H, label: &str) -> bool { + h.get_by_role_and_label(Role::CheckBox, label).accesskit_node().toggled() == Some(Toggled::True) +} + +fn click(h: &mut H, role: Role, label: &str) { + h.get_by_role_and_label(role, label).click(); + h.run_steps(3); +} + +fn rect(h: &H, role: Role, label: &str) -> egui::Rect { + h.get_by_role_and_label(role, label).rect() +} + +fn mode(h: &H) -> ColorMode { + h.state().session.active().unwrap().doc.mode +} + +fn steps(h: &H) -> usize { + h.state().session.active().unwrap().history.entries().len() +} + +/// OK as the default button files it; the family runs the command in the passes after it. +fn ok(h: &mut H, id: DialogId) { + crate::dialogs::testing::confirm(h.state_mut(), id).expect("OK is accepted"); + h.run_steps(3); +} + +fn set(h: &mut H, id: DialogId, key: &str, value: Value) { + let ctx = h.ctx.clone(); + h.state_mut().framework.dialogs.set_json_field(&ctx, id, key, value).unwrap_or_else(|e| panic!("{key}: {e}")); + h.run_steps(3); +} + +#[test] +fn source_mode_has_eight_checkboxes_in_two_columns_with_all_and_none_under_them() { + let (h, _) = opened(); + for label in ["Source Mode", "Target Mode", "Mode"] { + assert!(h.query_all_by_label(label).next().is_some(), "{label} in the dialog"); + } + let mut above = f32::MIN; + for (left, right) in LEFT.into_iter().zip(RIGHT) { + let (l, r) = (rect(&h, Role::CheckBox, left), rect(&h, Role::CheckBox, right)); + assert!(l.right() < r.left() && (l.center().y - r.center().y).abs() < 1.0, "{left} and {right} share a row: {l:?} {r:?}"); + assert!(l.top() > above, "{left} is below the row above it"); + above = l.bottom(); + } + let (all, none) = (rect(&h, Role::Button, "All"), rect(&h, Role::Button, "None")); + assert!( + all.top() > above && (all.center().y - none.center().y).abs() < 1.0 && all.right() < none.left(), + "All, None under the checkboxes: {all:?} {none:?}" + ); + assert!( + h.query_all_by_label("Target Mode").map(|n| n.rect().top()).fold(f32::MAX, f32::min) > all.bottom(), + "the Target Mode section is below the Source Mode one" + ); + // Today's default ("any") ticks every source, and none is greyed for an RGB document. + assert!(LEFT.into_iter().chain(RIGHT).all(|l| checked(&h, l))); + assert!(LEFT.into_iter().chain(RIGHT).all(|l| !h.get_by_role_and_label(Role::CheckBox, l).accesskit_node().is_disabled())); + for raw in ["Any", "From", "To"] { + assert!(h.query_all_by_label(raw).next().is_none(), "no raw {raw} control"); + } +} + +#[test] +fn none_clears_and_all_ticks_every_source() { + let (mut h, id) = opened(); + click(&mut h, Role::Button, "None"); + assert!(LEFT.into_iter().chain(RIGHT).all(|l| !checked(&h, l)), "None clears all eight"); + assert_eq!(field(&h, id, "from"), json!([])); + click(&mut h, Role::Button, "All"); + assert!(LEFT.into_iter().chain(RIGHT).all(|l| checked(&h, l)), "All ticks all eight"); + assert_eq!(field(&h, id, "from"), json!(ALL_IDS)); + // Until #13681 adds its alert, OK with nothing ticked converts nothing and closes. + let before = steps(&h); + click(&mut h, Role::Button, "None"); + ok(&mut h, id); + assert!(!is_open(&h, id)); + assert_eq!((mode(&h), steps(&h)), (ColorMode::Rgb, before)); +} + +#[test] +fn the_mode_list_names_the_targets_in_full() { + let rows = super::rows(); + let to = rows.iter().find(|p| p.key == "to").expect("a Mode list"); + let craft::ui::patterns::form_dialogs::ParamKind::Choice { options, default, .. } = &to.kind else { panic!("Mode is a list") }; + let names: Vec<&str> = options.iter().map(|(_, label)| label.as_str()).collect(); + assert_eq!(names, ["Grayscale", "RGB Color", "CMYK Color", "Lab Color"]); + assert_eq!(default.as_deref(), Some("rgb")); + assert_eq!(to.label.as_str(), "Mode"); +} + +#[test] +fn ticked_sources_go_to_the_command_in_the_list_order_and_convert() { + let (mut h, id) = opened(); + click(&mut h, Role::Button, "None"); + for label in ["Lab Color", "RGB Color", "Grayscale"] { + click(&mut h, Role::CheckBox, label); + } + assert_eq!(field(&h, id, "from"), json!(["grayscale", "rgb", "lab"])); + set(&mut h, id, "to", json!("lab")); + ok(&mut h, id); + assert!(!is_open(&h, id)); + assert_eq!(mode(&h), ColorMode::Lab); +} + +#[test] +fn ok_leaves_a_document_whose_mode_is_not_ticked() { + let (mut h, id) = opened(); + let before = steps(&h); + click(&mut h, Role::CheckBox, "RGB Color"); + set(&mut h, id, "to", json!("grayscale")); + ok(&mut h, id); + assert!(!is_open(&h, id)); + assert_eq!((mode(&h), steps(&h)), (ColorMode::Rgb, before), "RGB Color unticked: nothing converts"); +} + +/// `ui.dialog.set` can still give `from` as "any", in a list or as one mode: the boxes show it, and OK passes it on +/// unchanged. +#[test] +fn values_set_over_the_control_channel_show_and_reach_the_command() { + let every: Vec<&str> = LEFT.into_iter().chain(RIGHT).collect(); + for (from, ticked, to, converted) in [ + (json!("any"), every.as_slice(), "grayscale", ColorMode::Grayscale), + (json!(["any"]), every.as_slice(), "rgb", ColorMode::Rgb), + (json!(["lab", "ANY"]), every.as_slice(), "cmyk", ColorMode::Cmyk), + (json!("gray"), &["Grayscale"][..], "cmyk", ColorMode::Rgb), + (json!("rgb"), &["RGB Color"][..], "lab", ColorMode::Lab), + ] { + let (mut h, id) = opened(); + set(&mut h, id, "from", from.clone()); + set(&mut h, id, "to", json!(to)); + for l in &every { + assert_eq!(checked(&h, l), ticked.contains(l), "{from}: {l}"); + } + assert_eq!(field(&h, id, "from"), from, "drawing keeps the value"); + ok(&mut h, id); + assert!(!is_open(&h, id)); + assert_eq!(mode(&h), converted, "{from} to {to}"); + } +} diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index f4de4e5b25d..6455264b37a 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -702,6 +702,7 @@ pub(crate) mod color_picker_preview; pub mod color_picker_ui; pub mod color_range_ui; pub mod color_table_ui; +pub(crate) mod conditional_mode_change_dialog; pub mod count_group_name_ui; pub mod credits; pub mod dds_options_ui; diff --git a/crates/ui-egui/src/dialogs/conditional_mode_change_dialog.rs b/crates/ui-egui/src/dialogs/conditional_mode_change_dialog.rs new file mode 100644 index 00000000000..52d2e840bda --- /dev/null +++ b/crates/ui-egui/src/dialogs/conditional_mode_change_dialog.rs @@ -0,0 +1,101 @@ +//! File › Automate › Conditional Mode Change… (#13676), laid out like Photoshop's dialog: a +//! Source Mode section of eight independent checkboxes in two columns with All and None, and a +//! Target Mode section whose Mode list holds the modes the command converts to. +//! +//! The checkboxes are one custom row of the command's form: `from` is the list of ticked source +//! ids and `to` the target id, so OK, Reset and `ui.dialog.set` work as for every form. + +use craft::ui::buttons::Button; +use craft::ui::choice::Checkbox; +use craft::ui::contract::{Bind, Props as _, Show as _}; +use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, Param, ParamKind}; +use craft::ui::runtime::env::Label; +use serde_json::{Value, json}; + +pub(crate) const COMMAND: &str = "file.automate.conditionalModeChange"; + +/// Photoshop's mode order. The checkboxes fill the left column, then the right one, and the +/// ticked ones go to `from` in this order. +const SOURCES: [(&str, &str); 8] = [ + ("bitmap", "Bitmap"), + ("grayscale", "Grayscale"), + ("duotone", "Duotone"), + ("indexed", "Indexed Color"), + ("rgb", "RGB Color"), + ("cmyk", "CMYK Color"), + ("lab", "Lab Color"), + ("multichannel", "Multichannel"), +]; + +/// The targets the command converts to, in the same order. +const TARGETS: [(&str, &str); 4] = [("grayscale", "Grayscale"), ("rgb", "RGB Color"), ("cmyk", "CMYK Color"), ("lab", "Lab Color")]; + +/// The rows of the dialog: every source ticked ("any") and RGB Color. +pub(crate) fn rows() -> Vec { + let targets = TARGETS.iter().map(|(id, label)| ((*id).to_owned(), Label::from(tl!(label)))).collect(); + vec![ + Param::section(tl!("Source Mode")), + Param::custom("from", Label::Empty, sources_row).opens_with(FieldValue::Text("any".into())).full_width(), + Param::section(tl!("Target Mode")), + Param::new("to", tl!("Mode"), ParamKind::choice(targets, Some("rgb".into()))), + ] +} + +/// A mode id spelled as the command reads it. +fn norm(v: &str) -> String { + let v = v.to_ascii_lowercase(); + if v == "gray" { "grayscale".into() } else { v } +} + +/// `from` as JSON: the opening "any", the list the checkboxes write, or what `ui.dialog.set` gave. +fn from_value(fields: &DialogFields) -> Value { + fields.get("from").and_then(|v| serde_json::to_value(v).ok()).unwrap_or(Value::Null) +} + +/// Is source `id` ticked? `from` takes every form the command does ("any", one mode, a list), +/// and keeps it as it is until a checkbox changes. +fn ticked(from: &Value, id: &str) -> bool { + let is = |v: &str| { + let v = norm(v); + v == "any" || v == id + }; + match from { + Value::Array(a) => a.iter().filter_map(Value::as_str).any(is), + Value::String(v) => is(v), + _ => true, + } +} + +/// The eight checkboxes in two columns, All and None under them. +fn sources_row(ui: &mut egui::Ui, fields: &mut DialogFields, _: &mut craft::ui::patterns::form_dialogs::CustomContext) -> egui::Response { + let from = from_value(fields); + let mut on = SOURCES.map(|(id, _)| ticked(&from, id)); + let before = on; + let response = egui::Grid::new("conditional-mode-sources") + .num_columns(2) + .spacing([24.0, 6.0]) + .show(ui, |ui| { + for row in 0..4 { + for i in [row, row + 4] { + Checkbox::new(Bind::bound(&mut on[i])).label(tl!(SOURCES[i].1)).show(ui); + } + ui.end_row(); + } + for (label, all) in [(tl!("All"), true), (tl!("None"), false)] { + if Button::new(label).show(ui).clicked() { + on = [all; 8]; + } + } + ui.end_row(); + }) + .response; + if on != before { + let ticked: Vec<&str> = SOURCES.iter().zip(on).filter(|(_, on)| *on).map(|((id, _), _)| *id).collect(); + fields.set("from", FieldValue::Json(json!(ticked))); + } + response +} + +#[cfg(test)] +#[path = "../conditional_mode_change_controls_tests.rs"] +mod tests; diff --git a/crates/ui-egui/src/menus/view_cmds/dialogs.rs b/crates/ui-egui/src/menus/view_cmds/dialogs.rs index bbb78ac7aba..d4074eed571 100644 --- a/crates/ui-egui/src/menus/view_cmds/dialogs.rs +++ b/crates/ui-egui/src/menus/view_cmds/dialogs.rs @@ -257,21 +257,7 @@ pub(super) fn params_for(app: &mut PhotocraftApp, id: &str) -> Option<(Vec { - let modes = opts(&[ - ("any", tl!("Any")), - ("rgb", tl!("RGB Color")), - ("grayscale", tl!("Grayscale")), - ("cmyk", tl!("CMYK Color")), - ("lab", tl!("Lab Color")), - ("indexed", tl!("Indexed Color")), - ("bitmap", tl!("Bitmap")), - ("duotone", tl!("Duotone")), - ("multichannel", tl!("Multichannel")), - ]); - let targets = opts(&[("rgb", tl!("RGB Color")), ("grayscale", tl!("Grayscale")), ("cmyk", tl!("CMYK Color")), ("lab", tl!("Lab Color"))]); - plain(vec![choice("from", tl!("From"), modes, "any"), choice("to", tl!("To"), targets, "rgb")]) - } + crate::dialogs::conditional_mode_change_dialog::COMMAND => plain(crate::dialogs::conditional_mode_change_dialog::rows()), "file.export.layersToFiles" => { let (_, _, name) = doc?; plain(vec![ diff --git a/crates/ui-egui/src/warp_text_dialog_tests.rs b/crates/ui-egui/src/warp_text_dialog_tests.rs index 6394530c64c..ea1ff5310e9 100644 --- a/crates/ui-egui/src/warp_text_dialog_tests.rs +++ b/crates/ui-egui/src/warp_text_dialog_tests.rs @@ -41,7 +41,7 @@ fn steps(app: &PhotocraftApp) -> usize { } /// The open dialogs drawn over `app`, as the window shows them. -fn harness(app: Box) -> Harness<'static, Box> { +pub(crate) fn harness(app: Box) -> Harness<'static, Box> { let h = Harness::builder().with_context(app.ctx().clone()).with_size(vec2(900.0, 700.0)).build_ui_state( |ui, app: &mut Box| { if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {