Skip to content

Commit

Permalink
feat: first implementation of print API
Browse files Browse the repository at this point in the history
  • Loading branch information
Mildred KiLya authored and mildred committed Jul 13, 2024
1 parent 6d965e9 commit d62ed89
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion core/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
16 changes: 16 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1187,6 +1188,7 @@ pub enum WebviewMessage {
SynthesizedWindowEvent(SynthesizedWindowEvent),
Navigate(Url),
Print,
PrintWithOptions(Vec<wry::PrintOption>),
Close,
SetPosition(Position),
SetSize(Size),
Expand Down Expand Up @@ -1351,6 +1353,17 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
)
}

fn print_with_options(&self, opts: Vec<PrintOption>) -> 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,
Expand Down Expand Up @@ -3015,6 +3028,9 @@ fn handle_user_message<T: UserEvent>(
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) {
Expand Down
5 changes: 5 additions & 0 deletions core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -480,6 +482,9 @@ pub trait WebviewDispatch<T: UserEvent>: 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<PrintOption>) -> Result<()>;

/// Closes the webview.
fn close(&self) -> Result<()>;

Expand Down
11 changes: 10 additions & 1 deletion core/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -888,16 +889,24 @@ impl<R: Runtime> Webview<R> {
}
}

/// Alias for a list of print options
pub type PrintOptions = Vec<PrintOption>;

/// Desktop webview setters and actions.
#[cfg(desktop)]
impl<R: Runtime> Webview<R> {
/// 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.
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/src/webview/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ mod desktop_commands {
getter!(webview_size, size, tauri_runtime::dpi::PhysicalSize<u32>);
//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);
Expand Down Expand Up @@ -240,7 +240,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
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,
Expand Down
41 changes: 41 additions & 0 deletions tooling/api/src/print.ts
Original file line number Diff line number Diff line change
@@ -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 }
10 changes: 10 additions & 0 deletions tooling/api/src/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@ class Webview {
return false
}

/**
* Prints the current webview given the provided options
*/
async print(options: PrintOption[]): Promise<void> {
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.
Expand Down

0 comments on commit d62ed89

Please sign in to comment.