Skip to content

Commit d6f2c15

Browse files
committed
fix(tv): keep repeated up in content
1 parent ade712c commit d6f2c15

8 files changed

Lines changed: 90 additions & 15 deletions

File tree

‎androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/calendar/TvCalendarScreen.kt‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import androidx.compose.runtime.mutableIntStateOf
4848
import androidx.compose.runtime.mutableStateOf
4949
import androidx.compose.runtime.remember
5050
import androidx.compose.runtime.rememberCoroutineScope
51+
import androidx.compose.runtime.rememberUpdatedState
5152
import androidx.compose.runtime.setValue
5253
import androidx.compose.ui.Alignment
5354
import androidx.compose.ui.Modifier
@@ -679,19 +680,22 @@ internal fun shouldReturnCalendarFocusToControls(
679680
internal enum class CalendarUpFallbackAction {
680681
EnterMenu,
681682
ReturnToControls,
683+
StayInContent,
682684
MoveWithinContent,
683685
}
684686

685687
internal fun calendarUpFallbackAction(
686688
focusedShelfIndex: Int?,
687689
firstFocusableShelfIndex: Int,
688690
isReturningToControls: Boolean,
691+
isRepeat: Boolean = false,
689692
): CalendarUpFallbackAction = when {
690693
shouldReturnCalendarFocusToControls(
691694
focusedShelfIndex = focusedShelfIndex,
692695
firstFocusableShelfIndex = firstFocusableShelfIndex,
693696
isReturningToControls = isReturningToControls,
694-
) -> CalendarUpFallbackAction.ReturnToControls
697+
) -> if (isRepeat) CalendarUpFallbackAction.StayInContent else CalendarUpFallbackAction.ReturnToControls
698+
focusedShelfIndex == null && isRepeat -> CalendarUpFallbackAction.StayInContent
695699
focusedShelfIndex == null -> CalendarUpFallbackAction.EnterMenu
696700
else -> CalendarUpFallbackAction.MoveWithinContent
697701
}
@@ -754,12 +758,12 @@ private fun CalendarList(
754758
// when focus is already in the controls item, mirror the shell's default
755759
// (moveFocus within content; false -> shell hands off to the menu bar).
756760
var focusedShelfIndex by remember { mutableStateOf<Int?>(null) }
757-
val calendarUpFallback = remember(firstFocusableDayIndex) {
758-
{ _: Boolean ->
761+
val currentCalendarUpFallback = rememberUpdatedState<(Boolean) -> Boolean> { isRepeat ->
759762
when (calendarUpFallbackAction(
760763
focusedShelfIndex = focusedShelfIndex,
761764
firstFocusableShelfIndex = firstFocusableDayIndex,
762765
isReturningToControls = isReturningToControls,
766+
isRepeat = isRepeat,
763767
)
764768
) {
765769
CalendarUpFallbackAction.EnterMenu -> {
@@ -770,15 +774,21 @@ private fun CalendarList(
770774
onMoveUpToControls()
771775
true
772776
}
777+
CalendarUpFallbackAction.StayInContent -> true
773778
CalendarUpFallbackAction.MoveWithinContent -> {
774779
focusManager.moveFocus(androidx.compose.ui.focus.FocusDirection.Up)
775780
}
776781
}
777-
}
778782
}
779-
DisposableEffect(onContentUpFallbackChanged) {
780-
onContentUpFallbackChanged?.invoke(calendarUpFallback)
781-
onDispose { onContentUpFallbackChanged?.invoke(calendarUpFallback) }
783+
// Keep the registered identity stable while its implementation reads the
784+
// current loaded-day state. A new lambda keyed by firstFocusableDayIndex
785+
// would otherwise leave the shell holding the pre-load callback until this
786+
// screen disposes.
787+
val calendarUpFallbackRegistration: (Boolean) -> Boolean =
788+
remember { { isRepeat -> currentCalendarUpFallback.value(isRepeat) } }
789+
DisposableEffect(onContentUpFallbackChanged, calendarUpFallbackRegistration) {
790+
onContentUpFallbackChanged?.invoke(calendarUpFallbackRegistration)
791+
onDispose { onContentUpFallbackChanged?.invoke(calendarUpFallbackRegistration) }
782792
}
783793

784794
// The day-snap is the ONLY vertical scroller: with the default spec the
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.siloserver.silo.tv.ui.shell
2+
3+
/**
4+
* A held Up may traverse content, but it must not escape from the first
5+
* content row into the top menu after focus movement has already failed.
6+
*/
7+
internal fun shouldRequestMenuAfterContentUp(
8+
movedWithinContent: Boolean,
9+
isRepeat: Boolean,
10+
): Boolean = !movedWithinContent && !isRepeat

‎androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/shell/TvMainShell.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ fun TvMainShell(
805805
val isRepeat = ev.nativeKeyEvent.repeatCount > 0
806806
val contentHandledUp = contentUpFallback?.invoke(isRepeat)
807807
if (contentHandledUp != null) {
808-
if (!contentHandledUp) {
808+
if (shouldRequestMenuAfterContentUp(contentHandledUp, isRepeat)) {
809809
focusState.requestMenuFocusIfAvailable(
810810
selectedMenuFocusTarget,
811811
allowNullTarget = currentRoute == TvMainRoute.Search.route,
@@ -816,7 +816,7 @@ fun TvMainShell(
816816
// fails (we're already on the top row), hand
817817
// focus to the menu bar.
818818
val moved = focusManager.moveFocus(FocusDirection.Up)
819-
if (!moved) {
819+
if (shouldRequestMenuAfterContentUp(moved, isRepeat)) {
820820
focusState.requestMenuFocusIfAvailable(
821821
selectedMenuFocusTarget,
822822
allowNullTarget = currentRoute == TvMainRoute.Search.route,

‎androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/shell/TvTopMenuBar.kt‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import androidx.compose.runtime.LaunchedEffect
2626
import androidx.compose.runtime.getValue
2727
import androidx.compose.runtime.mutableStateOf
2828
import androidx.compose.runtime.remember
29+
import androidx.compose.runtime.rememberUpdatedState
2930
import androidx.compose.runtime.setValue
3031
import androidx.compose.ui.Alignment
3132
import androidx.compose.ui.ExperimentalComposeUiApi
@@ -229,20 +230,23 @@ fun TvTopMenuBar(
229230
// suppression lift (e.g. closing the profile dropdown, which flips
230231
// isFocusSuppressed false) does NOT also re-grab the selected tab and fight
231232
// the dedicated profile-avatar focus path below.
232-
var lastHandledFocusRequest by remember { mutableStateOf(0) }
233-
LaunchedEffect(focusRequest, isFocusSuppressed) {
233+
val currentFocusRequestTarget by rememberUpdatedState(focusRequestTarget)
234+
var lastHandledFocusRequest by remember { mutableStateOf<Pair<Int, TvTopMenuPanel?>>(0 to null) }
235+
val focusRequestIdentity = focusRequest to focusRequestTarget
236+
LaunchedEffect(focusRequest, focusRequestTarget, isFocusSuppressed) {
234237
if (isFocusSuppressed) return@LaunchedEffect
235-
if (focusRequest == lastHandledFocusRequest) return@LaunchedEffect
238+
if (focusRequestIdentity == lastHandledFocusRequest) return@LaunchedEffect
236239
val explicitFocus = focusRequestTarget?.let(::focusForPanel)
237240
dwellSuppressedButton = explicitFocus
238241
val requester = explicitFocus?.let(::requesterForFocus) ?: selectedEntryRequester()
239242
requestTopMenuFocusUntilApplied(
240243
awaitFrame = { androidx.compose.runtime.withFrameNanos { } },
244+
isTargetCurrent = { currentFocusRequestTarget == focusRequestTarget },
241245
requestFocus = {
242246
runCatching { requester.requestFocus() }.getOrDefault(false)
243247
},
244248
)
245-
lastHandledFocusRequest = focusRequest
249+
lastHandledFocusRequest = focusRequestIdentity
246250
}
247251

248252
LaunchedEffect(focusedButton) {

‎androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/shell/TvTopMenuFocusRequest.kt‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package org.siloserver.silo.tv.ui.shell
22

33
internal suspend fun requestTopMenuFocusUntilApplied(
44
awaitFrame: suspend () -> Unit,
5+
isTargetCurrent: () -> Boolean = { true },
56
requestFocus: () -> Boolean,
67
) {
7-
do {
8+
while (isTargetCurrent()) {
89
awaitFrame()
9-
} while (!requestFocus())
10+
if (!isTargetCurrent()) return
11+
if (requestFocus()) return
12+
}
1013
}

‎androidTvApp/src/androidUnitTest/kotlin/org/siloserver/silo/tv/ui/screens/calendar/TvCalendarFocusRoutingTest.kt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,17 @@ class TvCalendarFocusRoutingTest {
3737
fun returnInFlightDoesNotRestartChoreography() {
3838
assertFalse(shouldReturnCalendarFocusToControls(2, 2, true))
3939
}
40+
41+
@Test
42+
fun heldUpOnCalendarControlsStaysInContent() {
43+
assertEquals(
44+
CalendarUpFallbackAction.StayInContent,
45+
calendarUpFallbackAction(
46+
focusedShelfIndex = null,
47+
firstFocusableShelfIndex = 2,
48+
isReturningToControls = false,
49+
isRepeat = true,
50+
),
51+
)
52+
}
4053
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.siloserver.silo.tv.ui.shell
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertFalse
5+
import kotlin.test.assertTrue
6+
7+
class TvContentUpNavigationTest {
8+
@Test
9+
fun heldUpAfterContentCannotMoveStaysInContent() {
10+
assertFalse(shouldRequestMenuAfterContentUp(movedWithinContent = false, isRepeat = true))
11+
}
12+
13+
@Test
14+
fun freshUpAfterContentCannotMoveEntersMenu() {
15+
assertTrue(shouldRequestMenuAfterContentUp(movedWithinContent = false, isRepeat = false))
16+
}
17+
}

‎androidTvApp/src/androidUnitTest/kotlin/org/siloserver/silo/tv/ui/shell/TvTopMenuFocusRequestTest.kt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,22 @@ class TvTopMenuFocusRequestTest {
3333

3434
assertEquals(listOf("frame", "focus", "frame", "focus"), events)
3535
}
36+
37+
@Test
38+
fun retryStopsWhenItsTargetIsNoLongerCurrent() = runTest {
39+
val events = mutableListOf<String>()
40+
var targetIsCurrent = true
41+
42+
requestTopMenuFocusUntilApplied(
43+
awaitFrame = { events += "frame" },
44+
isTargetCurrent = { targetIsCurrent },
45+
requestFocus = {
46+
events += "focus"
47+
targetIsCurrent = false
48+
false
49+
},
50+
)
51+
52+
assertEquals(listOf("frame", "focus"), events)
53+
}
3654
}

0 commit comments

Comments
 (0)