diff --git a/crates/ui-egui/src/menus/state.rs b/crates/ui-egui/src/menus/state.rs index 7053f2158c..9c8fac79a5 100644 --- a/crates/ui-egui/src/menus/state.rs +++ b/crates/ui-egui/src/menus/state.rs @@ -5,6 +5,10 @@ use photocraft_engine::color_cmds::ColorExt; use super::invoke::{panel_alias, proof_preset, workspace_id}; use super::{DOCUMENT_ITEM, PhotocraftApp, Value}; +#[cfg(test)] +#[path = "type_paste_lorem_greyed_tests.rs"] +mod type_paste_lorem_greyed_tests; + /// Whether View › Zoom Out has stopped at a document-specific minimum. pub(crate) fn zoom_out_at_minimum(app: &PhotocraftApp) -> bool { let Some(document) = app.session.active() else { return false }; @@ -114,6 +118,7 @@ pub fn is_enabled(app: &PhotocraftApp, id: &str) -> bool { "edit.paste" | "file.newFromClipboard" => app.session.is_enabled(id) || app.services.clipboard_get_image.is_some(), "edit.pasteSpecial.pasteInPlace" => app.session.is_enabled(id) || (app.services.clipboard_get_image.is_some() && app.session.active().is_some()), "select.selectAndMask" => app.session.is_enabled("select.refineEdge"), + "type.pasteLoremIpsum" => app.ui.text_edit.is_some(), "type.editText" => app .session .active() diff --git a/crates/ui-egui/src/menus/type_paste_lorem_greyed_tests.rs b/crates/ui-egui/src/menus/type_paste_lorem_greyed_tests.rs new file mode 100644 index 0000000000..92cfb400ac --- /dev/null +++ b/crates/ui-egui/src/menus/type_paste_lorem_greyed_tests.rs @@ -0,0 +1,56 @@ +use serde_json::json; + +use crate::PhotocraftApp; + +const PASTE: &str = "type.pasteLoremIpsum"; + +fn enabled(app: &PhotocraftApp) -> bool { + let rows = crate::menus::menu_rows(app); + let row = crate::menus::row(&rows, PASTE).expect("Paste Lorem Ipsum is in the menu"); + assert_eq!(row.path, ["Type"]); + row.enabled +} + +#[test] +fn paste_lorem_menu_requires_an_active_text_edit() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + let ctx = app.ctx().clone(); + assert!(!enabled(&app), "no document"); + + app.run("file.new", json!({"width": 120, "height": 80, "background": "white"})).unwrap(); + app.sync_views(); + app.run("layer.new.layer", json!({"name": "Paint"})).unwrap(); + assert!(!enabled(&app), "pixel layer selected"); + app.run("select.deselectLayers", json!({})).unwrap(); + assert!(!enabled(&app), "no layer selected"); + + for params in [json!({"x": 4, "y": 24, "text": "Sample"}), json!({"box": [4, 8, 112, 64], "text": "Sample"})] { + let layer = app.run("type.create", params).unwrap()["layer"].as_u64().unwrap(); + assert!(!enabled(&app), "selecting a type layer does not start editing"); + crate::menus::invoke(&mut app, &ctx, "type.editText", json!({})).unwrap(); + let edit = app.ui.text_edit.as_mut().unwrap(); + edit.anchor = edit.caret; + assert!(enabled(&app), "enabled while editing"); + let result = crate::menus::invoke(&mut app, &ctx, PASTE, json!({})).unwrap(); + let info = app.run("type.info", json!({"layer": layer})).unwrap(); + let text = info["text"].as_str().unwrap(); + assert!(text.starts_with("Sample") && text.len() > "Sample".len()); + assert_eq!(result["caret"].as_u64().unwrap(), text.chars().count() as u64); + crate::type_tool::commit(&mut app); + assert!(!enabled(&app), "disabled after committing the edit"); + } +} + +#[test] +fn paste_lorem_engine_command_still_works_outside_the_editor() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 120, "height": 80, "background": "white"})).unwrap(); + let created = app.run(PASTE, json!({"new": true})).unwrap()["layer"].as_u64().unwrap(); + let info = app.run("type.info", json!({"layer": created})).unwrap(); + let original = info["text"].as_str().unwrap(); + let inserted = app.run(PASTE, json!({"layer": created, "at": 0})).unwrap(); + let updated = app.run("type.info", json!({"layer": created})).unwrap(); + assert_eq!(inserted["caret"].as_u64().unwrap(), original.chars().count() as u64); + assert_eq!(updated["text"].as_str().unwrap(), original.repeat(2)); + assert!(!enabled(&app)); +}