diff --git a/crates/ui-egui/src/tools/type_tool/controls.rs b/crates/ui-egui/src/tools/type_tool/controls.rs index cc771516551..363f0bf70e5 100644 --- a/crates/ui-egui/src/tools/type_tool/controls.rs +++ b/crates/ui-egui/src/tools/type_tool/controls.rs @@ -191,6 +191,14 @@ fn editor_rect(app: &mut PhotocraftApp, view: &craft::behavior::CanvasView, id: Some(if boxed { bounds } else { bounds.expand(EDITOR_SLOP_PT) }) } +/// Use the language shown for the edit's caret or selection. Other languages keep the English marks. +fn smart_quote_style(app: &PhotocraftApp) -> QuoteStyle { + let german = super::styles_at(app).is_some_and(|(style, _)| { + style.language.as_deref().and_then(|tag| tag.split(['-', '_']).next()).is_some_and(|language| language.eq_ignore_ascii_case("de")) + }); + if german { QuoteStyle { apostrophe: QuoteStyle::GERMAN.single.1, ..QuoteStyle::GERMAN } } else { QuoteStyle::ENGLISH } +} + /// The text editor over the open edit: typing, the caret and selection, clicks and the input method. Its commit and /// cancel end the session; its selection is written back to `TextEdit` in the same pass. fn editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, view: &craft::behavior::CanvasView, id: LayerId) { @@ -201,6 +209,7 @@ fn editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, view: &craft::behavior::Ca let prefs = &app.session.prefs().type_; (prefs.smart_quotes, prefs.use_esc_to_commit) }; + let quote_style = smart_quotes.then(|| smart_quote_style(app)); let mask = app.ui.text_edit.as_ref().is_some_and(|ed| ed.mask); let out = { let mut host = LayerHost::new(app, id); @@ -208,8 +217,8 @@ fn editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, view: &craft::behavior::Ca // A Type Mask commits on Escape whatever "Use ESC to commit" says; the options bar's Cancel still throws it away (#12070). .esc_commits(mask || esc_commits) .doc_key(TYPE_EDITOR); - if smart_quotes { - editor = editor.smart_quotes(QuoteStyle::ENGLISH); + if let Some(style) = quote_style { + editor = editor.smart_quotes(style); } editor.show_at(ui, rect) }; @@ -249,6 +258,10 @@ fn editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, view: &craft::behavior::Ca } } +#[cfg(test)] +#[path = "smart_quotes_language_tests.rs"] +mod smart_quotes_language_tests; + /// The box of `state`, as `type.edit`'s `box` takes it: the top-left corner in document units, then the width and /// height in text space, through the layer's transform `aff`. fn box_of(state: &BoxState, aff: &Affine) -> Option<[f64; 4]> { diff --git a/crates/ui-egui/src/tools/type_tool/smart_quotes_language_tests.rs b/crates/ui-egui/src/tools/type_tool/smart_quotes_language_tests.rs new file mode 100644 index 00000000000..a86c4e21c54 --- /dev/null +++ b/crates/ui-egui/src/tools/type_tool/smart_quotes_language_tests.rs @@ -0,0 +1,68 @@ +use egui::Event; +use photocraft_doc::{LayerContent, LayerId}; +use serde_json::json; + +use crate::type_tool::commit_tests::{edit_active, frames, type_keys}; +use crate::{PhotocraftApp, Tool}; + +const STRAIGHT: &str = "\"q\" 'r' it's"; +const ENGLISH: &str = "“q” ‘r’ it’s"; +const GERMAN: &str = "„q“ ‚r‘ it‘s"; + +fn point_text(initial: &str, language: Option<&str>, smart_quotes: bool) -> Result<(Box, LayerId), String> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 200}))?; + app.session.edit_prefs(|p| p.type_.smart_quotes = smart_quotes); + let result = app.run("type.create", json!({"text": initial, "x": 50, "y": 100, "size": 24, "language": language}))?; + let id = LayerId(result["layer"].as_u64().ok_or("Type Create returned no layer id")?); + app.sync_views(); + app.ui.tool = Tool::Type; + Ok((app, id)) +} + +fn text(app: &PhotocraftApp, id: LayerId) -> Option<&str> { + match &app.session.active()?.doc.layer(id)?.content { + LayerContent::Text(text) => Some(&text.text), + _ => None, + } +} + +fn start_editing(app: &mut PhotocraftApp, initial: &str) -> Result<(), String> { + edit_active(app); + let ed = app.ui.text_edit.as_mut().ok_or("the point text is not being edited")?; + ed.caret = initial.chars().count(); + ed.anchor = ed.caret; + Ok(()) +} + +#[test] +fn typed_smart_quotes_follow_the_german_run_language() -> Result<(), String> { + let initial = "Start "; + for (tag, enabled, expected) in [ + (Some("de"), true, GERMAN), + (Some("de-DE"), true, GERMAN), + (Some("en-US"), true, ENGLISH), + (None, true, ENGLISH), + (Some("fr-FR"), true, ENGLISH), + (Some("de"), false, STRAIGHT), + ] { + let (mut app, id) = point_text(initial, tag, enabled)?; + start_editing(&mut app, initial)?; + type_keys(&mut app, STRAIGHT); + let expected = format!("{initial}{expected}"); + assert_eq!(text(&app, id), Some(expected.as_str()), "language={tag:?}, preference={enabled}"); + } + Ok(()) +} + +#[test] +fn german_smart_quotes_do_not_change_command_or_pasted_text() -> Result<(), String> { + let initial = format!("{STRAIGHT} "); + let (mut app, id) = point_text(&initial, Some("de-DE"), true)?; + assert_eq!(text(&app, id), Some(initial.as_str()), "the engine command leaves its supplied characters verbatim"); + start_editing(&mut app, &initial)?; + let app = frames(app, vec![Event::Paste(STRAIGHT.into())]); + let expected = format!("{initial}{STRAIGHT}"); + assert_eq!(text(&app, id), Some(expected.as_str()), "pasted quotes remain straight"); + Ok(()) +}