Skip to content

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

New Document: presets follow Units & Rulers › Preset Resolutions (#204) (#3126)

Paulo Viniciusd45b3718cef1
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs
index 469aeeabd..eb8151508 100644
--- a/crates/engine/src/prefs.rs
+++ b/crates/engine/src/prefs.rs
@@ -1008,7 +1008,6 @@ pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &[
"scratchDisks.disks",
"unitsAndRulers.columnWidth",
"unitsAndRulers.gutter",
- "unitsAndRulers.printResolution",
"plugIns.showExtensionPanels",
"plugIns.allowScriptsToConnect",
"plugIns.generatorEnabled",
diff --git a/crates/ui-egui/src/new_doc_ui.rs b/crates/ui-egui/src/new_doc_ui.rs
index 43db284c3..dcb7b3449 100644
--- a/crates/ui-egui/src/new_doc_ui.rs
+++ b/crates/ui-egui/src/new_doc_ui.rs
@@ -6,6 +6,8 @@
use egui::{Align2, Rect, RichText, Sense, Stroke, StrokeKind, pos2, vec2};
use serde_json::{Map, Value, json};
+use photocraft_engine::prefs::UnitsAndRulers;
+
use crate::theme::Tokens;
use crate::{icons, widgets};
@@ -111,7 +113,7 @@ pub fn set_clipboard(f: &mut Map<String, Value>, w: u32, h: u32) {
return;
}
f.insert("__clipboard".into(), json!([w, h]));
- apply_preset(f, &(CLIPBOARD, w, h, 72.0));
+ apply_preset(f, &(CLIPBOARD, w, h, 72.0), &UnitsAndRulers::default());
}
/// The clipboard image's size, when the dialog offers the Clipboard preset.
@@ -163,15 +165,39 @@ pub fn swap_size(f: &mut Map<String, Value>) {
}
}
-/// Apply a preset to the dialog fields.
-pub fn apply_preset(f: &mut Map<String, Value>, p: &Preset) {
+/// Print and photo presets (listed at 300 ppi) are specified in inches, screen presets in pixels.
+fn is_print(p: &Preset) -> bool {
+ p.3 >= 300.0
+}
+
+/// A built-in preset at Preferences › Units & Rulers › New Document Preset Resolutions, as in
+/// Photoshop: a print preset keeps its size in inches at the Print Resolution (Letter at 200 ppi is
+/// 1700 x 2200 px), a screen preset keeps its pixels at the Screen Resolution. The Clipboard preset
+/// is the copied pixels and keeps its own resolution. A resolution that isn't a positive finite
+/// number leaves the preset as listed.
+pub fn at_pref_resolutions(p: &Preset, units: &UnitsAndRulers) -> Preset {
+ let ppi = (if is_print(p) { units.print_resolution } else { units.screen_resolution }) as f32;
+ if p.0 == CLIPBOARD || !(ppi.is_finite() && ppi > 0.0) {
+ return *p;
+ }
+ if !is_print(p) {
+ return (p.0, p.1, p.2, ppi);
+ }
+ let scale = ppi / p.3;
+ let px = |v: u32| (v as f32 * scale).round().clamp(1.0, 300_000.0) as u32;
+ (p.0, px(p.1), px(p.2), ppi)
+}
+
+/// Apply a built-in preset (at the preference resolutions, see [`at_pref_resolutions`]) to the
+/// dialog fields.
+pub fn apply_preset(f: &mut Map<String, Value>, p: &Preset, units: &UnitsAndRulers) {
+ let (name, w, h, ppi) = at_pref_resolutions(p, units);
f.remove("__savedPreset");
- f.insert("width".into(), json!(p.1));
- f.insert("height".into(), json!(p.2));
- f.insert("resolution".into(), json!(p.3));
- f.insert("__preset".into(), json!(p.0));
- // Print and photo presets are specified in inches, screen presets in pixels.
- f.insert("__unit".into(), json!(if p.3 >= 300.0 { "in" } else { "px" }));
+ f.insert("width".into(), json!(w));
+ f.insert("height".into(), json!(h));
+ f.insert("resolution".into(), json!(ppi));
+ f.insert("__preset".into(), json!(name));
+ f.insert("__unit".into(), json!(if is_print(p) { "in" } else { "px" }));
}
/// Fields `file.new` takes (drops the dialog's `__` UI keys).
@@ -358,6 +384,7 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strin
// The clipboard image's size comes first among the Recent presets.
let presets: Vec<Preset> = clipboard_preset(f).filter(|_| cat == CATEGORIES[0].0).into_iter().chain(presets.iter().copied()).collect();
let chosen = get_s(f, "__preset", "");
+ let units = app.session.prefs().units_and_rulers.clone();
ui.horizontal_top(|ui| {
// Left: preset grid.
ui.vertical(|ui| {
@@ -372,7 +399,8 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strin
for row in presets.chunks(3) {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 8.0;
- for p in row {
+ for listed in row {
+ let p = &at_pref_resolutions(listed, &units);
let (r, resp) = ui.allocate_exact_size(card, Sense::click());
let on = chosen == p.0;
ui.painter().rect_filled(
@@ -393,8 +421,7 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strin
let title = card_title(ui.painter(), tl!(p.0), card.x - 12.0, t.text);
let elided = title.elided;
ui.painter().galley(pos2(r.center().x, r.top() + 70.0 - title.size().y / 2.0), title, t.text);
- let unit = if p.3 >= 300.0 { "in" } else { "px" };
- let size = if unit == "in" {
+ let size = if is_print(listed) {
format!(
"{} x {} in @ {} ppi",
widgets::fmt_num(to_unit(p.1 as f32, "in", p.3) as f64),
@@ -407,7 +434,7 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strin
ui.painter().text(pos2(r.center().x, r.top() + 97.0), Align2::CENTER_CENTER, size, egui::FontId::proportional(10.5), t.text_faint);
let resp = if elided { resp.on_hover_text(tl!(p.0)) } else { resp };
if resp.clicked() {
- apply_preset(f, p);
+ apply_preset(f, listed, &units);
create |= resp.double_clicked() || resp.triple_clicked();
}
}
@@ -577,7 +604,7 @@ mod tests {
fn resolution_keeps_physical_size_in_physical_units_and_pixels_in_px() {
let a4 = CATEGORIES.iter().find(|c| c.0 == "Print").unwrap().1.iter().find(|p| p.0 == "A4").unwrap();
let mut f = crate::state::UiState::new_document_fields();
- apply_preset(&mut f, a4);
+ apply_preset(&mut f, a4, &Default::default());
f.insert("__unit".into(), json!("in"));
set_resolution(&mut f, 150.0);
let p = command_params(&f);
@@ -585,13 +612,48 @@ mod tests {
assert!(!f.contains_key("__preset"));
let mut f = crate::state::UiState::new_document_fields();
- apply_preset(&mut f, a4);
+ apply_preset(&mut f, a4, &Default::default());
f.insert("__unit".into(), json!("px"));
set_resolution(&mut f, 150.0);
let p = command_params(&f);
assert_eq!((p["width"].clone(), p["height"].clone(), p["resolution"].clone()), (json!(2480), json!(3508), json!(150.0)));
}
+ /// Units & Rulers › New Document Preset Resolutions: print presets keep their inches at the
+ /// Print Resolution, screen presets keep their pixels at the Screen Resolution.
+ #[test]
+ fn presets_follow_the_preset_resolution_preferences() {
+ let find = |cat: &str, name: &str| *CATEGORIES.iter().find(|c| c.0 == cat).unwrap().1.iter().find(|p| p.0 == name).unwrap();
+ let (letter, web) = (find("Print", "Letter"), find("Web", "Web Minimum"));
+ let stock = UnitsAndRulers::default();
+ assert_eq!(at_pref_resolutions(&letter, &stock), letter, "the defaults are the listed sizes");
+ assert_eq!(at_pref_resolutions(&web, &stock), web);
+
+ let units = UnitsAndRulers { print_resolution: 200.0, screen_resolution: 96.0, ..stock.clone() };
+ assert_eq!(at_pref_resolutions(&letter, &units), ("Letter", 1700, 2200, 200.0), "8.5 x 11 in at 200 ppi");
+ assert_eq!(at_pref_resolutions(&find("Print", "A4"), &units), ("A4", 1653, 2339, 200.0));
+ assert_eq!(at_pref_resolutions(&web, &units), ("Web Minimum", 1024, 768, 96.0), "pixels kept");
+ let clipboard = (CLIPBOARD, 123, 45, 72.0);
+ assert_eq!(at_pref_resolutions(&clipboard, &units), clipboard, "the copied pixels keep their resolution");
+
+ let mut f = crate::state::UiState::new_document_fields();
+ apply_preset(&mut f, &letter, &units);
+ assert_eq!((f["width"].as_u64(), f["height"].as_u64(), f["resolution"].as_f64()), (Some(1700), Some(2200), Some(200.0)));
+ assert_eq!((f["__preset"].as_str(), f["__unit"].as_str()), (Some("Letter"), Some("in")), "still a print preset below 300 ppi");
+ apply_preset(&mut f, &web, &units);
+ assert_eq!((f["width"].as_u64(), f["resolution"].as_f64(), f["__unit"].as_str()), (Some(1024), Some(96.0), Some("px")));
+
+ // Out-of-range values (a hand-edited preferences file) never make an unusable size.
+ for bad in [0.0, -5.0, f64::NAN, f64::INFINITY] {
+ let units = UnitsAndRulers { print_resolution: bad, screen_resolution: bad, ..stock.clone() };
+ assert_eq!(at_pref_resolutions(&letter, &units), letter, "{bad}");
+ assert_eq!(at_pref_resolutions(&web, &units), web, "{bad}");
+ }
+ let huge = UnitsAndRulers { print_resolution: 1e9, ..stock };
+ let (_, w, h, _) = at_pref_resolutions(&find("Art & Illustration", "Poster"), &huge);
+ assert_eq!((w, h), (300_000, 300_000), "clamped to the file.new limit");
+ }
+
#[test]
fn preset_card_titles_fit_the_card_in_every_language() {
let ctx = egui::Context::default();
@@ -640,7 +702,7 @@ mod tests {
fn preset_sets_size_resolution_and_create_params_drop_ui_keys() {
let mut f = crate::state::UiState::new_document_fields();
let a4 = CATEGORIES.iter().find(|c| c.0 == "Print").unwrap().1.iter().find(|p| p.0 == "A4").unwrap();
- apply_preset(&mut f, a4);
+ apply_preset(&mut f, a4, &Default::default());
let p = command_params(&f);
assert_eq!(p["width"], 2480);
assert_eq!(p["height"], 3508);
@@ -680,7 +742,7 @@ mod tests {
assert_eq!(command_params(&f), expected);
f.insert("width".into(), json!(42));
assert_eq!(session.presets.documents[0], preset, "editing the form does not edit the snapshot");
- apply_preset(&mut f, &(CLIPBOARD, 20, 30, 72.0));
+ apply_preset(&mut f, &(CLIPBOARD, 20, 30, 72.0), &Default::default());
assert!(f.get("__savedPreset").is_none());
assert_eq!((f["width"].as_u64(), f["height"].as_u64()), (Some(20), Some(30)));
}
@@ -914,7 +976,7 @@ mod tests {
let mut h = harness();
let mut f = fields(&h);
super::super::set_clipboard(&mut f, 37, 23);
- apply_preset(&mut f, &("Other size", 100, 200, 300.0));
+ apply_preset(&mut f, &("Other size", 100, 200, 300.0), &Default::default());
f.extend(serde_json::json!({"mode":"gray","depth":8,"background":"white"}).as_object().unwrap().clone());
set_fields(&mut h, f);
let at = preset_title_at(&h, super::super::CLIPBOARD);
@@ -963,6 +1025,29 @@ mod tests {
assert_pixel(&mut h, [0.125, 0.375, 0.625, 1.0]);
}
+ #[test]
+ fn preset_cards_use_the_preset_resolution_preferences() {
+ let mut h = harness();
+ h.state_mut().run("prefs.set", serde_json::json!({"path": "unitsAndRulers.printResolution", "value": 150.0})).unwrap();
+ h.state_mut().run("prefs.set", serde_json::json!({"path": "unitsAndRulers.screenResolution", "value": 96.0})).unwrap();
+ h.get_by_label("Photo").click();
+ h.run_steps(2);
+ let at = preset_title_at(&h, "Landscape, 6 x 4");
+ click_at(&mut h, at);
+ assert_eq!(fields(&h)["__unit"], "in");
+ second_click_creates_once(&mut h, at);
+ assert_eq!(created(&h), (900, 600, 150.0), "6 x 4 in at 150 ppi");
+
+ h.state_mut().ui.open_dialog(DialogKind::NewDocument, UiState::new_document_fields());
+ h.run_steps(2);
+ h.get_by_label("Web").click();
+ h.run_steps(2);
+ let at = preset_title_at(&h, "Web Minimum");
+ click_at(&mut h, at);
+ enter(&mut h);
+ assert_eq!(created(&h), (1024, 768, 96.0));
+ }
+
#[test]
fn arithmetic_dimensions_create_the_evaluated_size() {
let mut h = harness();
@@ -999,7 +1084,7 @@ mod tests {
let mut h = harness();
let mut f = fields(&h);
let web = CATEGORIES.iter().find(|c| c.0 == "Web").unwrap().1.iter().find(|p| p.0 == "Web Minimum").unwrap();
- apply_preset(&mut f, web);
+ apply_preset(&mut f, web, &Default::default());
set_fields(&mut h, f);
type_into(&mut h, 0, "512");
type_into(&mut h, 1, "512");
diff --git a/docs/scorecard.md b/docs/scorecard.md
index 3357d5465..c0c15c318 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, 17 not run, 5 not measurable yet (baseline 2026-10-05) |
| [File compatibility](#file-compatibility) | 6 | 12 | 9 | 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) | 25 | 16 | 2 | |
-| [Workspace and UI](#workspace-and-ui) | 9 | 8 | 6 | settings that do nothing: 33 of 154 (target 0) |
+| [Workspace and UI](#workspace-and-ui) | 9 | 8 | 6 | settings that do nothing: 32 of 154 (target 0) |
| [Type](#type) | 1 | 4 | 4 | |
| [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 |
@@ -176,9 +176,9 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t
## Workspace and UI
-**Settings that do nothing: 33 of 154** (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: 32 of 154** (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.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`, `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`, `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_.text_engine`, `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.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`, `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`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.missing_glyph_protection`, `type_.text_engine`, `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 |
|---|---|---|---|---|