Skip to content

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

Centre stacked ruler digits and the Properties W/H/X/Y labels (#7314) (#7349)

MarekSpodBiedrye04f52266731
diff --git a/crates/ui-egui/src/doc_props_ui.rs b/crates/ui-egui/src/doc_props_ui.rs
index fbdbc9a95..6a959c38d 100644
--- a/crates/ui-egui/src/doc_props_ui.rs
+++ b/crates/ui-egui/src/doc_props_ui.rs
@@ -6,6 +6,7 @@ use egui::{Align2, Rect, RichText, Sense, Stroke, StrokeKind, pos2, vec2};
use photocraft_doc::{ColorMode, Document, Layer, LayerId, SampleType};
use serde_json::{Value, json};
+use crate::props_layout::stacked_label;
use crate::theme::Tokens;
use crate::{PhotocraftApp, icons, widgets};
@@ -151,19 +152,19 @@ pub fn properties(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
});
ui.vertical(|ui| {
ui.horizontal(|ui| {
- field_label(ui, "W", 18.0);
+ stacked_label(ui, "W", &["W", "H"], 18.0, 4.0);
if let Some(v) = committed_field(ui, "w", w as f32, lu) {
run.extend(canvas_resize_params((w, h), Some(v), None, linked).map(|p| ("image.canvasSize".to_string(), p)));
}
- field_label(ui, "X", 22.0);
+ stacked_label(ui, "X", &["X", "Y"], 22.0, 4.0);
dim_field(ui, "0 px");
});
ui.horizontal(|ui| {
- field_label(ui, "H", 18.0);
+ stacked_label(ui, "H", &["W", "H"], 18.0, 4.0);
if let Some(v) = committed_field(ui, "h", h as f32, lu) {
run.extend(canvas_resize_params((w, h), None, Some(v), linked).map(|p| ("image.canvasSize".to_string(), p)));
}
- field_label(ui, "Y", 22.0);
+ stacked_label(ui, "Y", &["X", "Y"], 22.0, 4.0);
dim_field(ui, "0 px");
});
});
@@ -342,6 +343,40 @@ mod tests {
assert_eq!(st.history.entries().len(), undo, "no history step");
}
+ /// Horizontal centre of every single-letter label the panel drew, by letter.
+ fn letter_centres(h: &egui_kittest::Harness<'_, PhotocraftApp>) -> Vec<(String, f32)> {
+ h.output()
+ .shapes
+ .iter()
+ .filter_map(|s| if let egui::Shape::Text(t) = &s.shape { Some(t) } else { None })
+ .filter(|t| t.galley.text().chars().count() == 1)
+ .map(|t| (t.galley.text().to_string(), t.pos.x + t.galley.size().x * 0.5))
+ .collect()
+ }
+
+ /// #7314: the Canvas section's W over H (and X over Y) share a centre; a narrow "H" used to sit
+ /// flush right under the wider "W".
+ #[test]
+ fn canvas_size_labels_are_centred_on_each_other() {
+ use egui_kittest::Harness;
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 40, "height": 20, "background": "white"})).unwrap();
+ let mut h = Harness::builder().with_size(vec2(320.0, 600.0)).build_ui_state(
+ |ui, app: &mut PhotocraftApp| {
+ if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
+ properties(app, ui);
+ }
+ },
+ app,
+ );
+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
+ h.run_steps(3);
+ let centres = letter_centres(&h);
+ let at = |l: &str| centres.iter().find(|(s, _)| s == l).map(|(_, x)| *x).unwrap_or_else(|| panic!("no {l} label in {centres:?}"));
+ assert!((at("W") - at("H")).abs() < 0.01, "W and H: {centres:?}");
+ assert!((at("X") - at("Y")).abs() < 0.01, "X and Y: {centres:?}");
+ }
+
#[test]
fn document_dropdowns_map_to_live_mode_commands() {
for id in MODES.iter().map(|m| m.2).chain(DEPTHS.iter().map(|d| d.2)) {
diff --git a/crates/ui-egui/src/layer_props_ui.rs b/crates/ui-egui/src/layer_props_ui.rs
index c2ea848c5..57335a897 100644
--- a/crates/ui-egui/src/layer_props_ui.rs
+++ b/crates/ui-egui/src/layer_props_ui.rs
@@ -6,7 +6,7 @@ use egui::{Rect, Sense, Stroke, pos2, vec2};
use photocraft_doc::{Layer, LayerContent};
use serde_json::{Value, json};
-use crate::props_layout::{LABEL_GAP, LABEL_W, quick_actions_ui, section};
+use crate::props_layout::{LABEL_GAP, LABEL_W, quick_actions_ui, section, stacked_label};
use crate::theme::{ROW_GAP, Tokens};
use crate::{PhotocraftApp, widgets};
@@ -33,11 +33,10 @@ pub fn transform_params(layer: u64, b: [i32; 4], w: Option<f32>, h: Option<f32>,
Some(json!({"layer": layer, "rect": b, "quad": quad}))
}
-/// A number field that reports a value once committed (drag released, Enter, focus lost).
-fn field(ui: &mut egui::Ui, id: &str, label: &str, current: f32, width: f32, u: LenUnit, extent: f64) -> Option<f32> {
- let t = Tokens::get(ui.ctx());
- let (r, _) = ui.allocate_exact_size(vec2(LABEL_W, 22.0), Sense::hover());
- ui.painter().text(pos2(r.right() - 2.0, r.center().y), egui::Align2::RIGHT_CENTER, tl!(label), egui::FontId::proportional(12.0), t.text_dim);
+/// A number field that reports a value once committed (drag released, Enter, focus lost). `label` is its own
+/// label and the one stacked with it in the same column ("W" over "X"), which share a centre.
+fn field(ui: &mut egui::Ui, id: &str, label: (&str, &str), current: f32, width: f32, u: LenUnit, extent: f64) -> Option<f32> {
+ stacked_label(ui, tl!(label.0), &[tl!(label.0), tl!(label.1)], LABEL_W, 2.0);
let key = ui.make_persistent_id(("layer-props-field", id));
let mut v = ui.data(|d| d.get_temp::<f32>(key)).unwrap_or_else(|| u.shown(f64::from(current), extent));
let resp = widgets::value_field(ui, &mut v, -300_000.0..=300_000.0, u.unit.suffix(), width);
@@ -137,24 +136,24 @@ pub fn properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {
let w = ((ui.available_width() - 2.0 * (LABEL_W + LABEL_GAP) - LINK_W - 2.0 * LABEL_GAP) / 2.0).max(36.0);
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = LABEL_GAP;
- if let Some(v) = field(ui, "w", "W", b.width() as f32, w, u, dw) {
+ if let Some(v) = field(ui, "w", ("W", "X"), b.width() as f32, w, u, dw) {
run.extend(transform_params(layer.id.0, bb, Some(v), None, linked).map(|p| ("edit.transform".to_string(), p)));
}
if crate::icons::button(ui, if linked { "link" } else { "unlink" }, LINK_W, linked, tl!("Link width and height")).clicked() {
ui.data_mut(|d| d.insert_temp(link_key, !linked));
}
- if let Some(v) = field(ui, "h", "H", b.height() as f32, w, u, dh) {
+ if let Some(v) = field(ui, "h", ("H", "Y"), b.height() as f32, w, u, dh) {
run.extend(transform_params(layer.id.0, bb, None, Some(v), linked).map(|p| ("edit.transform".to_string(), p)));
}
});
ui.add_space(ROW_GAP);
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = LABEL_GAP;
- if let Some(v) = field(ui, "x", "X", b.x0 as f32, w, u, dw) {
+ if let Some(v) = field(ui, "x", ("X", "W"), b.x0 as f32, w, u, dw) {
run.push(("layer.translate".into(), json!({"layer": layer.id.0, "dx": (v - b.x0 as f32).round() as i32, "dy": 0})));
}
ui.add_space(LINK_W + LABEL_GAP);
- if let Some(v) = field(ui, "y", "Y", b.y0 as f32, w, u, dh) {
+ if let Some(v) = field(ui, "y", ("Y", "H"), b.y0 as f32, w, u, dh) {
run.push(("layer.translate".into(), json!({"layer": layer.id.0, "dx": 0, "dy": (v - b.y0 as f32).round() as i32})));
}
});
@@ -281,6 +280,40 @@ mod tests {
}
}
+ /// #7314: in the Transform section W sits over X and H over Y, and each pair shares a centre.
+ #[test]
+ fn transform_labels_are_centred_on_each_other() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 200, "height": 120})).unwrap();
+ app.run("layer.new.layer", json!({})).unwrap();
+ app.run("select.rect", json!({"x": 10, "y": 10, "width": 100, "height": 60})).unwrap();
+ app.run("edit.fill", json!({"contents": "color", "color": "#336699"})).unwrap();
+ let mut h = egui_kittest::Harness::builder().with_size(vec2(280.0, 900.0)).build_ui_state(
+ |ui, app: &mut PhotocraftApp| {
+ if ui.ctx().cumulative_pass_nr() == 0 {
+ PhotocraftApp::setup_context(ui.ctx(), crate::theme::ThemeKind::ProMedium);
+ return;
+ }
+ let state = app.session.active().unwrap();
+ let layer = state.doc.layer(state.active_layer.unwrap()).unwrap().clone();
+ properties(app, ui, &layer);
+ },
+ app,
+ );
+ h.run_steps(3);
+ let centres: Vec<(String, f32)> = h
+ .output()
+ .shapes
+ .iter()
+ .filter_map(|s| if let egui::Shape::Text(t) = &s.shape { Some(t) } else { None })
+ .filter(|t| t.galley.text().chars().count() == 1)
+ .map(|t| (t.galley.text().to_string(), t.pos.x + t.galley.size().x * 0.5))
+ .collect();
+ let at = |l: &str| centres.iter().find(|(s, _)| s == l).map(|(_, x)| *x).unwrap_or_else(|| panic!("no {l} label in {centres:?}"));
+ assert!((at("W") - at("X")).abs() < 0.01, "W and X: {centres:?}");
+ assert!((at("H") - at("Y")).abs() < 0.01, "H and Y: {centres:?}");
+ }
+
#[test]
fn transform_fields_show_and_take_the_ruler_unit() {
use photocraft_engine::prefs::Unit;
diff --git a/crates/ui-egui/src/props_layout.rs b/crates/ui-egui/src/props_layout.rs
index 674ff4dba..ea67b3714 100644
--- a/crates/ui-egui/src/props_layout.rs
+++ b/crates/ui-egui/src/props_layout.rs
@@ -17,6 +17,17 @@ pub const LABEL_GAP: f32 = 4.0;
/// Horizontal gap between two label+field columns.
pub const COL_GAP: f32 = 8.0;
+/// A one-letter field label ("W", "H", "X", "Y") in a `w`-wide column, ending `pad` before its field. The letters
+/// stacked in one column share a centre (#7314): the widest of `column` stays flush right, and the narrower ones
+/// are centred on it rather than pushed against its right edge.
+pub fn stacked_label(ui: &mut egui::Ui, s: &str, column: &[&str], w: f32, pad: f32) {
+ let t = Tokens::get(ui.ctx());
+ let font = egui::FontId::proportional(12.0);
+ let widest = column.iter().map(|c| ui.painter().layout_no_wrap((*c).to_owned(), font.clone(), t.text_dim).size().x).fold(0.0, f32::max);
+ let (r, _) = ui.allocate_exact_size(vec2(w, 22.0), Sense::hover());
+ ui.painter().text(pos2(r.right() - pad - widest / 2.0, r.center().y), egui::Align2::CENTER_CENTER, s, font, t.text_dim);
+}
+
/// Icon for a layer kind (the same glyphs as the Layers panel's kind filter).
pub fn kind_icon(content: &LayerContent) -> &'static str {
match content {
diff --git a/crates/ui-egui/src/rulers.rs b/crates/ui-egui/src/rulers.rs
index 61ea25d96..995f04a5b 100644
--- a/crates/ui-egui/src/rulers.rs
+++ b/crates/ui-egui/src/rulers.rs
@@ -393,9 +393,10 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &
let c = at(v);
if vertical {
p.line_segment([pos2(left.right() - RULER, c), pos2(left.right(), c)], tick);
- // Labels stacked, as in Photoshop.
+ // Labels stacked, as in Photoshop, each character centred in the ruler's width
+ // (a narrow "1" used to hug the left edge while a "0" filled the strip).
for (i, ch) in ruler_label(v, step, whole).chars().enumerate() {
- p.text(pos2(left.left() + 3.0, c + 2.0 + i as f32 * 8.5), Align2::LEFT_TOP, ch, font.clone(), t.text_dim);
+ p.text(pos2(left.left() + RULER * 0.5, c + 2.0 + i as f32 * 8.5), Align2::CENTER_TOP, ch, font.clone(), t.text_dim);
}
} else {
p.line_segment([pos2(c, top.bottom() - RULER), pos2(c, top.bottom())], tick);
@@ -534,6 +535,28 @@ mod tests {
assert!(failures.is_empty(), "ruler drawing overlaps the origin corner: {failures:?}");
}
+ /// #7314: the stacked characters of the left ruler's labels are centred on the strip, so a
+ /// narrow "1" lines up with a wide "0".
+ #[test]
+ fn left_ruler_label_characters_are_centred() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 256, "height": 128})).unwrap();
+ let (mut h, full, _) = ruler_harness(app);
+ h.run_steps(2);
+ let centres: Vec<f32> = h
+ .output()
+ .shapes
+ .iter()
+ .filter_map(|s| if let egui::Shape::Text(t) = &s.shape { Some(t) } else { None })
+ .filter(|t| t.pos.x < full.min.x + RULER && t.pos.y > full.min.y + RULER)
+ .map(|t| t.pos.x + t.galley.size().x * 0.5)
+ .collect();
+ assert!(centres.len() > 2, "the left ruler draws labels: {centres:?}");
+ for c in &centres {
+ assert!((c - (full.min.x + RULER * 0.5)).abs() < 0.01, "character centre {c} in {centres:?}");
+ }
+ }
+
#[test]
fn shift_snaps_the_zero_point_to_ruler_ticks_from_the_current_origin() {
assert_eq!(snap_px_to_tick(47.0, 0.0, 10.0), 50.0);