Skip to content

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

Character and Paragraph panels: size, leading, baseline shift and indents follow the Type units preference (fixes #6491)

Marco Arnulfof59b75b058b9
diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs
index d73bb2e2c..f95b1bdf1 100644
--- a/crates/ui-egui/src/type_tool.rs
+++ b/crates/ui-egui/src/type_tool.rs
@@ -2130,6 +2130,8 @@ fn type_sections(app: &mut PhotocraftApp, ui: &mut egui::Ui, character: bool, pa
use crate::props_layout::{COL_GAP, LABEL_GAP, LABEL_W, field_width, section};
use crate::theme::ROW_GAP;
let Some((c, para)) = styles_at(app) else { return };
+ // Preferences ▸ Units & Rulers ▸ Type, as in the options bar: the model keeps points.
+ let (u, unit) = type_unit(app);
let t = crate::theme::Tokens::get(ui.ctx());
// Row helper: `ui.horizontal` with the column gap, then the shared row gap.
let row = |ui: &mut egui::Ui, add: &mut dyn FnMut(&mut egui::Ui)| {
@@ -2167,13 +2169,13 @@ fn type_sections(app: &mut PhotocraftApp, ui: &mut egui::Ui, character: bool, pa
let k = shown_scale(app);
// Hover tips stay English until wired through `tl!` in a multi-locale change
// (`every_tl_literal_is_translated` requires every complete catalog).
- if let Some(v) = num_field(ui, "tT", "Font size", c.size_pt * k, 0.1..=1296.0, "pt", w) {
- app.ui.tool_options.type_size = v;
- apply(app, ui.ctx(), json!({"size": v, "metricsAsShown": true}));
+ if let Some(v) = num_field(ui, "tT", "Font size", c.size_pt * k * u, 0.1 * u..=1296.0 * u, unit, w) {
+ app.ui.tool_options.type_size = v / u;
+ apply(app, ui.ctx(), json!({"size": v / u, "metricsAsShown": true}));
}
let lead = c.leading_pt.unwrap_or(c.size_pt * para.auto_leading.max(0.01)) * k;
- if let Some(v) = num_field(ui, "A↕", "Leading (set to the font size × auto-leading when Auto)", lead, 0.1..=5000.0, "pt", w) {
- apply(app, ui.ctx(), json!({"leading": v, "metricsAsShown": true}));
+ if let Some(v) = num_field(ui, "A↕", "Leading (set to the font size × auto-leading when Auto)", lead * u, 0.1 * u..=5000.0 * u, unit, w) {
+ apply(app, ui.ctx(), json!({"leading": v / u, "metricsAsShown": true}));
}
});
row(ui, &mut |ui| {
@@ -2198,8 +2200,8 @@ fn type_sections(app: &mut PhotocraftApp, ui: &mut egui::Ui, character: bool, pa
}
});
row(ui, &mut |ui| {
- if let Some(v) = num_field(ui, "Aª", "Baseline shift", c.baseline_shift_pt, -1296.0..=1296.0, "pt", w) {
- apply(app, ui.ctx(), json!({"baselineShift": v}));
+ if let Some(v) = num_field(ui, "Aª", "Baseline shift", c.baseline_shift_pt * u, -1296.0 * u..=1296.0 * u, unit, w) {
+ apply(app, ui.ctx(), json!({"baselineShift": v / u}));
}
// Colour: label + a swatch filling the rest of the second column.
ui.horizontal(|ui| {
@@ -2306,24 +2308,24 @@ fn type_sections(app: &mut PhotocraftApp, ui: &mut egui::Ui, character: bool, pa
});
ui.add_space(ROW_GAP);
row(ui, &mut |ui| {
- if let Some(v) = num_field(ui, "→|", "Indent left margin", para.start_indent_pt, -1296.0..=1296.0, "pt", w) {
- apply(app, ui.ctx(), json!({"startIndent": v}));
+ if let Some(v) = num_field(ui, "→|", "Indent left margin", para.start_indent_pt * u, -1296.0 * u..=1296.0 * u, unit, w) {
+ apply(app, ui.ctx(), json!({"startIndent": v / u}));
}
- if let Some(v) = num_field(ui, "|←", "Indent right margin", para.end_indent_pt, -1296.0..=1296.0, "pt", w) {
- apply(app, ui.ctx(), json!({"endIndent": v}));
+ if let Some(v) = num_field(ui, "|←", "Indent right margin", para.end_indent_pt * u, -1296.0 * u..=1296.0 * u, unit, w) {
+ apply(app, ui.ctx(), json!({"endIndent": v / u}));
}
});
row(ui, &mut |ui| {
- if let Some(v) = num_field(ui, "¶→", "Indent first line", para.first_line_indent_pt, -1296.0..=1296.0, "pt", w) {
- apply(app, ui.ctx(), json!({"firstLineIndent": v}));
+ if let Some(v) = num_field(ui, "¶→", "Indent first line", para.first_line_indent_pt * u, -1296.0 * u..=1296.0 * u, unit, w) {
+ apply(app, ui.ctx(), json!({"firstLineIndent": v / u}));
}
});
row(ui, &mut |ui| {
- if let Some(v) = num_field(ui, "↑¶", "Add space before paragraph", para.space_before_pt, 0.0..=1296.0, "pt", w) {
- apply(app, ui.ctx(), json!({"spaceBefore": v}));
+ if let Some(v) = num_field(ui, "↑¶", "Add space before paragraph", para.space_before_pt * u, 0.0 * u..=1296.0 * u, unit, w) {
+ apply(app, ui.ctx(), json!({"spaceBefore": v / u}));
}
- if let Some(v) = num_field(ui, "¶↓", "Add space after paragraph", para.space_after_pt, 0.0..=1296.0, "pt", w) {
- apply(app, ui.ctx(), json!({"spaceAfter": v}));
+ if let Some(v) = num_field(ui, "¶↓", "Add space after paragraph", para.space_after_pt * u, 0.0 * u..=1296.0 * u, unit, w) {
+ apply(app, ui.ctx(), json!({"spaceAfter": v / u}));
}
});
let mut hy = para.hyphenate;
diff --git a/crates/ui-egui/src/type_tool_tests.rs b/crates/ui-egui/src/type_tool_tests.rs
index 7a23c9ccc..c2931fc46 100644
--- a/crates/ui-egui/src/type_tool_tests.rs
+++ b/crates/ui-egui/src/type_tool_tests.rs
@@ -1549,6 +1549,27 @@ mod multi_layer_formatting {
assert_eq!(text(h.state(), ids[1]).char_runs(), peer);
}
+ /// #6491: with Units & Rulers ▸ Type set to pixels, the Character and Paragraph fields show
+ /// and take pixels, like the options bar. 144 ppi so a pixel is not a point.
+ #[test]
+ fn character_and_paragraph_fields_follow_the_type_units_preference() {
+ use egui_kittest::kittest::NodeT;
+ let (mut app, ids) = fixture();
+ app.run("prefs.set", json!({"path": "unitsAndRulers.typeUnits", "value": "pixels"})).unwrap();
+ app.run("image.imageSize", json!({"resolution": 144, "resample": "none"})).unwrap();
+ selected(&mut app, &[ids[0]]);
+ let mut h = controls(app, true);
+ let shown = h.get_by_role_and_label(Role::SpinButton, "Font size").accesskit_node().numeric_value();
+ assert_eq!(shown, Some(24.0), "12 pt at 144 ppi");
+ number(&mut h, "Font size", "60");
+ number(&mut h, "Leading (set to the font size × auto-leading when Auto)", "84");
+ number(&mut h, "Baseline shift", "10");
+ number(&mut h, "Indent left margin", "9");
+ let c = style(h.state(), ids[0], 0);
+ assert_eq!((c.size_pt, c.leading_pt, c.baseline_shift_pt), (30.0, Some(42.0), 5.0));
+ assert_eq!(text(h.state(), ids[0]).paragraph_runs()[0].style.start_indent_pt, 4.5);
+ }
+
/// #5717: a layer scaled so its shown size or leading is past a field's range (2400 pt, or
/// 0.08 pt) keeps it while the Character panel and the options bar only show it.
#[test]