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

source

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.

source

pub fn get_native_handle(&self) -> id

Get the WKWebView instance

source

pub fn get_user_content_controller_handle(&self) -> id

Get the WKUserContentController instance

source

pub fn get_configuration_handle(&self) -> id

Get the WKWebViewConfiguration instance

source

pub unsafe fn load_url(&self, url: &str)

Load an URL onto the WebView.

Safety

All the FFI functions are unsafe.

source

pub unsafe fn load_html_string(&self, html: &str, base_url: &str)

Load an HTML string onto the WebView.

Safety

All the FFI functions are unsafe.

source

pub unsafe fn evaluate_javascript(&self, javascript: &str)

Evaluate a JavaScript string on the WebView

Safety

All the FFI functions are unsafe.

source

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§

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.