Skip to content

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

Properties: Align and Distribute with the Background in a multiple selection (#4608) (#4637)

F4LCONf4e3dc63b6f4
diff --git a/crates/ui-egui/src/layer_props_ui.rs b/crates/ui-egui/src/layer_props_ui.rs
index a0d2d7f05..c2ea848c5 100644
--- a/crates/ui-egui/src/layer_props_ui.rs
+++ b/crates/ui-egui/src/layer_props_ui.rs
@@ -124,7 +124,6 @@ const LINK_W: f32 = 20.0;
/// sections (Character/Paragraph/Type Options for type, Appearance/Shape for shapes), then Quick
/// Actions, all with the same collapsible headers (#155).
pub fn properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {
- let t = Tokens::get(ui.ctx());
let mut run: Vec<(String, Value)> = Vec::new();
if let Some(s) = layer.surface() {
let b = app.cached_bounds(layer.id.0, s);
@@ -167,38 +166,8 @@ pub fn properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {
if gradient {
crate::gradient_ui::properties(app, ui, layer);
}
- if !gradient && section(ui, "align", tl!("Align and Distribute")) {
- ui.horizontal(|ui| {
- ui.spacing_mut().item_spacing.x = 2.0;
- for (i, kind) in ["leftEdges", "horizontalCenters", "rightEdges", "topEdges", "verticalCenters", "bottomEdges"].into_iter().enumerate() {
- if i == 3 {
- ui.add_space(8.0);
- }
- let id = format!("layer.align.{kind}");
- let on = app.session.is_enabled(&id);
- let label = photocraft_engine::commands::find(&id).map_or(kind, |c| c.label);
- let (r, resp) = ui.allocate_exact_size(vec2(24.0, 24.0), if on { Sense::click() } else { Sense::hover() });
- if resp.hovered() && on {
- ui.painter().rect_filled(r, t.radius_sm, t.hover);
- }
- align_glyph(ui, r, kind, if on { t.icon } else { t.text_faint });
- if resp.on_hover_text(crate::i18n::fmt(tl!("Align {what}"), &[("what", tl!(&label))])).clicked() {
- run.push((id, json!({})));
- }
- }
- });
- // 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);
+ if !gradient {
+ align_and_distribute(app, ui, &mut run);
}
for (id, p) in run.drain(..) {
if let Err(e) = app.run(&id, p) {
@@ -222,6 +191,61 @@ pub fn properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {
}
}
+/// The Align and Distribute section: its buttons run `layer.align.*` and `layer.distribute.*` on
+/// the selected layers (pushed onto `run`), greyed where the engine says they can't.
+fn align_and_distribute(app: &PhotocraftApp, ui: &mut egui::Ui, run: &mut Vec<(String, Value)>) {
+ let t = Tokens::get(ui.ctx());
+ if !section(ui, "align", tl!("Align and Distribute")) {
+ return;
+ }
+ ui.horizontal(|ui| {
+ ui.spacing_mut().item_spacing.x = 2.0;
+ for (i, kind) in ["leftEdges", "horizontalCenters", "rightEdges", "topEdges", "verticalCenters", "bottomEdges"].into_iter().enumerate() {
+ if i == 3 {
+ ui.add_space(8.0);
+ }
+ let id = format!("layer.align.{kind}");
+ let on = app.session.is_enabled(&id);
+ let label = photocraft_engine::commands::find(&id).map_or(kind, |c| c.label);
+ let (r, resp) = ui.allocate_exact_size(vec2(24.0, 24.0), if on { Sense::click() } else { Sense::hover() });
+ if resp.hovered() && on {
+ ui.painter().rect_filled(r, t.radius_sm, t.hover);
+ }
+ align_glyph(ui, r, kind, if on { t.icon } else { t.text_faint });
+ let tip = crate::i18n::fmt(tl!("Align {what}"), &[("what", tl!(&label))]);
+ resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, on, &tip));
+ if resp.on_hover_text(tip).clicked() {
+ run.push((id, json!({})));
+ }
+ }
+ });
+ // 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);
+}
+
+/// Several layers selected with the Background active: Photoshop's Properties keeps Align and
+/// Distribute for the selection there, above the Document properties (#4608).
+pub fn selection_align(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
+ let mut run = Vec::new();
+ align_and_distribute(app, ui, &mut run);
+ for (id, p) in run {
+ if let Err(e) = app.run(&id, p) {
+ app.ui.status = e;
+ app.ui.status_error = true;
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 1d3259a71..07653150e 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -3287,8 +3287,12 @@ fn properties_body_inner(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
crate::mask_props_ui::vector_properties(app, ui, layer);
return;
}
- // Photoshop shows the Document properties when nothing or the Background layer is selected.
+ // Photoshop shows the Document properties when nothing or the Background layer is selected,
+ // with Align and Distribute above them when the Background is one of several (#4608).
if crate::doc_props_ui::shows_document(&st.doc, st.active_layer) {
+ if t.pro && st.selected_layers().len() > 1 {
+ crate::layer_props_ui::selection_align(app, ui);
+ }
crate::doc_props_ui::properties(app, ui);
return;
}
diff --git a/crates/ui-egui/src/props_layout_tests.rs b/crates/ui-egui/src/props_layout_tests.rs
index 8f7fbc15f..74fd29121 100644
--- a/crates/ui-egui/src/props_layout_tests.rs
+++ b/crates/ui-egui/src/props_layout_tests.rs
@@ -313,3 +313,36 @@ fn properties_distribute_buttons_move_selected_layers_and_undo_in_both_axes() {
assert_eq!(restored, before, "{button}: undo restores its location");
}
}
+
+/// #4608: with the Background one of several selected layers (and the active one), Properties
+/// shows the Document properties as before, with Align and Distribute above them, and its buttons
+/// align the selection: the Background stays, the other layer lines up with it.
+#[test]
+fn background_in_a_multiple_selection_keeps_align_and_distribute() {
+ use photocraft_doc::LayerId;
+ let mut s = photocraft_engine::Session::new();
+ s.execute("file.new", json!({"width": 300, "height": 200})).unwrap();
+ let bg = s.active().unwrap().doc.layers[0].id.0;
+ let block = s.execute("layer.new.layer", json!({"name": "Block"})).unwrap()["layer"].as_u64().unwrap();
+ s.execute("select.rect", json!({"x": 20, "y": 30, "width": 40, "height": 20})).unwrap();
+ s.execute("edit.fill", json!({"contents": "color", "color": "#336699"})).unwrap();
+ s.execute("select.deselect", json!({})).unwrap();
+ // Background alone: the Document properties only.
+ s.execute("layer.select", json!({"layer": bg})).unwrap();
+ let mut h = harness(s, 1.0, 320.0);
+ assert!(h.query_by_label("Align Horizontal Centers").is_none(), "one layer: Document properties only");
+ // The Background is still the active layer once Block joins the selection.
+ h.state_mut().run("layer.select", json!({"layer": block, "mode": "add"})).unwrap();
+ h.state_mut().run("layer.select", json!({"layer": bg, "mode": "add"})).unwrap();
+ h.run_steps(8);
+ assert_eq!(h.state().session.active().unwrap().active_layer, Some(LayerId(bg)));
+ assert_eq!(h.state().session.active().unwrap().selected_layers().len(), 2);
+ let names: Vec<String> = headers(&h).into_iter().map(|(n, _)| n).collect();
+ assert!(names.iter().any(|n| n == "Align and Distribute"), "{names:?}");
+ h.get_by_label("Align Horizontal Centers").click();
+ h.run_steps(4);
+ let st = h.state().session.active().unwrap();
+ let b = st.doc.layer(LayerId(block)).unwrap().surface().unwrap().content_bounds();
+ assert_eq!((b.x0 + b.x1) / 2, 150, "centred on the Background, which spans the canvas: {b:?}");
+ assert_eq!(b.y0, 30, "only horizontally");
+}