1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Printing a [`Paper`]: the system's print dialog, where the person picks a printer, the pages
//! and the copies, then the pages go to that printer.
//!
//! On Windows the pages go to the printer's spooler as pixels (`spooler.rs`); on Linux the
//! desktop portal shows its dialog and takes the pages as a PDF (`printer.rs`). Where neither
//! exists, nothing is printed.
//!
//! The call returns at once; [`Printing`] says what came of it later, and the window runs again
//! when it is known. A program that never calls `print` carries none of this.
use crate::paper::Paper;
use std::sync::{Arc, Mutex};
#[cfg(all(unix, not(target_os = "macos")))]
use crate::printer::ask;
#[cfg(windows)]
use crate::spooler::ask;
/// What came of printing.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Printed {
/// The pages went to the printer the person picked.
Sent,
/// The person closed the dialog without printing.
Closed,
/// Printing is not possible here: no printer, no dialog, or the system refused the pages.
Unable,
}
/// Printing that was asked for, and what came of it once known.
#[derive(Clone, Debug)]
pub struct Printing {
told: Arc<Mutex<Option<Printed>>>,
}
impl Printing {
/// What came of it: nothing while the dialog is up and the pages are on their way. The
/// window runs again once it is known.
pub fn done(&self) -> Option<Printed> {
*self.told.lock().unwrap_or_else(|held| held.into_inner())
}
}
/// Prints the pages of `paper`: shows the system's print dialog (printer, pages, copies) and
/// sends the pages there, listed as `title` in the printer's queue. Returns at once;
/// [`Printing::done`] says what came of it. On Windows the dialog shows over the window in use
/// once the thread's messages are taken: at once under a taut window, else from the program's
/// own message loop.
///
/// ```no_run
/// # use taut::{Font, Color, Paper, Printing, Ui};
/// # fn describe(ui: &mut Ui, printing: &mut Option<Printing>) {
/// if ui.button("Print").clicked {
/// let mut paper = Paper::new();
/// paper.page(Paper::A4);
/// paper.text("Hello, printer", 72.0, 72.0, Font::new(14.0), Color::BLACK);
/// *printing = Some(taut::print(paper, "A letter"));
/// }
/// if printing.as_ref().and_then(Printing::done).is_some() {
/// *printing = None; // done: printed, or the dialog closed
/// }
/// # }
/// ```
pub fn print(paper: Paper, title: &str) -> Printing {
let printing = Printing { told: Arc::new(Mutex::new(None)) };
ask(paper, title, printing.told.clone());
printing
}
/// Says what came of printing, and has the window run again to hear it.
pub(crate) fn tell(told: &Mutex<Option<Printed>>, printed: Printed) {
*told.lock().unwrap_or_else(|held| held.into_inner()) = Some(printed);
#[cfg(any(windows, all(unix, not(target_os = "macos"))))]
crate::platform::Waker.wake();
}
/// No way of printing: nothing is printed.
#[cfg(not(any(windows, all(unix, not(target_os = "macos")))))]
fn ask(paper: Paper, title: &str, told: Arc<Mutex<Option<Printed>>>) {
let _ = (paper, title);
tell(&told, Printed::Unable);
}