Skip to content

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

Layers panel: double-clicking an effect row opens that layer's Layer Style (#4157) (#4131)

David Mannd323d872b2b2
diff --git a/crates/ui-egui/src/layer_style.rs b/crates/ui-egui/src/layer_style.rs
index 9a64a1f73..d3606e30b 100644
--- a/crates/ui-egui/src/layer_style.rs
+++ b/crates/ui-egui/src/layer_style.rs
@@ -515,6 +515,16 @@ pub fn open(app: &mut PhotocraftApp, select: Option<&str>) -> Option<u64> {
Some(app.ui.open_dialog(crate::state::DialogKind::LayerStyle, f))
}
+/// Double-click on an effect row of the Layers panel: open Layer Style for that row's layer. A
+/// row of another layer selects it first, as its right-click menu does (Photoshop does the same).
+pub fn open_for_layer(app: &mut PhotocraftApp, layer: photocraft_doc::LayerId, select: Option<&str>) -> Option<u64> {
+ let active = app.session.active().is_some_and(|s| s.active_layer == Some(layer));
+ if !active {
+ app.run("layer.select", json!({"layer": layer.0})).ok()?;
+ }
+ open(app, select)
+}
+
/// `[[id, name], …]` of the patterns a style can use (document's, then the library's).
pub fn pattern_list(app: &PhotocraftApp) -> Value {
let mut out: Vec<Value> = Vec::new();
@@ -1383,6 +1393,30 @@ mod tests {
assert_eq!(rows, KINDS.len(), "one empty checkbox per effect row");
}
+ /// Double-clicking an effect row of a layer that isn't active selects that layer first, so
+ /// the dialog holds its effects (and OK edits it), not the active layer's.
+ #[test]
+ fn effect_row_of_another_layer_opens_that_layers_style() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 32, "height": 32})).unwrap();
+ app.run("layer.new.layer", json!({})).unwrap();
+ let with_fx = app.session.active().unwrap().active_layer.unwrap();
+ app.run("layer.layerStyle.replace", json!({"layer": with_fx.0, "effects": [{"kind": "dropShadow", "params": defaults("dropShadow")}]})).unwrap();
+ app.run("layer.new.layer", json!({})).unwrap();
+ let plain = app.session.active().unwrap().active_layer.unwrap();
+ assert_ne!(plain, with_fx);
+ assert!(app.session.active().unwrap().doc.layer(plain).unwrap().effects.items.is_empty());
+
+ let id = open_for_layer(&mut app, with_fx, Some("dropShadow")).unwrap();
+ assert_eq!(app.session.active().unwrap().active_layer, Some(with_fx), "the row's layer becomes active");
+ let d = app.ui.dialogs.iter().find(|d| d.id == id).unwrap();
+ assert_eq!(d.fields["layer"], json!(with_fx.0));
+ let fx = effects_of(&d.fields);
+ assert_eq!(fx.len(), 1);
+ assert_eq!(fx[0]["kind"], json!("dropShadow"));
+ assert!(fx[0]["fx"].is_object(), "the existing drop shadow, not a new instance");
+ }
+
#[test]
fn every_kind_has_spec_and_defaults() {
for &(kind, _) in KINDS {
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 619371753..bb4d2f1e9 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -3932,7 +3932,7 @@ fn effect_rows(app: &mut PhotocraftApp, ui: &mut egui::Ui, l: &Layer, depth: usi
);
resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, name.clone()));
if resp.double_clicked() {
- crate::layer_style::open(app, kind);
+ crate::layer_style::open_for_layer(app, l.id, kind);
}
// #2846: right-click opens the layer style menu (Create Layer, Copy Layer Style…), as in Photoshop.
resp.context_menu(|ui| {