A viewer presses Back after a film. Focus lands on a different poster because the shelf changed while the film played.

The shelf in Swiftfin, Jellyfin’s Apple TV client, gives us a concrete workflow for testing that return trip.

Here is how it goes wrong. A viewer starts the fourth title in a shelf. While the film plays, a library refresh inserts a new title at the front. The app restores focus to index three. Index three is now a different film.

Nothing failed. The index was correct when it was saved and wrong when it was used. The app remembered a position in a list that had changed underneath it, and position was never what the viewer chose.

What the viewer chose was a title. The authority over where focus lands is the focus engine, and the app’s job is to tell it, in terms of identity, what it would prefer.

Selection and focus are different facts

Selection records the title the viewer chose. Focus records which control currently accepts input. During playback, focus belongs to the player. The shelf still has to remember where the viewer came from, so it can express a preference when it becomes visible again.

The example below uses Swiftfin’s browsing workflow as a reference. The code and data model are original.

SwiftGitHub
import AVKit
import SwiftUI

struct ShelfTitle: Identifiable {
    let id: String
    let title: String
    let mediaURL: URL
}

struct VideoShelf: View {
    let titles: [ShelfTitle]
    @Namespace private var shelfFocus
    @Environment(\.resetFocus) private var resetFocus
    @State private var playing: ShelfTitle?
    @State private var lastPlayedID: ShelfTitle.ID?

    private var preferredID: ShelfTitle.ID? {
        titles.first { $0.id == lastPlayedID }?.id ?? titles.first?.id
    }

    var body: some View {
        ScrollViewReader { proxy in
            ScrollView(.horizontal) {
                LazyHStack(spacing: 28) {
                    ForEach(titles) { item in
                        Button(item.title) {
                            lastPlayedID = item.id
                            playing = item
                        }
                        .id(item.id)
                        .prefersDefaultFocus(item.id == preferredID, in: shelfFocus)
                    }
                }
            }
            .focusScope(shelfFocus)
            .fullScreenCover(item: $playing, onDismiss: {
                if let preferredID { proxy.scrollTo(preferredID, anchor: .center) }
                resetFocus(in: shelfFocus)
            }) { item in
                ShelvedPlayback(url: item.mediaURL)
            }
        }
    }
}

private struct ShelvedPlayback: View {
    let url: URL
    @State private var player = AVPlayer()

    var body: some View {
        VideoPlayer(player: player)
            .task(id: url) {
                player.replaceCurrentItem(with: AVPlayerItem(url: url))
                player.play()
            }
            .onDisappear { player.pause() }
    }
}

Three decisions carry the behavior.

The preference is an identity, resolved against the current shelf. preferredID looks the last-played title up in whatever titles contains now. If the library refresh moved it, the preference moves with it. If the refresh removed it, the fallback is the first title, chosen deliberately rather than by accident.

The scroll position is restored before focus is re-evaluated. A lazy shelf creates only the views near the visible region. If the preferred title is fifty posters away, there is no view for the focus engine to prefer. scrollTo brings it into existence; resetFocus(in:) then asks the engine to re-evaluate the default inside the shelf’s scope.

Restoration happens on the return transition, and only then. It is tempting to re-assert focus whenever artwork loads or a fetch completes. Each of those moments can arrive while the viewer is already moving through the shelf, and yanking focus from under a remote in motion is worse than not restoring it at all.

The player here belongs to the playback presentation and stops when it disappears. An app that keeps audio playing after the player closes would give playback a longer-lived owner. The focus preference still belongs to the shelf.

Decide the fallbacks before you need them

The last-played identity has a scope: the active library and the signed-in profile. A different profile may have a different library in which the same ID means nothing, or something else. Key the remembered identity by both, and clear it when either changes.

Then write down where focus goes in each case:

  • the title is still in the shelf: that title;
  • the title was removed during playback: its former neighbor, or the first title, by a rule you chose;
  • the whole shelf is gone: let the parent focus scope choose a reachable heading or the neighboring shelf.

Focus must always have somewhere useful to go. A tvOS screen where nothing is focused is a screen the remote cannot operate.

Test the return path with a remote in hand

The simulator shows where the posters landed. Only the remote shows whether the viewer can continue.

  • Start a title that requires horizontal scrolling, press Back, and confirm the same title is focused.
  • Insert titles before it during playback, press Back, and confirm the same title, not the same index.
  • Remove it during playback and confirm the defined fallback.
  • Switch profile, return, and confirm the previous profile’s choice is gone.
  • Repeat with VoiceOver on, where a wrong focus is also a wrong announcement.

Run the sequence on an Apple TV, with the Siri Remote. Swipe speed, click versus touch, and the engine’s own heuristics behave differently there than with arrow keys in the simulator.

What to ask of your own code

  • Does the app remember a position, or an identity?
  • Is the remembered identity scoped to the library and the profile?
  • In a lazy container, does the preferred view exist when focus is evaluated?
  • Does anything move focus while the viewer is already navigating?
  • Where does focus go when the remembered title no longer exists?

The viewer pressed Back to continue where they were. The app does not decide where that is. It only has to remember what they chose, and say so to the engine that does.

References

If this edition sharpened your next review, you can buy me a coffee.