Skip to content

Commit 8ea87e9

Browse files
authored
feat(android): with_webview access for jni execution (#5148)
1 parent c9ad2a7 commit 8ea87e9

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

.changes/mobile-webview-access.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime-wry": minor
3+
"tauri": minor
4+
---
5+
6+
Support `with_webview` for Android platform alowing execution of JNI code in context.

core/tauri-runtime-wry/src/lib.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ type FileDropHandler = dyn Fn(&Window, WryFileDropEvent) -> bool + 'static;
102102
#[cfg(all(desktop, feature = "system-tray"))]
103103
pub use tauri_runtime::TrayId;
104104

105-
#[cfg(desktop)]
105+
#[cfg(any(desktop, target_os = "android"))]
106106
mod webview;
107-
#[cfg(desktop)]
107+
#[cfg(any(desktop, target_os = "android"))]
108108
pub use webview::Webview;
109109

110110
#[cfg(all(desktop, feature = "system-tray"))]
@@ -987,7 +987,7 @@ pub struct RawWindowHandle(pub raw_window_handle::RawWindowHandle);
987987
unsafe impl Send for RawWindowHandle {}
988988

989989
pub enum WindowMessage {
990-
#[cfg(desktop)]
990+
#[cfg(any(desktop, target_os = "android"))]
991991
WithWebview(Box<dyn FnOnce(Webview) + Send>),
992992
AddEventListener(Uuid, Box<dyn Fn(&WindowEvent) + Send>),
993993
AddMenuEventListener(Uuid, Box<dyn Fn(&MenuEvent) + Send>),
@@ -1133,7 +1133,7 @@ pub struct WryDispatcher<T: UserEvent> {
11331133
unsafe impl<T: UserEvent> Sync for WryDispatcher<T> {}
11341134

11351135
impl<T: UserEvent> WryDispatcher<T> {
1136-
#[cfg(desktop)]
1136+
#[cfg(any(desktop, target_os = "android"))]
11371137
pub fn with_webview<F: FnOnce(Webview) + Send + 'static>(&self, f: F) -> Result<()> {
11381138
send_user_message(
11391139
&self.context,
@@ -2166,7 +2166,7 @@ fn handle_user_message<T: UserEvent>(
21662166
});
21672167
if let Some((Some(window), window_event_listeners, menu_event_listeners)) = w {
21682168
match window_message {
2169-
#[cfg(desktop)]
2169+
#[cfg(any(target_os = "android", desktop))]
21702170
WindowMessage::WithWebview(f) => {
21712171
if let WindowHandle::Webview(w) = window {
21722172
#[cfg(any(
@@ -2189,13 +2189,17 @@ fn handle_user_message<T: UserEvent>(
21892189
ns_window: w.ns_window(),
21902190
});
21912191
}
2192-
21932192
#[cfg(windows)]
21942193
{
21952194
f(Webview {
21962195
controller: w.controller(),
21972196
});
21982197
}
2198+
#[cfg(target_os = "android")]
2199+
{
2200+
use wry::webview::WebviewExtAndroid;
2201+
f(w.handle())
2202+
}
21992203
}
22002204
}
22012205

core/tauri-runtime-wry/src/webview.rs

+6
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ mod imp {
3434
}
3535
}
3636

37+
#[cfg(target_os = "android")]
38+
mod imp {
39+
use wry::webview::JniHandle;
40+
pub type Webview = JniHandle;
41+
}
42+
3743
pub use imp::*;

core/tauri/src/window.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,14 @@ impl<'de, R: Runtime> CommandArg<'de, R> for Window<R> {
587587
}
588588

589589
/// The platform webview handle. Accessed with [`Window#method.with_webview`];
590-
#[cfg(all(desktop, feature = "wry"))]
591-
#[cfg_attr(doc_cfg, doc(cfg(all(desktop, feature = "wry"))))]
590+
#[cfg(all(any(desktop, target_os = "android"), feature = "wry"))]
591+
#[cfg_attr(
592+
doc_cfg,
593+
doc(cfg(all(any(desktop, target_os = "android"), feature = "wry")))
594+
)]
592595
pub struct PlatformWebview(tauri_runtime_wry::Webview);
593596

594-
#[cfg(all(desktop, feature = "wry"))]
597+
#[cfg(all(any(desktop, target_os = "android"), feature = "wry"))]
595598
impl PlatformWebview {
596599
/// Returns [`webkit2gtk::WebView`] handle.
597600
#[cfg(any(
@@ -650,6 +653,12 @@ impl PlatformWebview {
650653
pub fn ns_window(&self) -> cocoa::base::id {
651654
self.0.ns_window
652655
}
656+
657+
/// Returns handle for JNI execution.
658+
#[cfg(target_os = "android")]
659+
pub fn jni_handle(&self) -> tauri_runtime_wry::wry::webview::JniHandle {
660+
self.0
661+
}
653662
}
654663

655664
/// APIs specific to the wry runtime.
@@ -693,13 +702,24 @@ impl Window<crate::Wry> {
693702
/// let bg_color: cocoa::base::id = msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.];
694703
/// let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color];
695704
/// }
705+
///
706+
/// #[cfg(target_os = "android")]
707+
/// {
708+
/// use jni::objects::JValue;
709+
/// webview.jni_handle().exec(|env, _, webview| {
710+
/// env.call_method(webview, "zoomBy", "(F)V", &[JValue::Float(4.)]).unwrap();
711+
/// })
712+
/// }
696713
/// });
697714
/// Ok(())
698715
/// });
699716
/// }
700717
/// ```
701-
#[cfg(desktop)]
702-
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "wry", desktop))))]
718+
#[cfg(any(desktop, target_os = "android"))]
719+
#[cfg_attr(
720+
doc_cfg,
721+
doc(cfg(all(feature = "wry", any(desktop, target_os = "android"))))
722+
)]
703723
pub fn with_webview<F: FnOnce(PlatformWebview) + Send + 'static>(
704724
&self,
705725
f: F,

0 commit comments

Comments
 (0)