diff --git a/Cargo.lock b/Cargo.lock index 37f8c22e742f..ca9e35b7fe3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3830,6 +3830,7 @@ dependencies = [ "thiserror", "url", "windows 0.57.0", + "wry", ] [[package]] @@ -5016,8 +5017,6 @@ dependencies = [ [[package]] name = "wry" version = "0.41.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68b00c945786b02d7805d09a969fa36d0eee4e0bd4fb3ec2a79d2bf45a1b44cd" dependencies = [ "base64 0.22.1", "block", @@ -5042,6 +5041,7 @@ dependencies = [ "once_cell", "percent-encoding", "raw-window-handle 0.6.2", + "serde", "sha2", "soup3", "tao-macros", diff --git a/core/tauri-runtime-wry/Cargo.toml b/core/tauri-runtime-wry/Cargo.toml index e9de3540eae5..df548a828314 100644 --- a/core/tauri-runtime-wry/Cargo.toml +++ b/core/tauri-runtime-wry/Cargo.toml @@ -13,7 +13,8 @@ edition = { workspace = true } rust-version = { workspace = true } [dependencies] -wry = { version = "0.41", default-features = false, features = [ "drag-drop", "protocol", "os-webview" ] } +#wry = { version = "0.41", default-features = false, features = [ "drag-drop", "protocol", "os-webview" ] } +wry = { version = "0.41", path = "../../../wry", default-features = false, features = [ "drag-drop", "protocol", "os-webview" ] } tao = { version = "0.28.1", default-features = false, features = [ "rwh_06" ] } tauri-runtime = { version = "2.0.0-beta.20", path = "../tauri-runtime" } tauri-utils = { version = "2.0.0-beta.19", path = "../tauri-utils" } diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index 84cda0736596..b9d0b83e751d 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -71,6 +71,7 @@ pub use tao; pub use tao::window::{Window, WindowBuilder as TaoWindowBuilder, WindowId as TaoWindowId}; pub use wry; pub use wry::webview_version; +pub use wry::{PrintOption, PrintMargin}; #[cfg(windows)] use wry::WebViewExtWindows; @@ -1187,6 +1188,7 @@ pub enum WebviewMessage { SynthesizedWindowEvent(SynthesizedWindowEvent), Navigate(Url), Print, + PrintWithOptions(Vec), Close, SetPosition(Position), SetSize(Size), @@ -1351,6 +1353,17 @@ impl WebviewDispatch for WryWebviewDispatcher { ) } + fn print_with_options(&self, opts: Vec) -> Result<()> { + send_user_message( + &self.context, + Message::Webview( + *self.window_id.lock().unwrap(), + self.webview_id, + WebviewMessage::PrintWithOptions(opts), + ), + ) + } + fn close(&self) -> Result<()> { send_user_message( &self.context, @@ -3015,6 +3028,9 @@ fn handle_user_message( WebviewMessage::Print => { let _ = webview.print(); } + WebviewMessage::PrintWithOptions(opts) => { + let _ = webview.print_with_options(&opts); + } WebviewMessage::Close => { windows.0.borrow_mut().get_mut(&window_id).map(|window| { if let Some(i) = window.webviews.iter().position(|w| w.id == webview.id) { diff --git a/core/tauri-runtime/src/lib.rs b/core/tauri-runtime/src/lib.rs index 5c66a1de81af..1b832689c2c0 100644 --- a/core/tauri-runtime/src/lib.rs +++ b/core/tauri-runtime/src/lib.rs @@ -38,6 +38,8 @@ use http::{ /// UI scaling utilities. pub use dpi; +pub use wry::{PrintOption, PrintMargin}; + pub type WindowEventId = u32; pub type WebviewEventId = u32; @@ -480,6 +482,9 @@ pub trait WebviewDispatch: Debug + Clone + Send + Sync + Sized + ' /// Opens the dialog to prints the contents of the webview. fn print(&self) -> Result<()>; + /// Triggers a configurable print for the content of the webview + fn print_with_options(&self, opts: Vec) -> Result<()>; + /// Closes the webview. fn close(&self) -> Result<()>; diff --git a/core/tauri/src/webview/mod.rs b/core/tauri/src/webview/mod.rs index f19b90ab7854..5e91754e7b5a 100644 --- a/core/tauri/src/webview/mod.rs +++ b/core/tauri/src/webview/mod.rs @@ -22,6 +22,7 @@ use tauri_runtime::{ webview::{DetachedWebview, PendingWebview, WebviewAttributes}, Rect, WebviewDispatch, }; +use tauri_runtime_wry::PrintOption; use tauri_utils::config::{WebviewUrl, WindowConfig}; pub use url::Url; @@ -888,16 +889,24 @@ impl Webview { } } +/// Alias for a list of print options +pub type PrintOptions = Vec; + /// Desktop webview setters and actions. #[cfg(desktop)] impl Webview { - /// Opens the dialog to prints the contents of the webview. + /// Opens the dialog to prints the contents of the webview. Print options are not used. /// Currently only supported on macOS on `wry`. /// `window.print()` works on all platforms. pub fn print(&self) -> crate::Result<()> { self.webview.dispatcher.print().map_err(Into::into) } + /// Executes a print operation with the options given + pub fn print_with_options(&self, opts: PrintOptions) -> crate::Result<()> { + self.webview.dispatcher.print_with_options(opts).map_err(Into::into) + } + /// Get the cursor position relative to the top-left hand corner of the desktop. /// /// Note that the top-left hand corner of the desktop is not necessarily the same as the screen. diff --git a/core/tauri/src/webview/plugin.rs b/core/tauri/src/webview/plugin.rs index 09d2aca5942f..e425980c777b 100644 --- a/core/tauri/src/webview/plugin.rs +++ b/core/tauri/src/webview/plugin.rs @@ -152,7 +152,7 @@ mod desktop_commands { getter!(webview_size, size, tauri_runtime::dpi::PhysicalSize); //getter!(is_focused, bool); - setter!(print); + setter!(print_with_options, print_with_options, crate::webview::PrintOptions); setter!(webview_close, close); setter!(set_webview_size, set_size, Size); setter!(set_webview_position, set_position, Position); @@ -240,7 +240,7 @@ pub fn init() -> TauriPlugin { desktop_commands::set_webview_position, desktop_commands::set_webview_focus, desktop_commands::set_webview_zoom, - desktop_commands::print, + desktop_commands::print_with_options, desktop_commands::reparent, #[cfg(any(debug_assertions, feature = "devtools"))] desktop_commands::internal_toggle_devtools, diff --git a/tooling/api/src/print.ts b/tooling/api/src/print.ts new file mode 100644 index 000000000000..fc899fe157c9 --- /dev/null +++ b/tooling/api/src/print.ts @@ -0,0 +1,41 @@ +// Copyright 2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +/** + * Start printing without a user dialog + * + * @since 2.0.0 + */ +class OptionSilent { + Silent: bool +} + +/** + * The printing margins in mm + * + * @since 2.0.0 + */ +class OptionMargins { + Margins: { + top: number + bottom: number + left: number + right: number + } +} + +/** + * The printing margins in mm + * + * @since 2.0.0 + */ +class OptionGeneratePDF { + GeneratePDF: { + filename: string + } +} + +type PrintOption = OptionSilent | OptionMargins | OptionGeneratePDF + +export { PrintOption, OptionSilent, OptionMargins, OptionGeneratePDF } diff --git a/tooling/api/src/webview.ts b/tooling/api/src/webview.ts index e2036f5456b0..af115c655915 100644 --- a/tooling/api/src/webview.ts +++ b/tooling/api/src/webview.ts @@ -336,6 +336,16 @@ class Webview { return false } + /** + * Prints the current webview given the provided options + */ + async print(options: PrintOption[]): Promise { + return invoke('plugin:webview|print', { + label: this.label, + value: options + }) + } + // Getters /** * The position of the top-left hand corner of the webview's client area relative to the top-left hand corner of the desktop.