Skip to content

Commit

Permalink
fix(macOS): Printing (#235) (#236)
Browse files Browse the repository at this point in the history
* fix(macOS): Printing

Related to #235

* Add change file
  • Loading branch information
lemarier authored May 6, 2021
1 parent 7c9abf7 commit 5206db6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/macos-printing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Expose `print()` function to the webview. Work only on macOS for now.
3 changes: 3 additions & 0 deletions src/webview/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ impl InnerWebView {
Ok(w)
}

// not supported yet
pub fn print(&self) {}

pub fn eval(&self, js: &str) -> Result<()> {
let cancellable: Option<&Cancellable> = None;
self.webview.run_javascript(js, cancellable, |_| ());
Expand Down
21 changes: 20 additions & 1 deletion src/webview/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod file_drop;

pub struct InnerWebView {
webview: Id<Object>,
ns_window: id,
manager: id,
rpc_handler_ptr: *mut (
Box<dyn Fn(&Window, RpcRequest) -> Option<RpcResponse>>,
Expand Down Expand Up @@ -230,8 +231,12 @@ impl InnerWebView {
None => set_file_drop_handler(webview, window.clone(), Box::new(|_, _| false)),
};

// ns window is required for the print operation
let ns_window = window.ns_window() as id;

let w = Self {
webview: Id::from_ptr(webview),
ns_window,
manager,
rpc_handler_ptr,
file_drop_ptr,
Expand Down Expand Up @@ -290,7 +295,6 @@ impl InnerWebView {
// Tell the webview we use layers
let _: () = msg_send![webview, setWantsLayer: YES];
// Inject the web view into the window as main content
let ns_window = window.ns_window() as id;
let _: () = msg_send![ns_window, setContentView: webview];

Ok(w)
Expand Down Expand Up @@ -333,6 +337,21 @@ impl InnerWebView {
let () = msg_send![self.webview, loadHTMLString:NSString::new(url) baseURL:empty];
}
}

pub fn print(&self) {
// Safety: objc runtime calls are unsafe
unsafe {
// Create a shared print info
let print_info: id = msg_send![class!(NSPrintInfo), sharedPrintInfo];
let print_info: id = msg_send![print_info, init];
// Create new print operation from the webview content
let print_operation: id = msg_send![self.webview, printOperationWithPrintInfo: print_info];
// Allow the modal to detach from the current thread and be non-blocker
let () = msg_send![print_operation, setCanSpawnSeparateThread: YES];
// Launch the modal
let () = msg_send![print_operation, runOperationModalForWindow: self.ns_window delegate: null::<*const c_void>() didRunSelector: null::<*const c_void>() contextInfo: null::<*const c_void>()];
}
}
}

impl Drop for InnerWebView {
Expand Down
7 changes: 7 additions & 0 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ impl WebView {
Ok(())
}

/// Launch print modal for the webview content.
/// Works only on macOS for now.
pub fn print(&self) -> Result<()> {
self.webview.print();
Ok(())
}

/// Resize the WebView manually. This is required on Windows because its WebView API doesn't
/// provide a way to resize automatically.
pub fn resize(&self) -> Result<()> {
Expand Down
3 changes: 3 additions & 0 deletions src/webview/win32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ impl InnerWebView {
})
}

// not supported yet
pub fn print(&self) {}

pub fn eval(&self, js: &str) -> Result<()> {
if let Some(c) = self.controller.get() {
let webview = c.get_webview()?;
Expand Down
3 changes: 3 additions & 0 deletions src/webview/winrt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ impl InnerWebView {
})
}

// not supported yet
pub fn print(&self) {}

pub fn eval(&self, js: &str) -> Result<()> {
if let Some(w) = self.webview.get() {
let _ = w.ExecuteScriptAsync(js)?;
Expand Down

0 comments on commit 5206db6

Please sign in to comment.