mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge #16401 into the merge queue batch
Fill and Stroke: Preserve Transparency greyed off on the Background, greyed on with locked transparency (#14651)
This commit is contained in:
commit
549f77ed24
7 files changed
+131
-31
No files matched your search
@@ -2,13 +2,16 @@
|
||||
//! Layers panel's, so a mode has one key and one shortcut everywhere. Fill, Stroke and Fade grey the modes the active
|
||||
//! document's colour mode and depth can't use (#13806, #14647).
|
||||
|
||||
use craft::ui::patterns::form_dialogs::{DialogFields, Param, ParamKind};
|
||||
use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, Param, ParamKind, ParamResult};
|
||||
use craft::ui::runtime::env::Label;
|
||||
use craft::ui::runtime::memory::{SaveKey, saved_value, set_saved_value};
|
||||
use photocraft_color::BlendMode;
|
||||
use serde_json::{Map, Value, json};
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
|
||||
const PRESERVE: &str = "preserveTransparency";
|
||||
|
||||
/// A blend-mode row `key` over the Layers panel's mode list (`group` adds Pass Through), opening on `current`.
|
||||
pub(crate) fn blend_param(key: &str, label: impl Into<Label>, group: bool, current: BlendMode) -> Param {
|
||||
let modes = crate::panels::blend_options(group, current)
|
||||
@@ -31,14 +34,46 @@ pub(crate) fn blend_mode(fields: &DialogFields, key: &str) -> BlendMode {
|
||||
fields.text(key).and_then(photocraft_engine::commands::parse_blend).unwrap_or(BlendMode::Normal)
|
||||
}
|
||||
|
||||
/// The Blending section, Mode, Opacity (percent) and Preserve Transparency: the rows Fill and Stroke share.
|
||||
pub(crate) fn blending_rows(app: &PhotocraftApp) -> Vec<Param> {
|
||||
vec![
|
||||
Param::section(tl!("Blending")),
|
||||
document_mode_param(app, tl!("Mode:")),
|
||||
super::new_layer_ui::opacity_param(),
|
||||
Param::new("preserveTransparency", tl!("Preserve Transparency"), ParamKind::bool(false)),
|
||||
]
|
||||
/// The Blending section, Mode, Opacity (percent) and Preserve Transparency: the rows Fill and Stroke share. A
|
||||
/// `forced` Preserve Transparency ([`forced_preserve_transparency`]) shows greyed at that state and is not remembered.
|
||||
pub(crate) fn blending_rows(app: &PhotocraftApp, forced: Option<bool>) -> Vec<Param> {
|
||||
let mut preserve = Param::new(PRESERVE, tl!("Preserve Transparency"), ParamKind::bool(forced.unwrap_or(false)));
|
||||
if forced.is_some() {
|
||||
// No value enables it, so neither the user nor automation can change the target's state.
|
||||
preserve = preserve.enabled_when_in(PRESERVE, Vec::<FieldValue>::new()).not_remembered();
|
||||
}
|
||||
vec![Param::section(tl!("Blending")), document_mode_param(app, tl!("Mode:")), super::new_layer_ui::opacity_param(), preserve]
|
||||
}
|
||||
|
||||
/// The Preserve Transparency that Fill's or Stroke's `target` (as `edit.fill` takes it) forces: off on the Background
|
||||
/// layer or other pixels without transparency, on with Lock Transparent Pixels. `None` for other layers, masks and
|
||||
/// channels, where the box offers the remembered choice.
|
||||
pub(crate) fn forced_preserve_transparency(app: &PhotocraftApp, target: &Value) -> Option<bool> {
|
||||
let st = app.session.active()?;
|
||||
let id = st.active_layer?;
|
||||
if photocraft_engine::channel_cmds::gray_target(&json!({ "target": target })).is_some() {
|
||||
return None;
|
||||
}
|
||||
// The Background also carries the transparency lock, so it is tested first.
|
||||
if st.doc.is_background(id) || st.doc.layer(id)?.surface().is_some_and(|s| !s.format().alpha) {
|
||||
return Some(false);
|
||||
}
|
||||
st.doc.effective_locks(id).transparency.then_some(true)
|
||||
}
|
||||
|
||||
/// Remembers the choices of `result`. A forced Preserve Transparency is not among them, so the choice remembered
|
||||
/// before is kept for the next open.
|
||||
pub(crate) fn remember_blending(ctx: &egui::Context, result: &ParamResult) {
|
||||
let key = SaveKey::named(super::params::memory_scope(&result.key));
|
||||
let before = saved_value::<Option<DialogFields>>(ctx, &key).and_then(|saved| saved.get(PRESERVE).cloned());
|
||||
result.remember(ctx);
|
||||
if let Some(before) = before
|
||||
&& let Some(mut saved) = saved_value::<Option<DialogFields>>(ctx, &key)
|
||||
&& !saved.contains(PRESERVE)
|
||||
{
|
||||
saved.set(PRESERVE, before);
|
||||
set_saved_value(ctx, &key, Some(saved));
|
||||
}
|
||||
}
|
||||
|
||||
/// The engine params of [`blending_rows`].
|
||||
|
||||
@@ -16,7 +16,7 @@ fn app(mode: &str, depth: u32) -> Box<PhotocraftApp> {
|
||||
|
||||
/// The labels of the Mode row's disabled entries in the Fill and Stroke rows of `app`.
|
||||
fn greyed(app: &PhotocraftApp) -> Vec<&'static str> {
|
||||
let rows = blending_rows(app);
|
||||
let rows = blending_rows(app, None);
|
||||
let row = rows.iter().find(|p| p.key == "mode").unwrap();
|
||||
let ParamKind::Blend { modes, .. } = &row.kind else { panic!("Mode is a blend row") };
|
||||
assert_eq!(modes.len(), BlendMode::LAYER_MODES.len(), "every mode stays listed");
|
||||
@@ -67,8 +67,8 @@ fn without_a_document_nothing_is_greyed() {
|
||||
fn a_remembered_greyed_mode_opens_as_normal() {
|
||||
let mut saved = DialogFields::new();
|
||||
saved.set("mode", BlendMode::Darken.key().to_string());
|
||||
let lab = ParamDialog::remembered(&blending_rows(&app("lab", 8)), &saved);
|
||||
let lab = ParamDialog::remembered(&blending_rows(&app("lab", 8), None), &saved);
|
||||
assert_eq!(lab.text("mode"), None, "Darken is greyed in Lab 8-bit");
|
||||
let rgb = ParamDialog::remembered(&blending_rows(&app("rgb", 16)), &saved);
|
||||
let rgb = ParamDialog::remembered(&blending_rows(&app("rgb", 16), None), &saved);
|
||||
assert_eq!(rgb.text("mode"), Some(BlendMode::Darken.key()), "Darken stays in RGB 16-bit");
|
||||
}
|
||||
@@ -13,7 +13,7 @@ use photocraft_color::BlendMode;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::dialogs::blend_options::{blending_params, blending_rows};
|
||||
use crate::dialogs::blend_options::{blending_params, blending_rows, forced_preserve_transparency, remember_blending};
|
||||
use crate::dialogs::params::{captured, open_param};
|
||||
|
||||
/// The command of the dialog, which is also its dialog key.
|
||||
@@ -40,8 +40,8 @@ pub(crate) fn hex(c: [f32; 3]) -> String {
|
||||
format!("#{:02x}{:02x}{:02x}", b(c[0]), b(c[1]), b(c[2]))
|
||||
}
|
||||
|
||||
/// The dialog's rows.
|
||||
fn rows(app: &PhotocraftApp, foreground: [f32; 3], first_pattern: Option<String>) -> Vec<Param> {
|
||||
/// 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 contents = CONTENTS.iter().map(|(k, l)| (k.to_string(), Label::from(tl!(l)))).collect();
|
||||
let mut rows = vec![
|
||||
Param::new("contents", tl!("Contents:"), ParamKind::choice(contents, Some("foreground".into()))),
|
||||
@@ -49,12 +49,13 @@ fn rows(app: &PhotocraftApp, foreground: [f32; 3], first_pattern: Option<String>
|
||||
Param::new("pattern", tl!("Custom Pattern:"), ParamKind::pattern(first_pattern)).visible_when("contents", "pattern"),
|
||||
Param::new("colorAdaptation", tl!("Color Adaptation"), ParamKind::bool(true)).visible_when("contents", "contentAware"),
|
||||
];
|
||||
rows.extend(blending_rows(app));
|
||||
rows.extend(blending_rows(app, preserve));
|
||||
rows
|
||||
}
|
||||
|
||||
/// Photoshop's Fill defaults, which Alt+Reset restores instead of the last-used values.
|
||||
fn defaults(foreground: [f32; 3], first_pattern: Option<String>, inks: Option<[f32; 4]>) -> DialogFields {
|
||||
/// Photoshop's Fill defaults, which Alt+Reset restores instead of the last-used values; a forced Preserve Transparency
|
||||
/// keeps its state.
|
||||
fn defaults(foreground: [f32; 3], first_pattern: Option<String>, inks: Option<[f32; 4]>, preserve: Option<bool>) -> DialogFields {
|
||||
let mut f = DialogFields::new();
|
||||
f.set("contents", "foreground");
|
||||
f.set("color", FieldValue::Color(foreground));
|
||||
@@ -65,7 +66,7 @@ fn defaults(foreground: [f32; 3], first_pattern: Option<String>, inks: Option<[f
|
||||
f.set("colorAdaptation", true);
|
||||
f.set("mode", BlendMode::Normal.key().to_string());
|
||||
f.set("opacity", 100.0);
|
||||
f.set("preserveTransparency", false);
|
||||
f.set("preserveTransparency", preserve.unwrap_or(false));
|
||||
f
|
||||
}
|
||||
|
||||
@@ -75,6 +76,7 @@ pub fn open(app: &mut PhotocraftApp) -> DialogId {
|
||||
let target = target.get("target").cloned().unwrap_or_else(|| crate::canvas::paint_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);
|
||||
let mut held = json!({ "target": target });
|
||||
if let Some(st) = app.session.active() {
|
||||
held["document"] = json!(st.doc.id.0);
|
||||
@@ -85,9 +87,9 @@ pub fn open(app: &mut PhotocraftApp) -> DialogId {
|
||||
let [r, g, b, _] = app.session.tools.foreground;
|
||||
let first = app.session.patterns.items.first().map(|p| p.id.clone());
|
||||
let patterns = crate::panels::preset_browser::pattern_swatches(app);
|
||||
let rows = rows(app, [r, g, b], first.clone());
|
||||
let rows = rows(app, [r, g, b], first.clone(), preserve);
|
||||
let inks = crate::color_picker_ui::ink_value(craft::theme::Srgba::rgb(r, g, b), app.session.tools.picked_role_cmyk(0));
|
||||
let reset = defaults([r, g, b], first, app.session.tools.picked_role_cmyk(0));
|
||||
let reset = defaults([r, g, b], first, app.session.tools.picked_role_cmyk(0), preserve);
|
||||
let mut fields = DialogFields::new();
|
||||
let remembered: Option<DialogFields> = MemoryStage::peek_saved(app.ctx(), &SaveKey::named(REMEMBER_INKS));
|
||||
let remembered_inks = remembered.as_ref().and_then(|saved| saved.get(&crate::color_picker_ui::ink_field("color"))).cloned();
|
||||
@@ -154,7 +156,7 @@ 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(_) => {
|
||||
result.remember(ctx);
|
||||
remember_blending(ctx, &result);
|
||||
// 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")) {
|
||||
@@ -166,5 +168,7 @@ pub(crate) fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamRes
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod preserve_transparency_state_tests;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,60 @@
|
||||
//! Preserve Transparency in Fill and Stroke follows the target layer (#14651): greyed off on the Background, greyed on
|
||||
//! with Lock Transparent Pixels, and neither forced state replaces the remembered choice.
|
||||
|
||||
use craft::ui::patterns::form_dialogs::HostedParamDialog;
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
use crate::smart_filter_dialog::tests::{cancel, confirm, set};
|
||||
|
||||
/// A document with a white Background and a pixel layer "Paint" above it, active, with a selection to fill or stroke.
|
||||
fn app() -> (Box<PhotocraftApp>, photocraft_doc::LayerId, photocraft_doc::LayerId) {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 40, "height": 30, "background": "white"})).unwrap();
|
||||
let background = app.session.active().unwrap().active_layer.unwrap();
|
||||
app.run("layer.new.layer", json!({"name": "Paint"})).unwrap();
|
||||
let paint = app.session.active().unwrap().active_layer.unwrap();
|
||||
app.run("select.rect", json!({"x": 5, "y": 5, "width": 20, "height": 15})).unwrap();
|
||||
(app, background, paint)
|
||||
}
|
||||
|
||||
/// The box's value in the open dialog `id`, and whether it takes a change.
|
||||
fn preserve(app: &mut PhotocraftApp, id: DialogId) -> (bool, bool) {
|
||||
let dialog = app.framework.dialogs.get_by_id::<HostedParamDialog>(id).expect("a hosted dialog");
|
||||
let on = dialog.fields().bool("preserveTransparency").expect("the box has a value");
|
||||
let ctx = app.ctx().clone();
|
||||
let editable = app.framework.dialogs.set_json_field(&ctx, id, "preserveTransparency", json!(on)).is_ok();
|
||||
(on, editable)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_box_is_greyed_at_the_targets_state_and_the_remembered_choice_survives() {
|
||||
for (name, open) in [("Fill", open as fn(&mut PhotocraftApp) -> DialogId), ("Stroke", crate::stroke_ui::open)] {
|
||||
let (mut app, background, paint) = app();
|
||||
let id = open(&mut app);
|
||||
assert_eq!(preserve(&mut app, id), (false, true), "{name}: a pixel layer offers the default");
|
||||
set(&mut app, id, "preserveTransparency", json!(true));
|
||||
confirm(&mut app, id);
|
||||
|
||||
app.session.select_layer(background).unwrap();
|
||||
let id = open(&mut app);
|
||||
assert_eq!(preserve(&mut app, id), (false, false), "{name}: greyed off on the Background");
|
||||
confirm(&mut app, id);
|
||||
|
||||
app.session.select_layer(paint).unwrap();
|
||||
let id = open(&mut app);
|
||||
assert_eq!(preserve(&mut app, id), (true, true), "{name}: the Background's forced state is not remembered");
|
||||
set(&mut app, id, "preserveTransparency", json!(false));
|
||||
confirm(&mut app, id);
|
||||
|
||||
app.run("layer.toggleLockTransparency", json!({})).unwrap();
|
||||
let id = open(&mut app);
|
||||
assert_eq!(preserve(&mut app, id), (true, false), "{name}: greyed on with Lock Transparent Pixels");
|
||||
confirm(&mut app, id);
|
||||
|
||||
app.run("layer.toggleLockTransparency", json!({})).unwrap();
|
||||
let id = open(&mut app);
|
||||
assert_eq!(preserve(&mut app, id), (false, true), "{name}: the lock's forced state is not remembered");
|
||||
cancel(&mut app, id);
|
||||
}
|
||||
}
|
||||
@@ -313,8 +313,8 @@ fn reset_fill_restores_the_original_native_inks_for_the_same_rgb() {
|
||||
use craft::ui::testing::Fixture;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
let original = [0., 0., 0., 1.];
|
||||
let reset = defaults([0.; 3], None, Some(original));
|
||||
let rows = rows(&app(), [0.; 3], None);
|
||||
let reset = defaults([0.; 3], None, Some(original), None);
|
||||
let rows = rows(&app(), [0.; 3], None, None);
|
||||
let fields = Rc::new(RefCell::new(reset.clone()));
|
||||
let shown = fields.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
|
||||
@@ -11,7 +11,7 @@ use craft::ui::runtime::memory::SaveKey;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::dialogs::blend_options::{blending_params, blending_rows};
|
||||
use crate::dialogs::blend_options::{blending_params, blending_rows, forced_preserve_transparency, remember_blending};
|
||||
use crate::dialogs::fill_ui::hex;
|
||||
use crate::dialogs::params::{captured, open_param};
|
||||
|
||||
@@ -20,15 +20,15 @@ pub const COMMAND: &str = "edit.stroke";
|
||||
const REMEMBER: &str = "photocraft.dialog.edit.stroke";
|
||||
const LOCATIONS: [(&str, &str); 3] = [("inside", "Inside"), ("center", "Center"), ("outside", "Outside")];
|
||||
|
||||
/// The dialog's rows.
|
||||
fn rows(app: &PhotocraftApp, foreground: [f32; 3]) -> Vec<Param> {
|
||||
/// The dialog's rows, with the Preserve Transparency the target forces.
|
||||
fn rows(app: &PhotocraftApp, foreground: [f32; 3], preserve: Option<bool>) -> Vec<Param> {
|
||||
let locations = LOCATIONS.iter().map(|(k, l)| (k.to_string(), Label::from(tl!(l)))).collect();
|
||||
let mut rows = vec![
|
||||
Param::new("width", tl!("Width:"), ParamKind::number(Some(1.0), Some(250.0), 1.0, 1.0, 0)).unit(UnitKind::Pixels),
|
||||
Param::new("color", tl!("Color:"), ParamKind::color(Some(foreground))),
|
||||
Param::new("location", tl!("Location:"), ParamKind::radio(locations, Some("center".into()))),
|
||||
];
|
||||
rows.extend(blending_rows(app));
|
||||
rows.extend(blending_rows(app, preserve));
|
||||
rows
|
||||
}
|
||||
|
||||
@@ -36,13 +36,14 @@ fn rows(app: &PhotocraftApp, foreground: [f32; 3]) -> Vec<Param> {
|
||||
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 preserve = 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);
|
||||
held["layer"] = json!(st.active_layer);
|
||||
}
|
||||
let [r, g, b, _] = app.session.tools.foreground;
|
||||
let rows = rows(app, [r, g, b]);
|
||||
let rows = rows(app, [r, g, b], preserve);
|
||||
let title = Label::from(tl!("Stroke"));
|
||||
open_param(app, COMMAND, DialogFields::new(), held, move |fields| {
|
||||
ParamDialog::new(COMMAND, title.clone(), Cow::Owned(rows.clone())).values(Bind::bound(fields)).remember(SaveKey::named(REMEMBER))
|
||||
@@ -86,7 +87,7 @@ pub(crate) fn check(app: &mut PhotocraftApp, result: &ParamResult) -> Result<(),
|
||||
pub(crate) fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamResult) {
|
||||
let captured = captured(app, &result.key);
|
||||
match app.run(COMMAND, params(&result.fields, &captured)) {
|
||||
Ok(_) => result.remember(ctx),
|
||||
Ok(_) => remember_blending(ctx, &result),
|
||||
Err(e) => crate::notices::error(app, e),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ fn stroke_choices_apply_on_confirm_and_cancel_does_not_modify_document() {
|
||||
/// #15539: Width shows px and Opacity % after their numbers.
|
||||
#[test]
|
||||
fn width_and_opacity_declare_their_units() {
|
||||
let rows = rows(&app(), [0.0, 0.0, 0.0]);
|
||||
let rows = rows(&app(), [0.0, 0.0, 0.0], None);
|
||||
let unit = |key: &str| rows.iter().find(|p| p.key == key).and_then(|p| p.unit);
|
||||
assert_eq!(unit("width"), Some(UnitKind::Pixels));
|
||||
assert_eq!(unit("opacity"), Some(UnitKind::Percent));
|
||||
|
||||
Reference in new issue
Block a user