package eu.friedcutlet.yokan.ui.tv import androidx.compose.ui.input.key.Key import eu.friedcutlet.yokan.database.DbSettings import eu.friedcutlet.yokan.ui.navigation.NavSection /** * Resolves the effective UI scale for rendering. * * On Android TV devices, [DbSettings.tvUiScale] is used (defaulting to 1.0) and clamped * to 0.85f..1.25f (5% increments in settings). * On non-TV devices, [DbSettings.autoUiScale] uses 1.0f or falls back to [DbSettings.uiScale]. */ fun resolveTvUiScale( isTv: Boolean, settings: DbSettings, ): Float = if (isTv) { (settings.tvUiScale ?: 1.0).toFloat().coerceIn(0.85f, 1.25f) } else if (settings.autoUiScale) { 1f } else { settings.uiScale.toFloat() } /** * Whether the anime-only layout gate is active. * * Defaults to true on TV devices unless explicitly overridden by [animeOnlyTvMode]. * Off everywhere else. */ fun resolveTvAnimeOnly( isTv: Boolean, animeOnlyTvMode: Boolean?, ): Boolean = isTv && (animeOnlyTvMode ?: true) /** * Checks if a [NavSection] should be hidden in TV anime-only mode. */ fun shouldHideNavSectionForTv( section: NavSection, isTv: Boolean, animeOnlyTvMode: Boolean?, ): Boolean { if (!resolveTvAnimeOnly(isTv, animeOnlyTvMode)) return false return section == NavSection.MangaLibrary || section == NavSection.NovelLibrary } /** * Handles D-pad crossing between the navigation rail and main content container on TV. * * When focus is in content and DirectionLeft is pressed at the leftmost boundary * (where moveLeft returns false), rail focus is requested and the event is consumed. * When focus is in the rail and DirectionRight is pressed, content focus is requested * and the event is consumed. * * Returns true if the key event was consumed. */ fun handleTvDpadNavigation( key: Key, isKeyDown: Boolean, railHasFocus: Boolean, moveLeft: () -> Boolean, moveRight: () -> Boolean, requestRailFocus: () -> Unit, requestContentFocus: () -> Unit, ): Boolean { if (!isKeyDown) return false return when { key == Key.DirectionLeft && !railHasFocus -> { val moved = moveLeft() if (!moved) { requestRailFocus() } true } key == Key.DirectionRight && railHasFocus -> { val moved = moveRight() if (!moved) { requestContentFocus() } true } else -> false } }