diff --git a/crates/ui-egui/src/dialogs/new_text_style.rs b/crates/ui-egui/src/dialogs/new_text_style.rs index bc133a77e7..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(()) }