taut 0.1.0

A Rust GUI library for Windows and Linux that aims to be small, fast and accessible, with zero dependencies and nothing compiled in that your program doesn't use.
Documentation
//! 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);
}