Skip to content

Commit 8dfeb76

Browse files
feat: add Webview.reload (tauri-apps#1500)
* feat: add `Webview.reload` - first step MacOS * add linux and windows support * android support * change from minor to patch --------- Co-authored-by: Lucas Nogueira <[email protected]>
1 parent 94ecadb commit 8dfeb76

File tree

9 files changed

+44
-2
lines changed

9 files changed

+44
-2
lines changed

.changes/reload.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'wry': 'patch'
3+
---
4+
5+
feat: add `Webview.reload`

src/android/kotlin/RustWebView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import kotlin.collections.Map
1616
@SuppressLint("RestrictedApi")
1717
class RustWebView(context: Context, val initScripts: Array<String>, val id: String): WebView(context) {
1818
val isDocumentStartScriptEnabled: Boolean
19-
19+
2020
init {
2121
settings.javaScriptEnabled = true
2222
settings.domStorageEnabled = true

src/android/kotlin/proguard-wry.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030

3131
-keep class {{package-unescaped}}.RustWebChromeClient,{{package-unescaped}}.RustWebViewClient {
3232
public <init>(...);
33-
}
33+
}

src/android/main_pipe.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ impl<'a> MainPipe<'a> {
341341
load_html(&mut self.env, webview.as_obj(), &html)?;
342342
}
343343
}
344+
WebViewMessage::Reload => {
345+
if let Some(webview) = &self.webview {
346+
reload(&mut self.env, webview.as_obj())?;
347+
}
348+
}
344349
WebViewMessage::GetCookies(tx, url) => {
345350
if let Some(webview) = &self.webview {
346351
let url = self.env.new_string(url)?;
@@ -425,6 +430,11 @@ fn load_html<'a>(env: &mut JNIEnv<'a>, webview: &JObject<'a>, html: &JString<'a>
425430
Ok(())
426431
}
427432

433+
fn reload<'a>(env: &mut JNIEnv<'a>, webview: &JObject<'a>) -> JniResult<()> {
434+
env.call_method(webview, "reload", "()V", &[])?;
435+
Ok(())
436+
}
437+
428438
fn set_background_color<'a>(
429439
env: &mut JNIEnv<'a>,
430440
webview: &JObject<'a>,
@@ -445,6 +455,7 @@ pub(crate) enum WebViewMessage {
445455
Jni(Box<dyn FnOnce(&mut JNIEnv, &JObject, &JObject) + Send>),
446456
LoadUrl(String, Option<http::HeaderMap>),
447457
LoadHtml(String),
458+
Reload,
448459
ClearAllBrowsingData,
449460
OnDestroy,
450461
}

src/android/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ impl InnerWebView {
400400
Ok(())
401401
}
402402

403+
pub fn reload(&self) -> Result<()> {
404+
MainPipe::send(WebViewMessage::Reload);
405+
Ok(())
406+
}
407+
403408
pub fn clear_all_browsing_data(&self) -> Result<()> {
404409
MainPipe::send(WebViewMessage::ClearAllBrowsingData);
405410
Ok(())

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,11 @@ impl WebView {
18831883
self.webview.load_url(url)
18841884
}
18851885

1886+
/// Reloads the current page.
1887+
pub fn reload(&self) -> crate::Result<()> {
1888+
self.webview.reload()
1889+
}
1890+
18861891
/// Navigate to the specified url using the specified headers
18871892
pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) -> Result<()> {
18881893
self.webview.load_url_with_headers(url, headers)

src/webkitgtk/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,11 @@ impl InnerWebView {
695695
Ok(())
696696
}
697697

698+
pub fn reload(&self) -> Result<()> {
699+
self.webview.reload();
700+
Ok(())
701+
}
702+
698703
pub fn clear_all_browsing_data(&self) -> Result<()> {
699704
if let Some(context) = self.webview.context() {
700705
if let Some(data_manger) = context.website_data_manager() {

src/webview2/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,10 @@ impl InnerWebView {
13001300
unsafe { self.webview.NavigateToString(&html) }.map_err(Into::into)
13011301
}
13021302

1303+
pub fn reload(&self) -> Result<()> {
1304+
unsafe { self.webview.Reload() }.map_err(Into::into)
1305+
}
1306+
13031307
pub fn bounds(&self) -> Result<Rect> {
13041308
let mut bounds = Rect::default();
13051309
let mut rect = RECT::default();

src/wkwebview/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,13 @@ r#"Object.defineProperty(window, 'ipc', {
683683
Ok(())
684684
}
685685

686+
/// Reloads the current page.
687+
pub fn reload(&self) -> crate::Result<()> {
688+
// Safety: objc runtime calls are unsafe
689+
unsafe { self.webview.reload() };
690+
Ok(())
691+
}
692+
686693
pub fn clear_all_browsing_data(&self) -> Result<()> {
687694
unsafe {
688695
let config = self.webview.configuration();

0 commit comments

Comments
 (0)