Skip to content

Commit

Permalink
feat: add WebView::load_html for loading HTML after intialization (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zephraph authored Sep 25, 2024
1 parent aa42ca4 commit e332eff
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/load-html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": minor
---

Add `Webview::load_html`.
7 changes: 7 additions & 0 deletions src/android/main_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -369,6 +375,7 @@ pub(crate) enum WebViewMessage {
GetUrl(Sender<String>),
Jni(Box<dyn FnOnce(&mut JNIEnv, &JObject, &JObject) + Send>),
LoadUrl(String, Option<http::HeaderMap>),
LoadHtml(String),
ClearAllBrowsingData,
}

Expand Down
5 changes: 5 additions & 0 deletions src/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions src/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions src/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rect> {
let mut bounds = Rect::default();
let mut rect = RECT::default();
Expand Down
5 changes: 5 additions & 0 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit e332eff

Please sign in to comment.