From 52e80286095b4007a2e73ac2335c90baf554cdd0 Mon Sep 17 00:00:00 2001 From: Dantalian Date: Sun, 15 Nov 2026 22:35:10 +0800 Subject: [PATCH] fix(ui): leave the file manager after a drive hop instead of replaying hops (#2567) Back replays the history stack, so a round trip between drives leaves one entry per hop: internal storage, Linux, internal storage, then Back lands on Linux again and leaving takes one press per hop. Two roots in a row means the user hopped rather than descended into a folder, so Back leaves instead. Returning to a folder from a drive root still works as before. Co-authored-by: Dantalian --- .../com/droiddeck/launcher/files/FileManagerScreen.kt | 10 ++++++++-- .../launcher/files/FileManagerNavigationTest.kt | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt b/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt index 2d6cd6d2c4..4da772b1e8 100644 --- a/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt +++ b/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt @@ -112,6 +112,10 @@ private const val HISTORY_LIMIT = 50 internal fun canNavigateUp(currentDir: File, currentRoot: File): Boolean = currentDir != currentRoot && currentDir.parentFile?.exists() == true +// Back leaves after a drive hop instead of replaying a stack of hops one press at a time. +internal fun backExitsAtRootHop(currentDir: File, currentRoot: File, previous: Pair): Boolean = + currentDir == currentRoot && previous.first == previous.second + @OptIn(ExperimentalMaterial3Api::class) @Composable fun FileManagerScreen( @@ -269,8 +273,10 @@ fun FileManagerScreen( if (showFavorites) showFavorites = false else { while (history.isNotEmpty() && !history.last().first.isDirectory) history.removeAt(history.lastIndex) - val previous = history.removeLastOrNull() - if (previous != null) { + val previous = history.lastOrNull() + if (previous != null && backExitsAtRootHop(currentDir, currentRoot, previous)) onExit?.invoke() + else if (previous != null) { + history.removeLastOrNull() currentRoot = previous.second loadDirectory(previous.first, record = false) } else if (canNavigateUp(currentDir, currentRoot)) currentDir.parentFile?.let { loadDirectory(it, record = false) } diff --git a/app/src/test/java/com/droiddeck/launcher/files/FileManagerNavigationTest.kt b/app/src/test/java/com/droiddeck/launcher/files/FileManagerNavigationTest.kt index cbcf207553..0b04410a25 100644 --- a/app/src/test/java/com/droiddeck/launcher/files/FileManagerNavigationTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/files/FileManagerNavigationTest.kt @@ -17,4 +17,14 @@ class FileManagerNavigationTest { assertTrue(canNavigateUp(child, drive)) assertFalse(canNavigateUp(drive, drive)) } + + @Test fun backLeavesInsteadOfHoppingBetweenTwoRoots() { + val drive = tmp.newFolder("drive") + val other = tmp.newFolder("other") + val child = File(drive, "folder").apply { mkdir() } + + assertTrue(backExitsAtRootHop(drive, drive, other to other)) + assertFalse(backExitsAtRootHop(drive, drive, child to other)) + assertFalse(backExitsAtRootHop(child, drive, other to other)) + } }