diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 2d172e772..104e17c2f 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -197,6 +197,8 @@ impl SlideApp { #[cfg(not(target_arch = "wasm32"))] print_spool: None, #[cfg(not(target_arch = "wasm32"))] + print_receipt: None, + #[cfg(not(target_arch = "wasm32"))] updates_rt: Default::default(), selpane: Default::default(), align_to: None, @@ -264,6 +266,24 @@ impl SlideApp { } } + #[cfg(not(target_arch = "wasm32"))] + fn poll_print_receipt(&mut self, ctx: &egui::Context) { + use std::sync::mpsc::TryRecvError; + + let Some((printer, receipt)) = &self.print_receipt else { return }; + let status = match receipt.try_recv() { + Ok(Ok(message)) => format!("Sent to {printer}: {message}"), + Ok(Err(error)) => format!("Couldn't print: {error}"), + Err(TryRecvError::Disconnected) => "Couldn't confirm whether the print job reached the printer".into(), + Err(TryRecvError::Empty) => { + ctx.request_repaint_after(std::time::Duration::from_millis(200)); + return; + } + }; + self.print_receipt = None; + self.set_status(status); + } + /// Displays a transient message in the status bar. pub fn set_status(&mut self, msg: impl Into) { self.status = Some((msg.into(), self.clock.now() * 1000.0)); @@ -386,6 +406,8 @@ impl SlideApp { if !Self::root_viewport_admitted(ctx) { return; } + #[cfg(not(target_arch = "wasm32"))] + self.poll_print_receipt(ctx); self.tick_hidden_show(ctx, web_time::Instant::now()); if WindowHost::of(ctx).injected_idle() { self.drain_intake(ctx); diff --git a/crates/ui-egui/src/app/catalog.rs b/crates/ui-egui/src/app/catalog.rs index f1936d41e..b89f28d41 100644 --- a/crates/ui-egui/src/app/catalog.rs +++ b/crates/ui-egui/src/app/catalog.rs @@ -58,7 +58,7 @@ pub static UI_COMMANDS: &[UiCommand] = &[ UiCommand { id: "app.print", label: "Print", - params: "{layout?, perPage?, slides?, includeHidden?, frame?, paper?: letter|a4|legal|a3, orientation?: portrait|landscape}", + params: "{layout?, perPage?, slides?, includeHidden?, frame?, paper?: letter|a4|legal|a3, orientation?: portrait|landscape} (no options opens desktop print settings when a system spooler is installed)", }, UiCommand { id: "app.about", label: "About DeckCraft", params: "{}" }, UiCommand { id: "app.checkUpdates", label: "Check for Updates…", params: "{}" }, diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index 39bfb65c4..f5589ee06 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -275,6 +275,11 @@ impl SlideApp { if self.session.active().is_none() { return Err("nothing to print: no presentation is open".into()); } + #[cfg(not(target_arch = "wasm32"))] + if self.print_spool.is_some() && (p.is_null() || p.as_object().is_some_and(|args| args.is_empty())) { + self.open_backstage(backstage::Page::Print); + return Ok(json!({"printed": false, "page": "print"})); + } return Ok(json!({"printed": self.print_pdf(p.clone())})); } "app.about" => self.open_dialog("about", p.clone()), diff --git a/crates/ui-egui/src/backstage/print.rs b/crates/ui-egui/src/backstage/print.rs index c45229219..b504a4401 100644 --- a/crates/ui-egui/src/backstage/print.rs +++ b/crates/ui-egui/src/backstage/print.rs @@ -414,9 +414,6 @@ pub(super) fn export(app: &mut SlideApp, ui: &mut Ui, _cx: &mut BodyCx<'_>, bs: #[cfg(not(target_arch = "wasm32"))] type PrinterDiscovery = Rc>>; -#[cfg(not(target_arch = "wasm32"))] -type PrintReceipt = Rc>>; - #[cfg(not(target_arch = "wasm32"))] #[derive(Clone, Debug)] pub struct DirectPrint { @@ -424,14 +421,12 @@ pub struct DirectPrint { discovery: Option, printer: String, copies: String, - /// The job being sent: the printer it went to, and its result. - job: Option<(String, PrintReceipt)>, } #[cfg(not(target_arch = "wasm32"))] impl Default for DirectPrint { fn default() -> Self { - DirectPrint { printers: None, discovery: None, printer: String::new(), copies: "1".into(), job: None } + DirectPrint { printers: None, discovery: None, printer: String::new(), copies: "1".into() } } } @@ -457,25 +452,8 @@ fn direct_print(app: &mut SlideApp, ui: &mut Ui, bs: &mut Backstage, doc: &deckc } } } - if let Some((sent_to, rx)) = &dp.job { - match rx.try_recv() { - Ok(Ok(message)) => { - app.set_status(format!("Sent to {sent_to}: {message}")); - dp.job = None; - } - Ok(Err(e)) => { - app.set_status(format!("Couldn't print: {e}")); - dp.job = None; - } - Err(std::sync::mpsc::TryRecvError::Empty) => ui.ctx().request_repaint_after(std::time::Duration::from_millis(200)), - Err(std::sync::mpsc::TryRecvError::Disconnected) => { - app.set_status("Couldn't confirm whether the print job reached the printer"); - dp.job = None; - } - } - } let copies = dp.copies.trim().parse::().ok().filter(|c| (1..=999).contains(c)); - let sending = dp.job.is_some(); + let sending = app.print_receipt.is_some(); let ready = slides.is_some() && !sending && !dp.printer.is_empty() && copies.is_some(); let detail = if sending { "Sending the job to the printer…" } else { "Sends the slides straight to the printer chosen below." }; if task_row(ui, Icon::Print, "Print", "Print", detail, ready) @@ -486,7 +464,8 @@ fn direct_print(app: &mut SlideApp, ui: &mut Ui, bs: &mut Backstage, doc: &deckc let dp = &mut bs.direct_print; let title = app.session.active().map(|d| d.title()).unwrap_or_else(|| "DeckCraft".into()); let job = craft::print::Job { printer: Some(dp.printer.clone()), copies, collate: true, title, ..Default::default() }; - dp.job = Some((dp.printer.clone(), Rc::new(spool.submit(bytes, job)))); + app.print_receipt = Some((dp.printer.clone(), spool.submit(bytes, job))); + ui.ctx().request_repaint(); app.set_status(format!("Sending to {}…", dp.printer)); } Err(e) => app.set_status(format!("Couldn't make the pages to print: {e}")), diff --git a/crates/ui-egui/src/backstage/tests.rs b/crates/ui-egui/src/backstage/tests.rs index 60880da27..b99fc3b4d 100644 --- a/crates/ui-egui/src/backstage/tests.rs +++ b/crates/ui-egui/src/backstage/tests.rs @@ -652,6 +652,27 @@ fn direct_print_reports_the_spooler_failure_and_keeps_the_viewer() { assert!(h.query_by_label("Open as PDF instead").is_some()); } +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn print_from_quick_access_opens_settings_and_job_feedback_survives_leaving() { + let mut h = new_app(); + h.state_mut().enable_direct_print(); + h.state_mut().run("app.qat", json!({"add": "app.print", "label": "Print"})).unwrap(); + let index = h.state().ui.qat.iter().position(|q| q.id == "app.print").unwrap(); + assert!(crate::qat::run_index(h.state_mut(), index)); + assert_eq!(page(&h), Some(Page::Print)); + assert!(h.state().print_receipt.is_none()); + + let (sender, receiver) = std::sync::mpsc::channel(); + h.state_mut().print_receipt = Some(("Studio printer".into(), receiver)); + h.state_mut().run("app.backstage", json!({"on": false})).unwrap(); + assert_eq!(page(&h), None); + sender.send(Err("queue rejected these sheets".into())).unwrap(); + h.run_steps(3); + assert!(h.state().print_receipt.is_none()); + assert!(h.state().status.as_ref().is_some_and(|(message, _)| message.contains("queue rejected these sheets"))); +} + #[test] fn print_slides_follow_the_settings() { let mut s = Session::with_new(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 7e9e6a452..f01db1abb 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -223,6 +223,9 @@ pub struct SlideApp { print_previews: craft::platform::temp::TempFiles, #[cfg(not(target_arch = "wasm32"))] print_spool: Option, + /// The printer and its pending receipt outlive the File > Print view. + #[cfg(not(target_arch = "wasm32"))] + print_receipt: Option<(String, std::sync::mpsc::Receiver>)>, #[cfg(not(target_arch = "wasm32"))] updates_rt: updates_ui::Runtime, /// Selection pane: folded groups and the Shift-run anchor.