diff --git a/crates/ui-egui/src/dialogs/print.rs b/crates/ui-egui/src/dialogs/print.rs index 8ec1176cfa..6546b2ff5b 100644 --- a/crates/ui-egui/src/dialogs/print.rs +++ b/crates/ui-egui/src/dialogs/print.rs @@ -512,24 +512,29 @@ fn serve(app: &mut VectorcraftApp, ctx: &egui::Context, dialogs: &WindowDialogs, ctx.request_repaint(); } // A paper the new printer doesn't list, or lists under the same keyword at another size, becomes a custom size of - // the same Width and Height. + // the same Width and Height. One the new printer lists at the size of one of the dialog's own papers becomes that + // paper, the choice the new list offers for it. if papers_changed && media.starts_with(PRINTER_PAPER) - && let Some(s) = on(dialogs, |p| { + && let Some((id, s)) = on(dialogs, |p| { let fields = p.fields_mut(); - if printer_paper(fields, &open.papers.1).is_some_and(|new| chosen.as_ref().is_none_or(|old| same_size(old, new))) { - return None; + if let Some(new) = printer_paper(fields, &open.papers.1) + && chosen.as_ref().is_none_or(|old| same_size(old, new)) + { + let own = own_media_of(new)?; + fields.set("media", own.id()); + return Some((own.id().to_string(), settings(fields))); } if let Some(paper) = &chosen { fields.set("width", paper.width); fields.set("height", paper.height); } fields.set("media", Media::Custom.id()); - Some(settings(fields)) + Some((Media::Custom.id().to_string(), settings(fields))) }) .flatten() { - media = Media::Custom.id().to_string(); + media = id; d.settings = s; } if papers_changed || (!media.is_empty() && !open.media.contains(&media)) { diff --git a/crates/ui-egui/src/dialogs/tests_print.rs b/crates/ui-egui/src/dialogs/tests_print.rs index f325276084..d2ebf74122 100644 --- a/crates/ui-egui/src/dialogs/tests_print.rs +++ b/crates/ui-egui/src/dialogs/tests_print.rs @@ -444,6 +444,42 @@ fn a_paper_keyword_another_printer_lists_at_another_size_becomes_a_custom_size() assert_eq!((f["width"].as_f64(), f["height"].as_f64()), (Some(153.0), Some(243.0)), "{f}"); } +/// Two printers list a paper named Wide: Near at 611.2 × 792 points, Far at 611.6 × 792, within half a point of Letter. +struct NearLetter; + +impl PrintService for NearLetter { + fn printers(&mut self) -> Vec { + vec![Printer::new("Near").is_default(true), Printer::new("Far")] + } + + fn print(&mut self, job: &PrintJob) -> Result { + MockPrint(Jobs::default()).print(job) + } + + fn papers(&mut self, printer: &str, _fresh: bool) -> Papers { + let width = if printer == "Far" { 611.6 } else { 611.2 }; + Papers::Ready(vec![crate::print::Paper { key: "Wide".into(), label: "Wide".into(), width, height: 792.0 }]) + } +} + +#[test] +fn a_kept_paper_the_new_printer_lists_at_a_built_in_size_becomes_that_size() { + let services = Services { print: Some(Box::new(NearLetter)), ..Default::default() }; + let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), services)); + app.run("file.new", json!({"width": 120, "height": 90})).unwrap(); + app.run("file.print", json!({})).unwrap(); + pump(&mut app); + set(&mut app, "media", json!("printer:Wide")); + pump(&mut app); + // Far's Wide is within half a point of Near's and of Letter, and its Size list offers it as Letter. + set(&mut app, "printer", json!("Far")); + pump(&mut app); + pump(&mut app); + let f = fields(&app); + assert_eq!(f["media"], "letter", "{f}"); + assert!(try_set(&mut app, "media", json!("printer:Wide")).is_err(), "the Size list offers Letter in its place"); +} + /// Card lists a CR80 card; Twin's driver answers with the same CR80 once `ready` is set. struct PendingTwin(Rc>);