From 2a939130435d50c119793cb168af968c5d63e618 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 14:51:56 -0800 Subject: [PATCH 1/2] Exercise Character Style Options after switching documents --- .../src/dialogs/char_style_options/tests.rs | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/crates/ui-egui/src/dialogs/char_style_options/tests.rs b/crates/ui-egui/src/dialogs/char_style_options/tests.rs index 048e16077d..6b39964c0f 100644 --- a/crates/ui-egui/src/dialogs/char_style_options/tests.rs +++ b/crates/ui-egui/src/dialogs/char_style_options/tests.rs @@ -126,6 +126,70 @@ fn styled(app: &mut VectorcraftApp, attrs: Value, text: &str) -> u64 { id } +fn options_in_the_first_of_two_documents() -> VectorcraftApp { + let mut app = app(); + styled(&mut app, json!({"size": 24.0}), "Opening document"); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + styled(&mut app, json!({"size": 12.0}), "Other document"); + app.run("document.activate", json!({"index": 0})).unwrap(); + open(&mut app, "Body"); + app +} + +#[test] +fn the_style_preview_stays_in_its_opening_document() { + let mut app = options_in_the_first_of_two_documents(); + let opening = app.session.doc().unwrap().doc.clone(); + set::(&app, "name", json!("Revised")); + pump(&mut app); + assert!(app.session.in_interaction()); + app.run("document.activate", json!({"index": 1})).unwrap(); + let other = app.session.doc().unwrap().doc.clone(); + set::(&app, "size", json!(18)); + pump(&mut app); + assert_eq!(app.session.doc().unwrap().doc, other, "Preview leaves the other document's Body style unchanged"); + assert!(!app.session.in_interaction()); + + app.run("document.activate", json!({"index": 0})).unwrap(); + pump(&mut app); + assert!(app.session.doc().unwrap().doc.char_styles.iter().any(|style| style.name == "Revised")); + cancel::(&mut app); + assert_eq!(app.session.doc().unwrap().doc, opening); +} + +#[test] +fn a_style_options_confirmation_stays_in_its_opening_document() { + let mut app = options_in_the_first_of_two_documents(); + set::(&app, "preview", json!(false)); + set::(&app, "name", json!("Revised")); + app.run("document.activate", json!({"index": 1})).unwrap(); + let other = app.session.doc().unwrap().doc.clone(); + let closed = ok::(&mut app); + + assert_eq!(app.session.doc().unwrap().doc, other, "OK leaves the other document's Body style unchanged"); + assert!(!closed, "the same draft remains available"); + app.run("document.activate", json!({"index": 0})).unwrap(); + assert!(ok::(&mut app)); + assert!(app.session.doc().unwrap().doc.char_styles.iter().any(|style| style.name == "Revised")); +} + +#[test] +fn cancelling_style_options_leaves_the_other_documents_gesture() { + let mut app = options_in_the_first_of_two_documents(); + set::(&app, "name", json!("Revised")); + pump(&mut app); + assert!(app.session.in_interaction()); + app.run("document.activate", json!({"index": 1})).unwrap(); + app.session.begin_interaction("Another gesture").unwrap(); + app.session.preview("object.move", &json!({"dx": 20, "dy": 0})).unwrap(); + let gesture = app.session.doc().unwrap().doc.clone(); + + cancel::(&mut app); + + assert_eq!(app.session.doc().unwrap().doc, gesture); + assert!(app.session.in_interaction()); +} + #[test] fn unchanged_basic_fields_preserve_original_keys() { for attrs in [ From ef7d2286a95cff7987685d6b2330448505546d6e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 14:53:19 -0800 Subject: [PATCH 2/2] Keep Character Style Options in its opening document --- .../ui-egui/src/dialogs/char_style_options.rs | 45 ++++++++++++++++--- .../src/dialogs/char_style_options/tests.rs | 21 +++++++++ crates/ui-egui/src/lib.rs | 2 +- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/dialogs/char_style_options.rs b/crates/ui-egui/src/dialogs/char_style_options.rs index f1d1b618af..51c00df1fe 100644 --- a/crates/ui-egui/src/dialogs/char_style_options.rs +++ b/crates/ui-egui/src/dialogs/char_style_options.rs @@ -1165,6 +1165,7 @@ pub(crate) use style_dialog; pub(crate) struct CharStyleOptions { inner: HostedPropertiesDialog, opening: Arc, + document: u64, } impl CharStyleOptions { @@ -1180,12 +1181,20 @@ impl CharStyleOptions { } style_dialog!(CharStyleOptions, KIND); +/// The preview and the document activation that owns it, retained until the host reports Cancel. +#[derive(Default)] +pub(crate) struct Preview { + live: LivePreview, + activation: Option<(u64, u64)>, +} + pub fn open(app: &mut VectorcraftApp, name: &str) -> Result { crate::panels::character::end_typing(app); - let doc = &app.session.active().ok_or("no document open")?.doc; + let state = app.session.active().ok_or("no document open")?; + let (document, doc) = (state.uid, &state.doc); let def = doc.char_styles.iter().find(|s| s.name == name).ok_or_else(|| format!("no character style `{name}`"))?; let opening = Arc::new(Opening::new(name, def.attrs.clone(), &doc.setup, swatch_choices(doc))); - let dialog = CharStyleOptions { inner: hosted(KIND, tl!("Character Style Options"), &opening, name).previews(), opening }; + let dialog = CharStyleOptions { inner: hosted(KIND, tl!("Character Style Options"), &opening, name).previews(), opening, document }; app.dialogs().ok_or("no window")?.with(|host| host.open(dialog)); Ok(json!({"dialog": KIND})) } @@ -1210,15 +1219,37 @@ pub fn reset_panel(app: &mut VectorcraftApp) -> Result { /// The dialog's frame work: previews the values, commits at OK, and rolls the preview back at Cancel. pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { let Some(dialogs) = app.dialogs() else { return }; - let Some(frame) = dialogs.with(|host| host.get_mut::().map(CharStyleOptions::frame)) else { + let active = app.session.active().map(|doc| (doc.uid, app.session.tool_resets())); + let Some((document, frame)) = dialogs.with(|host| host.get_mut::().map(|d| (d.document, d.frame()))) else { if matches!(dialogs.with(|host| host.take::()), Some(DialogResult::Cancel)) { - std::mem::take(&mut app.char_style_preview).cancel(app); + let preview = std::mem::take(&mut app.char_style_preview); + if preview.activation == active { + preview.live.cancel(app); + } } return; }; - let live = std::mem::take(&mut app.char_style_preview); - let (live, done) = drive(app, live, tl!("Character Style Options"), "charStyle.edit", &frame); - app.char_style_preview = live; + let done = if let Some(activation) = active.filter(|(uid, _)| *uid == document) { + let previous = app.char_style_preview.activation; + if previous.is_some() && previous != Some(activation) && app.session.in_interaction() { + frame.ok.then(|| Err("finish the active edit before applying Character Style Options".into())) + } else { + let preview = std::mem::take(&mut app.char_style_preview); + let live = if previous == Some(activation) { preview.live } else { LivePreview::default() }; + let (live, answer) = drive(app, live, tl!("Character Style Options"), "charStyle.edit", &frame); + app.char_style_preview = Preview { live, activation: Some(activation) }; + answer + } + } else if frame.ok { + let reason = if app.session.documents().iter().any(|doc| doc.uid == document) { + "switch back to the document where Character Style Options was opened" + } else { + "the document where Character Style Options was opened has closed" + }; + Some(Err(reason.into())) + } else { + None + }; if let Some(done) = done { let answer = done.map(drop).map_err(|why| reason(&why)); dialogs.with(|host| { diff --git a/crates/ui-egui/src/dialogs/char_style_options/tests.rs b/crates/ui-egui/src/dialogs/char_style_options/tests.rs index 6b39964c0f..cbf7c386cf 100644 --- a/crates/ui-egui/src/dialogs/char_style_options/tests.rs +++ b/crates/ui-egui/src/dialogs/char_style_options/tests.rs @@ -190,6 +190,27 @@ fn cancelling_style_options_leaves_the_other_documents_gesture() { assert!(app.session.in_interaction()); } +#[test] +fn switching_back_leaves_a_new_gesture_in_the_opening_document() { + let mut app = options_in_the_first_of_two_documents(); + set::(&app, "name", json!("Revised")); + pump(&mut app); + assert!(app.session.in_interaction()); + app.run("document.activate", json!({"index": 1})).unwrap(); + app.run("document.activate", json!({"index": 0})).unwrap(); + app.session.begin_interaction("Another gesture").unwrap(); + app.session.preview("object.move", &json!({"dx": 20, "dy": 0})).unwrap(); + let gesture = app.session.doc().unwrap().doc.clone(); + + set::(&app, "size", json!(18)); + pump(&mut app); + assert!(!ok::(&mut app)); + assert_eq!(app.session.doc().unwrap().doc, gesture); + cancel::(&mut app); + assert_eq!(app.session.doc().unwrap().doc, gesture); + assert!(app.session.in_interaction()); +} + #[test] fn unchanged_basic_fields_preserve_original_keys() { for attrs in [ diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index a9b143e397..d63ae41a52 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -311,7 +311,7 @@ pub struct VectorcraftApp { /// Check Spelling's walk through the documents' type, while its dialog is open. pub(crate) spelling: Option, /// The Character Style Options dialog's live preview. - pub(crate) char_style_preview: dialogs::preview::LivePreview, + pub(crate) char_style_preview: dialogs::char_style_options::Preview, /// The Paragraph Style Options dialog's live preview. pub(crate) para_style_preview: dialogs::preview::LivePreview, /// The Justification dialog, while it is open.