diff --git a/crates/ui-egui/src/dialogs/fill_quick_mask_greyed_tests.rs b/crates/ui-egui/src/dialogs/fill_quick_mask_greyed_tests.rs new file mode 100644 index 0000000000..72a9746407 --- /dev/null +++ b/crates/ui-egui/src/dialogs/fill_quick_mask_greyed_tests.rs @@ -0,0 +1,120 @@ +//! #17488: in Quick Mask the Fill dialog greys what doesn't work on the channel: the fill from surroundings and History +//! in Contents, Preserve Transparency (off), and Darker Color, Lighter Color, Hue, Saturation, Color and Luminosity in +//! Mode. A remembered choice that is greyed opens on an enabled one and stays remembered; out of Quick Mask the dialog +//! is as before. + +use craft::ui::patterns::form_dialogs::HostedParamDialog; +use photocraft_color::BlendMode; +use serde_json::json; + +use super::*; +use crate::smart_filter_dialog::tests::{cancel, confirm, set}; + +/// A white 40×30 RGB document with a pixel layer "Paint" active and a selection. +fn app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 40, "height": 30, "background": "white"})).unwrap(); + app.run("layer.new.layer", json!({"name": "Paint"})).unwrap(); + app.run("select.rect", json!({"x": 5, "y": 5, "width": 20, "height": 15})).unwrap(); + app +} + +fn quick_mask_on(app: &mut PhotocraftApp, on: bool) { + app.run("select.editInQuickMaskMode", json!({"on": on})).unwrap(); +} + +fn row(app: &PhotocraftApp, key: &str) -> Param { + rows(app, [0.0; 3], None, None).into_iter().find(|r| r.key == key).unwrap_or_else(|| panic!("a {key} row")) +} + +/// A field of the open dialog `id`, and whether it takes `value`. +fn field(app: &mut PhotocraftApp, id: DialogId, key: &str) -> serde_json::Value { + let dialog = app.framework.dialogs.get_by_id::(id).expect("a hosted dialog"); + let fields = dialog.fields(); + fields.text(key).map(|t| json!(t)).or_else(|| fields.bool(key).map(|b| json!(b))).unwrap_or(serde_json::Value::Null) +} + +fn takes(app: &mut PhotocraftApp, id: DialogId, key: &str, value: serde_json::Value) -> bool { + let ctx = app.ctx().clone(); + app.framework.dialogs.set_json_field(&ctx, id, key, value).is_ok() +} + +const CHANNEL_GREYED: [BlendMode; 6] = + [BlendMode::DarkerColor, BlendMode::LighterColor, BlendMode::Hue, BlendMode::Saturation, BlendMode::Color, BlendMode::Luminosity]; + +#[test] +fn quick_mask_greys_contents_mode_and_preserve_transparency() { + let mut app = app(); + quick_mask_on(&mut app, true); + let contents = row(&app, "contents"); + for (key, _) in CONTENTS { + assert_eq!(contents.option_enabled(key), !matches!(key, "contentAware" | "history"), "Contents {key}"); + } + let mode = row(&app, "mode"); + for m in BlendMode::LAYER_MODES { + assert_eq!(mode.option_enabled(m.key()), !CHANNEL_GREYED.contains(&m), "Mode {}", m.label()); + } + assert!(!mode.option_enabled("behind") && !mode.option_enabled("clear"), "Behind and Clear greyed once listed"); + + let id = open(&mut app); + assert_eq!(field(&mut app, id, "preserveTransparency"), json!(false), "Preserve Transparency unchecked"); + assert!(!takes(&mut app, id, "preserveTransparency", json!(true)), "Preserve Transparency greyed"); + assert!(!takes(&mut app, id, "mode", json!("hue")), "Hue greyed"); + assert!(!takes(&mut app, id, "contents", json!("history")), "History greyed"); + assert!(takes(&mut app, id, "mode", json!("multiply")), "Multiply enabled"); + assert!(takes(&mut app, id, "contents", json!("pattern")), "Pattern enabled"); + cancel(&mut app, id); +} + +#[test] +fn greyed_remembered_choices_open_enabled_and_stay_remembered() { + let mut app = app(); + let id = open(&mut app); + set(&mut app, id, "mode", json!("hue")); + set(&mut app, id, "preserveTransparency", json!(true)); + set(&mut app, id, "contents", json!("black")); + confirm(&mut app, id); + + quick_mask_on(&mut app, true); + let id = open(&mut app); + assert_eq!(field(&mut app, id, "mode"), json!("normal"), "a greyed remembered Mode opens on Normal"); + assert_eq!(field(&mut app, id, "preserveTransparency"), json!(false), "and Preserve Transparency unchecked"); + set(&mut app, id, "contents", json!("white")); + confirm(&mut app, id); + quick_mask_on(&mut app, false); + + let id = open(&mut app); + assert_eq!(field(&mut app, id, "mode"), json!("hue"), "the remembered Mode is kept"); + assert_eq!(field(&mut app, id, "preserveTransparency"), json!(true), "the remembered Preserve Transparency is kept"); + cancel(&mut app, id); +} + +#[test] +fn a_remembered_mode_the_document_greys_in_quick_mask_stays_remembered() { + let mut app = app(); + let id = open(&mut app); + set(&mut app, id, "mode", json!("overlay")); + confirm(&mut app, id); + + // RGB 32 bit greys Overlay by its depth, not by the channel. + app.run("file.new", json!({"width": 40, "height": 30, "background": "white", "depth": 32})).unwrap(); + app.run("select.rect", json!({"x": 5, "y": 5, "width": 20, "height": 15})).unwrap(); + quick_mask_on(&mut app, true); + let id = open(&mut app); + assert_eq!(field(&mut app, id, "mode"), json!("normal"), "a greyed remembered Mode opens on Normal"); + set(&mut app, id, "contents", json!("white")); + confirm(&mut app, id); + let ctx = app.ctx().clone(); + assert_eq!(remembered_mode(&ctx, COMMAND).as_deref(), Some("overlay"), "OK in Quick Mask keeps the remembered Mode"); +} + +#[test] +fn out_of_quick_mask_the_dialog_is_unchanged() { + let app = app(); + let contents = row(&app, "contents"); + assert!(CONTENTS.iter().all(|(key, _)| contents.option_enabled(key)), "every Contents entry enabled with a selection"); + let mode = row(&app, "mode"); + assert!(BlendMode::LAYER_MODES.iter().all(|m| mode.option_enabled(m.key())), "every Mode enabled in RGB 8 bit"); + let pt = row(&app, "preserveTransparency"); + assert!(matches!(pt.kind, ParamKind::Bool { .. })); +} diff --git a/crates/ui-egui/src/dialogs/fill_ui.rs b/crates/ui-egui/src/dialogs/fill_ui.rs index 40f8dea2d4..d34c2ce694 100644 --- a/crates/ui-egui/src/dialogs/fill_ui.rs +++ b/crates/ui-egui/src/dialogs/fill_ui.rs @@ -8,7 +8,7 @@ use craft::ui::contract::Bind; use craft::ui::dialog::DialogId; use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, Param, ParamDialog, ParamKind, ParamResult}; use craft::ui::runtime::env::Label; -use craft::ui::runtime::memory::{MemoryStage, SaveKey, set_saved_value}; +use craft::ui::runtime::memory::{MemoryStage, SaveKey, saved_value, set_saved_value}; use photocraft_color::BlendMode; use serde_json::{Value, json}; @@ -57,14 +57,39 @@ pub(crate) fn hex(c: [f32; 3]) -> String { format!("#{:02x}{:02x}{:02x}", b(c[0]), b(c[1]), b(c[2])) } +/// The blend modes Fill greys on the Quick Mask channel (#17488): the colour-only modes, which a channel can't take, +/// and Behind and Clear. +const QUICK_MASK_GREYED_MODES: [BlendMode; 6] = + [BlendMode::DarkerColor, BlendMode::LighterColor, BlendMode::Hue, BlendMode::Saturation, BlendMode::Color, BlendMode::Luminosity]; + +/// The target Fill edits: a layer mask or channel the panels target, else the paint target. +fn fill_target(app: &PhotocraftApp) -> Value { + let target = app.with_mask_target(COMMAND, json!({})); + target.get("target").cloned().unwrap_or_else(|| crate::canvas::paint_target(app)) +} + +/// Whether Fill opens on the Quick Mask channel. +fn quick_mask(app: &PhotocraftApp) -> bool { + fill_target(app).as_str() == Some("quickMask") +} + +/// The keys of the Mode entries greyed on the Quick Mask channel. +fn quick_mask_greyed_keys() -> impl Iterator { + QUICK_MASK_GREYED_MODES.iter().map(|m| m.key().to_string()).chain(["behind".to_string(), "clear".to_string()]) +} + /// The dialog's rows, with the Preserve Transparency the target forces. fn rows(app: &PhotocraftApp, foreground: [f32; 3], first_pattern: Option, preserve: Option) -> Vec { + let quick_mask = quick_mask(app); let contents = CONTENTS.iter().map(|(k, l)| (k.to_string(), Label::from(tl!(l)))).collect(); let mut contents = Param::new("contents", tl!("Contents:"), ParamKind::choice(contents, Some(opening_contents(app).into()))); // Content-Aware fills a selection from around it: without one it is listed greyed, as in the reference (#17462). - // History is greyed whenever `edit.fill` would refuse its source, by the engine's own check (#17464). - let greyed = - [("contentAware", !has_selection(app)), ("history", app.session.active().is_none_or(|st| !photocraft_engine::history_source::fill_source_usable(st)))]; + // History is greyed whenever `edit.fill` would refuse its source, by the engine's own check (#17464). On the Quick + // Mask channel the fill from surroundings and History are greyed too (#17488). + let greyed = [ + ("contentAware", quick_mask || !has_selection(app)), + ("history", quick_mask || app.session.active().is_none_or(|st| !photocraft_engine::history_source::fill_source_usable(st))), + ]; contents = contents.disabled_options(greyed.into_iter().filter(|(_, off)| *off).map(|(key, _)| key)); let mut rows = vec![ contents, @@ -80,6 +105,14 @@ fn rows(app: &PhotocraftApp, foreground: [f32; 3], first_pattern: Option Param::new("colorAdaptation", tl!("Color Adaptation"), ParamKind::bool(true)).visible_when("contents", "contentAware"), ]; rows.extend(blending_rows(app, preserve)); + if quick_mask { + // The modes the document greys stay greyed; the channel adds its own (#17488). + for row in rows.iter_mut().filter(|row| row.key == "mode") { + let greyed: Vec = + BlendMode::LAYER_MODES.iter().map(|m| m.key().to_string()).filter(|k| !row.option_enabled(k)).chain(quick_mask_greyed_keys()).collect(); + *row = row.clone().disabled_options(greyed); + } + } rows } @@ -102,11 +135,11 @@ fn defaults(foreground: [f32; 3], first_pattern: Option, inks: Option<[f /// Open Fill with the current editing target captured, so live UI changes cannot redirect confirmation. pub fn open(app: &mut PhotocraftApp) -> DialogId { - let target = app.with_mask_target(COMMAND, json!({})); - let target = target.get("target").cloned().unwrap_or_else(|| crate::canvas::paint_target(app)); + let target = fill_target(app); // Alpha and Quick Mask belong to the document, independently of its active layer. let layer_bound = target.as_str() != Some("quickMask") && target.get("channel").is_none(); - let preserve = forced_preserve_transparency(app, &target); + // Preserve Transparency has no meaning on the Quick Mask channel: greyed off there (#17488). + let preserve = if target.as_str() == Some("quickMask") { Some(false) } else { forced_preserve_transparency(app, &target) }; let mut held = json!({ "target": target }); if let Some(st) = app.session.active() { held["document"] = json!(st.doc.id.0); @@ -188,18 +221,25 @@ pub(crate) fn check(app: &mut PhotocraftApp, result: &ParamResult) -> Result<(), /// Fill with the confirmed choices; they are remembered only when the fill succeeded. pub(crate) fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamResult) { let captured = captured(app, &result.key); + // The Mode entries the dialog greyed on the Quick Mask channel: the channel's and the document's. + let greyed: Vec = if captured.get("target").and_then(Value::as_str) == Some("quickMask") { + let document = app.session.active().map_or(&[][..], |st| photocraft_engine::fill_cmds::greyed_modes(st.doc.mode, st.doc.depth)); + quick_mask_greyed_keys().chain(document.iter().map(|m| m.key().to_string())).collect() + } else { + Vec::new() + }; let p = params(&result.fields, &captured); // With Script on, OK opens the script's dialog, whose OK fills; Cancel there fills nothing. The choice of script is // remembered either way, as the reference application reopens on the last script. if let Some(script) = p.get("script").and_then(Value::as_str).map(str::to_owned) { - remember_blending(ctx, &result); + remember_choices(ctx, &result, &greyed); if super::fill_script_ui::open(app, &script, p.clone(), &captured).is_some() { return; } } match app.run(COMMAND, p) { Ok(_) => { - remember_blending(ctx, &result); + remember_choices(ctx, &result, &greyed); // The framework remembers visible parameter rows only; keep their ink companion in the same saved tier. let mut inks = DialogFields::new(); if let Some(value) = result.fields.get(&crate::color_picker_ui::ink_field("color")) { @@ -211,12 +251,40 @@ pub(crate) fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamRes } } +/// Remembers the choices of `result`. A Mode remembered before that the Quick Mask dialog `greyed` is kept for the +/// next fill elsewhere (#17488). +fn remember_choices(ctx: &egui::Context, result: &ParamResult, greyed: &[String]) { + let kept_mode = (!greyed.is_empty()).then(|| remembered_mode(ctx, &result.key)).flatten(); + remember_blending(ctx, result); + if let Some(mode) = kept_mode.filter(|mode| greyed.contains(mode)) { + keep_mode(ctx, &result.key, mode); + } +} + +/// The Mode the dialog `key` remembers. +fn remembered_mode(ctx: &egui::Context, key: &str) -> Option { + let saved = saved_value::>(ctx, &SaveKey::named(super::params::memory_scope(key)))?; + saved.text("mode").map(str::to_owned) +} + +/// Puts `mode` back as the Mode the dialog `key` remembers. +fn keep_mode(ctx: &egui::Context, key: &str, mode: String) { + let save = SaveKey::named(super::params::memory_scope(key)); + if let Some(mut saved) = saved_value::>(ctx, &save) { + saved.set("mode", mode); + set_saved_value(ctx, &save, Some(saved)); + } +} + #[cfg(test)] mod content_aware_greyed_tests; #[cfg(test)] #[path = "fill_history_greyed_tests.rs"] mod fill_history_greyed_tests; #[cfg(test)] +#[path = "fill_quick_mask_greyed_tests.rs"] +mod fill_quick_mask_greyed_tests; +#[cfg(test)] mod no_selection_fallback_tests; #[cfg(test)] mod opacity_alert_tests;