PhotoCraft: An open-source, photo editor written by scratch in pure Rust
New Document: blank presets follow the picked orientation and physical unit (part of #4668) (#4686)
Marco Arnulfo45ce763ebb1f
diff --git a/crates/ui-egui/src/new_doc_ui.rs b/crates/ui-egui/src/new_doc_ui.rsindex 9fe06a1a5..361c3af4e 100644--- a/crates/ui-egui/src/new_doc_ui.rs+++ b/crates/ui-egui/src/new_doc_ui.rs@@ -171,6 +171,33 @@ fn is_print(p: &Preset) -> bool {p.3 >= 300.0}+/// A built-in preset turned to the orientation picked with the Orientation buttons (#4668), so the+/// cards and what they apply follow it. Square presets and the Clipboard preset stay as they are.+fn oriented(p: &Preset, f: &Map<String, Value>) -> Preset {+ let portrait = match f.get("__orientation").and_then(Value::as_str) {+ Some("portrait") => true,+ Some("landscape") => false,+ _ => return *p,+ };+ if p.0 == CLIPBOARD || p.1 == p.2 || (p.2 > p.1) == portrait {+ return *p;+ }+ (p.0, p.2, p.1, p.3)+}++/// The unit a built-in preset shows and applies: a print preset in the physical unit picked for+/// Width/Height (#4668), else in inches; a screen preset in pixels.+fn preset_unit(p: &Preset, f: &Map<String, Value>) -> String {+ let unit = get_s(f, "__unit", "px");+ if !is_print(p) {+ "px".into()+ } else if unit != "px" && UNITS.iter().any(|u| u.0 == unit) {+ unit+ } else {+ "in".into()+ }+}+/// 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@@ -193,12 +220,13 @@ pub fn at_pref_resolutions(p: &Preset, units: &UnitsAndRulers) -> Preset {/// 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);+ let unit = preset_unit(p, f);f.remove("__savedPreset");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" }));+ f.insert("__unit".into(), json!(unit));}/// Fields `file.new` takes (drops the dialog's `__` UI keys).@@ -324,18 +352,17 @@ impl Card {}}- /// The size line, in the unit the preset is specified in (a recent one: the unit it was made in).- fn size_line(&self, units: &UnitsAndRulers) -> String {+ /// The size line, in the unit the preset applies (a recent one: the unit it was made in).+ fn size_line(&self, units: &UnitsAndRulers, f: &Map<String, Value>) -> String {let (w, h, ppi) = self.size(units);let unit = match self {- Card::Builtin(p) if is_print(p) => "in",- Card::Builtin(_) => "px",- Card::Recent(s) => s.unit.as_str(),+ Card::Builtin(p) => preset_unit(p, f),+ Card::Recent(s) => s.unit.clone(),};if unit == "px" {format!("{w} x {h} px @ {} ppi", widgets::fmt_num(f64::from(ppi)))} else {- let v = |n: u32| widgets::fmt_num(f64::from(to_unit(n as f32, unit, ppi)));+ let v = |n: u32| widgets::fmt_num(f64::from(to_unit(n as f32, &unit, ppi)));format!("{} x {} {unit} @ {} ppi", v(w), v(h), widgets::fmt_num(f64::from(ppi)))}}@@ -469,6 +496,13 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<StrinSome(c) if c.0 != CATEGORIES[0].0 => c.1.iter().copied().map(Card::Builtin).collect(),_ => recent_cards(f, &app.session.presets.recent_documents, &units),};+ let presets: Vec<Card> = presets+ .into_iter()+ .map(|c| match c {+ Card::Builtin(p) => Card::Builtin(oriented(&p, f)),+ recent => recent,+ })+ .collect();let chosen = get_s(f, "__preset", "");ui.horizontal_top(|ui| {// Left: preset grid.@@ -511,7 +545,7 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strinlet title = card_title(ui.painter(), tl!(c.title()), 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 size = c.size_line(&units);+ let size = c.size_line(&units, f);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 = match c {// A recent document's tooltip names everything it restores.@@ -593,8 +627,12 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strinlet (w, h) = (get_f(f, "width", 1920.0), get_f(f, "height", 1080.0));for (icon, portrait) in [("rectangle-vertical", true), ("rectangle-horizontal", false)] {// Tooltip strings are looked up via `icons::button` → `tl!`; enum/state is aspect swap only.- if icons::button(ui, icon, 24.0, (h > w) == portrait, if portrait { "Portrait" } else { "Landscape" }).clicked() && (h > w) != portrait {- swap_size(f);+ if icons::button(ui, icon, 24.0, (h > w) == portrait, if portrait { "Portrait" } else { "Landscape" }).clicked() {+ // The pick also turns the presets (`oriented`), even when the size already has it.+ f.insert("__orientation".into(), json!(if portrait { "portrait" } else { "landscape" }));+ if (h > w) != portrait {+ swap_size(f);+ }}}});@@ -807,7 +845,7 @@ mod tests {let mut f = Map::new();set_clipboard(&mut f, 20, 30);let cards = recent_cards(&f, &[settings(800, 600, 72.0), settings(1920, 1080, 72.0)], &UnitsAndRulers::default());- let titles: Vec<(&str, String)> = cards.iter().map(|c| (c.title(), c.size_line(&UnitsAndRulers::default()))).collect();+ let titles: Vec<(&str, String)> = cards.iter().map(|c| (c.title(), c.size_line(&UnitsAndRulers::default(), &f))).collect();assert_eq!(titles,[@@ -1320,6 +1358,45 @@ mod tests {assert_eq!(created(&h), (256, 512, 72.0));}+ /// #4668: once an orientation is picked, the presets turn to it, on the cards and when applied.+ #[test]+ fn picked_orientation_turns_the_presets() {+ let mut h = harness();+ h.get_by_label("Print").click();+ h.run_steps(2);+ // The default 1920 x 1080 is already landscape: Landscape swaps nothing, but is picked.+ let label = h.get_by_label("Orientation").rect();+ let gap = h.ctx.global_style().spacing.item_spacing.x;+ click_at(&mut h, egui::pos2(label.right() + gap + 24.0 + gap + 12.0, label.center().y));+ assert_eq!((fields(&h)["width"].as_u64(), fields(&h)["height"].as_u64()), (Some(1920), Some(1080)));+ let _ = preset_title_at(&h, "11.7 x 8.3 in @ 300 ppi");+ let at = preset_title_at(&h, "A4");+ click_at(&mut h, at);+ enter(&mut h);+ assert_eq!(created(&h), (3508, 2480, 300.0));+ }++ /// #4668: a physical unit picked for Width/Height is the one print presets show and keep.+ #[test]+ fn picked_physical_unit_is_the_print_presets_unit() {+ let mut h = harness();+ let mut f = fields(&h);+ f.insert("__unit".into(), serde_json::json!("mm"));+ set_fields(&mut h, f);+ h.get_by_label("Print").click();+ h.run_steps(2);+ let _ = preset_title_at(&h, "210 x 297 mm @ 300 ppi");+ let at = preset_title_at(&h, "A4");+ click_at(&mut h, at);+ assert_eq!(fields(&h)["__unit"], "mm");+ // Screen presets stay in pixels.+ h.get_by_label("Web").click();+ h.run_steps(2);+ let _ = preset_title_at(&h, "1024 x 768 px @ 72 ppi");+ enter(&mut h);+ assert_eq!(created(&h), (2480, 3508, 300.0));+ }+#[test]fn hovering_preset_tabs_shows_pointing_hand_not_text_cursor() {let mut h = harness();