Skip to content

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

fix(properties): expose equal-gap layer distribution (#2349) (#2398)

Nigel Ihza Firdausy13d89378d8c3
diff --git a/crates/ui-egui/src/layer_props_ui.rs b/crates/ui-egui/src/layer_props_ui.rs
index b9ae55e83..14c6bf5c0 100644
--- a/crates/ui-egui/src/layer_props_ui.rs
+++ b/crates/ui-egui/src/layer_props_ui.rs
@@ -155,6 +155,17 @@ pub fn properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {
}
}
});
+ // The engine already supports equal-gap distribution in both axes. Make those
+ // existing commands discoverable here as well as through Layer > Distribute (#2349).
+ // Three or more selected layers are required; the engine remains authoritative.
+ ui.horizontal_wrapped(|ui| {
+ for (kind, text) in [("horizontally", tl!("Horizontally")), ("vertically", tl!("Vertically"))] {
+ let id = format!("layer.distribute.{kind}");
+ if ui.add_enabled(app.session.is_enabled(&id), egui::Button::new(text)).clicked() {
+ run.push((id, json!({})));
+ }
+ }
+ });
ui.add_space(ROW_GAP);
}
for (id, p) in run.drain(..) {
diff --git a/crates/ui-egui/src/props_layout_tests.rs b/crates/ui-egui/src/props_layout_tests.rs
index fd41fce35..0b99569fe 100644
--- a/crates/ui-egui/src/props_layout_tests.rs
+++ b/crates/ui-egui/src/props_layout_tests.rs
@@ -263,3 +263,49 @@ fn layers_opacity_and_fill_share_one_right_column() {
assert!((ol.right() - fl.right()).abs() < 0.5, "{theme}: Opacity label ends at {}, Fill label at {}", ol.right(), fl.right());
}
}
+
+#[test]
+fn properties_distribute_buttons_move_selected_layers_and_undo_in_both_axes() {
+ use photocraft_doc::LayerId;
+ for horizontal in [true, false] {
+ let mut s = photocraft_engine::Session::new();
+ s.execute("file.new", json!({"width": 300, "height": 300, "background": "transparent"})).unwrap();
+ let mut ids = Vec::new();
+ for (i, at) in [10, 40, 160].into_iter().enumerate() {
+ let id = s.execute("layer.new.layer", json!({"name": format!("Block {i}")})).unwrap()["layer"].as_u64().unwrap();
+ let (x, y) = if horizontal { (at, 10) } else { (10, at) };
+ s.execute("select.rect", json!({"x": x, "y": y, "width": 10, "height": 10})).unwrap();
+ s.execute("edit.fill", json!({"contents": "color", "color": "#336699"})).unwrap();
+ s.execute("select.deselect", json!({})).unwrap();
+ ids.push(id);
+ }
+ for (i, id) in ids.iter().copied().enumerate() {
+ s.execute("layer.select", json!({"layer": id, "mode": if i == 0 { "replace" } else { "add" }})).unwrap();
+ }
+ let mut h = harness(s, 1.0, 320.0);
+ let button = if horizontal { "Horizontally" } else { "Vertically" };
+ // An enabled Properties button invokes the existing command (not a UI-only gesture).
+ let before = {
+ let st = h.state().session.active().unwrap();
+ let b = st.doc.layer(LayerId(ids[1])).unwrap().surface().unwrap().content_bounds();
+ if horizontal { b.x0 } else { b.y0 }
+ };
+ let history = h.state().session.active().unwrap().history.entries().len();
+ h.get_by_label(button).click();
+ h.run_steps(4);
+ let after = {
+ let st = h.state().session.active().unwrap();
+ let b = st.doc.layer(LayerId(ids[1])).unwrap().surface().unwrap().content_bounds();
+ if horizontal { b.x0 } else { b.y0 }
+ };
+ assert_ne!(after, before, "{button} moved the middle layer");
+ assert_eq!(h.state().session.active().unwrap().history.entries().len(), history + 1);
+ h.state_mut().session.undo();
+ let restored = {
+ let st = h.state().session.active().unwrap();
+ let b = st.doc.layer(LayerId(ids[1])).unwrap().surface().unwrap().content_bounds();
+ if horizontal { b.x0 } else { b.y0 }
+ };
+ assert_eq!(restored, before, "{button}: undo restores its location");
+ }
+}