PhotoCraft: An open-source, photo editor written by scratch in pure Rust
New Document starts from the last document's settings (#1869)
kayrockscreenprintingfdfa57f456e8
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex 27487e13f..9570d5d68 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -362,6 +362,9 @@ pub fn confirm(app: &mut PhotocraftApp, id: u64) -> Result<Value, String> {if let Some(i) = app.session.active_index() {app.ui.views[i].fit_pending = true;}+ if r.is_ok() {+ crate::remember_new_document(app, &d.fields);+ }r}DialogKind::Command if crate::fill_ui::owns(&d.fields) => crate::fill_ui::confirm(app, &d.fields),diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 3ee2f5df8..5ce31d28f 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1541,6 +1541,30 @@ impl PhotocraftApp {}}+/// The New Document dialog's key in the preferences' `dialogs` map.+const NEW_DOCUMENT: &str = "file.new";++/// New Document fields its OK remembers (#1810): the size, resolution, mode, depth, background and+/// display units. Not the name, the preset or the clipboard size.+const NEW_DOCUMENT_REMEMBERED: [&str; 8] = ["width", "height", "resolution", "mode", "depth", "background", "__unit", "__resUnit"];++/// A remembered value the dialog can show (a corrupt preference is ignored).+fn remembered_new_document_value(key: &str, v: &serde_json::Value) -> bool {+ match key {+ "width" | "height" => v.as_u64().is_some_and(|n| (1..=300_000).contains(&n)),+ "resolution" => v.as_f64().is_some_and(|r| r.is_finite() && r > 0.0 && r <= 30_000.0),+ "depth" => v.as_u64().is_some_and(|d| matches!(d, 1 | 8 | 16 | 32)),+ _ => v.as_str().is_some_and(|s| !s.is_empty() && s.len() <= 64),+ }+}++/// Remember the New Document fields `f` its OK used, for the next New Document.+pub(crate) fn remember_new_document(app: &mut PhotocraftApp, f: &serde_json::Map<String, serde_json::Value>) {+ let kept: serde_json::Map<String, serde_json::Value> =+ NEW_DOCUMENT_REMEMBERED.iter().filter_map(|k| f.get(*k).filter(|v| remembered_new_document_value(k, v)).map(|v| (k.to_string(), v.clone()))).collect();+ app.session.prefs.edit(|p| p.dialogs.insert(NEW_DOCUMENT.into(), serde_json::Value::Object(kept)));+}+fn clip_signature(w: u32, h: u32, px: &[u8]) -> u64 {let mut sig = (w as u64) << 32 | h as u64;for b in px.iter().step_by(997) {@@ -1565,11 +1589,19 @@ impl PhotocraftApp {}}- /// File › New's fields: the defaults, plus the Clipboard preset (the clipboard image's size,- /// selected) when the clipboard holds an image. Opening the dialog is an explicit request, so- /// the OS clipboard is read here, as for a paste.+ /// File › New's fields: the defaults, then the settings of the last document made with the+ /// dialog (Photoshop starts from them, #1810), then the Clipboard preset (the clipboard image's+ /// size, selected) when the clipboard holds an image. Opening the dialog is an explicit+ /// request, so the OS clipboard is read here, as for a paste.pub(crate) fn new_document_fields(&mut self) -> serde_json::Map<String, serde_json::Value> {let mut f = crate::state::UiState::new_document_fields();+ if let Some(serde_json::Value::Object(saved)) = self.session.prefs().dialogs.get(NEW_DOCUMENT) {+ for k in NEW_DOCUMENT_REMEMBERED {+ if let Some(v) = saved.get(k).filter(|v| remembered_new_document_value(k, v)) {+ f.insert(k.into(), v.clone());+ }+ }+ }self.import_os_clipboard();if let Some(c) = self.session.clipboard.as_ref().filter(|c| !c.bounds.is_empty()) {crate::new_doc_ui::set_clipboard(&mut f, c.bounds.width(), c.bounds.height());@@ -1612,6 +1644,9 @@ mod transform_undo_tests;#[cfg(test)]mod move_auto_select_tests;+#[cfg(test)]+mod new_doc_remember_tests;+#[cfg(test)]mod hidden_layer_tests;diff --git a/crates/ui-egui/src/new_doc_remember_tests.rs b/crates/ui-egui/src/new_doc_remember_tests.rsnew file mode 100644index 000000000..3c0929333--- /dev/null+++ b/crates/ui-egui/src/new_doc_remember_tests.rs@@ -0,0 +1,78 @@+//! #1810: File › New starts from the last document made with the dialog (Photoshop does), with the+//! clipboard image's size still taking priority.++use serde_json::{Value, json};++use crate::PhotocraftApp;+use crate::state::DialogKind;++fn app() -> PhotocraftApp {+ PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default())+}++/// Open New Document, set `fields`, press Create.+fn create(app: &mut PhotocraftApp, fields: Value) {+ let f = app.new_document_fields();+ let id = app.ui.open_dialog(DialogKind::NewDocument, f);+ let d = app.ui.dialog_mut(id).unwrap();+ for (k, v) in fields.as_object().unwrap() {+ d.fields.insert(k.clone(), v.clone());+ }+ crate::dialogs::confirm(app, id).unwrap();+}++#[test]+fn new_document_starts_from_the_last_settings() {+ let mut app = app();+ // The first time: the defaults (1920 × 1080, 72 ppi).+ assert_eq!(app.new_document_fields(), crate::state::UiState::new_document_fields());+ create(+ &mut app,+ json!({"name": "Poster", "width": 640, "height": 480, "resolution": 300.0, "mode": "cmyk", "depth": 16, "background": "transparent", "__unit": "in", "__resUnit": "ppi"}),+ );+ let d = &app.session.active().unwrap().doc;+ assert_eq!((d.size.width, d.size.height, d.resolution_dpi), (640, 480, 300.0));+ // The next time: those settings, but not the name.+ let f = app.new_document_fields();+ for (k, v) in [+ ("width", json!(640)),+ ("height", json!(480)),+ ("resolution", json!(300.0)),+ ("mode", json!("cmyk")),+ ("depth", json!(16)),+ ("background", json!("transparent")),+ ("__unit", json!("in")),+ ("__resUnit", json!("ppi")),+ ] {+ assert_eq!(f.get(k), Some(&v), "{k}");+ }+ assert_eq!(f["name"], "Untitled-1");+ // They live in the preferences, so they survive a restart.+ assert_eq!(app.session.prefs().dialogs["file.new"]["resolution"], json!(300.0));++ // An image on the clipboard still sets the size (the Clipboard preset); the rest is remembered.+ app.run("select.rect", json!({"x": 0, "y": 0, "width": 123, "height": 45})).unwrap();+ app.run("edit.fill", json!({"color": "#336699"})).unwrap();+ app.run("edit.copy", json!({})).unwrap();+ let f = app.new_document_fields();+ assert_eq!((f["width"].as_u64(), f["height"].as_u64()), (Some(123), Some(45)));+ assert_eq!((f["mode"].as_str(), f["depth"].as_u64()), (Some("cmyk"), Some(16)));+}++#[test]+fn a_corrupt_remembered_value_is_ignored() {+ let mut app = app();+ app.session.prefs.edit(|p| p.dialogs.insert("file.new".into(), json!({"width": -5, "height": 600, "resolution": "x", "depth": 7, "mode": 3})));+ let f = app.new_document_fields();+ let defaults = crate::state::UiState::new_document_fields();+ assert_eq!(f["height"], json!(600), "a valid value is used");+ for k in ["width", "depth", "mode"] {+ assert_eq!(f.get(k), defaults.get(k), "{k}");+ }+ assert!(f.get("resolution").is_none());+ // A failed Create remembers nothing.+ let id = app.ui.open_dialog(DialogKind::NewDocument, f);+ app.ui.dialog_mut(id).unwrap().fields.insert("width".into(), json!(0));+ let _ = crate::dialogs::confirm(&mut app, id);+ assert_eq!(app.session.prefs().dialogs["file.new"]["height"], json!(600), "unchanged");+}