From e332eff6ac41ff0f4ed6cf5196c3a78c776912d3 Mon Sep 17 00:00:00 2001 From: Justin Bennett Date: Wed, 25 Sep 2024 19:29:03 -0400 Subject: [PATCH] feat: add `WebView::load_html` for loading HTML after intialization (#1368) --- .changes/load-html.md | 5 +++++ src/android/main_pipe.rs | 7 +++++++ src/android/mod.rs | 5 +++++ src/lib.rs | 5 +++++ src/webkitgtk/mod.rs | 5 +++++ src/webview2/mod.rs | 5 +++++ src/wkwebview/mod.rs | 5 +++++ 7 files changed, 37 insertions(+) create mode 100644 .changes/load-html.md diff --git a/.changes/load-html.md b/.changes/load-html.md new file mode 100644 index 000000000..0cab0240f --- /dev/null +++ b/.changes/load-html.md @@ -0,0 +1,5 @@ +--- +"wry": minor +--- + +Add `Webview::load_html`. diff --git a/src/android/main_pipe.rs b/src/android/main_pipe.rs index ae7ebc460..37f5f8708 100644 --- a/src/android/main_pipe.rs +++ b/src/android/main_pipe.rs @@ -300,6 +300,12 @@ impl<'a> MainPipe<'a> { .call_method(webview, "clearAllBrowsingData", "()V", &[])?; } } + WebViewMessage::LoadHtml(html) => { + if let Some(webview) = &self.webview { + let html = self.env.new_string(html)?; + load_html(&mut self.env, webview.as_obj(), &html)?; + } + } } } Ok(()) @@ -369,6 +375,7 @@ pub(crate) enum WebViewMessage { GetUrl(Sender), Jni(Box), LoadUrl(String, Option), + LoadHtml(String), ClearAllBrowsingData, } diff --git a/src/android/mod.rs b/src/android/mod.rs index 401d25397..273117df8 100644 --- a/src/android/mod.rs +++ b/src/android/mod.rs @@ -351,6 +351,11 @@ impl InnerWebView { Ok(()) } + pub fn load_html(&self, html: &str) -> Result<()> { + MainPipe::send(WebViewMessage::LoadHtml(html.to_string())); + Ok(()) + } + pub fn clear_all_browsing_data(&self) -> Result<()> { MainPipe::send(WebViewMessage::ClearAllBrowsingData); Ok(()) diff --git a/src/lib.rs b/src/lib.rs index 3f052be27..d6fdb94f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1477,6 +1477,11 @@ impl WebView { self.webview.load_url_with_headers(url, headers) } + /// Load html content into the webview + pub fn load_html(&self, html: &str) -> Result<()> { + self.webview.load_html(html) + } + /// Clear all browsing data pub fn clear_all_browsing_data(&self) -> Result<()> { self.webview.clear_all_browsing_data() diff --git a/src/webkitgtk/mod.rs b/src/webkitgtk/mod.rs index e92ee5651..14583ddd2 100644 --- a/src/webkitgtk/mod.rs +++ b/src/webkitgtk/mod.rs @@ -673,6 +673,11 @@ impl InnerWebView { Ok(()) } + pub fn load_html(&self, html: &str) -> Result<()> { + self.webview.load_html(html, None); + Ok(()) + } + pub fn clear_all_browsing_data(&self) -> Result<()> { if let Some(context) = self.webview.context() { if let Some(data_manger) = context.website_data_manager() { diff --git a/src/webview2/mod.rs b/src/webview2/mod.rs index 2dfb53474..18ff184eb 100644 --- a/src/webview2/mod.rs +++ b/src/webview2/mod.rs @@ -1205,6 +1205,11 @@ impl InnerWebView { load_url_with_headers(&self.webview, &self.env, url, headers) } + pub fn load_html(&self, html: &str) -> Result<()> { + let html = HSTRING::from(html); + unsafe { self.webview.NavigateToString(&html) }.map_err(Into::into) + } + pub fn bounds(&self) -> Result { let mut bounds = Rect::default(); let mut rect = RECT::default(); diff --git a/src/wkwebview/mod.rs b/src/wkwebview/mod.rs index 752211a48..6ff79df70 100644 --- a/src/wkwebview/mod.rs +++ b/src/wkwebview/mod.rs @@ -1130,6 +1130,11 @@ r#"Object.defineProperty(window, 'ipc', { self.navigate_to_url(url, Some(headers)) } + pub fn load_html(&self, html: &str) -> crate::Result<()> { + self.navigate_to_string(html); + Ok(()) + } + pub fn clear_all_browsing_data(&self) -> Result<()> { unsafe { let config: id = msg_send![self.webview, configuration];