Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Print: keep the picked printer for the next print (#3537)

Adrian1298aa285c01
diff --git a/crates/ui-egui/src/print_ui.rs b/crates/ui-egui/src/print_ui.rs
index f4e039d51..d4445e871 100644
--- a/crates/ui-egui/src/print_ui.rs
+++ b/crates/ui-egui/src/print_ui.rs
@@ -289,6 +289,11 @@ fn default_printer(printers: &[spool::Printer]) -> Option<String> {
printers.iter().find(|p| p.default).or(printers.first()).map(|p| p.name.clone())
}
+/// Whether `printer` can still be chosen from `printers`: Save as PDF (`None`) always can.
+fn offered(printer: Option<&str>, printers: &[spool::Printer]) -> bool {
+ printer.is_none_or(|name| printers.iter().any(|p| p.name == name))
+}
+
impl PdfCraftApp {
pub fn open_print(&mut self) {
let Some((i, _)) = self.active_ids() else { return };
@@ -305,6 +310,9 @@ impl PdfCraftApp {
let current = self.views[i].current;
let selected: Vec<usize> = self.views[i].selected.iter().copied().collect();
let keep = std::mem::take(&mut self.print_draft);
+ // The printer picked last time, Save as PDF included, while it is still listed (#3537).
+ let picked = keep.printer_picked && offered(keep.printer.as_deref(), &printers);
+ let printer = if picked { keep.printer.clone() } else { default };
// Picked pages are what Print is for (Acrobat's "Selected pages"); without any, a choice
// left over from another document falls back to the whole document.
let which = match (selected.is_empty(), keep.which) {
@@ -317,9 +325,9 @@ impl PdfCraftApp {
crop_pages: None,
printers,
printers_loading,
- printer_picked: false,
+ printer_picked: picked,
units: self.units,
- printer: default,
+ printer,
current_page: current,
sheet: 0,
selected,
@@ -407,7 +415,9 @@ impl PdfCraftApp {
if let Some(printers) = printers {
self.print_tasks.printers = None;
let d = &mut self.print_draft;
- if !d.printer_picked {
+ // A picked printer that is no longer listed falls back to the default too.
+ if !d.printer_picked || !offered(d.printer.as_deref(), &printers) {
+ d.printer_picked = false;
d.printer = default_printer(&printers);
}
d.printers = printers;
diff --git a/crates/ui-egui/tests/print.rs b/crates/ui-egui/tests/print.rs
index f3d10238c..6e45022c1 100644
--- a/crates/ui-egui/tests/print.rs
+++ b/crates/ui-egui/tests/print.rs
@@ -317,6 +317,57 @@ fn printers_listed_in_the_background_preselect_the_default_unless_one_was_picked
assert_eq!(h.state().print_draft.printer, None, "Save as PDF, as picked");
}
+/// The printer picked in the dialog, Save as PDF included, is still chosen the next time Print
+/// opens (#3537), until a fresh list no longer has it.
+#[test]
+fn the_picked_printer_is_kept_for_the_next_print_while_it_is_listed() {
+ use pdfcraft_engine::print::spool::Printer;
+ use std::sync::{Arc, Mutex};
+ let mut h = harness();
+ let listed = Arc::new(Mutex::new(vec!["Office", "Front Desk"]));
+ let source = listed.clone();
+ h.state_mut().printer_source = Some(Arc::new(move || {
+ let names = source.lock().map(|n| n.clone()).unwrap_or_default();
+ names.into_iter().map(|name| Printer { name: name.into(), default: name == "Front Desk", two_sided: None }).collect()
+ }));
+ let open = |h: &mut Harness<'static, PdfCraftApp>| {
+ h.state_mut().dialog = None;
+ assert!(h.state_mut().execute("print.dialog"));
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
+ while h.state().print_draft.printers_loading && std::time::Instant::now() < deadline {
+ h.run_steps(1);
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ assert!(!h.state().print_draft.printers_loading);
+ h.run_steps(2);
+ };
+ open(&mut h);
+ assert_eq!(h.state().print_draft.printer.as_deref(), Some("Front Desk"), "the default first");
+
+ // As the Printer list records a choice.
+ {
+ let d = &mut h.state_mut().print_draft;
+ d.printer = None;
+ d.printer_picked = true;
+ }
+ open(&mut h);
+ assert_eq!(h.state().print_draft.printer, None, "Save as PDF, as picked last time");
+
+ {
+ let d = &mut h.state_mut().print_draft;
+ d.printer = Some("Office".into());
+ d.printer_picked = true;
+ }
+ open(&mut h);
+ assert_eq!(h.state().print_draft.printer.as_deref(), Some("Office"), "Office, as picked last time");
+
+ // Unplugged: the fresh list doesn't have it, so the dialog goes back to the default.
+ *listed.lock().unwrap() = vec!["Front Desk"];
+ open(&mut h);
+ assert_eq!(h.state().print_draft.printer.as_deref(), Some("Front Desk"));
+ assert!(!h.state().print_draft.printer_picked);
+}
+
#[test]
fn architectural_page_can_be_matched_at_100_percent() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1000.0)).build_eframe(|_cc| {