diff --git a/crates/ui-egui/src/dialogs/composite_fonts.rs b/crates/ui-egui/src/dialogs/composite_fonts.rs index 0142a0070f..f71d4ea35d 100644 --- a/crates/ui-egui/src/dialogs/composite_fonts.rs +++ b/crates/ui-egui/src/dialogs/composite_fonts.rs @@ -135,6 +135,8 @@ enum PartEdit { /// The Composite Fonts dialog on the dialog host. OK files the fonts for [`pump`], which runs /// [`COMMAND`] and answers whether the dialog may close. pub struct CompositeFontsDialog { + /// The document whose fonts the draft edits. + document: u64, fonts: Vec, /// The index of the font picked. selected: usize, @@ -167,9 +169,9 @@ pub struct CompositeFontsDialog { /// Open Composite Fonts on the active document's composite fonts, the first one picked, with the /// custom character sets and the sample hidden and nothing typed. pub fn open(app: &mut VectorcraftApp) -> Result { - let fonts = app.session.active().ok_or("no document open")?.doc.composite_fonts.to_vec(); + let document = app.session.active().ok_or("no document open")?; let dialogs = app.dialogs().ok_or("no window to show the dialog in")?; - let dialog = CompositeFontsDialog::new(fonts, crate::font_menu::hold(app)); + let dialog = CompositeFontsDialog::new(document.uid, document.doc.composite_fonts.to_vec(), crate::font_menu::hold(app)); dialogs.with(|host| host.open(dialog)); Ok(Value::Null) } @@ -241,10 +243,11 @@ fn part_mut(f: &mut CompositeFont, at: usize) -> Option<&mut CompositePart> { impl CompositeFontsDialog { /// The dialog over `fonts`, the first picked. - fn new(fonts: Vec, hold: PickerHold) -> Self { + fn new(document: u64, fonts: Vec, hold: PickerHold) -> Self { let mut sample = Map::new(); sample::defaults(&mut sample); CompositeFontsDialog { + document, fonts, selected: 0, set: 0, @@ -852,15 +855,18 @@ impl StackDialog for CompositeFontsDialog { /// file dialog a Load…, Replace… or Save… asked for. pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &Context) { let Some((held, request)) = with_dialog(app, |d| { - let held = d.held.take(); + let held = d.held.take().map(|fonts| (d.document, fonts)); let request = d.file.take().map(|request| (request, Arc::clone(&d.file_owner))); (held, request) }) else { return; }; - if let Some(fonts) = held { - // A name two fonts share or a value out of range keeps the dialog open with the reason. - let answer = app.run(COMMAND, json!({ "fonts": fonts })).map(|_| ()).map_err(Label::from); + if let Some((document, fonts)) = held { + let answer = if app.session.active().is_some_and(|active| active.uid == document) { + app.run(COMMAND, json!({ "fonts": fonts })).map(|_| ()).map_err(Label::from) + } else { + Err(Label::from(tl!("Composite Fonts can only change the document where it was opened.").to_owned())) + }; with_dialog(app, |d| d.held.answer(answer)); } if let Some((request, owner)) = request { diff --git a/crates/ui-egui/src/dialogs/composite_fonts/tests.rs b/crates/ui-egui/src/dialogs/composite_fonts/tests.rs index 04af573087..8cc64e2de8 100644 --- a/crates/ui-egui/src/dialogs/composite_fonts/tests.rs +++ b/crates/ui-egui/src/dialogs/composite_fonts/tests.rs @@ -169,6 +169,41 @@ fn composite_fonts_change_on_ok_as_one_undo_step() { assert!(names(&app).is_empty()); } +#[test] +fn a_held_composite_fonts_ok_keeps_the_draft_with_its_opening_document() { + let mut app = app(); + app.run("file.new", json!({"width": 440, "height": 440})).unwrap(); + open(&mut app).unwrap(); + dialog(&mut app, |d| d.new_font("Destination only".into())); + ok(&mut app).unwrap(); + let destination = (names(&app), steps(&app)); + + app.run("document.activate", json!({"index": 0})).unwrap(); + let source = (names(&app), steps(&app)); + open(&mut app).unwrap(); + dialog(&mut app, |d| d.new_font("From the source".into())); + let ctx = ctx(&app); + let id = dialog_id(KIND); + let dialogs = app.dialogs().unwrap(); + dialogs.with(|host| { + assert!(!host.confirm_by_id(&ctx, id)); + assert_eq!(host.held_state(id), Some(HeldOk::Waiting)); + }); + + app.run("document.activate", json!({"index": 1})).unwrap(); + pump(&mut app, &ctx); + assert_eq!((names(&app), steps(&app)), destination, "the other document keeps its fonts and undo history"); + assert!(matches!(dialogs.with(|host| host.held_state(id)), Some(HeldOk::Refused(_)))); + assert!(is_open(&app)); + + app.run("document.activate", json!({"index": 0})).unwrap(); + assert_eq!((names(&app), steps(&app)), source); + ok(&mut app).unwrap(); + assert_eq!(names(&app), ["From the source"]); + assert_eq!(steps(&app), source.1 + 1); + assert!(!is_open(&app)); +} + /// New is based on the font picked and takes the next free name; a name two fonts share, or one /// left empty, keeps the dialog open and changes nothing. #[test]