Struct darwin_webkit::helpers::dwk_webview::DarwinWKWebView
source · pub struct DarwinWKWebView { /* private fields */ }
Expand description
Wraps a WKWebView
, WKWebViewConfiguration
& WKUserContentController
Can be used from a cocoa application with get_native_handle
or with DWKApp
.
Example
extern crate cocoa;
use cocoa::base::id;
use darwin_webkit::helpers::dwk_app::DarwinWKApp;
use std::sync::Arc;
unsafe {
let app = DarwinWKApp::new("Host an app");
let webview = Arc::new(app.create_webview());
let callback_webview = webview.clone();
let callback = Box::into_raw(Box::new(Box::new(|_: id, _: id| {
println!("JavaScript called rust!");
callback_webview.evaluate_javascript(
"document.body.innerHTML += ' -> response from rust<br />';"
);
})));
webview.add_message_handler("hello", callback);
webview.load_html_string("
<script>
document.body.innerHTML += 'start';
window.webkit.messageHandlers.hello.postMessage('hello');
</script>
", "", );
}
Implementations§
source§impl DarwinWKWebView
impl DarwinWKWebView
sourcepub unsafe fn new(frame: NSRect) -> DarwinWKWebView
pub unsafe fn new(frame: NSRect) -> DarwinWKWebView
Create a webview with the given frame rect. Also creates the supporting configuration and content controller.
Thee view is resizable other options are empty.
Safety
All the FFI functions are unsafe.
sourcepub fn get_native_handle(&self) -> id
pub fn get_native_handle(&self) -> id
Get the WKWebView
instance
sourcepub fn get_user_content_controller_handle(&self) -> id
pub fn get_user_content_controller_handle(&self) -> id
Get the WKUserContentController
instance
sourcepub fn get_configuration_handle(&self) -> id
pub fn get_configuration_handle(&self) -> id
Get the WKWebViewConfiguration
instance
sourcepub unsafe fn load_html_string(&self, html: &str, base_url: &str)
pub unsafe fn load_html_string(&self, html: &str, base_url: &str)
sourcepub unsafe fn evaluate_javascript(&self, javascript: &str)
pub unsafe fn evaluate_javascript(&self, javascript: &str)
sourcepub unsafe fn add_message_handler<Func>(&self, name: &str, callback: *mut Func)where
Func: FnMut(id, id),
pub unsafe fn add_message_handler<Func>(&self, name: &str, callback: *mut Func)where Func: FnMut(id, id),
Register a callback into the WebView.
Calls make_new_handler
under the hood. The callback should have form:
ⓘ
FnMut(id /* WKUserContentController */, id /* WKScriptMessage */)
The handler will be available from JavaScript through:
window.webkit.messageHandlers.name.postMessage('some message');
Safety
All the FFI functions are unsafe.
Your callback will be called from WebKit. If the WebView outlives it: 💥.
Trait Implementations§
impl Send for DarwinWKWebView
impl Sync for DarwinWKWebView
Auto Trait Implementations§
impl RefUnwindSafe for DarwinWKWebView
impl Unpin for DarwinWKWebView
impl UnwindSafe for DarwinWKWebView
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more