Use typed blend modes in Layers menu

This commit is contained in:
FredApps committed 2026-11-19 08:35:06 +01:00
1 parent 856f865cf9
commit 5e6b4c0945
2 files changed
+71 -8

No files matched your search

+61
View File
@@ -47,6 +47,52 @@ pub enum BlendMode {
}
impl BlendMode {
/// Stable key used by the Layers panel.
pub fn key(self) -> &'static str {
match self {
Self::PassThrough => "passthrough",
Self::Normal => "normal",
Self::Dissolve => "dissolve",
Self::Darken => "darken",
Self::Multiply => "multiply",
Self::ColorBurn => "colorburn",
Self::LinearBurn => "linearburn",
Self::DarkerColor => "darkercolor",
Self::Lighten => "lighten",
Self::Screen => "screen",
Self::ColorDodge => "colordodge",
Self::LinearDodge => "lineardodgeadd",
Self::LighterColor => "lightercolor",
Self::Overlay => "overlay",
Self::SoftLight => "softlight",
Self::HardLight => "hardlight",
Self::VividLight => "vividlight",
Self::LinearLight => "linearlight",
Self::PinLight => "pinlight",
Self::HardMix => "hardmix",
Self::Difference => "difference",
Self::Exclusion => "exclusion",
Self::Subtract => "subtract",
Self::Divide => "divide",
Self::Hue => "hue",
Self::Saturation => "saturation",
Self::Color => "color",
Self::Luminosity => "luminosity",
Self::Reflect => "reflect",
Self::Glow => "glow",
Self::Negation => "negation",
Self::Xor => "xor",
Self::PaintNetColorBurn => "colorburnpaint.net",
Self::PaintNetColorDodge => "colordodgepaint.net",
}
}
/// Resolve a canonical Layers panel key.
pub fn from_key(key: &str) -> Option<Self> {
static MODES: std::sync::OnceLock<std::collections::HashMap<&'static str, BlendMode>> = std::sync::OnceLock::new();
MODES.get_or_init(|| std::iter::once(Self::PassThrough).chain(Self::layer_modes()).map(|mode| (mode.key(), mode)).collect()).get(key).copied()
}
/// Paint.NET modes without an exact Photoshop equivalent.
pub const PAINT_NET_MODES: [BlendMode; 6] = [Self::Reflect, Self::Glow, Self::Negation, Self::Xor, Self::PaintNetColorBurn, Self::PaintNetColorDodge];
@@ -623,3 +669,18 @@ pub fn composite_atop(mode: BlendMode, backdrop: [f32; 4], source: [f32; 4], opa
#[cfg(test)]
mod tests;
#[cfg(test)]
mod key_tests {
use super::BlendMode;
#[test]
fn canonical_keys_match_legacy_panel_names_and_round_trip() {
for mode in std::iter::once(BlendMode::PassThrough).chain(BlendMode::layer_modes()) {
let old = mode.label().to_ascii_lowercase().replace([' ', '_', '-', '(', ')'], "");
assert_eq!(mode.key(), old);
assert_eq!(BlendMode::from_key(&old), Some(mode));
}
assert_eq!(BlendMode::from_key("no-such-mode"), None);
}
}
+10 -8
View File
@@ -28,7 +28,7 @@ use crate::PhotocraftApp;
/// The key the panel and the filter use for a blend mode: its label folded as the engine's
/// `blend_from_str` reads it ("normal", "lineardodgeadd").
pub(crate) fn blend_key(m: BlendMode) -> String {
m.label().to_ascii_lowercase().replace([' ', '_', '-', '(', ')'], "")
m.key().to_string()
}
/// The blend mode a panel key names.
@@ -382,13 +382,15 @@ impl LayerTree for PhotoLayers<'_> {
}
fn blend_modes(&self, group: bool, current: &str) -> Vec<Entry<String>> {
let current = blend_of(current).unwrap_or(BlendMode::Normal);
let modes: Vec<BlendKeyEntry<String>> =
super::layers::blend_options(group, current).into_iter().map(|(m, _)| super::blend_entry(blend_key(m), m)).collect();
blend_entries(&modes, |e| {
let label = blend_of(&e.key).map_or(e.key.as_str(), |m| tl!(m.label()));
Label::from(label.to_string())
})
let current = BlendMode::from_key(current).or_else(|| blend_of(current)).unwrap_or(BlendMode::Normal);
let modes: Vec<BlendKeyEntry<BlendMode>> = super::layers::blend_options(group, current).into_iter().map(|(m, _)| super::blend_entry(m, m)).collect();
blend_entries(&modes, |mode| Label::from(tl!(mode.key.label()).to_string()))
.into_iter()
.map(|entry| match entry.value() {
Some(mode) => Entry::new(mode.key().to_string(), entry.label().clone()),
None => Entry::separator(),
})
.collect()
}
fn footer(&self) -> LayersFooterState {