#[cfg(target_os = "android")]
#[manganis::ffi("src/android/back_button")]
unsafe extern "Kotlin" {
pub type BackButtonPlugin;
}
#[cfg(target_os = "ios")]
#[manganis::ffi("src/ios")]
unsafe extern "Swift" {
pub type BackButtonPlugin;
pub fn setInterceptingFromRust(this: &BackButtonPlugin, intercepting: bool);
pub fn fallThroughFromRust(this: &BackButtonPlugin);
}
#[cfg(target_os = "android")]
use jni::{
JavaVM,
objects::{GlobalRef, JClass, JObject, JValue},
};
#[cfg(target_os = "android")]
const BACK_BUTTON_PLUGIN_CLASS: &str = "dev.dioxus.g3_native_plugins.back_button.BackButtonPlugin";
pub const NATIVE_BACK_EVENT: &str = "g3nativeback";
#[cfg(target_os = "android")]
pub struct BackButton {
plugin: Option<GlobalRef>,
intercepting: bool,
}
#[cfg(target_os = "ios")]
pub struct BackButton {
plugin: Option<BackButtonPlugin>,
intercepting: bool,
}
#[cfg(any(target_arch = "wasm32", target_os = "macos"))]
pub struct BackButton {
intercepting: bool,
}
#[cfg(target_os = "android")]
impl BackButton {
pub fn new() -> Self {
Self {
plugin: None,
intercepting: false,
}
}
fn get_plugin(&mut self) -> Result<&GlobalRef, String> {
if self.plugin.is_none() {
let android = ndk_context::android_context();
let vm = unsafe { JavaVM::from_raw(android.vm().cast()) }
.map_err(|error| format!("Failed to access Android VM: {error}"))?;
let mut env = vm
.attach_current_thread_permanently()
.map_err(|error| format!("Failed to attach plugin thread: {error}"))?;
let activity = unsafe { JObject::from_raw(android.context().cast()) };
let loader = env
.call_method(
&activity,
"getClassLoader",
"()Ljava/lang/ClassLoader;",
&[],
)
.and_then(|value| value.l())
.map_err(|error| format!("Failed to obtain app class loader: {error}"))?;
let name = env
.new_string(BACK_BUTTON_PLUGIN_CLASS)
.map_err(|error| format!("Failed to create plugin class name: {error}"))?;
let name_object = JObject::from(name);
let class_object = env
.call_method(
loader,
"loadClass",
"(Ljava/lang/String;)Ljava/lang/Class;",
&[JValue::Object(&name_object)],
)
.and_then(|value| value.l())
.map_err(|error| format!("Failed to load BackButtonPlugin: {error}"))?;
let class = JClass::from(class_object);
let instance = env
.new_object(
class,
"(Landroid/app/Activity;)V",
&[JValue::Object(&activity)],
)
.map_err(|error| format!("Failed to create BackButtonPlugin: {error}"))?;
self.plugin = Some(
env.new_global_ref(instance)
.map_err(|error| format!("Failed to retain BackButtonPlugin: {error}"))?,
);
std::mem::forget(activity);
}
Ok(self.plugin.as_ref().unwrap())
}
pub fn prepare(&mut self) -> Result<(), String> {
self.get_plugin()?;
Ok(())
}
pub fn set_intercepting(&mut self, intercepting: bool) -> Result<(), String> {
if self.intercepting == intercepting {
return Ok(());
}
let plugin = self.get_plugin()?;
let android = ndk_context::android_context();
let vm = unsafe { JavaVM::from_raw(android.vm().cast()) }
.map_err(|error| format!("Failed to access Android VM: {error}"))?;
let mut env = vm
.attach_current_thread_permanently()
.map_err(|error| format!("Failed to attach plugin thread: {error}"))?;
env.call_method(
plugin.as_obj(),
"setInterceptingFromRust",
"(Z)V",
&[JValue::Bool(intercepting.into())],
)
.map_err(|error| format!("Failed to update Back interception: {error}"))?;
self.intercepting = intercepting;
Ok(())
}
pub fn fall_through(&mut self) -> Result<(), String> {
let plugin = self.get_plugin()?;
let android = ndk_context::android_context();
let vm = unsafe { JavaVM::from_raw(android.vm().cast()) }
.map_err(|error| format!("Failed to access Android VM: {error}"))?;
let mut env = vm
.attach_current_thread_permanently()
.map_err(|error| format!("Failed to attach plugin thread: {error}"))?;
env.call_method(plugin.as_obj(), "fallThroughFromRust", "()V", &[])
.map_err(|error| format!("Failed to pass Back to Android: {error}"))?;
Ok(())
}
pub fn is_intercepting(&self) -> bool {
self.intercepting
}
}
#[cfg(target_os = "ios")]
impl BackButton {
pub fn new() -> Self {
Self {
plugin: None,
intercepting: false,
}
}
fn get_plugin(&mut self) -> Result<&BackButtonPlugin, String> {
if self.plugin.is_none() {
self.plugin = Some(
BackButtonPlugin::new()
.map_err(|error| format!("Failed to create BackButtonPlugin: {error:?}"))?,
);
}
Ok(self.plugin.as_ref().unwrap())
}
pub fn prepare(&mut self) -> Result<(), String> {
self.get_plugin()?;
Ok(())
}
pub fn set_intercepting(&mut self, intercepting: bool) -> Result<(), String> {
if self.intercepting == intercepting {
return Ok(());
}
let plugin = self.get_plugin()?;
setInterceptingFromRust(plugin, intercepting)?;
self.intercepting = intercepting;
Ok(())
}
pub fn fall_through(&mut self) -> Result<(), String> {
let plugin = self.get_plugin()?;
fallThroughFromRust(plugin)?;
Ok(())
}
pub fn is_intercepting(&self) -> bool {
self.intercepting
}
}
#[cfg(any(target_arch = "wasm32", target_os = "macos"))]
impl BackButton {
pub fn new() -> Self {
Self {
intercepting: false,
}
}
pub fn prepare(&mut self) -> Result<(), String> {
Ok(())
}
pub fn set_intercepting(&mut self, intercepting: bool) -> Result<(), String> {
self.intercepting = intercepting;
Ok(())
}
pub fn fall_through(&mut self) -> Result<(), String> {
Ok(())
}
pub fn is_intercepting(&self) -> bool {
self.intercepting
}
}
#[cfg(any(
target_arch = "wasm32",
target_os = "android",
target_os = "ios",
target_os = "macos"
))]
impl Default for BackButton {
fn default() -> Self {
Self::new()
}
}