feat: back handler mapping

close #15
This commit is contained in:
Miuzarte
2026-04-25 13:19:14 +08:00
parent 06316f8fee
commit e876a32bdf
5 changed files with 54 additions and 11 deletions

View File

@@ -2,6 +2,10 @@
~~什么我居然开始写更新日志了~~
## 0.2.6
- 新增: 全屏下将系统返回手势映射为发送返回按键
## 0.2.5
- 修复: 预览销毁再重组会导致画面冻结

View File

@@ -54,8 +54,8 @@ android {
applicationId = "io.github.miuzarte.scrcpyforandroid"
minSdk = 26
targetSdk = 37
versionCode = 21
versionName = "0.2.5"
versionCode = 22
versionName = "0.2.6"
externalNativeBuild {
cmake {

View File

@@ -5,6 +5,7 @@ import android.content.Context
import android.graphics.Rect
import android.hardware.display.DisplayManager
import android.util.Log
import android.view.KeyEvent
import android.view.Surface
import androidx.activity.compose.BackHandler
import androidx.activity.compose.LocalActivity
@@ -84,8 +85,6 @@ fun FullscreenControlScreen(
onVideoSizeChanged: (width: Int, height: Int) -> Unit,
onVideoBoundsInWindowChanged: (Rect?) -> Unit,
) {
BackHandler(enabled = true, onBack = onBack)
val activity = LocalActivity.current
val context = LocalContext.current
val fragmentActivity = remember(activity) { activity as? FragmentActivity }
@@ -283,6 +282,25 @@ fun FullscreenControlScreen(
}
}
suspend fun sendBackOrTurnScreenOn() {
runCatching {
withContext(Dispatchers.IO) {
scrcpy.pressBackOrTurnScreenOn(KeyEvent.ACTION_DOWN)
scrcpy.pressBackOrTurnScreenOn(KeyEvent.ACTION_UP)
}
}.onFailure { e ->
Log.w("FullscreenControlPage", "send back failed", e)
}
}
BackHandler(enabled = true) {
if (asBundle.fullscreenControlBackToDevice && currentSession != null)
taskScope.launch {
sendBackOrTurnScreenOn()
}
else onBack()
}
suspend fun refreshApps() {
runCatching {
withContext(Dispatchers.IO) {

View File

@@ -405,6 +405,20 @@ fun SettingsPage(
)
},
)
SwitchPreference(
title = "全屏时返回键发送到远程",
summary =
"""
启用后系统返回键会发送给设备,不再退出全屏控制页
此时退出全屏需要回到桌面通过图标重新进入应用
""".trimIndent(),
checked = asBundle.fullscreenControlBackToDevice,
onCheckedChange = {
asBundle = asBundle.copy(
fullscreenControlBackToDevice = it
)
},
)
SwitchPreference(
title = "全屏显示虚拟按钮",
summary = "在全屏控制页中显示返回键、主页键等虚拟按钮",

View File

@@ -122,14 +122,14 @@ class AppSettings(context: Context) : Settings(context, "AppSettings") {
intPreferencesKey("device_preview_card_height_dp"),
1080 / 3,
)
val DEVICE_TWO_PANE_CONFIG_ON_RIGHT = Pair(
booleanPreferencesKey("device_two_pane_config_on_right"),
false,
)
val FULLSCREEN_CONTROL_IGNORE_SYSTEM_ROTATION_LOCK = Pair(
booleanPreferencesKey("fullscreen_control_ignore_system_rotation_lock"),
true,
)
val FULLSCREEN_CONTROL_BACK_TO_DEVICE = Pair(
booleanPreferencesKey("fullscreen_control_back_to_device"),
false,
)
val SHOW_FULLSCREEN_VIRTUAL_BUTTONS = Pair(
booleanPreferencesKey("show_fullscreen_virtual_buttons"),
true,
@@ -193,6 +193,10 @@ class AppSettings(context: Context) : Settings(context, "AppSettings") {
"",
)
val DEVICE_TWO_PANE_CONFIG_ON_RIGHT = Pair(
booleanPreferencesKey("device_two_pane_config_on_right"),
false,
)
// Scrcpy Server
val CUSTOM_SERVER_URI = Pair(
@@ -278,8 +282,8 @@ class AppSettings(context: Context) : Settings(context, "AppSettings") {
val fullscreenDebugInfo: Boolean,
val hideSimpleConfigItems: Boolean,
val devicePreviewCardHeightDp: Int,
val deviceTwoPaneConfigOnRight: Boolean,
val fullscreenControlIgnoreSystemRotationLock: Boolean,
val fullscreenControlBackToDevice: Boolean,
val showFullscreenVirtualButtons: Boolean,
val fullscreenVirtualButtonHeightDp: Int,
val fullscreenVirtualButtonDock: String,
@@ -293,6 +297,7 @@ class AppSettings(context: Context) : Settings(context, "AppSettings") {
val fullscreenFloatingButtonYFraction: Float,
val previewVirtualButtonShowText: Boolean,
val virtualButtonsLayout: String,
val deviceTwoPaneConfigOnRight: Boolean,
// Scrcpy Server
val customServerUri: String,
@@ -335,8 +340,8 @@ class AppSettings(context: Context) : Settings(context, "AppSettings") {
bundleField(FULLSCREEN_DEBUG_INFO) { it.fullscreenDebugInfo },
bundleField(HIDE_SIMPLE_CONFIG_ITEMS) { it.hideSimpleConfigItems },
bundleField(DEVICE_PREVIEW_CARD_HEIGHT_DP) { it.devicePreviewCardHeightDp },
bundleField(DEVICE_TWO_PANE_CONFIG_ON_RIGHT) { it.deviceTwoPaneConfigOnRight },
bundleField(FULLSCREEN_CONTROL_IGNORE_SYSTEM_ROTATION_LOCK) { it.fullscreenControlIgnoreSystemRotationLock },
bundleField(FULLSCREEN_CONTROL_BACK_TO_DEVICE) { it.fullscreenControlBackToDevice },
bundleField(SHOW_FULLSCREEN_VIRTUAL_BUTTONS) { it.showFullscreenVirtualButtons },
bundleField(FULLSCREEN_VIRTUAL_BUTTON_HEIGHT_DP) { it.fullscreenVirtualButtonHeightDp },
bundleField(FULLSCREEN_VIRTUAL_BUTTON_DOCK) { it.fullscreenVirtualButtonDock },
@@ -350,6 +355,7 @@ class AppSettings(context: Context) : Settings(context, "AppSettings") {
bundleField(FULLSCREEN_FLOATING_BUTTON_Y_FRACTION) { it.fullscreenFloatingButtonYFraction },
bundleField(PREVIEW_VIRTUAL_BUTTON_SHOW_TEXT) { it.previewVirtualButtonShowText },
bundleField(VIRTUAL_BUTTONS_LAYOUT) { it.virtualButtonsLayout },
bundleField(DEVICE_TWO_PANE_CONFIG_ON_RIGHT) { it.deviceTwoPaneConfigOnRight },
// Scrcpy Server
bundleField(CUSTOM_SERVER_URI) { it.customServerUri },
@@ -393,9 +399,9 @@ class AppSettings(context: Context) : Settings(context, "AppSettings") {
fullscreenDebugInfo = preferences.read(FULLSCREEN_DEBUG_INFO),
hideSimpleConfigItems = preferences.read(HIDE_SIMPLE_CONFIG_ITEMS),
devicePreviewCardHeightDp = preferences.read(DEVICE_PREVIEW_CARD_HEIGHT_DP),
deviceTwoPaneConfigOnRight = preferences.read(DEVICE_TWO_PANE_CONFIG_ON_RIGHT),
fullscreenControlIgnoreSystemRotationLock =
preferences.read(FULLSCREEN_CONTROL_IGNORE_SYSTEM_ROTATION_LOCK),
fullscreenControlBackToDevice = preferences.read(FULLSCREEN_CONTROL_BACK_TO_DEVICE),
showFullscreenVirtualButtons = preferences.read(SHOW_FULLSCREEN_VIRTUAL_BUTTONS),
fullscreenVirtualButtonHeightDp = preferences.read(FULLSCREEN_VIRTUAL_BUTTON_HEIGHT_DP),
fullscreenVirtualButtonDock = preferences.read(FULLSCREEN_VIRTUAL_BUTTON_DOCK),
@@ -411,6 +417,7 @@ class AppSettings(context: Context) : Settings(context, "AppSettings") {
fullscreenFloatingButtonYFraction = preferences.read(FULLSCREEN_FLOATING_BUTTON_Y_FRACTION),
previewVirtualButtonShowText = preferences.read(PREVIEW_VIRTUAL_BUTTON_SHOW_TEXT),
virtualButtonsLayout = preferences.read(VIRTUAL_BUTTONS_LAYOUT),
deviceTwoPaneConfigOnRight = preferences.read(DEVICE_TWO_PANE_CONFIG_ON_RIGHT),
// Scrcpy Server
customServerUri = preferences.read(CUSTOM_SERVER_URI),