Skip to main content

tauri_plugin_printer/
lib.rs

1use tauri::{
2    plugin::{Builder, TauriPlugin},
3    Runtime,
4};
5
6#[cfg(desktop)]
7use tauri::Manager;
8
9pub use models::*;
10
11#[cfg(desktop)]
12mod desktop;
13
14#[cfg(desktop)]
15mod commands;
16#[cfg(desktop)]
17mod desktopapp;
18mod error;
19mod models;
20pub use error::{Error, Result};
21
22#[cfg(desktop)]
23pub use desktop::Printer;
24
25/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the printer APIs.
26#[cfg(desktop)]
27pub trait PrinterExt<R: Runtime> {
28    fn printer(&self) -> &Printer<R>;
29}
30
31#[cfg(desktop)]
32impl<R: Runtime, T: Manager<R>> crate::PrinterExt<R> for T {
33    fn printer(&self) -> &Printer<R> {
34        self.state::<Printer<R>>().inner()
35    }
36}
37
38/// Initializes the plugin.
39pub fn init<R: Runtime>() -> TauriPlugin<R> {
40    let builder = Builder::new("printer");
41
42    #[cfg(desktop)]
43    let builder = builder
44        .invoke_handler(tauri::generate_handler![
45            commands::get_printers,
46            commands::get_printer,
47            commands::get_default_printer,
48            commands::set_default_printer,
49            commands::get_printer_capabilities,
50            commands::get_jobs,
51            commands::get_job,
52            commands::resume_job,
53            commands::restart_job,
54            commands::pause_job,
55            commands::remove_job,
56            commands::print_pdf,
57            commands::print_pdf_data,
58        ])
59        .setup(|app, api| {
60            let printer = desktop::init(app, api)?;
61            app.manage(printer);
62            Ok(())
63        });
64
65    builder.build()
66}