diff --git a/crates/ui-egui/src/dialogs/new_doc_ui.rs b/crates/ui-egui/src/dialogs/new_doc_ui.rs index 2cba2a6af3..acefd7abad 100644 --- a/crates/ui-egui/src/dialogs/new_doc_ui.rs +++ b/crates/ui-egui/src/dialogs/new_doc_ui.rs @@ -479,44 +479,70 @@ fn saved_presets(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map< create } +/// The name Save Document Preset proposes: the first `Saved N` no saved preset has (#14043). +pub fn next_saved_name(presets: &[photocraft_engine::document_preset_cmds::DocumentPreset]) -> String { + // n presets leave one of `Saved 1`..`Saved n+1` free. + let free = (1..=presets.len().saturating_add(1)).map(|n| format!("Saved {n}")).find(|name| !presets.iter().any(|p| p.name.eq_ignore_ascii_case(name))); + free.unwrap_or_else(|| "Saved".into()) +} + +/// Whether the Save Document Preset pane stands in for Preset Details. +fn saving_preset(f: &Map) -> bool { + f.get("__savingPreset").and_then(Value::as_bool) == Some(true) +} + +/// Photoshop's Save Document Preset pane, opened by the icon beside Name (#14043): the header +/// SAVE DOCUMENT PRESET, a name proposing the next free `Saved N` with its text selected, and +/// Save Preset / Cancel. Cancel goes back to Preset Details without saving. fn save_preset(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map) { + let t = Tokens::get(ui.ctx()); + ui.label(RichText::new(tl!("SAVE DOCUMENT PRESET")).size(11.0).color(t.text_faint)); + ui.add_space(4.0); + // Consume before TextEdit sees Enter, so naming can never confirm the outer dialog. + let enter = ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter)); + let mut name = f.get("__presetName").and_then(Value::as_str).map_or_else(|| next_saved_name(&app.session.presets.documents), str::to_owned); + let r = ui + .add(egui::TextEdit::singleline(&mut name).id_salt("document-preset-name").desired_width(250.0).font(egui::FontId::proportional(15.0)).char_limit(255)) + .accessible_name(tl!("Preset Name")); + f.insert("__presetName".into(), json!(name)); + crate::field_tab::register(ui.ctx(), r.id); + // The pane opens with the proposed name selected, so typing replaces it. + if f.remove("__presetNameFocus").is_some() { + crate::field_tab::focus(ui.ctx(), r.id); + } + if r.gained_focus() { + crate::field_tab::select_all(ui.ctx(), r.id, &name); + } ui.add_space(12.0); - if f.get("__savingPreset").and_then(Value::as_bool) != Some(true) { - if widgets::secondary_button(ui, tl!("Save Preset…"), 240.0).clicked() { - f.insert("__savingPreset".into(), json!(true)); - f.remove("__presetError"); - } - } else { - // Consume before TextEdit sees Enter, so naming can never confirm the outer dialog. - let enter = ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter)); - let label = ui.label(tl!("Preset Name")); - let mut name = get_s(f, "__presetName", ""); - ui.add(egui::TextEdit::singleline(&mut name).id_salt("document-preset-name").desired_width(240.0).char_limit(255)).labelled_by(label.id); - f.insert("__presetName".into(), json!(name)); - ui.horizontal(|ui| { - if widgets::secondary_button(ui, tl!("Save"), 110.0).clicked() || enter { - match app.run("document.presets.save", json!({"name":name,"settings":document_settings(f)})) { - Ok(_) => { - if let Some(preset) = app.session.presets.documents.last() { - apply_saved_preset(f, preset); - } - f.insert("__category".into(), json!("Saved")); - f.remove("__savingPreset"); - f.remove("__presetName"); - f.remove("__presetError"); - ui.ctx().request_repaint(); - } - Err(e) => { - f.insert("__presetError".into(), json!(e.to_string())); + ui.horizontal(|ui| { + if widgets::secondary_button(ui, tl!("Save Preset"), 120.0).clicked() || enter { + match app.run("document.presets.save", json!({"name":name,"settings":document_settings(f)})) { + Ok(_) => { + if let Some(preset) = app.session.presets.documents.last() { + apply_saved_preset(f, preset); } + f.insert("__category".into(), json!("Saved")); + f.remove("__savingPreset"); + f.remove("__presetName"); + f.remove("__presetError"); + ui.ctx().request_repaint(); + } + Err(e) => { + f.insert("__presetError".into(), json!(e.to_string())); } } - if widgets::secondary_button(ui, tl!("Cancel"), 110.0).clicked() { - f.remove("__savingPreset"); - f.remove("__presetError"); - } - }); - } + } + if widgets::secondary_button(ui, tl!("Cancel"), 120.0).clicked() { + f.remove("__savingPreset"); + f.remove("__presetName"); + f.remove("__presetError"); + } + }); + preset_error(ui, f); +} + +/// The last failed save or delete of a preset. +fn preset_error(ui: &mut egui::Ui, f: &Map) { if let Some(error) = f.get("__presetError").and_then(Value::as_str) { // The engine's message stays copyable, unlike the static labels around it. ui.add(egui::Label::new(error).wrap().selectable(true)); @@ -642,12 +668,31 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map, name: &str) { - h.get_by_label("Save Preset…").click(); - h.run_steps(2); - h.get_by_role_and_label(Role::TextInput, "Preset Name").click(); - h.run_steps(1); + h.get_by_label("Save Document Preset").click(); + h.run_steps(3); h.event(egui::Event::Text(name.into())); h.run_steps(1); } @@ -402,7 +401,7 @@ mod dialog { f.insert("background".into(), serde_json::json!("transparent")); set_fields(&mut h, f); name_preset(&mut h, " Product square "); - h.get_by_label("Save").click(); + h.get_by_label("Save Preset").click(); h.run_steps(3); assert!(h.state().session.documents().is_empty(), "saving does not create a document"); assert_eq!(h.state().session.presets.documents[0].name, "Product square"); @@ -447,7 +446,7 @@ mod dialog { assert!(h.state().session.documents().is_empty(), "Enter must not also press Create"); assert_eq!(h.state().ui.dialogs.len(), 1); name_preset(&mut h, " ENTER TEST "); - h.get_by_label("Save").click(); + h.get_by_label("Save Preset").click(); h.run_steps(2); assert!(fields(&h).get("__presetError").is_some()); assert_eq!(h.state().session.presets.documents.len(), 1); @@ -455,10 +454,10 @@ mod dialog { h.get_by_label("Cancel").click(); h.run_steps(2); name_preset(&mut h, ""); - // Cancel keeps the entered name; clear it as an automation edit, then use the button. + // Clear the proposed name as an automation edit, then use the button. h.state_mut().ui.dialogs[0].fields.insert("__presetName".into(), serde_json::json!(" ")); h.run_steps(2); - h.get_by_label("Save").click(); + h.get_by_label("Save Preset").click(); h.run_steps(2); assert!(fields(&h).get("__presetError").is_some()); assert_eq!(h.state().session.presets.documents.len(), 1); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 7928bf442c..63eb57446a 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1033,6 +1033,9 @@ mod view_sync_tests; #[cfg(test)] mod stamp_tests; +#[cfg(test)] +mod new_doc_save_preset_icon_tests; + #[cfg(test)] mod alt_click_tests; #[cfg(test)] diff --git a/crates/ui-egui/src/new_doc_save_preset_icon_tests.rs b/crates/ui-egui/src/new_doc_save_preset_icon_tests.rs new file mode 100644 index 0000000000..55bfc5c687 --- /dev/null +++ b/crates/ui-egui/src/new_doc_save_preset_icon_tests.rs @@ -0,0 +1,110 @@ +//! #14043: File › New saves a document preset from an icon right of Name, as Photoshop does. The +//! icon turns the pane into SAVE DOCUMENT PRESET, proposing the next free `Saved N` with its text +//! selected, with Save Preset / Cancel; the old "Save Preset…" button under the pane is gone. + +use egui::accesskit::Role; +use egui_kittest::{Harness, kittest::Queryable}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::dialogs::new_doc_ui::next_saved_name; +use crate::state::{DialogKind, UiState}; + +/// The default New Document settings, as the dialog saves them. +fn settings() -> serde_json::Value { + crate::dialogs::new_doc_ui::document_settings(&UiState::new_document_fields()) +} + +fn harness(saved: &[&str]) -> Harness<'static, PhotocraftApp> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + for name in saved { + app.run("document.presets.save", json!({ "name": name, "settings": settings() })).unwrap(); + } + let mut h = + Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 60.0).build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), app); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + h.state_mut().ui.open_dialog(DialogKind::NewDocument, UiState::new_document_fields()); + h.run_steps(3); + h +} + +fn field(h: &Harness<'static, PhotocraftApp>, key: &str) -> Option { + h.state().ui.dialogs.first().and_then(|d| d.fields.get(key).cloned()) +} + +/// Click the icon beside Name and let the pane settle (the name field takes focus a frame later). +fn open_save(h: &mut Harness<'static, PhotocraftApp>) { + h.get_by_label("Save Document Preset").click(); + h.run_steps(3); +} + +#[test] +fn the_save_icon_sits_right_of_name_and_the_old_button_is_gone() { + let h = harness(&[]); + assert!(h.query_by_label("Save Preset…").is_none(), "no Save Preset… button under the pane"); + let name = h.get_by_role_and_label(Role::TextInput, "Name").rect(); + let icon = h.get_by_label("Save Document Preset").rect(); + assert!(icon.left() >= name.right(), "icon {icon:?} right of Name {name:?}"); + assert!(icon.top() < name.bottom() && name.top() < icon.bottom(), "icon {icon:?} on the Name row {name:?}"); +} + +#[test] +fn the_icon_opens_save_document_preset_proposing_saved_1_selected() { + let mut h = harness(&[]); + assert!(h.query_by_label("PRESET DETAILS").is_some()); + open_save(&mut h); + assert!(h.query_by_label("SAVE DOCUMENT PRESET").is_some()); + assert!(h.query_by_label("PRESET DETAILS").is_none(), "the header turns into SAVE DOCUMENT PRESET"); + assert!(h.query_by_label("Save Preset").is_some()); + assert!(h.query_by_label("Cancel").is_some()); + assert_eq!(field(&h, "__presetName"), Some(json!("Saved 1"))); + // The proposed name is selected: typing replaces it. + h.event(egui::Event::Text("Banner".into())); + h.run_steps(1); + assert_eq!(field(&h, "__presetName"), Some(json!("Banner"))); + h.get_by_label("Save Preset").click(); + h.run_steps(3); + let names: Vec<_> = h.state().session.presets.documents.iter().map(|p| p.name.clone()).collect(); + assert_eq!(names, ["Banner"]); + assert!(h.query_by_label("PRESET DETAILS").is_some(), "saving goes back to Preset Details"); +} + +#[test] +fn the_proposal_is_the_next_free_saved_number() { + let mut h = harness(&["Saved 1", "saved 2", "Saved 4"]); + open_save(&mut h); + assert_eq!(field(&h, "__presetName"), Some(json!("Saved 3"))); + h.get_by_label("Save Preset").click(); + h.run_steps(3); + assert!(h.state().session.presets.documents.iter().any(|p| p.name == "Saved 3")); + // The next time proposes the next free number again. + open_save(&mut h); + assert_eq!(field(&h, "__presetName"), Some(json!("Saved 5"))); +} + +#[test] +fn cancel_returns_to_preset_details_without_saving() { + let mut h = harness(&[]); + open_save(&mut h); + h.event(egui::Event::Text("Draft".into())); + h.run_steps(1); + h.get_by_label("Cancel").click(); + h.run_steps(3); + assert!(h.state().session.presets.documents.is_empty(), "Cancel saves nothing"); + assert!(h.query_by_label("PRESET DETAILS").is_some()); + assert!(h.query_by_label("SAVE DOCUMENT PRESET").is_none()); + assert_eq!(h.state().ui.dialogs.len(), 1, "Cancel keeps the New Document dialog open"); + // Opening it again proposes a fresh name, not the cancelled one. + open_save(&mut h); + assert_eq!(field(&h, "__presetName"), Some(json!("Saved 1"))); +} + +#[test] +fn next_saved_name_skips_taken_numbers_ignoring_case() { + let mut s = photocraft_engine::Session::new(); + assert_eq!(next_saved_name(&s.presets.documents), "Saved 1"); + for name in ["SAVED 1", "Saved 3", "Poster"] { + s.execute("document.presets.save", json!({ "name": name, "settings": settings() })).unwrap(); + } + assert_eq!(next_saved_name(&s.presets.documents), "Saved 2"); +} diff --git a/docs/control-protocol.md b/docs/control-protocol.md index ecf603fe9a..af6cf07bbd 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -381,9 +381,10 @@ group, `ui.dialog.set {"choice":"replace"}` then `ui.dialog.confirm` replaces, a `ui.dialog.cancel` imports nothing. Named New Document presets also persist with preferences (`presets.documents`) on desktop and -web. File › New › Save Preset… saves the current form; the Saved tab selects a configuration or -deletes it. Selection only fills the form: Create still runs `file.new`, and the document name -is independent of the preset name. Settings include pixel width/height, resolution in ppi, +web. In File › New, the save icon right of Name (Save Document Preset, proposing the next free +`Saved N`) saves the current form; the Saved tab selects a configuration or deletes it. +Selection only fills the form: Create still runs `file.new`, and the document name is +independent of the preset name. Settings include pixel width/height, resolution in ppi, mode, depth, background, and display `unit` (`px`, `in`, `cm`, `mm`, `pt`, `pica`) and `resolutionUnit` (`in`, `cm`). The defaults match File › New. Background Color captures the toolbox colour as `backgroundColor: [r,g,b]` (0..1), so reusing it does not follow later toolbox