Keep new styles in their opening document

This commit is contained in:
dotCooCoo committed 2026-11-26 14:46:26 -08:00
1 parent ebf3510245
commit cb73a47f0c
1 file changed
+15 -7
+15 -7
View File
@@ -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<Value, String> {
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<Value
let inner = char_style_options::hosted(KIND, tl!("New Character Style"), &opening, &name);
(Opening::Char(opening), inner)
};
app.dialogs().ok_or("no window")?.with(|host| host.open(NewTextStyle { inner, opening, para, target }));
app.dialogs().ok_or("no window")?.with(|host| host.open(NewTextStyle { inner, opening, para, document, target }));
Ok(json!({ "dialog": KIND }))
}
@@ -78,7 +80,7 @@ pub fn open(app: &mut VectorcraftApp, para: bool, target: Value) -> Result<Value
/// into view when it next draws.
pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
let Some(dialogs) = app.dialogs() else { return };
let Some((para, frame)) = dialogs.with(|host| host.get_mut::<NewTextStyle>().map(|d| (d.para, d.frame()))) else {
let Some((para, document, frame)) = dialogs.with(|host| host.get_mut::<NewTextStyle>().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::<NewTextStyle>());
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(())
}