From 0b1d89f06dfdf6e99335c3aff904cc5fb381fddb Mon Sep 17 00:00:00 2001 From: xfy Date: Tue, 19 May 2026 17:34:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20BackHandler=20expe?= =?UTF-8?q?ct/actual=20=E6=8B=A6=E6=88=AA=E7=B3=BB=E7=BB=9F=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=89=8B=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Android 通过 androidx.activity.compose.BackHandler 接管返回键, 在关于页与许可页返回上一级;iOS 无系统返回键,actual 为空实现。 Co-Authored-By: Claude Opus 4.7 (1M context) --- .../plus/rua/project/Platform.android.kt | 8 +++++++- .../commonMain/kotlin/plus/rua/project/App.kt | 20 ++++++++++++------- .../kotlin/plus/rua/project/Platform.kt | 13 +++++++++++- .../kotlin/plus/rua/project/Platform.ios.kt | 8 +++++++- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/shared/src/androidMain/kotlin/plus/rua/project/Platform.android.kt b/shared/src/androidMain/kotlin/plus/rua/project/Platform.android.kt index e0ddf8b..39289ab 100644 --- a/shared/src/androidMain/kotlin/plus/rua/project/Platform.android.kt +++ b/shared/src/androidMain/kotlin/plus/rua/project/Platform.android.kt @@ -1,6 +1,7 @@ package plus.rua.project import android.os.Build +import androidx.compose.runtime.Composable class AndroidPlatform : Platform { override val name: String = "Android ${Build.VERSION.SDK_INT}" @@ -10,4 +11,9 @@ actual fun getPlatform(): Platform = AndroidPlatform() actual fun getGifUri(gifFile: String): String = "file:///android_asset/gifs/$gifFile" -actual fun getAppIconUri(): String = "file:///android_asset/app_icon.png" \ No newline at end of file +actual fun getAppIconUri(): String = "file:///android_asset/app_icon.png" + +@Composable +actual fun BackHandler(enabled: Boolean, onBack: () -> Unit) { + androidx.activity.compose.BackHandler(enabled = enabled, onBack = onBack) +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/plus/rua/project/App.kt b/shared/src/commonMain/kotlin/plus/rua/project/App.kt index abc6d21..ed79e01 100644 --- a/shared/src/commonMain/kotlin/plus/rua/project/App.kt +++ b/shared/src/commonMain/kotlin/plus/rua/project/App.kt @@ -32,13 +32,19 @@ fun App() { modifier = Modifier, onNavigateToAbout = { currentScreen = Screen.About } ) - Screen.About -> AboutScreen( - onBack = { currentScreen = Screen.Main }, - onNavigateToLicenses = { currentScreen = Screen.Licenses } - ) - Screen.Licenses -> LicensesScreen( - onBack = { currentScreen = Screen.About } - ) + Screen.About -> { + BackHandler { currentScreen = Screen.Main } + AboutScreen( + onBack = { currentScreen = Screen.Main }, + onNavigateToLicenses = { currentScreen = Screen.Licenses } + ) + } + Screen.Licenses -> { + BackHandler { currentScreen = Screen.About } + LicensesScreen( + onBack = { currentScreen = Screen.About } + ) + } } } } diff --git a/shared/src/commonMain/kotlin/plus/rua/project/Platform.kt b/shared/src/commonMain/kotlin/plus/rua/project/Platform.kt index d76804f..6faab22 100644 --- a/shared/src/commonMain/kotlin/plus/rua/project/Platform.kt +++ b/shared/src/commonMain/kotlin/plus/rua/project/Platform.kt @@ -1,5 +1,7 @@ package plus.rua.project +import androidx.compose.runtime.Composable + interface Platform { val name: String } @@ -14,4 +16,13 @@ expect fun getPlatform(): Platform */ expect fun getGifUri(gifFile: String): String -expect fun getAppIconUri(): String \ No newline at end of file +expect fun getAppIconUri(): String + +/** + * 拦截系统返回手势。 + * + * @param enabled 是否启用拦截 + * @param onBack 返回回调 + */ +@Composable +expect fun BackHandler(enabled: Boolean = true, onBack: () -> Unit) \ No newline at end of file diff --git a/shared/src/iosMain/kotlin/plus/rua/project/Platform.ios.kt b/shared/src/iosMain/kotlin/plus/rua/project/Platform.ios.kt index 09db9f7..3c1efc8 100644 --- a/shared/src/iosMain/kotlin/plus/rua/project/Platform.ios.kt +++ b/shared/src/iosMain/kotlin/plus/rua/project/Platform.ios.kt @@ -1,5 +1,6 @@ package plus.rua.project +import androidx.compose.runtime.Composable import platform.UIKit.UIDevice class IOSPlatform : Platform { @@ -11,4 +12,9 @@ actual fun getPlatform(): Platform = IOSPlatform() actual fun getGifUri(gifFile: String): String = "compose.resource://files/$gifFile" -actual fun getAppIconUri(): String = "compose.resource://files/app_icon.png" \ No newline at end of file +actual fun getAppIconUri(): String = "compose.resource://files/app_icon.png" + +@Composable +actual fun BackHandler(enabled: Boolean, onBack: () -> Unit) { + // iOS 没有系统返回键,由导航栏按钮处理 +} \ No newline at end of file