Skip to content

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

Layers: right-click an effect row for the layer style menu (#2846) (#2950)

David Mannf781cb70fc5d
diff --git a/crates/ui-egui/src/fx_drag_tests.rs b/crates/ui-egui/src/fx_drag_tests.rs
index a2f2f7ca6..238063f89 100644
--- a/crates/ui-egui/src/fx_drag_tests.rs
+++ b/crates/ui-egui/src/fx_drag_tests.rs
@@ -84,3 +84,31 @@ fn dragging_effects_onto_a_layer_moves_them_and_alt_copies() {
// The drag moved effects, not the layer: `b` is still above `a`.
assert!(row(&h, b).row.top() < row(&h, a).row.top());
}
+
+/// #2846: right-clicking an effect row opens Photoshop's layer style menu for that layer; Create
+/// Layer turns its effects into layers of their own.
+#[test]
+fn right_clicking_an_effect_row_offers_create_layer_for_that_layer() {
+ let (mut h, a, b) = harness();
+ assert_eq!(h.state().session.active().unwrap().active_layer, Some(b), "`b` is active, not the clicked layer");
+ let at = effect_row(&h, a, "Color Overlay").center();
+ h.hover_at(at);
+ h.run_steps(1);
+ for pressed in [true, false] {
+ h.event(egui::Event::PointerButton { pos: at, button: PointerButton::Secondary, pressed, modifiers: Modifiers::NONE });
+ h.run_steps(1);
+ }
+ h.run_steps(3);
+ for label in ["Blending Options…", "Copy Layer Style", "Paste Layer Style", "Clear Layer Style", "Global Light…", "Hide All Effects", "Scale Effects…"]
+ {
+ h.get_by_label(label);
+ }
+ h.get_by_label("Create Layer").click();
+ h.run_steps(4);
+ let st = h.state().session.active().unwrap();
+ assert_eq!(st.active_layer, Some(a), "the menu acts on the right-clicked layer");
+ assert!(effects(&h, a).is_empty(), "the effects became layers");
+ let names: Vec<&str> = st.doc.layers.iter().map(|l| l.name.as_str()).collect();
+ assert!(names.contains(&"a's Drop Shadow") && names.contains(&"a's Color Overlay"), "{names:?}");
+ assert!(h.query_by_label("Create Layer").is_none(), "the menu closed");
+}
diff --git a/crates/ui-egui/src/layer_menu_ui.rs b/crates/ui-egui/src/layer_menu_ui.rs
index dbd359012..12e220930 100644
--- a/crates/ui-egui/src/layer_menu_ui.rs
+++ b/crates/ui-egui/src/layer_menu_ui.rs
@@ -120,42 +120,31 @@ pub fn entries(l: &Layer, multi: bool, has_selection: bool) -> Vec<Entry> {
v
}
+/// The layer style menu of an effect row ("Effects" or one effect) under a layer (#2846), in
+/// Photoshop's order. It acts on that layer, like Layer ▸ Layer Style.
+pub fn effects_entries() -> Vec<Entry> {
+ vec![
+ Some((tl!("Blending Options…"), "layer.layerStyle.blendingOptions")),
+ None,
+ Some((tl!("Copy Layer Style"), "layer.layerStyle.copyLayerStyle")),
+ Some((tl!("Paste Layer Style"), "layer.layerStyle.pasteLayerStyle")),
+ Some((tl!("Clear Layer Style"), "layer.layerStyle.clear")),
+ None,
+ Some((tl!("Global Light…"), "layer.layerStyle.globalLight")),
+ Some((tl!("Create Layer"), "layer.layerStyle.createLayer")),
+ Some((tl!("Hide All Effects"), "layer.layerStyle.hideAllEffects")),
+ Some((tl!("Scale Effects…"), "layer.layerStyle.scaleEffects")),
+ ]
+}
+
/// Render the menu. Pushes `(command, params)` actions; `Value::Null` params mean "invoke like the
/// menu item" (opens the command's dialog when it has one).
pub fn show(app: &crate::PhotocraftApp, ui: &mut egui::Ui, l: &Layer, on_set: bool, actions: &mut Vec<(String, Value)>) -> bool {
crate::widgets::menu_scroll(ui, |ui| {
ui.set_min_width(220.0);
let mut rename = false;
- let mut last_sep = true;
let has_selection = app.session.active().is_some_and(|s| s.doc.selection.is_some());
- for e in entries(l, on_set, has_selection) {
- match e {
- None => {
- if !last_sep {
- ui.separator();
- }
- last_sep = true;
- }
- Some((label, id)) => {
- // Skip commands this build doesn't have rather than showing dead items.
- if photocraft_engine::commands::find(id).is_none() && !crate::menu_catalog::CATALOG.iter().any(|m| m.3 == id) {
- continue;
- }
- last_sep = false;
- // Enablement is exact for the active layer (or the selection); another row is
- // 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 = !(on_set && single_layer_only(id)) && if on_set || is_active { crate::menus::is_enabled(app, id) } else { true };
- if ui.add_enabled(enabled, egui::Button::new(tl!(&label))).clicked() {
- if !on_set {
- actions.push(("layer.select".into(), json!({"layer": l.id.0})));
- }
- actions.push((id.into(), Value::Null));
- ui.close();
- }
- }
- }
- }
+ items(app, ui, l, on_set, entries(l, on_set, has_selection), actions);
ui.separator();
if ui.button(tl!("Rename Layer…")).clicked() {
rename = true;
@@ -166,6 +155,48 @@ pub fn show(app: &crate::PhotocraftApp, ui: &mut egui::Ui, l: &Layer, on_set: bo
})
}
+/// Render the layer style menu of `l`'s effect rows ([`effects_entries`]); the actions select `l`
+/// first when it isn't the active layer.
+pub fn show_effects(app: &crate::PhotocraftApp, ui: &mut egui::Ui, l: &Layer, actions: &mut Vec<(String, Value)>) {
+ crate::widgets::menu_scroll(ui, |ui| {
+ ui.set_min_width(200.0);
+ items(app, ui, l, false, effects_entries(), actions);
+ });
+}
+
+/// The command items of a layer menu, separators collapsed.
+fn items(app: &crate::PhotocraftApp, ui: &mut egui::Ui, l: &Layer, on_set: bool, entries: Vec<Entry>, actions: &mut Vec<(String, Value)>) {
+ let mut last_sep = true;
+ for e in entries {
+ match e {
+ None => {
+ if !last_sep {
+ ui.separator();
+ }
+ last_sep = true;
+ }
+ Some((label, id)) => {
+ // Skip commands this build doesn't have rather than showing dead items.
+ if photocraft_engine::commands::find(id).is_none() && !crate::menu_catalog::CATALOG.iter().any(|m| m.3 == id) {
+ continue;
+ }
+ last_sep = false;
+ // Enablement is exact for the active layer (or the selection); another row is
+ // 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 = !(on_set && single_layer_only(id)) && if on_set || is_active { crate::menus::is_enabled(app, id) } else { true };
+ if ui.add_enabled(enabled, egui::Button::new(tl!(&label))).clicked() {
+ if !on_set {
+ actions.push(("layer.select".into(), json!({"layer": l.id.0})));
+ }
+ actions.push((id.into(), Value::Null));
+ ui.close();
+ }
+ }
+ }
+ }
+}
+
fn color_name(color: LabelColor) -> &'static str {
crate::i18n::tr_ctx(crate::i18n::current(), "layerLabel", color.label())
}
@@ -680,7 +711,8 @@ mod tests {
let mut s = photocraft_engine::Session::new();
s.execute("file.new", json!({"width": 10, "height": 10})).unwrap();
let l = s.active().unwrap().doc.layers[0].clone();
- for (_, id) in entries(&l, false, true).into_iter().chain(entries(&l, false, false)).chain(entries(&l, true, false)).flatten() {
+ for (_, id) in entries(&l, false, true).into_iter().chain(entries(&l, false, false)).chain(entries(&l, true, false)).chain(effects_entries()).flatten()
+ {
assert!(crate::menu_catalog::CATALOG.iter().any(|m| m.3 == id) || photocraft_engine::commands::find(id).is_some(), "{id}");
}
}
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index bde5b84bb..c0250e876 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -3640,6 +3640,11 @@ fn effect_rows(app: &mut PhotocraftApp, ui: &mut egui::Ui, l: &Layer, depth: usi
if resp.double_clicked() {
crate::layer_style::open(app, kind);
}
+ // #2846: right-click opens the layer style menu (Create Layer, Copy Layer Style…), as in Photoshop.
+ resp.context_menu(|ui| {
+ crate::widgets::style_spectrum_popup_menu(ui);
+ crate::layer_menu_ui::show_effects(app, ui, l, actions);
+ });
}
}