From 5ec3b172e8851bd095e47595a64eb17237057d93 Mon Sep 17 00:00:00 2001 From: David Lemarier Date: Mon, 26 Apr 2021 08:09:13 -0400 Subject: [PATCH 1/2] feat(webview): Allow loading of HTML string instead of URL. --- src/webview/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 5edcef7..c6d8757 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -253,6 +253,15 @@ impl WebView { }); } + /// Given a HTML string, instructs the WebView to load it. + /// Usefull for small html file, but better to use custom protocol. + pub fn load_html(&self, html_string: &str) { + self.objc.with_mut(|obj| unsafe { + let empty: id = msg_send![class!(NSURL), URLWithString: NSString::new("")]; + let _: () = msg_send![&*obj, loadHTMLString:NSString::new(html_string) baseURL:empty]; + }); + } + /// Go back in history, if possible. pub fn go_back(&self) { self.objc.with_mut(|obj| unsafe { From eb35b647f70b1abd1146a837a9b286448932aa0e Mon Sep 17 00:00:00 2001 From: Ryan McGrath Date: Mon, 26 Apr 2021 14:22:07 -0700 Subject: [PATCH 2/2] Update mod.rs Slight edit for docstring + dereferencing the `NSString`s. --- src/webview/mod.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/webview/mod.rs b/src/webview/mod.rs index c6d8757..5e39897 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -254,11 +254,14 @@ impl WebView { } /// Given a HTML string, instructs the WebView to load it. - /// Usefull for small html file, but better to use custom protocol. + /// Useful for small html files, but often better to use custom protocol. pub fn load_html(&self, html_string: &str) { + let html = NSString::new(html_string); + let blank = NSString::no_copy(""); + self.objc.with_mut(|obj| unsafe { - let empty: id = msg_send![class!(NSURL), URLWithString: NSString::new("")]; - let _: () = msg_send![&*obj, loadHTMLString:NSString::new(html_string) baseURL:empty]; + let empty: id = msg_send![class!(NSURL), URLWithString:&*blank]; + let _: () = msg_send![&*obj, loadHTMLString:&*html baseURL:empty]; }); }