diff --git a/crates/ui-egui/src/dialogs/kinsoku_sets.rs b/crates/ui-egui/src/dialogs/kinsoku_sets.rs index 668368e1b9..85e7b68389 100644 --- a/crates/ui-egui/src/dialogs/kinsoku_sets.rs +++ b/crates/ui-egui/src/dialogs/kinsoku_sets.rs @@ -5,7 +5,8 @@ //! of a Unicode code point into a category. Nothing changes in the document until OK, //! which runs `type.kinsokuSets.set` with every set as one undo step; Cancel leaves them as they were. //! -//! Fields: `sets` (the sets as `type.kinsokuSets.set` takes them, each with the name it had in the +//! Fields: `__doc` (the uid of the document the dialog opened on, which OK sets the sets of), +//! `sets` (the sets as `type.kinsokuSets.set` takes them, each with the name it had in the //! document as `from`), `selected` (the index of the one picked), `name`, `cannotStart`, //! `cannotEnd`, `hanging` and `inseparable` (its name and categories as typed, the inseparable //! sequences separated by spaces), `base` (what New starts from), `code` (the code point Add @@ -29,6 +30,10 @@ pub const KIND: &str = "kinsokuSets"; /// The command OK runs, and the menu item's. pub const COMMAND: &str = vectorcraft_engine::cmd::kinsokusets::SET; +/// The field that holds the uid of the document whose sets the dialog shows (`ui.dialog.set` +/// refuses `__` fields). +const DOC: &str = "__doc"; + /// The categories, as the command's keys, in the order the dialog shows them. const CATEGORIES: [&str; 4] = ["cannotStart", "cannotEnd", "hanging", "inseparable"]; @@ -37,10 +42,9 @@ pub(super) const SPEC: DialogSpec = /// Open Kinsoku Shori Settings on the active document's custom sets, the first one picked. pub fn open(app: &mut VectorcraftApp) -> Result { - let sets: Vec = app - .session - .active() - .ok_or("no document open")? + let st = app.session.active().ok_or("no document open")?; + let uid = st.uid; + let sets: Vec = st .doc .kinsoku_sets .iter() @@ -52,7 +56,7 @@ pub fn open(app: &mut VectorcraftApp) -> Result { v }) .collect(); - let mut d = Dialog::new(KIND, json!({ "sets": sets, "selected": 0, "base": "hard", "into": "cannotStart", "code": "" })); + let mut d = Dialog::new(KIND, json!({ DOC: uid, "sets": sets, "selected": 0, "base": "hard", "into": "cannotStart", "code": "" })); show_selected(&mut d); app.ui.dialog = Some(d); Ok(Value::Null) @@ -256,7 +260,8 @@ fn run(app: &mut VectorcraftApp, d: &mut Dialog, act: Action) -> Result<(), Stri } }; set.remove("from"); - let doc = app.session.active().map(|t| t.doc.kinsoku_sets.to_vec()).unwrap_or_default(); + let uid = d.fields.get(DOC).and_then(Value::as_u64); + let doc = app.session.documents().iter().find(|t| Some(t.uid) == uid).map(|t| t.doc.kinsoku_sets.to_vec()).unwrap_or_default(); set.insert("name".into(), json!(new_name(&list, &doc))); list.push(set); put_sets(d, &list); @@ -303,9 +308,19 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { { take_fields(d, set); } + // The sets go to the document the dialog opened on, also when another tab is active by now; a + // document closed since closes the dialog and changes nothing. + let uid = d.fields.get(DOC).and_then(Value::as_u64); + if app.session.active().map(|st| st.uid) != uid { + let Some(index) = app.session.documents().iter().position(|doc| Some(doc.uid) == uid) else { + app.ui.dialog = None; + return Err("that document is no longer open".into()); + }; + app.run("document.activate", json!({ "index": index }))?; + } let r = app.run(COMMAND, json!({ "sets": list })); // A name two sets share, a reserved name or a category too long keeps the dialog open to be - // changed. + // changed, with its document active. if r.is_ok() { app.ui.dialog = None; } diff --git a/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs b/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs index c6d7363035..1b7493e147 100644 --- a/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs +++ b/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs @@ -203,6 +203,47 @@ fn ok_takes_fields_set_without_a_frame() { assert_eq!((d.str("name"), d.str("cannotStart")), ("Other".to_string(), "ー".to_string())); } +/// OK sets the sets of the document the dialog opened on, also when another document became the +/// active one while it was open; with that document closed, OK closes the dialog and changes no +/// document's sets. +#[test] +fn ok_sets_the_document_the_dialog_opened_on() { + let mut app = app(); + let first = app.session.active().unwrap().uid; + app.run("type.kinsokuSets.set", json!({ "sets": [{"name": "First", "cannotStart": "ゝ"}] })).unwrap(); + open(&mut app).unwrap(); + assert!(!super::super::accepts_field(dialog(&mut app), DOC), "`ui.dialog.set` cannot change the document"); + dialog(&mut app).fields.insert("name".into(), json!("Soft")); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + let other = app.session.active().unwrap().uid; + assert_ne!(other, first); + // A name OK refuses keeps the dialog open on its document, now the active one. + assert!(super::super::confirm(&mut app).is_err()); + assert!(app.ui.dialog.is_some()); + assert_eq!(app.session.active().unwrap().uid, first); + assert_eq!(doc_sets(&app)[0].name, "First"); + app.run("document.activate", json!({ "index": app.session.documents().iter().position(|d| d.uid == other).unwrap() })).unwrap(); + dialog(&mut app).fields.insert("name".into(), json!("Kinsoku Set 1")); + super::super::confirm(&mut app).unwrap(); + assert!(app.ui.dialog.is_none()); + assert_eq!(app.session.active().unwrap().uid, first); + assert_eq!(doc_sets(&app)[0].name, "Kinsoku Set 1"); + let second = app.session.documents().iter().find(|d| d.uid != first).unwrap(); + assert!(second.doc.kinsoku_sets.is_empty(), "the other document keeps its sets"); + + // New skips the names of the dialog's document's sets, not of the active document's. + open(&mut app).unwrap(); + press(&mut app, Action::Delete); + app.run("document.activate", json!({ "index": app.session.documents().iter().position(|d| d.uid == other).unwrap() })).unwrap(); + press(&mut app, Action::New); + assert_eq!(dialog(&mut app).str("name"), "Kinsoku Set 2"); + let i = app.session.documents().iter().position(|d| d.uid == first).unwrap(); + app.session.execute("file.close", &json!({ "index": i })).unwrap(); + assert!(super::super::confirm(&mut app).is_err()); + assert!(app.ui.dialog.is_none()); + assert!(app.session.documents().iter().all(|d| d.doc.kinsoku_sets.is_empty())); +} + /// Add takes a code point as U+XXXX, u+xxxx or bare hexadecimal, and never a control or space /// character or a value that is no character. #[test]