fix: dock navigation

This commit is contained in:
Miuzarte
2026-04-21 20:11:49 +08:00
parent 696e434b25
commit 9bbf30696a

View File

@@ -78,6 +78,7 @@ import io.github.miuzarte.scrcpyforandroid.ui.rememberBlurBackdrop
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -139,7 +140,12 @@ fun MainScreen() {
val pagerState = rememberPagerState( val pagerState = rememberPagerState(
initialPage = MainBottomTabDestination.Devices.ordinal, initialPage = MainBottomTabDestination.Devices.ordinal,
pageCount = { tabs.size }) pageCount = { tabs.size })
val currentTab = tabs[pagerState.currentPage] var selectedTabIndex by rememberSaveable {
mutableStateOf(MainBottomTabDestination.Devices.ordinal)
}
var pagerNavigationJob by remember { mutableStateOf<Job?>(null) }
var isPagerNavigating by remember { mutableStateOf(false) }
val currentTab = tabs[selectedTabIndex]
val rootBackStack = remember { mutableStateListOf<NavKey>(RootScreen.Home) } val rootBackStack = remember { mutableStateListOf<NavKey>(RootScreen.Home) }
val currentRootScreen = rootBackStack.lastOrNull() as? RootScreen ?: RootScreen.Home val currentRootScreen = rootBackStack.lastOrNull() as? RootScreen ?: RootScreen.Home
var showReorderDevices by rememberSaveable { mutableStateOf(false) } var showReorderDevices by rememberSaveable { mutableStateOf(false) }
@@ -274,7 +280,38 @@ fun MainScreen() {
// Derived flags // Derived flags
val canNavigateBack = rootBackStack.size > 1 val canNavigateBack = rootBackStack.size > 1
|| pagerState.currentPage != MainBottomTabDestination.Devices.ordinal || selectedTabIndex != MainBottomTabDestination.Devices.ordinal
fun navigateToTab(tab: MainBottomTabDestination) {
val targetIndex = tab.ordinal
if (targetIndex == selectedTabIndex) {
return
}
pagerNavigationJob?.cancel()
selectedTabIndex = targetIndex
isPagerNavigating = true
scope.launch {
val job = coroutineContext[Job]
pagerNavigationJob = job
try {
pagerState.animateScrollToPage(
page = targetIndex,
animationSpec = spring(
dampingRatio = UiMotion.PAGE_SWITCH_DAMPING_RATIO,
stiffness = UiMotion.PAGE_SWITCH_STIFFNESS,
),
)
} finally {
if (pagerNavigationJob == job) {
isPagerNavigating = false
pagerNavigationJob = null
if (pagerState.currentPage != targetIndex) {
selectedTabIndex = pagerState.currentPage
}
}
}
}
}
LaunchedEffect(asBundle.lastUpdateCheckAt) { LaunchedEffect(asBundle.lastUpdateCheckAt) {
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
@@ -288,16 +325,8 @@ fun MainScreen() {
fun handleBackNavigation() { fun handleBackNavigation() {
if (rootBackStack.size > 1) { if (rootBackStack.size > 1) {
rootNavigator.pop() rootNavigator.pop()
} else if (pagerState.currentPage != MainBottomTabDestination.Devices.ordinal) { } else if (selectedTabIndex != MainBottomTabDestination.Devices.ordinal) {
scope.launch { navigateToTab(MainBottomTabDestination.Devices)
pagerState.animateScrollToPage(
page = MainBottomTabDestination.Devices.ordinal,
animationSpec = spring(
dampingRatio = UiMotion.PAGE_SWITCH_DAMPING_RATIO,
stiffness = UiMotion.PAGE_SWITCH_STIFFNESS,
),
)
}
} else { } else {
val now = SystemClock.elapsedRealtime() val now = SystemClock.elapsedRealtime()
if (now - lastExitBackPressAtMs > 2_000L) { if (now - lastExitBackPressAtMs > 2_000L) {
@@ -344,6 +373,12 @@ fun MainScreen() {
asBundle.adbKeyName.ifBlank { AppSettings.ADB_KEY_NAME.defaultValue } asBundle.adbKeyName.ifBlank { AppSettings.ADB_KEY_NAME.defaultValue }
} }
LaunchedEffect(pagerState.currentPage) {
if (!isPagerNavigating && selectedTabIndex != pagerState.currentPage) {
selectedTabIndex = pagerState.currentPage
}
}
val rootEntryProvider = entryProvider<NavKey> { val rootEntryProvider = entryProvider<NavKey> {
entry(RootScreen.Home) { entry(RootScreen.Home) {
val blurBackdrop = rememberBlurBackdrop(enableBlur = asBundle.blur) val blurBackdrop = rememberBlurBackdrop(enableBlur = asBundle.blur)
@@ -354,18 +389,6 @@ fun MainScreen() {
drawContent() drawContent()
} }
fun navigateToTab(tab: MainBottomTabDestination) {
scope.launch {
pagerState.animateScrollToPage(
page = tab.ordinal,
animationSpec = spring(
dampingRatio = UiMotion.PAGE_SWITCH_DAMPING_RATIO,
stiffness = UiMotion.PAGE_SWITCH_STIFFNESS,
),
)
}
}
Scaffold( Scaffold(
bottomBar = { bottomBar = {
if (!asBundle.floatingBottomBar) { if (!asBundle.floatingBottomBar) {
@@ -450,17 +473,9 @@ fun MainScreen() {
indication = null, indication = null,
onClick = {}, onClick = {},
), ),
selectedIndex = { pagerState.currentPage }, selectedIndex = { selectedTabIndex },
onSelected = { index -> onSelected = { index ->
scope.launch { navigateToTab(tabs[index])
pagerState.animateScrollToPage(
page = index,
animationSpec = spring(
dampingRatio = UiMotion.PAGE_SWITCH_DAMPING_RATIO,
stiffness = UiMotion.PAGE_SWITCH_STIFFNESS,
),
)
}
}, },
backdrop = glassBackdrop, backdrop = glassBackdrop,
tabsCount = tabs.size, tabsCount = tabs.size,