package eu.friedcutlet.yokan.ui.navigation import androidx.compose.foundation.focusable import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.input.key.Key import androidx.compose.ui.input.key.KeyEventType import androidx.compose.ui.input.key.key import androidx.compose.ui.input.key.onKeyEvent import androidx.compose.ui.input.key.type import androidx.lifecycle.compose.collectAsStateWithLifecycle import eu.friedcutlet.yokan.domain.ItemType import eu.friedcutlet.yokan.ui.screens.library.rememberLibraryViewModel /** * Attaches keyboard movement between item-type tabs (Manga / Anime / Novel) * within the active section, outside the bottom-nav: * * - **Keyboard A / D** — left / right shifts `selectedItemType` through the * visible item-type list, wrapping at the ends. Wired via `onKeyEvent` * so child handlers (TextFields, etc.) get first dibs on the keystroke; * typing "a" in a focused search field never triggers tab switching. * * When the active section is one of the per-library variants (`MangaLibrary`, * `AnimeLibrary`, `NovelLibrary`), the gesture also re-routes the active * section to match the new item type so the user lands on the matching * library page rather than the Manga library showing Anime data. */ @Composable internal fun Modifier.rootNavigationGestures(nav: Navigator): Modifier { val focusRequester = remember { FocusRequester() } LaunchedEffect(Unit) { runCatching { focusRequester.requestFocus() } } val section by nav.section.collectAsStateWithLifecycle() // The visible library screen's ViewModel: same root store, same key. val library = section.itemType?.let { rememberLibraryViewModel(it, nav) } fun isLibrarySection() = nav.section.value.itemType != null fun shiftItemType(delta: Int) { val hidden = nav.hiddenSections.value val types = ItemType.entries.filter { it.librarySection() !in hidden } if (types.size < 2) return val currentIdx = types.indexOf(nav.itemType.value).takeIf { it >= 0 } ?: 0 val nextIdx = ((currentIdx + delta) % types.size + types.size) % types.size val nextType = types[nextIdx] nav.selectItemType(nextType) if (isLibrarySection()) nav.select(nextType.librarySection()) } /** * Pick the right horizontal-swipe action for the active section. * * - Library sections: cycle categories within the current item type. * Falls back to item-type cycling when the user only has one (or * zero) category for the active type, so a single-category library * can still be navigated. * - Everywhere else (Updates / History / Browse / Tracker / More): * cycle item types so each section's tab strip flips through Manga / * Anime / Novel without changing the active section itself. */ fun shift(delta: Int) { if (library?.shiftCategory(delta) == true) return shiftItemType(delta) } return this .focusRequester(focusRequester) .focusable() .onKeyEvent { event -> if (event.type != KeyEventType.KeyDown) return@onKeyEvent false when (event.key) { Key.A -> { shift(-1); true } Key.D -> { shift(+1); true } else -> false } } }