Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
Expand Down Expand Up @@ -451,10 +452,7 @@ private fun TvDetailContent(
val resources = localizedResources()
val configuration = LocalConfiguration.current
val playFocus = remember { FocusRequester() }
// The circular Version control in the hero action cluster. Hoisted here so
// an Up press from the body can return to the bottom-most playback control
// without skipping straight to Play.
val selectorFocus = remember { FocusRequester() }
val heroActionFocus = remember(detail.contentId) { TvHeroActionFocus(playFocus) }
val firstCastFocus = remember { FocusRequester() }
// Focus restore for the cast rail: remembers which cast card pushed the
// person page so the return trip lands back on it instead of Play.
Expand Down Expand Up @@ -855,9 +853,8 @@ private fun TvDetailContent(
// actions section.
val returnToHero: () -> Boolean = {
coroutineScope.launch {
// Land on the selector row (the bottom-most primary control) when
// it is composed; otherwise fall back to Play
// (audiobooks, or the next-up placeholder pill). Focus moves
// Land on the hero action the viewer left; otherwise fall back to
// Play (audiobooks, or the next-up placeholder pill). Focus moves
// BEFORE the scroll so the highlight travels with the window
// instead of appearing only after it settles; when the hero has
// been disposed off-screen the requests fail and we re-focus
Expand All @@ -866,14 +863,7 @@ private fun TvDetailContent(
// so a requestFocus that returned false still counted as focused —
// the scroll then ran as though the highlight had already moved.
// These report the request's own answer.
val focusedImmediately =
selectorFocus.claimFocusOrReport(
target = "detail_selector",
action = "return_to_top",
) || playFocus.claimFocusOrReport(
target = "detail_play",
action = "return_to_top",
)
val focusedImmediately = heroActionFocus.claim(action = "return_to_top")
if (focusedImmediately) {
// Let the focus system enqueue its automatic bring-into-view
// first, then cancel/replace that scroll with the paced
Expand All @@ -882,16 +872,7 @@ private fun TvDetailContent(
}
listState.animateScrollToItemPaced(0)
if (!focusedImmediately) {
if (!selectorFocus.claimFocusOrReport(
target = "detail_selector",
action = "return_to_top_retry",
)
) {
playFocus.claimFocusOrReport(
target = "detail_play",
action = "return_to_top_retry",
)
}
heroActionFocus.claim(action = "return_to_top_retry")
}
}
true
Expand Down Expand Up @@ -1121,8 +1102,7 @@ private fun TvDetailContent(
detail = detail,
state = state,
viewModel = viewModel,
playFocus = playFocus,
selectorFocus = selectorFocus,
focus = heroActionFocus,
onPlay = onPlay,
onSeriesClick = onSeriesClick,
onSeasonClick = onSeasonClick,
Expand Down Expand Up @@ -1324,12 +1304,9 @@ private fun TvDetailContent(
?.takeUnless { state.entryEpisodeSelectionApplied },
showsSeasonChips = showsSeasonChips,
onReturnToHero = returnToHero,
onReturnToSeriesSelector = if (isSeriesDetail) {
onReturnToHeroAction = if (isSeriesDetail) {
{
selectorFocus.claimFocusOrReport(
target = "detail_series_selector",
action = "season_up",
) || returnToHero()
heroActionFocus.claim(action = "season_up") || returnToHero()
}
} else {
null
Expand Down Expand Up @@ -1607,9 +1584,9 @@ private fun TvDetailContent(
},
// When Similar is the first body rail (movie with no
// episode rail, cast, trailers or box set), Up returns to the hero
// Play button instead of relying on geometry.
// action the viewer left instead of relying on geometry.
upFocusRequester = if (!hasEpisodeNavAbove && !showsCastSection && !showsTrailersSection && !showsBoxSetRow && collectionsRow == null) {
playFocus
heroActionFocus.target
Comment thread
Quick104 marked this conversation as resolved.
Resolved
} else {
null
},
Expand Down Expand Up @@ -1878,14 +1855,55 @@ internal fun seriesStarringNames(detail: ItemDetail): String? {
return names.takeIf { it.isNotEmpty() }?.joinToString(", ")
}

/**
* The hero action row's focus targets, hoisted above the lazy hero item so the
* action the viewer left survives the hero scrolling out of composition. Up
* from the body and entry into the row return to [last], else Play, matching
* tvOS `TVDetailActionRow`.
*/
@Stable
private class TvHeroActionFocus(val play: FocusRequester) {
val startOver = FocusRequester()
val edition = FocusRequester()
val version = FocusRequester()
val audio = FocusRequester()
val subtitles = FocusRequester()
val watchlist = FocusRequester()
val more = FocusRequester()
var last by mutableStateOf(play)
var startOverShown by mutableStateOf(false)
var editionShown by mutableStateOf(false)

/**
* Where focus returns: [last], unless that control is hidden right now.
* Resolved on each read, not stored, because Start Over and Edition
* hide only while the next episode's details load and come back after.
Comment thread
Quick104 marked this conversation as resolved.
Resolved
*/
val target: FocusRequester
get() = when {
last === startOver && !startOverShown -> play
last === edition && !editionShown -> version
Comment on lines +1889 to +1890

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kody code-review Bug medium

Temporary fallback focus overwrites the remembered hero action

When Start Over or Edition is temporarily hidden, the fallback focus callback replaces last, so the original action is not restored after the next episode loads. Preserve last for temporary fallback focus and update it only for an intentional action change.

Prompt for LLM
File androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/detail/TvItemDetailScreen.kt, lines 1884-1885:

Temporary fallback focus overwrites the remembered hero action

When Start Over or Edition is temporarily hidden, focusing its fallback invokes the focus callback and replaces `last` with Play or Version, so the original action is not restored after the next episode loads. Preserve the remembered action when focus is assigned to a temporary fallback, and update it only for an intentional action change.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

​

​

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this against the code and I'm leaving it as is. If the viewer presses Up while Start Over or Edition is hidden, focus lands on Play or Version and stays there, so that is the hero action they focused last. Up returning there next time is the rule #793 asks for. When the gap passes without the viewer entering the row, last is untouched and the hidden action comes back as the target, which is what resolving on read is for.

🤖 Claude Code (claude-opus-5-5)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That distinction makes sense. If the viewer enters the hero row during the loading gap, Play or Version becomes the action they most recently focused, so subsequent returns should go there. If they don’t enter the row during the gap, resolving the fallback on read preserves the original target.

I withdraw the suggestion to preserve the earlier action after focus lands on the fallback. The implementation matches the stated “last focused hero action” behavior.

else -> last
}

/** Focuses [target], else Play; false when neither is attached. */
fun claim(action: String): Boolean {
val requester = target
return (requester !== play && requester.claimFocusOrReport(target = "detail_hero_action", action = action)) ||
play.claimFocusOrReport(target = "detail_play", action = action)
}
}

private fun Modifier.rememberHeroAction(focus: TvHeroActionFocus, target: FocusRequester): Modifier =
onFocusChanged { state -> if (state.hasFocus) focus.last = target }
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

@OptIn(ExperimentalComposeUiApi::class)
@Composable
private fun HeroActionRow(
detail: ItemDetail,
state: TvItemDetailUiState,
viewModel: TvItemDetailViewModel,
playFocus: FocusRequester,
selectorFocus: FocusRequester,
focus: TvHeroActionFocus,
onPlay: (contentId: String, fileId: Int?, versionChosen: Boolean, audioTrackIndex: Int?, audioPickedThisSession: Boolean, subtitleSelection: TvSubtitleLaunchSelection?, itemType: String?, resumePositionSeconds: Double?) -> Unit,
onSeriesClick: (seriesId: String) -> Unit,
onSeasonClick: (seriesId: String, seasonNumber: Int) -> Unit,
Expand Down Expand Up @@ -2060,6 +2078,9 @@ private fun HeroActionRow(
// when next-up resolves would only risk yanking focus back if the viewer had
// already moved into the seasons/episodes rails.

// Start Over shows only while the item is in progress.
SideEffect { focus.startOverShown = hasResume }

Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
// Action row — approved tvOS `HStack(spacing: 18)` mapped through the
// half-scale dp port as the button internals. Version and Subtitles use
Expand All @@ -2069,10 +2090,10 @@ private fun HeroActionRow(
.fillMaxWidth()
.focusProperties {
// Entering the cluster from outside (Down from the
// synopsis/facts, Up from the body) always
// lands on Play — geometric nearest-child search would
// otherwise pick whichever toggle sits closest.
enter = { playFocus }
// synopsis/facts, Up from the body) lands on the action
// the viewer left, else Play — geometric nearest-child
// search would otherwise pick whichever toggle sits closest.
enter = { focus.target }
}
.focusGroup(),
) {
Expand Down Expand Up @@ -2122,8 +2143,8 @@ private fun HeroActionRow(
detail.type != "series" -> Modifier
EpisodeLabel.labelsByAirDate(detail.episodeLabels) -> Modifier.widthIn(min = 170.dp)
else -> Modifier.width(170.dp)
},
focusRequester = playFocus,
}.rememberHeroAction(focus, focus.play),
focusRequester = focus.play,
)


Expand All @@ -2137,6 +2158,8 @@ private fun HeroActionRow(
contentDescription = stringResource(CommonR.string.detail_startOver),
titleOnFocus = stringResource(CommonR.string.detail_startOver),
isActive = false,
modifier = Modifier.rememberHeroAction(focus, focus.startOver),
focusRequester = focus.startOver,
onClick = {
if (playReady && !playLaunchPending) {
playLaunchPending = true
Expand Down Expand Up @@ -2191,7 +2214,12 @@ private fun HeroActionRow(
} else {
viewModel::onSubtitleTrackSelected
},
versionFocusRequester = selectorFocus,
editionFocusRequester = focus.edition,
versionFocusRequester = focus.version,
audioFocusRequester = focus.audio,
subtitleFocusRequester = focus.subtitles,
onTriggerFocused = { focus.last = it },
onEditionsShown = { focus.editionShown = it },
)

TvSquareToggleButton(
Expand All @@ -2204,6 +2232,8 @@ private fun HeroActionRow(
stringResource(CommonR.string.detail_watchlistAdd)
},
onClick = viewModel::onToggleWatchlist,
modifier = Modifier.rememberHeroAction(focus, focus.watchlist),
focusRequester = focus.watchlist,
titleOnFocus = stringResource(CommonR.string.detail_watchlist),
)

Expand All @@ -2214,6 +2244,8 @@ private fun HeroActionRow(
contentDescription = stringResource(CommonR.string.detail_moreOptions),
titleOnFocus = stringResource(CommonR.string.detail_more),
onClick = { if (!state.isProvisional) moreOpen = true },
modifier = Modifier.rememberHeroAction(focus, focus.more),
focusRequester = focus.more,
)
}
}
Expand Down Expand Up @@ -2471,7 +2503,7 @@ private fun EpisodesSection(
entryEpisodeContentId: String?,
showsSeasonChips: Boolean,
onReturnToHero: () -> Boolean,
onReturnToSeriesSelector: (() -> Boolean)? = null,
onReturnToHeroAction: (() -> Boolean)? = null,
onSeasonSelected: (org.siloserver.silo.model.catalog.Season) -> Unit,
onEpisodeSelected: (EpisodeListItem) -> Unit,
onEpisodeFocused: (EpisodeListItem) -> Unit = {},
Expand Down Expand Up @@ -2515,9 +2547,8 @@ private fun EpisodesSection(
seasons = state.seasons,
selectedSeason = state.selectedSeason,
onSeasonSelected = onSeasonSelected,
// Return to the circular Version control in the hero; if
// it is not attached yet, fall back to Play.
onDirectionUp = onReturnToSeriesSelector ?: onReturnToHero,
// Return to the hero action the viewer left, else Play.
onDirectionUp = onReturnToHeroAction ?: onReturnToHero,
horizontalContentPadding = TvDetailHorizontalInset,
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import androidx.compose.material.icons.automirrored.filled.VolumeUp
import androidx.compose.material.icons.filled.Layers
import androidx.compose.material.icons.filled.VideoLibrary
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.res.stringResource
import org.siloserver.silo.common.R as CommonR
import org.siloserver.silo.model.catalog.PlaybackVariant
Expand Down Expand Up @@ -87,7 +90,14 @@ internal fun TvPlaybackActionSelectors(
onSelectEdition: (String) -> Unit,
onSelectAudioTrack: (Int?) -> Unit,
onSelectSubtitleTrack: (Int?) -> Unit,
editionFocusRequester: FocusRequester,
versionFocusRequester: FocusRequester,
audioFocusRequester: FocusRequester,
subtitleFocusRequester: FocusRequester,
/** Reports the focus target of the trigger that gains focus. */
onTriggerFocused: (FocusRequester) -> Unit = {},
/** Reports whether the Edition trigger is shown. */
onEditionsShown: (Boolean) -> Unit = {},
) {
val text = rememberTvPlaybackText()
val autoVersionDetail = stringResource(CommonR.string.detail_autoVersion)
Expand All @@ -96,6 +106,7 @@ internal fun TvPlaybackActionSelectors(
val offDetail = stringResource(CommonR.string.detail_offDetail)
val editions = playbackEditions(versions, playbackVariants)
val showEditions = editions.hasEditionChoices()
SideEffect { onEditionsShown(showEditions) }
val currentEdition = editions.firstOrNull { currentVersion?.fileId in it.fileIds }
?: editions.firstOrNull()
val scopedVersions = if (showEditions) currentEdition?.versions.orEmpty() else versions
Expand Down Expand Up @@ -237,7 +248,8 @@ internal fun TvPlaybackActionSelectors(
onSelect = { onSelectEdition(edition.id) },
)
},
triggerFocusRequester = versionFocusRequester,
triggerFocusRequester = editionFocusRequester,
modifier = Modifier.reportTriggerFocus(editionFocusRequester, onTriggerFocused),
interactive = currentVersion != null,
triggerStyle = TvSelectorTriggerStyle.CircularAction,
)
Expand All @@ -247,7 +259,8 @@ internal fun TvPlaybackActionSelectors(
label = stringResource(CommonR.string.detail_version),
value = versionValue,
options = versionOptions,
triggerFocusRequester = versionFocusRequester.takeUnless { showEditions },
triggerFocusRequester = versionFocusRequester,
modifier = Modifier.reportTriggerFocus(versionFocusRequester, onTriggerFocused),
interactive = currentVersion != null && selectorIsInteractive(scopedVersions.size),
triggerStyle = TvSelectorTriggerStyle.CircularAction,
)
Expand All @@ -256,6 +269,8 @@ internal fun TvPlaybackActionSelectors(
label = stringResource(CommonR.string.detail_audio),
value = audioValue,
options = audioOptions,
triggerFocusRequester = audioFocusRequester,
modifier = Modifier.reportTriggerFocus(audioFocusRequester, onTriggerFocused),
interactive = currentVersion != null && formattedAudioOptions.isNotEmpty(),
triggerStyle = TvSelectorTriggerStyle.CircularAction,
)
Expand All @@ -264,7 +279,14 @@ internal fun TvPlaybackActionSelectors(
label = stringResource(CommonR.string.detail_subtitles),
value = subtitleValue,
options = subtitleOptions,
triggerFocusRequester = subtitleFocusRequester,
modifier = Modifier.reportTriggerFocus(subtitleFocusRequester, onTriggerFocused),
interactive = currentVersion != null,
triggerStyle = TvSelectorTriggerStyle.CircularAction,
)
}

private fun Modifier.reportTriggerFocus(
target: FocusRequester,
onTriggerFocused: (FocusRequester) -> Unit,
): Modifier = onFocusChanged { state -> if (state.hasFocus) onTriggerFocused(target) }
Loading