Skip to content

Commit

Permalink
fix(macos): only use APIs when supported
Browse files Browse the repository at this point in the history
The `titlebarSeparatorStyle` and `printOperationWithPrintInfo` APIs are only available in macOS 11+, we need to support 10.x so we put a guard on it
  • Loading branch information
lucasfernog committed May 8, 2022
1 parent 8dd823a commit eb2dddb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-macos-10.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Fixes a crash on macOS below Big Sur due to `titlebarSeparatorStyle` (11+ API) usage.
5 changes: 5 additions & 0 deletions .changes/macos-print.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Only run `WebView::print` on macOS on v11+. This prevents a crash on older versions.
34 changes: 23 additions & 11 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,14 @@ impl InnerWebView {
let ns_window = {
let ns_window = window.ns_window() as id;

// `1` means `none`, see https://developer.apple.com/documentation/appkit/nstitlebarseparatorstyle/none
let () = msg_send![ns_window, setTitlebarSeparatorStyle: 1];
let can_set_titlebar_style: BOOL = msg_send![
ns_window,
respondsToSelector: sel!(setTitlebarSeparatorStyle:)
];
if can_set_titlebar_style {
// `1` means `none`, see https://developer.apple.com/documentation/appkit/nstitlebarseparatorstyle/none
let () = msg_send![ns_window, setTitlebarSeparatorStyle: 1];
}

ns_window
};
Expand Down Expand Up @@ -536,15 +542,21 @@ r#"Object.defineProperty(window, 'ipc', {
// Safety: objc runtime calls are unsafe
#[cfg(target_os = "macos")]
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>()];
let can_print: BOOL = msg_send![
self.webview,
respondsToSelector: sel!(printOperationWithPrintInfo:)
];
if can_print {
// 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>()];
}
}
}

Expand Down

0 comments on commit eb2dddb

Please sign in to comment.