PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Merge PR #2108: fix(print): Properties… reads CUPS driver options off the GUI thread, with a deadline for lpoptions (#1818)
aiongg4d1740a475a2
diff --cc crates/ui-egui/src/print_ui.rsindex deb31ce04,b53faac46..21b8fd3d6--- a/crates/ui-egui/src/print_ui.rs+++ b/crates/ui-egui/src/print_ui.rs@@@ -318,11 -303,22 +332,27 @@@ fn default_printer(printers: &[spool::Pprinters.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))+}++ /// Asks for a frame when a worker thread ends, also when it panics: [`PdfCraftApp::poll_print`]+ /// then sees the answer, or the disconnected channel of a worker that died, without waiting+ /// for some other event to draw (#2108 review). Declared before the worker's sender, so the+ /// sender is dropped first and the frame finds the channel closed.+ #[cfg(not(target_arch = "wasm32"))]+ struct WakeOnExit(Option<egui::Context>);++ #[cfg(not(target_arch = "wasm32"))]+ impl Drop for WakeOnExit {+ fn drop(&mut self) {+ if let Some(ctx) = &self.0 {+ ctx.request_repaint();+ }+ }+ }+impl PdfCraftApp {pub fn open_print(&mut self) {let Some((i, _)) = self.active_ids() else { return };diff --cc crates/ui-egui/tests/print.rsindex 7454c03a8,31e5e0299..37514bcec--- a/crates/ui-egui/tests/print.rs+++ b/crates/ui-egui/tests/print.rs@@@ -478,114 -427,99 +478,211 @@@ fn an_empty_printer_list_says_none_wereassert!(!h.get_by_role_and_label(Role::Button, "Save as PDF").accesskit_node().is_disabled());}+/// `PAPERS` index of the named sheet.+fn paper(name: &str) -> usize {+ pdfcraft_engine::print::PAPERS.iter().position(|p| p.0 == name).unwrap()+}++/// Label loads label stock; leaving it gives the paper the person had back (#3564).+#[test]+fn leaving_label_gives_back_the_paper_it_replaced() {+ use pdfcraft_engine::print::LABEL_PAPER;+ use pdfcraft_ui_egui::PrintHandling;+ let a4 = paper("A4");+ let mut h = harness();+ assert!(h.state_mut().execute("print.dialog"));+ h.run_steps(3);+ for (mode, tab) in+ [(PrintHandling::Size, "Size"), (PrintHandling::Poster, "Poster"), (PrintHandling::Multiple, "Multiple"), (PrintHandling::Booklet, "Booklet")]+ {+ h.state_mut().print_draft.paper = a4;+ h.state_mut().print_draft.handling = PrintHandling::Size;+ h.run_steps(2);+ h.get_by_role_and_label(Role::Button, "Label").click();+ h.run_steps(2);+ assert_eq!(h.state().print_draft.handling, PrintHandling::Label);+ assert_eq!(h.state().print_draft.paper, LABEL_PAPER, "Label puts label stock in");+ h.get_by_role_and_label(Role::Button, tab).click();+ h.run_steps(2);+ assert_eq!(h.state().print_draft.handling, mode);+ assert_eq!(h.state().print_draft.paper, a4, "{tab} gives A4 back after Label");+ h.get_by_value("A4");+ }+}++/// A paper picked while in Label is the person's choice, and leaving Label keeps it.+#[test]+fn a_paper_picked_while_in_label_is_kept_when_leaving_it() {+ use pdfcraft_engine::print::LABEL_PAPER;+ use pdfcraft_ui_egui::{PrintDraft, PrintHandling};+ let (a4, letter) = (paper("A4"), paper("US Letter"));+ let mut d = PrintDraft { paper: a4, ..Default::default() };+ d.set_handling(PrintHandling::Label);+ assert_eq!(d.paper, LABEL_PAPER);+ d.paper = letter;+ d.set_handling(PrintHandling::Size);+ assert_eq!(d.paper, letter);+ // A second round trip from the kept paper gives that paper back.+ d.set_handling(PrintHandling::Label);+ d.set_handling(PrintHandling::Poster);+ assert_eq!(d.paper, letter);+ // Clicking Label again while in Label doesn't forget the paper it replaced.+ d.set_handling(PrintHandling::Label);+ d.set_handling(PrintHandling::Label);+ d.set_handling(PrintHandling::Multiple);+ assert_eq!(d.paper, letter);+}++/// Picking the label stock from the Paper menu switches to Label (the existing behaviour); leaving+/// Label afterwards gives back the paper picked before it.+#[test]+fn label_stock_picked_from_the_paper_menu_is_undone_by_leaving_label() {+ use pdfcraft_engine::print::{LABEL_PAPER, PAPERS};+ use pdfcraft_ui_egui::PrintHandling;+ let a4 = paper("A4");+ let mut h = harness();+ assert!(h.state_mut().execute("print.dialog"));+ h.run_steps(3);+ h.state_mut().print_draft.paper = a4;+ h.run_steps(2);+ h.get_by_value("A4").click();+ h.run_steps(2);+ // The label stock is last in a menu that scrolls: bring it into view (it settles over a few+ // frames) before clicking it.+ h.get_by_label(PAPERS[LABEL_PAPER].0).scroll_to_me();+ h.run_steps(4);+ h.get_by_label(PAPERS[LABEL_PAPER].0).click();+ h.run_steps(2);+ assert_eq!(h.state().print_draft.paper, LABEL_PAPER);+ assert_eq!(h.state().print_draft.handling, PrintHandling::Label);+ h.get_by_role_and_label(Role::Button, "Size").click();+ h.run_steps(2);+ assert_eq!(h.state().print_draft.handling, PrintHandling::Size);+ assert_eq!(h.state().print_draft.paper, a4);+}++/// The same through the Paper menu: US Letter picked while in Label stays when Size is chosen,+/// rather than the A4 Label replaced coming back.+#[test]+fn a_paper_picked_from_the_menu_while_in_label_is_kept() {+ use pdfcraft_engine::print::{LABEL_PAPER, PAPERS};+ use pdfcraft_ui_egui::PrintHandling;+ let (a4, letter) = (paper("A4"), paper("US Letter"));+ let mut h = harness();+ assert!(h.state_mut().execute("print.dialog"));+ h.run_steps(3);+ h.state_mut().print_draft.paper = a4;+ h.run_steps(2);+ h.get_by_role_and_label(Role::Button, "Label").click();+ h.run_steps(2);+ assert_eq!(h.state().print_draft.paper, LABEL_PAPER);+ h.get_by_value(PAPERS[LABEL_PAPER].0).click();+ h.run_steps(2);+ h.get_by_label("US Letter").scroll_to_me();+ h.run_steps(4);+ h.get_by_label("US Letter").click();+ h.run_steps(2);+ assert_eq!(h.state().print_draft.paper, letter);+ h.get_by_role_and_label(Role::Button, "Size").click();+ h.run_steps(2);+ assert_eq!(h.state().print_draft.handling, PrintHandling::Size);+ assert_eq!(h.state().print_draft.paper, letter, "the paper picked in Label is kept");+ h.get_by_value("US Letter");+}+++ /// Properties… reads the printer's driver options off the GUI thread: the dialog keeps drawing+ /// while CUPS answers, and a read that dies leaves the panel with nothing rather than waiting+ /// (#1818). The click's request is made directly, so the test runs where Properties… opens the+ /// driver's own window instead (Windows).+ #[cfg(not(target_arch = "wasm32"))]+ #[test]+ fn printer_options_are_read_off_the_gui_thread_and_a_dead_read_offers_nothing() {+ use pdfcraft_engine::print::spool::PrinterOption;+ use std::sync::{Arc, Mutex};+ let mut h = harness();+ // The source waits until the test lets it answer, so the dialog is seen while it reads.+ let gate = Arc::new(Mutex::new(()));+ let hold = gate.lock().unwrap();+ let wait = gate.clone();+ h.state_mut().options_source = Some(Arc::new(move |printer: &str| {+ assert_ne!(printer, "Dead", "the read died");+ let _open = wait.lock();+ vec![PrinterOption {+ key: "InputSlot".into(),+ label: "Paper tray".into(),+ group: String::new(),+ choices: vec![("Auto".into(), "Automatic".into()), ("Tray2".into(), "Tray 2".into())],+ default: "Auto".into(),+ }]+ }));+ assert!(h.state_mut().execute("print.dialog"));+ h.run_steps(2);+ let ask = |h: &mut Harness<'static, PdfCraftApp>, printer: &str| {+ let d = &mut h.state_mut().print_draft;+ d.driver_options = None;+ d.driver_options_loading = Some(printer.into());+ d.read_options_for = Some(printer.into());+ d.show_driver_options = true;+ };+ let settle = |h: &mut Harness<'static, PdfCraftApp>| {+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);+ while h.state().print_draft.driver_options_loading.is_some() && std::time::Instant::now() < deadline {+ h.run_steps(1);+ std::thread::sleep(std::time::Duration::from_millis(5));+ }+ };+ ask(&mut h, "Office");+ h.run_steps(3);+ assert_eq!(h.state().print_draft.driver_options_loading.as_deref(), Some("Office"), "frames keep coming while the read waits");+ assert!(h.state().print_draft.driver_options.is_none());+ h.get_by_label("Reading the printer's settings…");+ drop(hold);+ settle(&mut h);+ let (read_for, options) = h.state().print_draft.driver_options.clone().expect("the options arrived");+ assert_eq!(read_for, "Office");+ assert_eq!(options.len(), 1);+ assert_eq!(options[0].key, "InputSlot");+ h.get_by_label("Paper tray");++ ask(&mut h, "Dead");+ settle(&mut h);+ assert_eq!(h.state().print_draft.driver_options, Some(("Dead".into(), Vec::new())), "a dead read offers nothing, and the panel says so");+ h.get_by_label("This printer offers no settings of its own.");+ }++ /// A read of the printer's options that dies still asks for a frame, so an idle dialog stops+ /// saying it is reading without waiting for the mouse to move (#2108 review).+ #[cfg(not(target_arch = "wasm32"))]+ #[test]+ fn a_dead_options_read_wakes_an_idle_dialog() {+ use std::sync::atomic::{AtomicBool, Ordering};+ use std::sync::{Arc, Mutex};+ let mut h = harness();+ let gate = Arc::new(Mutex::new(()));+ let hold = gate.lock().unwrap();+ let wait = gate.clone();+ let dying = Arc::new(AtomicBool::new(false));+ let flag = dying.clone();+ h.state_mut().options_source = Some(Arc::new(move |_: &str| {+ let _open = wait.lock();+ flag.store(true, Ordering::SeqCst);+ panic!("the read died");+ }));+ assert!(h.state_mut().execute("print.dialog"));+ h.run_steps(2);+ let d = &mut h.state_mut().print_draft;+ d.driver_options = None;+ d.driver_options_loading = Some("Office".into());+ d.read_options_for = Some("Office".into());+ d.show_driver_options = true;+ h.run();+ assert!(!h.ctx.has_requested_repaint(), "the dialog is idle while the read waits");+ drop(hold);+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);+ while !(dying.load(Ordering::SeqCst) && h.ctx.has_requested_repaint()) && std::time::Instant::now() < deadline {+ std::thread::sleep(std::time::Duration::from_millis(5));+ }+ assert!(h.ctx.has_requested_repaint(), "the dead read asked for a frame");+ h.run();+ assert_eq!(h.state().print_draft.driver_options, Some(("Office".into(), Vec::new())));+ }