RootNavigationGestures.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/navigation/RootNavigationGestures.kt
1package eu.friedcutlet.yokan.ui.navigation23import androidx.compose.foundation.focusable4import androidx.compose.runtime.Composable5import androidx.compose.runtime.LaunchedEffect6import androidx.compose.runtime.getValue7import androidx.compose.runtime.remember8import androidx.compose.ui.Modifier9import androidx.compose.ui.focus.FocusRequester10import androidx.compose.ui.focus.focusRequester11import androidx.compose.ui.input.key.Key12import androidx.compose.ui.input.key.KeyEventType13import androidx.compose.ui.input.key.key14import androidx.compose.ui.input.key.onKeyEvent15import androidx.compose.ui.input.key.type16import androidx.lifecycle.compose.collectAsStateWithLifecycle17import eu.friedcutlet.yokan.domain.ItemType18import eu.friedcutlet.yokan.ui.screens.library.rememberLibraryViewModel1920/**21 * Attaches keyboard movement between item-type tabs (Manga / Anime / Novel)22 * within the active section, outside the bottom-nav:23 *24 * - **Keyboard A / D** — left / right shifts `selectedItemType` through the25 * visible item-type list, wrapping at the ends. Wired via `onKeyEvent`26 * so child handlers (TextFields, etc.) get first dibs on the keystroke;27 * typing "a" in a focused search field never triggers tab switching.28 *29 * When the active section is one of the per-library variants (`MangaLibrary`,30 * `AnimeLibrary`, `NovelLibrary`), the gesture also re-routes the active31 * section to match the new item type so the user lands on the matching32 * library page rather than the Manga library showing Anime data.33 */34@Composable35internal fun Modifier.rootNavigationGestures(nav: Navigator): Modifier {36 val focusRequester = remember { FocusRequester() }37 LaunchedEffect(Unit) { runCatching { focusRequester.requestFocus() } }38 val section by nav.section.collectAsStateWithLifecycle()39 // The visible library screen's ViewModel: same root store, same key.40 val library = section.itemType?.let { rememberLibraryViewModel(it, nav) }4142 fun isLibrarySection() = nav.section.value.itemType != null4344 fun shiftItemType(delta: Int) {45 val hidden = nav.hiddenSections.value46 val types = ItemType.entries.filter { it.librarySection() !in hidden }47 if (types.size < 2) return48 val currentIdx = types.indexOf(nav.itemType.value).takeIf { it >= 0 } ?: 049 val nextIdx = ((currentIdx + delta) % types.size + types.size) % types.size50 val nextType = types[nextIdx]51 nav.selectItemType(nextType)52 if (isLibrarySection()) nav.select(nextType.librarySection())53 }5455 /**56 * Pick the right horizontal-swipe action for the active section.57 *58 * - Library sections: cycle categories within the current item type.59 * Falls back to item-type cycling when the user only has one (or60 * zero) category for the active type, so a single-category library61 * can still be navigated.62 * - Everywhere else (Updates / History / Browse / Tracker / More):63 * cycle item types so each section's tab strip flips through Manga /64 * Anime / Novel without changing the active section itself.65 */66 fun shift(delta: Int) {67 if (library?.shiftCategory(delta) == true) return68 shiftItemType(delta)69 }7071 return this72 .focusRequester(focusRequester)73 .focusable()74 .onKeyEvent { event ->75 if (event.type != KeyEventType.KeyDown) return@onKeyEvent false76 when (event.key) {77 Key.A -> { shift(-1); true }78 Key.D -> { shift(+1); true }79 else -> false80 }81 }82}83