mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Fill dialog in Quick Mask greys what the channel can't take
In Quick Mask, Fill lists the fill from surroundings and History greyed in Contents, Preserve Transparency greyed off, and Darker Color, Lighter Color, Hue, Saturation, Color and Luminosity (and Behind and Clear once listed) greyed in Mode. A remembered Mode that is greyed opens on Normal and stays remembered for fills elsewhere. Out of Quick Mask the dialog is unchanged. Fixes #17488.
This commit is contained in:
1 parent
6517a37a30
commit
f738fa4345
2 files changed
+163
-5
No files matched your search
@@ -0,0 +1,101 @@
|
||||
//! #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<PhotocraftApp> {
|
||||
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::<HostedParamDialog>(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 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 { .. }));
|
||||
}
|
||||
@@ -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};
|
||||
|
||||
@@ -40,12 +40,38 @@ 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<Item = String> {
|
||||
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<String>, preserve: Option<bool>) -> Vec<Param> {
|
||||
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("foreground".into())));
|
||||
// Content-Aware fills a selection from around it: without one it is listed greyed, as in the reference (#17462).
|
||||
if app.session.active().is_none_or(|st| st.doc.selection.is_none()) {
|
||||
// On the Quick Mask channel the fill from surroundings and History are greyed too (#17488).
|
||||
let no_selection = app.session.active().is_none_or(|st| st.doc.selection.is_none());
|
||||
if quick_mask {
|
||||
contents = contents.disabled_options(["contentAware", "history"]);
|
||||
} else if no_selection {
|
||||
contents = contents.disabled_options(["contentAware"]);
|
||||
}
|
||||
let mut rows = vec![
|
||||
@@ -55,6 +81,14 @@ fn rows(app: &PhotocraftApp, foreground: [f32; 3], first_pattern: Option<String>
|
||||
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<String> =
|
||||
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
|
||||
}
|
||||
|
||||
@@ -77,11 +111,11 @@ fn defaults(foreground: [f32; 3], first_pattern: Option<String>, 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);
|
||||
@@ -161,7 +195,12 @@ pub(crate) fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamRes
|
||||
let captured = captured(app, &result.key);
|
||||
match app.run(COMMAND, params(&result.fields, &captured)) {
|
||||
Ok(_) => {
|
||||
// A Mode remembered before that the Quick Mask channel greys is kept for the next fill elsewhere (#17488).
|
||||
let kept_mode = (captured.get("target").and_then(Value::as_str) == Some("quickMask")).then(|| remembered_mode(ctx, &result.key)).flatten();
|
||||
remember_blending(ctx, &result);
|
||||
if let Some(mode) = kept_mode.filter(|mode| quick_mask_greyed_keys().any(|k| &k == mode)) {
|
||||
keep_mode(ctx, &result.key, mode);
|
||||
}
|
||||
// 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")) {
|
||||
@@ -173,9 +212,27 @@ pub(crate) fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamRes
|
||||
}
|
||||
}
|
||||
|
||||
/// The Mode the dialog `key` remembers.
|
||||
fn remembered_mode(ctx: &egui::Context, key: &str) -> Option<String> {
|
||||
let saved = saved_value::<Option<DialogFields>>(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::<Option<DialogFields>>(ctx, &save) {
|
||||
saved.set("mode", mode);
|
||||
set_saved_value(ctx, &save, Some(saved));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod content_aware_greyed_tests;
|
||||
#[cfg(test)]
|
||||
#[path = "fill_quick_mask_greyed_tests.rs"]
|
||||
mod fill_quick_mask_greyed_tests;
|
||||
#[cfg(test)]
|
||||
mod opacity_alert_tests;
|
||||
#[cfg(test)]
|
||||
mod preserve_transparency_state_tests;
|
||||
|
||||
Reference in new issue
Block a user