Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Fix layer mask toggle action labels (#2156)

optimalbyteea9267e199d96
diff --git a/crates/ui-egui/src/channels_panel.rs b/crates/ui-egui/src/channels_panel.rs
index 55fb29419..ba1051cdd 100644
--- a/crates/ui-egui/src/channels_panel.rs
+++ b/crates/ui-egui/src/channels_panel.rs
@@ -256,7 +256,7 @@ pub fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
}
Row::LayerMask => {
let enabled = masked.as_ref().and_then(|l| l.mask.as_ref()).is_none_or(|m| m.enabled);
- item(ui, a, if enabled { "Disable Layer Mask" } else { "Enable Layer Mask" }, "layer.layerMask.enabled", json!({}));
+ item(ui, a, crate::layer_menu_ui::mask_toggle_label(enabled), "layer.layerMask.enabled", json!({}));
item(ui, a, "Delete Layer Mask", "layer.layerMask.delete", json!({}));
}
}
diff --git a/crates/ui-egui/src/layer_menu_ui.rs b/crates/ui-egui/src/layer_menu_ui.rs
index 71b3d9b90..2e9a41b83 100644
--- a/crates/ui-egui/src/layer_menu_ui.rs
+++ b/crates/ui-egui/src/layer_menu_ui.rs
@@ -8,6 +8,11 @@ use serde_json::{Value, json};
/// One entry: label, command id. `None` = separator.
pub type Entry = Option<(&'static str, &'static str)>;
+/// English action key; each menu translates it using its existing display context.
+pub(crate) fn mask_toggle_label(enabled: bool) -> &'static str {
+ if enabled { "Disable Layer Mask" } else { "Enable Layer Mask" }
+}
+
/// The command behind "Add Layer Mask" (the panel button and this menu). Like Photoshop, an active
/// selection becomes the mask; ⌥ inverts it (Hide Selection, or Hide All without a selection).
pub fn add_mask_command(has_selection: bool, alt: bool) -> &'static str {
@@ -63,8 +68,8 @@ pub fn entries(l: &Layer, multi: bool, has_selection: bool) -> Vec<Entry> {
_ => v.push(Some(("Rasterize Layer", "layer.rasterize.layer"))),
}
v.push(None);
- if l.mask.is_some() {
- v.push(Some((tl!("Disable Layer Mask"), "layer.layerMask.enabled")));
+ if let Some(mask) = &l.mask {
+ v.push(Some((tl!(mask_toggle_label(mask.enabled)), "layer.layerMask.enabled")));
v.push(Some((tl!("Apply Layer Mask"), "layer.layerMask.apply")));
v.push(Some((tl!("Delete Layer Mask"), "layer.layerMask.delete")));
} else {
@@ -118,6 +123,16 @@ pub fn show(app: &crate::PhotocraftApp, ui: &mut egui::Ui, l: &Layer, on_set: bo
// selected first when clicked, so its items stay available.
let is_active = app.session.active().is_some_and(|s| s.active_layer == Some(l.id));
let enabled = if on_set || is_active { crate::menus::is_enabled(app, id) } else { true };
+ // With a multi-selection this toggle still dispatches to the active layer.
+ let label = if on_set && id == "layer.layerMask.enabled" {
+ app.session
+ .active()
+ .and_then(|s| s.active_layer.and_then(|id| s.doc.layer(id)))
+ .and_then(|l| l.mask.as_ref())
+ .map_or(label, |mask| tl!(mask_toggle_label(mask.enabled)))
+ } else {
+ label
+ };
if ui.add_enabled(enabled, egui::Button::new(tl!(&label))).clicked() {
if !on_set {
actions.push(("layer.select".into(), json!({"layer": l.id.0})));
@@ -211,6 +226,100 @@ mod color_tests;
mod tests {
use super::*;
+ #[test]
+ fn mask_toggle_label_follows_history_and_selected_layer() {
+ let mut s = photocraft_engine::Session::new();
+ s.execute("file.new", json!({"width": 10, "height": 10})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ let mut app = crate::PhotocraftApp::new(s, Default::default());
+ let toggle = "layer.layerMask.enabled";
+ let check = |app: &crate::PhotocraftApp, label: Option<&str>| {
+ let st = app.session.active().unwrap();
+ let l = st.doc.layer(st.active_layer.unwrap()).unwrap();
+ for multi in [false, true] {
+ let entry = entries(l, multi, st.doc.selection.is_some()).into_iter().flatten().find(|e| e.1 == toggle);
+ assert_eq!(entry.map(|e| e.0), label);
+ }
+ let item = crate::menus::menu_items(app).into_iter().find(|i| i.id == toggle).unwrap();
+ assert_eq!(item.enabled, label.is_some());
+ assert_eq!(item.label, label.unwrap_or("Enable Layer Mask"));
+ for lang in [crate::i18n::Lang::EN, crate::i18n::Lang::from_pref("fr")] {
+ let layout = crate::native_menu::photocraft_layout(&crate::menus::menu_items(app), lang, lang.code());
+ let native = layout.bar.find(toggle).unwrap();
+ assert_eq!(native.label, crate::i18n::tr(lang, &item.label));
+ assert_eq!(native.enabled, item.enabled);
+ }
+ };
+ check(&app, None);
+ app.run("layer.layerMask.revealAll", json!({})).unwrap();
+ let masked = app.session.active().unwrap().active_layer.unwrap();
+ check(&app, Some("Disable Layer Mask"));
+ app.run(toggle, json!({})).unwrap();
+ check(&app, Some("Enable Layer Mask"));
+ app.run("edit.undo", json!({})).unwrap();
+ check(&app, Some("Disable Layer Mask"));
+ app.run("edit.redo", json!({})).unwrap();
+ check(&app, Some("Enable Layer Mask"));
+ app.run("layer.new.layer", json!({})).unwrap();
+ check(&app, None);
+ app.run("layer.layerMask.revealAll", json!({})).unwrap();
+ check(&app, Some("Disable Layer Mask"));
+ app.run("layer.select", json!({"layer": masked.0, "mode": "add"})).unwrap();
+ check(&app, Some("Enable Layer Mask"));
+ app.run("select.rect", json!({"x": 0, "y": 0, "width": 4, "height": 4})).unwrap();
+ check(&app, Some("Enable Layer Mask"));
+ app.session.active_mut().unwrap().active_layer = None;
+ assert!(!crate::menus::is_enabled(&app, toggle));
+ let empty = crate::PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
+ assert!(!crate::menus::is_enabled(&empty, toggle));
+ }
+
+ #[test]
+ fn mask_toggle_context_menu_uses_the_dispatch_target_and_existing_translations() {
+ use egui_kittest::{Harness, kittest::Queryable};
+
+ for language in ["en", "fr"] {
+ let lang = crate::i18n::Lang::from_pref(language);
+ let _language = crate::i18n::language_scope(lang);
+ let mut s = photocraft_engine::Session::new();
+ s.execute("file.new", json!({"width": 10, "height": 10})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.execute("layer.layerMask.revealAll", json!({})).unwrap();
+ let clicked = s.active().unwrap().active_layer.unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ let other = s.active().unwrap().active_layer.unwrap();
+ s.execute("layer.layerMask.revealAll", json!({})).unwrap();
+ s.execute("layer.layerMask.enabled", json!({})).unwrap();
+ let mut app = crate::PhotocraftApp::new(s, Default::default());
+ app.run("layer.select", json!({"layer": clicked.0, "mode": "add"})).unwrap();
+ app.run("layer.select", json!({"layer": other.0, "mode": "add"})).unwrap();
+ let mut h = Harness::builder().with_size(egui::vec2(400.0, 900.0)).build_ui_state(
+ move |ui, app| {
+ let st = app.session.active().unwrap();
+ let l = st.doc.layer(clicked).unwrap().clone();
+ let mut actions = Vec::new();
+ show(app, ui, &l, true, &mut actions);
+ for (id, params) in actions {
+ crate::menus::invoke(app, ui.ctx(), &id, params).unwrap();
+ }
+ },
+ app,
+ );
+ h.run_steps(3);
+ let enabled_label = crate::i18n::tr(lang, "Enable Layer Mask");
+ let disabled_label = crate::i18n::tr(lang, "Disable Layer Mask");
+ // Both rows stay selected. This menu toggles the active layer, even when opened
+ // over another selected row whose mask has a different enabled state.
+ h.get_by_label(enabled_label).click();
+ h.run_steps(3);
+ h.get_by_label(disabled_label).click();
+ h.run_steps(3);
+ h.get_by_label(enabled_label);
+ assert_eq!(h.state().session.active().unwrap().selected_layers().len(), 2);
+ assert!(h.state().session.active().unwrap().doc.layer(clicked).unwrap().mask.as_ref().unwrap().enabled);
+ }
+ }
+
#[test]
fn smart_object_context_menu_edits_the_clicked_layer() {
use crate::PhotocraftApp;
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 77f339ef1..3f3945c18 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -798,6 +798,11 @@ pub fn menu_items(app: &PhotocraftApp) -> Vec<MenuItem> {
}
}
crate::plugin_ui::insert_menu_items(app, &mut items);
+ if let Some(mask) = app.session.active().and_then(|s| s.active_layer.and_then(|id| s.doc.layer(id))).and_then(|l| l.mask.as_ref())
+ && let Some(item) = items.iter_mut().find(|i| i.id == "layer.layerMask.enabled")
+ {
+ item.label = crate::layer_menu_ui::mask_toggle_label(mask.enabled).into();
+ }
// The File quick-export command uses the format selected in Export Preferences.
// On the web it intentionally downloads PNG until the quick-export service supports
// the other formats; the Layer quick-export command also always produces PNG.