Skip to content

Commit

Permalink
Added WebView::load_html
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorKoenders committed Nov 6, 2023
1 parent 81cfa37 commit 4e2d6bc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/webview/android/main_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ impl<'a> MainPipe<'a> {
f(&mut self.env, activity, &JObject::null());
}
}
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)?;
}
}
WebViewMessage::LoadUrl(url, headers) => {
if let Some(webview) = &self.webview {
let url = self.env.new_string(url)?;
Expand Down Expand Up @@ -337,6 +343,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
4 changes: 4 additions & 0 deletions src/webview/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ impl InnerWebView {
Ok(())
}

pub fn load_html(&self, html: &str) {
MainPipe::send(WebViewMessage::LoadHtml(html.to_string()));
}

pub fn load_url(&self, url: &str) {
MainPipe::send(WebViewMessage::LoadUrl(url.to_string(), None));
}
Expand Down
5 changes: 5 additions & 0 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,11 @@ impl WebView {
self.webview.set_background_color(background_color)
}

/// Load in the given HTML
pub fn load_html(&self, html: &str) -> Result<()> {
self.webview.load_html(html)

Check failure on line 1098 in src/webview/mod.rs

View workflow job for this annotation

GitHub Actions / clippy_fmt_check

no method named `load_html` found for struct `webview::wkwebview::InnerWebView` in the current scope
}

/// Navigate to the specified url
pub fn load_url(&self, url: &str) {
self.webview.load_url(url)
Expand Down
4 changes: 4 additions & 0 deletions src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ impl InnerWebView {
self.webview.load_request(&req);
}

pub fn load_html(&self, html: &str) {
self.webview.load_html(html)
}

pub fn clear_all_browsing_data(&self) -> Result<()> {
if let Some(context) = WebViewExt::context(&*self.webview) {
use webkit2gtk::WebContextExt;
Expand Down
9 changes: 9 additions & 0 deletions src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,15 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
let _ = unsafe { self.webview.Navigate(PCWSTR::from_raw(url.as_ptr())) };
}

pub fn load_html(&self, html: &str) -> Result<()> {
unsafe {
self
.webview
.NavigateToString(PCWSTR::from_raw(encode_wide(html).as_ptr()))
.map_err(|e| Error::WebView2Error(webview2_com::Error::WindowsError(e)))
}
}

pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) {
load_url_with_headers(&self.webview, &self.env, url, headers);
}
Expand Down

0 comments on commit 4e2d6bc

Please sign in to comment.