Skip to content

Commit

Permalink
feat(android): add function to dispatch closure to the Android context (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Feb 6, 2023
1 parent a8f7cef commit a9e186c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/dispatch-android-fn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Add function to dispatch closure with the Android context.
6 changes: 4 additions & 2 deletions src/webview/android/main_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ impl MainPipe<'_> {
}
}
WebViewMessage::Jni(f) => {
if let Some(webview) = &self.webview {
f(env, activity, webview.as_obj());
if let Some(w) = &self.webview {
f(env, activity, w.as_obj());
} else {
f(env, activity, JObject::null());
}
}
WebViewMessage::LoadUrl(url, headers) => {
Expand Down
10 changes: 10 additions & 0 deletions src/webview/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@ pub fn find_class<'a>(
Ok(my_class.into())
}

/// Dispatch a closure to run on the Android context.
///
/// The closure takes the JNI env, the Android activity instance and the possibly null webview.
pub fn dispatch<F>(func: F)
where
F: FnOnce(JNIEnv, JObject, JObject) + Send + 'static,
{
MainPipe::send(WebViewMessage::Jni(Box::new(func)));
}

fn create_headers_map<'a, 'b>(
env: &'a JNIEnv,
headers: &http::HeaderMap,
Expand Down
2 changes: 1 addition & 1 deletion src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use web_context::WebContext;
pub(crate) mod android;
#[cfg(target_os = "android")]
pub mod prelude {
pub use super::android::{binding::*, find_class, setup, Context};
pub use super::android::{binding::*, dispatch, find_class, setup, Context};
}
#[cfg(target_os = "android")]
pub use android::JniHandle;
Expand Down

0 comments on commit a9e186c

Please sign in to comment.