refactor: use safe insets from native for Android, closes #1793 and closes #1886 (#1897)

This commit is contained in:
Huang Xin 2025-08-25 15:38:49 +08:00 committed by GitHub
parent 0b01132d88
commit 5ee860f1e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 282 additions and 64 deletions

View file

@ -198,6 +198,7 @@ class NativeBridgePlugin(private val activity: Activity): Plugin(activity) {
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
@Suppress("DEPRECATION")
window.setDecorFitsSystemWindows(false)
val controller = window.insetsController
if (controller != null) {
@ -228,7 +229,7 @@ class NativeBridgePlugin(private val activity: Activity): Plugin(activity) {
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val compatController = WindowCompat.getInsetsController(window, decorView)
compatController?.let {
compatController.let {
it.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
if (!isDarkMode) {
it.isAppearanceLightStatusBars = true
@ -259,7 +260,9 @@ class NativeBridgePlugin(private val activity: Activity): Plugin(activity) {
}
}.reduce { acc, flag -> acc or flag }
}
@Suppress("DEPRECATION")
window.statusBarColor = Color.TRANSPARENT
@Suppress("DEPRECATION")
window.navigationBarColor = Color.TRANSPARENT
ret.put("success", true)
} catch (e: Exception) {
@ -366,4 +369,59 @@ class NativeBridgePlugin(private val activity: Activity): Plugin(activity) {
}
invoke.resolve()
}
private fun getStatusBarHeightInternal(): Int {
val resourceId = activity.resources.getIdentifier("status_bar_height", "dimen", "android")
return if (resourceId > 0) activity.resources.getDimensionPixelSize(resourceId) else 0
}
private fun getNavigationBarHeight(): Int {
val resourceId = activity.resources.getIdentifier("navigation_bar_height", "dimen", "android")
return if (resourceId > 0) activity.resources.getDimensionPixelSize(resourceId) else 0
}
private fun hasNavigationBar(): Boolean {
val resourceId = activity.resources.getIdentifier("config_showNavigationBar", "bool", "android")
return if (resourceId > 0) {
activity.resources.getBoolean(resourceId)
} else {
!android.view.ViewConfiguration.get(activity).hasPermanentMenuKey()
}
}
@Command
fun get_safe_area_insets(invoke: Invoke) {
val ret = JSObject()
try {
val rootView = activity.findViewById<View>(android.R.id.content)
val windowInsets = androidx.core.view.ViewCompat.getRootWindowInsets(rootView)
if (windowInsets != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val insets = windowInsets.getInsets(
WindowInsetsCompat.Type.systemBars() or
WindowInsetsCompat.Type.displayCutout()
)
val density = activity.resources.displayMetrics.density
ret.put("top", insets.top / density)
ret.put("right", insets.right / density)
ret.put("bottom", insets.bottom / density)
ret.put("left", insets.left / density)
} else {
val statusBarHeight = getStatusBarHeightInternal()
val navBarHeight = getNavigationBarHeight()
val density = activity.resources.displayMetrics.density
ret.put("top", statusBarHeight / density)
ret.put("right", 0)
ret.put("bottom", if (hasNavigationBar()) navBarHeight / density else 0)
ret.put("left", 0)
}
} catch (e: Exception) {
ret.put("error", e.message)
ret.put("top", 0)
ret.put("right", 0)
ret.put("bottom", 0)
ret.put("left", 0)
}
invoke.resolve(ret)
}
}

View file

@ -14,6 +14,7 @@ const COMMANDS: &[&str] = &[
"iap_purchase_product",
"iap_restore_purchases",
"get_system_color_scheme",
"get_safe_area_insets",
];
fn main() {

View file

@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-get-safe-area-insets"
description = "Enables the get_safe_area_insets command without any pre-configured scope."
commands.allow = ["get_safe_area_insets"]
[[permission]]
identifier = "deny-get-safe-area-insets"
description = "Denies the get_safe_area_insets command without any pre-configured scope."
commands.deny = ["get_safe_area_insets"]

View file

@ -19,6 +19,7 @@ Default permissions for the plugin
- `allow-iap-purchase-product`
- `allow-iap-restore-purchases`
- `allow-get-system-color-scheme`
- `allow-get-safe-area-insets`
## Permission Table
@ -110,6 +111,32 @@ Denies the copy_uri_to_path command without any pre-configured scope.
<tr>
<td>
`native-bridge:allow-get-safe-area-insets`
</td>
<td>
Enables the get_safe_area_insets command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`native-bridge:deny-get-safe-area-insets`
</td>
<td>
Denies the get_safe_area_insets command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`native-bridge:allow-get-status-bar-height`
</td>

View file

@ -16,4 +16,5 @@ permissions = [
"allow-iap-purchase-product",
"allow-iap-restore-purchases",
"allow-get-system-color-scheme",
"allow-get-safe-area-insets",
]

View file

@ -330,6 +330,18 @@
"const": "deny-copy-uri-to-path",
"markdownDescription": "Denies the copy_uri_to_path command without any pre-configured scope."
},
{
"description": "Enables the get_safe_area_insets command without any pre-configured scope.",
"type": "string",
"const": "allow-get-safe-area-insets",
"markdownDescription": "Enables the get_safe_area_insets command without any pre-configured scope."
},
{
"description": "Denies the get_safe_area_insets command without any pre-configured scope.",
"type": "string",
"const": "deny-get-safe-area-insets",
"markdownDescription": "Denies the get_safe_area_insets command without any pre-configured scope."
},
{
"description": "Enables the get_status_bar_height command without any pre-configured scope.",
"type": "string",
@ -475,10 +487,10 @@
"markdownDescription": "Denies the use_background_audio command without any pre-configured scope."
},
{
"description": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-auth-with-safari`\n- `allow-auth-with-custom-tab`\n- `allow-copy-uri-to-path`\n- `allow-use-background-audio`\n- `allow-install-package`\n- `allow-set-system-ui-visibility`\n- `allow-get-status-bar-height`\n- `allow-get-sys-fonts-list`\n- `allow-intercept-keys`\n- `allow-lock-screen-orientation`\n- `allow-iap-initialize`\n- `allow-iap-fetch-products`\n- `allow-iap-purchase-product`\n- `allow-iap-restore-purchases`\n- `allow-get-system-color-scheme`",
"description": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-auth-with-safari`\n- `allow-auth-with-custom-tab`\n- `allow-copy-uri-to-path`\n- `allow-use-background-audio`\n- `allow-install-package`\n- `allow-set-system-ui-visibility`\n- `allow-get-status-bar-height`\n- `allow-get-sys-fonts-list`\n- `allow-intercept-keys`\n- `allow-lock-screen-orientation`\n- `allow-iap-initialize`\n- `allow-iap-fetch-products`\n- `allow-iap-purchase-product`\n- `allow-iap-restore-purchases`\n- `allow-get-system-color-scheme`\n- `allow-get-safe-area-insets`",
"type": "string",
"const": "default",
"markdownDescription": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-auth-with-safari`\n- `allow-auth-with-custom-tab`\n- `allow-copy-uri-to-path`\n- `allow-use-background-audio`\n- `allow-install-package`\n- `allow-set-system-ui-visibility`\n- `allow-get-status-bar-height`\n- `allow-get-sys-fonts-list`\n- `allow-intercept-keys`\n- `allow-lock-screen-orientation`\n- `allow-iap-initialize`\n- `allow-iap-fetch-products`\n- `allow-iap-purchase-product`\n- `allow-iap-restore-purchases`\n- `allow-get-system-color-scheme`"
"markdownDescription": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-auth-with-safari`\n- `allow-auth-with-custom-tab`\n- `allow-copy-uri-to-path`\n- `allow-use-background-audio`\n- `allow-install-package`\n- `allow-set-system-ui-visibility`\n- `allow-get-status-bar-height`\n- `allow-get-sys-fonts-list`\n- `allow-intercept-keys`\n- `allow-lock-screen-orientation`\n- `allow-iap-initialize`\n- `allow-iap-fetch-products`\n- `allow-iap-purchase-product`\n- `allow-iap-restore-purchases`\n- `allow-get-system-color-scheme`\n- `allow-get-safe-area-insets`"
}
]
}

View file

@ -119,3 +119,10 @@ pub(crate) async fn get_system_color_scheme<R: Runtime>(
) -> Result<GetSystemColorSchemeResponse> {
app.native_bridge().get_system_color_scheme()
}
#[command]
pub(crate) async fn get_safe_area_insets<R: Runtime>(
app: AppHandle<R>,
) -> Result<GetSafeAreaInsetsResponse> {
app.native_bridge().get_safe_area_insets()
}

View file

@ -102,4 +102,8 @@ impl<R: Runtime> NativeBridge<R> {
pub fn get_system_color_scheme(&self) -> crate::Result<GetSystemColorSchemeResponse> {
Err(crate::Error::UnsupportedPlatformError)
}
pub fn get_safe_area_insets(&self) -> crate::Result<GetSafeAreaInsetsResponse> {
Err(crate::Error::UnsupportedPlatformError)
}
}

View file

@ -52,6 +52,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
commands::iap_purchase_product,
commands::iap_restore_purchases,
commands::get_system_color_scheme,
commands::get_safe_area_insets,
])
.setup(|app, api| {
#[cfg(mobile)]

View file

@ -161,3 +161,11 @@ impl<R: Runtime> NativeBridge<R> {
.map_err(Into::into)
}
}
impl<R: Runtime> NativeBridge<R> {
pub fn get_safe_area_insets(&self) -> crate::Result<GetSafeAreaInsetsResponse> {
self.0
.run_mobile_plugin("get_safe_area_insets", ())
.map_err(Into::into)
}
}

View file

@ -158,3 +158,12 @@ pub struct IAPRestorePurchasesResponse {
pub struct GetSystemColorSchemeResponse {
pub color_scheme: String, // "light" or "dark"
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetSafeAreaInsetsResponse {
pub top: f64,
pub bottom: f64,
pub left: f64,
pub right: f64,
}