diff --git a/crates/ui-egui/src/dialogs/tate_chu_yoko.rs b/crates/ui-egui/src/dialogs/tate_chu_yoko.rs index e2f32ef4fd..34eb857f53 100644 --- a/crates/ui-egui/src/dialogs/tate_chu_yoko.rs +++ b/crates/ui-egui/src/dialogs/tate_chu_yoko.rs @@ -39,6 +39,7 @@ fn rows(lengths: LengthUnits) -> Vec { /// Open Tate-chu-yoko Settings on the characters the Character panel acts on. pub fn open(app: &mut VectorcraftApp) -> Result { crate::panels::character::text_style(app).ok_or("select some type first")?; + let target = crate::panels::character::format_target_kept(app)?; let active = app.session.active().ok_or("no document")?; let (document, reverts) = (active.uid, active.reverts); let (up, right) = crate::panels::character::shared_tcy_offsets(app); @@ -65,7 +66,7 @@ pub fn open(app: &mut VectorcraftApp) -> Result { if active.reverts != reverts { return Err(tl!("The document was reverted. Reopen this dialog to continue.").to_owned()); } - crate::panels::character::run_format(app, Value::Object(p)).map(|_| ()) + crate::panels::character::run_format_at_target(app, Value::Object(p), &target).map(|_| ()) }) .ok_or("no window")?; Ok(Value::Null) @@ -206,6 +207,32 @@ mod tests { assert_eq!(offsets(&app, id), [(0.0, 6.0), (0.0, 0.0)]); } + #[test] + fn tate_chu_yoko_approval_keeps_its_opening_text_target() { + let (mut app, id) = app(); + let select = |app: &mut VectorcraftApp, anchor, caret| { + app.session.set_tool_option("textSelection", &json!({"editing": id, "anchor": anchor, "caret": caret})); + }; + app.session.select_tool("type", Default::default()).unwrap(); + select(&mut app, 0, 2); + let undo = app.session.active().unwrap().history.undo.len(); + app.run("ui.tateChuYokoSettings", json!({})).unwrap(); + driver::set(&mut app, "tcyLeftRight", json!(6)); + + select(&mut app, 2, 4); + assert_eq!(crate::panels::character::text_editing(&app), Some((NodeId(id), 2, 4))); + let dialog = craft::ui::dialog::dialog_id(KIND); + crate::tests::picks::answer_dialog(&mut app, dialog, &[], true); + assert_eq!(offsets(&app, id), [(0.0, 0.0)], "the new text range keeps its formatting"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert!(matches!(app.dialogs().unwrap().with(|h| h.held_state(dialog)), Some(craft::ui::dialog::HeldOk::Refused(_)))); + + select(&mut app, 0, 2); + assert!(crate::dialogs::tests::confirm(&mut app)); + assert_eq!(offsets(&app, id), [(0.0, 6.0), (0.0, 0.0)]); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo + 1); + } + #[test] fn tate_chu_yoko_approval_refuses_to_change_restored_text() { let (mut app, id) = app(); diff --git a/crates/ui-egui/src/dialogs/warichu.rs b/crates/ui-egui/src/dialogs/warichu.rs index c297df5160..2001db0934 100644 --- a/crates/ui-egui/src/dialogs/warichu.rs +++ b/crates/ui-egui/src/dialogs/warichu.rs @@ -69,6 +69,7 @@ fn rows(lengths: LengthUnits) -> Vec { /// Open Warichu Settings on the characters the Character panel acts on. pub fn open(app: &mut VectorcraftApp) -> Result { crate::panels::character::text_style(app).ok_or("select some type first")?; + let target = crate::panels::character::format_target_kept(app)?; let active = app.session.active().ok_or("no document")?; let (document, reverts) = (active.uid, active.reverts); let shared = crate::panels::character::shared_warichu(app); @@ -96,7 +97,7 @@ pub fn open(app: &mut VectorcraftApp) -> Result { if active.reverts != reverts { return Err(tl!("The document was reverted. Reopen this dialog to continue.").to_owned()); } - crate::panels::character::run_format(app, Value::Object(p)).map(|_| ()) + crate::panels::character::run_format_at_target(app, Value::Object(p), &target).map(|_| ()) }) .ok_or("no window")?; Ok(Value::Null) @@ -342,6 +343,30 @@ mod tests { assert_eq!(settings(&app, id), [(false, 2, 0.0, 50.0, WarichuAlign::JustifyAll), (false, 2, 0.0, 50.0, WarichuAlign::Auto)]); } + #[test] + fn warichu_approval_keeps_its_opening_text_target() { + let (mut app, first) = app(); + let second = app.run("text.create", json!({"x": 12, "y": 100, "text": "second"})).unwrap()["id"].as_u64().unwrap(); + app.run("select.set", json!({"ids": [first]})).unwrap(); + let before = settings(&app, second); + let undo = app.session.active().unwrap().history.undo.len(); + app.run("ui.warichuSettings", json!({})).unwrap(); + driver::set(&mut app, "warichuLines", json!(4)); + + app.run("select.set", json!({"ids": [second]})).unwrap(); + let dialog = craft::ui::dialog::dialog_id(KIND); + crate::tests::picks::answer_dialog(&mut app, dialog, &[], true); + assert_eq!(settings(&app, second), before, "the second text object keeps its formatting"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert!(matches!(app.dialogs().unwrap().with(|h| h.held_state(dialog)), Some(craft::ui::dialog::HeldOk::Refused(_)))); + + app.run("select.set", json!({"ids": [first]})).unwrap(); + assert!(crate::dialogs::tests::confirm(&mut app)); + assert_eq!(settings(&app, first), [(false, 4, 0.0, 50.0, WarichuAlign::Auto)]); + assert_eq!(settings(&app, second), before); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo + 1); + } + #[test] fn warichu_approval_refuses_to_change_restored_text() { let (mut app, id) = app(); diff --git a/crates/ui-egui/src/panels/character.rs b/crates/ui-egui/src/panels/character.rs index 6804b53c4d..bfcb770b7e 100644 --- a/crates/ui-egui/src/panels/character.rs +++ b/crates/ui-egui/src/panels/character.rs @@ -33,7 +33,7 @@ use egui::Ui; use serde_json::{Value, json}; use vectorcraft_doc::setup::{LANGUAGES, char_language_name}; use vectorcraft_doc::text::TypeAntiAlias; -use vectorcraft_doc::{CharStyle, NodeId, NodeKind}; +use vectorcraft_doc::{CharStyle, NodeId, NodeKind, SymbolPart}; use vectorcraft_engine::DocState; use vectorcraft_tools::{Mods, ToolKey}; @@ -289,8 +289,13 @@ pub(crate) fn format(app: &mut VectorcraftApp, p: Value) { /// [`format`] that returns the command's result: `text.setRangeStyle` on the range the Type tool /// has selected (the whole text when nothing is selected) while it edits text, else /// `text.setFormat` on the selected stories ([`selected_stories`]). -pub(crate) fn run_format(app: &mut VectorcraftApp, mut p: Value) -> Result { - let Some((id, a, b)) = text_editing_kept(app)? else { +pub(crate) fn run_format(app: &mut VectorcraftApp, p: Value) -> Result { + let editing = text_editing_kept(app)?; + run_format_after_typing(app, p, editing) +} + +fn run_format_after_typing(app: &mut VectorcraftApp, mut p: Value, editing: Option<(NodeId, usize, usize)>) -> Result { + let Some((id, a, b)) = editing else { target_selected_stories(app, &mut p); return app.run("text.setFormat", p); }; @@ -305,6 +310,40 @@ pub(crate) fn run_format(app: &mut VectorcraftApp, mut p: Value) -> Result, Option), + Story(Vec), + Range(NodeId, usize, usize), +} + +fn format_target(app: &VectorcraftApp, editing: Option<(NodeId, usize, usize)>) -> FormatTarget { + if let Some((id, start, end)) = editing { + return text_story(app).map_or(FormatTarget::Range(id, start, end), FormatTarget::Story); + } + let mut ids = selected_stories(app); + ids.sort_unstable_by_key(|id| id.0); + let part = app.session.active().and_then(|st| st.selection.symbol_part.clone()); + FormatTarget::Objects(ids, part) +} + +pub(crate) fn format_target_kept(app: &mut VectorcraftApp) -> Result { + let editing = text_editing_kept(app)?; + Ok(format_target(app, editing)) +} + +/// Apply a settings dialog to its opening text only. Compare before keeping typing so a +/// refused confirmation on different text does not commit that text's in-progress edit. +pub(crate) fn run_format_at_target(app: &mut VectorcraftApp, p: Value, target: &FormatTarget) -> Result { + let check = |current: FormatTarget| { + if ¤t != target { Err(tl!("The selected text changed. Restore the selection to continue.").to_owned()) } else { Ok(()) } + }; + check(format_target(app, text_editing(app)))?; + let editing = text_editing_kept(app)?; + check(format_target(app, editing))?; + run_format_after_typing(app, p, editing) +} + /// The Tate-chu-yoko Settings Up/Down and Left/Right the characters the panels act on share, in /// document points; `None` where they differ, or with no type. pub(crate) fn shared_tcy_offsets(app: &VectorcraftApp) -> (Option, Option) {