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 cde120c
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 5 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.

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
1 change: 1 addition & 0 deletions core/tauri-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ http = "1.1"
raw-window-handle = "0.6"
url = { version = "2" }
dpi = { version = "0.1", features = [ "serde" ] }
wry = { version = "0.41", default-features = false, features = [ "drag-drop", "protocol", "os-webview", "serde" ] }

[target."cfg(windows)".dependencies.windows]
version = "0.57"
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
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

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
2 changes: 1 addition & 1 deletion 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, 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
40 changes: 40 additions & 0 deletions tooling/api/src/print.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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
*/
export type OptionSilent = {
Silent: boolean
}

/**
* The printing margins in mm
*
* @since 2.0.0
*/
export type OptionMargins = {
Margins: {
top: number,
bottom: number,
left: number,
right: number
}
}

/**
* The printing margins in mm
*
* @since 2.0.0
*/
export type OptionGeneratePDF = {
GeneratePDF: {
filename: string
}
}

export type PrintOption = OptionSilent | OptionMargins | OptionGeneratePDF

11 changes: 11 additions & 0 deletions tooling/api/src/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import { PhysicalPosition, PhysicalSize } from './dpi'
import type { LogicalPosition, LogicalSize } from './dpi'
import type { PrintOption } from './print'
import type { EventName, EventCallback, UnlistenFn } from './event'
import {
TauriEvent,
Expand Down Expand Up @@ -336,6 +337,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 cde120c

Please sign in to comment.