Skip to content

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

feat(ui): the Type units preference drives the type metric units (#204) (#2587)

Reynold Hude58a0a77803
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs
index 5c3448bc1..2df7e853b 100644
--- a/crates/engine/src/prefs.rs
+++ b/crates/engine/src/prefs.rs
@@ -1001,7 +1001,6 @@ pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &[
"performance.legacyCompositing",
"scratchDisks.disks",
"cursors.brushPreviewColor",
- "unitsAndRulers.typeUnits",
"unitsAndRulers.columnWidth",
"unitsAndRulers.gutter",
"unitsAndRulers.printResolution",
diff --git a/crates/ui-egui/src/type_panels_ui.rs b/crates/ui-egui/src/type_panels_ui.rs
index d7217a1c1..761dbc255 100644
--- a/crates/ui-egui/src/type_panels_ui.rs
+++ b/crates/ui-egui/src/type_panels_ui.rs
@@ -371,6 +371,9 @@ fn options_editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, paragraph: bool, s
}
});
let defined = |attrs: &Value, k: &str| attrs.get(k).is_some();
+ // Preferences ▸ Units & Rulers ▸ Type: the model stores points; the fields may show pixels
+ // (by the document's resolution) or millimetres.
+ let (type_unit, unit_label) = crate::type_tool::type_unit(app);
egui::Grid::new(("style-opts", paragraph, id)).num_columns(3).spacing([6.0, 3.0]).show(ui, |ui| {
// (label, model key, panel key, kind)
let rows: &[(&str, &str, &str, &str)] = &[
@@ -457,9 +460,9 @@ fn options_editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, paragraph: bool, s
}
}
"pt" => {
- let mut x = v.as_f64().unwrap_or(0.0) as f32;
- if crate::widgets::value_field(ui, &mut x, -1296.0..=1296.0, " pt", 70.0).changed() {
- set = Some(json!({ key: x }));
+ let mut x = v.as_f64().unwrap_or(0.0) as f32 * type_unit;
+ if crate::widgets::value_field(ui, &mut x, -1296.0 * type_unit..=1296.0 * type_unit, &format!(" {unit_label}"), 70.0).changed() {
+ set = Some(json!({ key: x / type_unit }));
}
}
_ => {
diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs
index 55e1dad24..2b5868eaa 100644
--- a/crates/ui-egui/src/type_tool.rs
+++ b/crates/ui-egui/src/type_tool.rs
@@ -1016,6 +1016,17 @@ fn shown_scale(app: &PhotocraftApp) -> f32 {
if k.is_finite() && k > 1e-3 { k } else { 1.0 }
}
+/// The display unit for type metrics (Preferences ▸ Units & Rulers ▸ Type): the multiplier from
+/// the model's points to the unit, and its label. Pixels follow the document's resolution.
+pub(crate) fn type_unit(app: &PhotocraftApp) -> (f32, &'static str) {
+ let dpi = app.session.active().map_or(72.0, |d| d.doc.resolution_dpi.max(1.0));
+ match app.session.prefs().units_and_rulers.type_units {
+ photocraft_engine::prefs::TypeUnit::Points => (1.0, "pt"),
+ photocraft_engine::prefs::TypeUnit::Pixels => (dpi / 72.0, "px"),
+ photocraft_engine::prefs::TypeUnit::Millimeters => (25.4 / 72.0, "mm"),
+ }
+}
+
/// Coalesce key while a field is being scrubbed: every step of one drag shares one history step
/// (Photoshop's single step per scrub, #124). Unique per press, so two drags are two steps.
fn drag_key(ctx: &egui::Context) -> Option<String> {
@@ -1070,7 +1081,7 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
Some((tl.font_family.clone(), run.as_ref().map(selected_style).unwrap_or_default(), size, styles_at(app).map_or(tl.color, |(c, _)| c.color)))
});
let o = app.ui.tool_options.clone();
- let (mut fam, mut style, mut size) = match &shown {
+ let (mut fam, mut style, size) = match &shown {
Some((f, s, z, _)) => (f.clone(), if s.is_empty() { "Regular".into() } else { s.clone() }, *z),
None => (o.type_font.clone(), o.type_style.clone(), o.type_size),
};
@@ -1099,7 +1110,10 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
}
let (r, _) = ui.allocate_exact_size(egui::vec2(18.0, 22.0), egui::Sense::hover());
crate::icons::paint(ui, r, "type", 13.0, t.icon);
- if crate::widgets::value_field(ui, &mut size, 1.0..=1296.0, "pt", 66.0).changed() {
+ let (unit, label) = type_unit(app);
+ let mut display = size * unit;
+ if crate::widgets::value_field(ui, &mut display, unit..=1296.0 * unit, label, 66.0).changed() {
+ let size = display / unit;
app.ui.tool_options.type_size = size;
apply(app, ui.ctx(), json!({"size": size, "metricsAsShown": true}));
}
@@ -1684,6 +1698,20 @@ mod tests {
assert!(app.ui.text_edit.is_none());
}
+ /// Preferences ▸ Units & Rulers ▸ Type switches the display unit of type metrics: points by
+ /// default, pixels by the document's resolution, or millimetres.
+ #[test]
+ fn type_unit_follows_the_units_and_rulers_preference() {
+ let mut app = app();
+ assert_eq!(type_unit(&app), (1.0, "pt"));
+ app.run("prefs.set", json!({"path": "unitsAndRulers.typeUnits", "value": "pixels"})).unwrap();
+ assert_eq!(type_unit(&app), (1.0, "px"), "a 72 ppi document: 1 pt is 1 px");
+ app.run("image.imageSize", json!({"resolution": 144, "resample": "none"})).unwrap();
+ assert_eq!(type_unit(&app), (2.0, "px"), "144 ppi: a point is two pixels");
+ app.run("prefs.set", json!({"path": "unitsAndRulers.typeUnits", "value": "mm"})).unwrap();
+ assert!((type_unit(&app).0 - 25.4 / 72.0).abs() < 1e-6, "millimetres ignore the resolution");
+ }
+
/// Preferences ▸ Type ▸ Fill new type layers with placeholder text: off, a click creates an
/// empty layer (nothing selected) and committing without typing removes it again.
#[test]
diff --git a/docs/scorecard.md b/docs/scorecard.md
index 8581358a8..54f831f88 100644
--- a/docs/scorecard.md
+++ b/docs/scorecard.md
@@ -9,7 +9,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js
| [Performance](#performance) | 3 | 22 | 22 | 56 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 16 not run, 6 not measurable yet (baseline 2026-10-05) |
| [File compatibility](#file-compatibility) | 5 | 11 | 11 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 236/309, round trip ≥ 308/309; photoshop oracles: oracle ≥ 133/256, round trip ≥ 258/256 |
| [Tools](#tools) | 21 | 17 | 4 | |
-| [Workspace and UI](#workspace-and-ui) | 6 | 7 | 10 | settings that do nothing: 42 of 152 (target 0) |
+| [Workspace and UI](#workspace-and-ui) | 6 | 7 | 10 | settings that do nothing: 41 of 152 (target 0) |
| [Type](#type) | 0 | 3 | 6 | |
| [Automation](#automation) | 3 | 2 | 6 | 630 of 630 menu items live (100.0%) |
| [Reliability](#reliability) | 5 | 2 | 0 | never-crash lints on 28 of 28 crates; panic_hunt present |
@@ -175,9 +175,9 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t
## Workspace and UI
-**Settings that do nothing: 42 of 152** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.
+**Settings that do nothing: 41 of 152** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.
-Unread: `general.color_picker`, `general.beep_when_done`, `general.always_create_smart_objects_when_placing`, `general.animated_zoom`, `general.zoom_resizes_windows`, `workspace.auto_collapse_icon_panels`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `workspace.enable_narrow_options_bar`, `tools.enable_flick_panning`, `tools.vary_round_brush_hardness_on_hud`, `tools.show_transformation_values`, `file_handling.image_previews`, `file_handling.save_in_background`, `file_handling.ignore_exif_profile_tag`, `file_handling.maximize_psd_compatibility`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `cursors.brush_preview_color`, `units_and_rulers.type_units`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `units_and_rulers.print_resolution`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.missing_glyph_protection`, `type_.show_font_names_in_english`, `type_.text_engine`, `type_.font_preview`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `enhanced_controls.touch_gestures`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`, `integrations.allow_agent_control`, `integrations.control_port`.
+Unread: `general.color_picker`, `general.beep_when_done`, `general.always_create_smart_objects_when_placing`, `general.animated_zoom`, `general.zoom_resizes_windows`, `workspace.auto_collapse_icon_panels`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `workspace.enable_narrow_options_bar`, `tools.enable_flick_panning`, `tools.vary_round_brush_hardness_on_hud`, `tools.show_transformation_values`, `file_handling.image_previews`, `file_handling.save_in_background`, `file_handling.ignore_exif_profile_tag`, `file_handling.maximize_psd_compatibility`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `cursors.brush_preview_color`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `units_and_rulers.print_resolution`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.missing_glyph_protection`, `type_.show_font_names_in_english`, `type_.text_engine`, `type_.font_preview`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `enhanced_controls.touch_gestures`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`, `integrations.allow_agent_control`, `integrations.control_port`.
| Id | Target | Status | Issue | Note |
|---|---|---|---|---|