diff --git a/crates/ui-egui/src/dialogs/new_text_style.rs b/crates/ui-egui/src/dialogs/new_text_style.rs index 2b13671cdd..3ef9024618 100644 --- a/crates/ui-egui/src/dialogs/new_text_style.rs +++ b/crates/ui-egui/src/dialogs/new_text_style.rs @@ -29,6 +29,7 @@ pub(crate) struct NewTextStyle { opening: Opening, /// Whether the dialog makes a paragraph style. pub(crate) para: bool, + document: u64, target: Value, } @@ -54,7 +55,8 @@ style_dialog!(NewTextStyle, KIND); /// Open New Character Style (`para`: New Paragraph Style) with the name `charStyle.new` would give /// the style, to make it from the text `target` names. pub fn open(app: &mut VectorcraftApp, para: bool, target: Value) -> Result { - 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 name = vectorcraft_engine::cmd::textstyles::new_style_name(doc, para); let mut target = if target.is_object() { target } else { json!({}) }; let selected_attrs = vectorcraft_engine::cmd::textstyles::target_style_attrs(&app.session, &target, para); @@ -69,7 +71,7 @@ pub fn open(app: &mut VectorcraftApp, para: bool, target: Value) -> Result Result().map(|d| (d.para, d.frame()))) else { + let Some((para, document, frame)) = dialogs.with(|host| host.get_mut::().map(|d| (d.para, d.document, d.frame()))) else { // The dialog closed: its answer or cancel has nothing left for the app to do. let _ = dialogs.with(|host| host.take::()); return; @@ -86,12 +88,18 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { if !frame.ok { return; } - let made = frame.params.and_then(|params| app.run(if para { "paraStyle.new" } else { "charStyle.new" }, params)); + let made = if app.session.active().is_some_and(|doc| doc.uid == document) { + frame.params.and_then(|params| app.run(if para { "paraStyle.new" } else { "charStyle.new" }, params)) + } else if app.session.documents().iter().any(|doc| doc.uid == document) { + Err("switch back to the document where this style was opened".into()) + } else { + Err("the document where this style was opened has closed".into()) + }; let answer = match &made { Ok(v) => { - if let Some((uid, name)) = app.session.active().and_then(|doc| v["name"].as_str().map(|name| (doc.uid, name.to_string()))) { - app.ui.text_styles.made.retain(|(pending_uid, pending_para, _)| *pending_uid != uid || *pending_para != para); - app.ui.text_styles.made.push((uid, para, name)); + if let Some(name) = v["name"].as_str() { + app.ui.text_styles.made.retain(|(pending_uid, pending_para, _)| *pending_uid != document || *pending_para != para); + app.ui.text_styles.made.push((document, para, name.to_string())); } Ok(()) } @@ -103,3 +111,53 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { } }); } + +#[cfg(test)] +mod tests { + use craft::ui::dialog::{HeldOk, StackDialog}; + use vectorcraft_engine::Session; + + use super::*; + + fn style_exists(app: &VectorcraftApp, para: bool, name: &str) -> bool { + let doc = &app.session.doc().unwrap().doc; + if para { doc.para_styles.iter().any(|style| style.name == name) } else { doc.char_styles.iter().any(|style| style.name == name) } + } + + fn assert_style_stays_in_its_opening_document(para: bool) { + let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default())); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + let opening = app.session.doc().unwrap().uid; + app.run(if para { "ui.newParaStyle" } else { "ui.newCharStyle" }, json!({})).unwrap(); + let ctx = app.ui_ctx.clone().unwrap(); + let host = app.dialogs().unwrap(); + let dialog = host.with(|h| h.get::().unwrap().id()); + let name = "Opening Document Style"; + host.with(|h| h.set_json_field(&ctx, dialog, "name", json!(name))).unwrap(); + + app.run("file.new", json!({"width": 300, "height": 100})).unwrap(); + assert_ne!(app.session.doc().unwrap().uid, opening); + host.with(|h| h.confirm_by_id(&ctx, dialog)); + pump(&mut app, &ctx); + + assert!(!style_exists(&app, para, name), "OK must leave styles in the other document unchanged"); + assert!(app.ui.text_styles.made.is_empty(), "the other document must not highlight a style"); + assert!(matches!(host.with(|h| h.held_state(dialog)), Some(HeldOk::Refused(_)))); + + app.run("document.activate", json!({"index": 0})).unwrap(); + host.with(|h| h.confirm_by_id(&ctx, dialog)); + pump(&mut app, &ctx); + assert!(style_exists(&app, para, name), "the original document can accept the same draft"); + assert_eq!(app.ui.text_styles.made, [(opening, para, name.to_string())]); + } + + #[test] + fn new_character_style_confirmation_stays_in_its_document() { + assert_style_stays_in_its_opening_document(false); + } + + #[test] + fn new_paragraph_style_confirmation_stays_in_its_document() { + assert_style_stays_in_its_opening_document(true); + } +}