TvFocusEngine.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/tv/TvFocusEngine.kt
1package eu.friedcutlet.yokan.ui.tv23import androidx.compose.ui.input.key.Key4import eu.friedcutlet.yokan.database.DbSettings5import eu.friedcutlet.yokan.ui.navigation.NavSection67/**8 * Resolves the effective UI scale for rendering.9 *10 * On Android TV devices, [DbSettings.tvUiScale] is used (defaulting to 1.0) and clamped11 * to 0.85f..1.25f (5% increments in settings).12 * On non-TV devices, [DbSettings.autoUiScale] uses 1.0f or falls back to [DbSettings.uiScale].13 */14fun resolveTvUiScale(15 isTv: Boolean,16 settings: DbSettings,17): Float = if (isTv) {18 (settings.tvUiScale ?: 1.0).toFloat().coerceIn(0.85f, 1.25f)19} else if (settings.autoUiScale) {20 1f21} else {22 settings.uiScale.toFloat()23}2425/**26 * Whether the anime-only layout gate is active.27 *28 * Defaults to true on TV devices unless explicitly overridden by [animeOnlyTvMode].29 * Off everywhere else.30 */31fun resolveTvAnimeOnly(32 isTv: Boolean,33 animeOnlyTvMode: Boolean?,34): Boolean = isTv && (animeOnlyTvMode ?: true)3536/**37 * Checks if a [NavSection] should be hidden in TV anime-only mode.38 */39fun shouldHideNavSectionForTv(40 section: NavSection,41 isTv: Boolean,42 animeOnlyTvMode: Boolean?,43): Boolean {44 if (!resolveTvAnimeOnly(isTv, animeOnlyTvMode)) return false45 return section == NavSection.MangaLibrary || section == NavSection.NovelLibrary46}4748/**49 * Handles D-pad crossing between the navigation rail and main content container on TV.50 *51 * When focus is in content and DirectionLeft is pressed at the leftmost boundary52 * (where moveLeft returns false), rail focus is requested and the event is consumed.53 * When focus is in the rail and DirectionRight is pressed, content focus is requested54 * and the event is consumed.55 *56 * Returns true if the key event was consumed.57 */58fun handleTvDpadNavigation(59 key: Key,60 isKeyDown: Boolean,61 railHasFocus: Boolean,62 moveLeft: () -> Boolean,63 moveRight: () -> Boolean,64 requestRailFocus: () -> Unit,65 requestContentFocus: () -> Unit,66): Boolean {67 if (!isKeyDown) return false68 return when {69 key == Key.DirectionLeft && !railHasFocus -> {70 val moved = moveLeft()71 if (!moved) {72 requestRailFocus()73 }74 true75 }76 key == Key.DirectionRight && railHasFocus -> {77 val moved = moveRight()78 if (!moved) {79 requestContentFocus()80 }81 true82 }83 else -> false84 }85}86