Focus the delayed tvOS Cancel when it appears

By the time the button shows, focus has already been lost, and the
focus engine is not guaranteed to pick up an element inserted later.
This commit is contained in:
Arkadiusz Fal
2026-10-18 23:30:22 +02:00
parent 22ec48af3d
commit 827d10eda7

View File

@@ -61,16 +61,22 @@ struct TVStalledLoadCancelButton: View {
let action: () -> Void
@State private var isShown = false
@FocusState private var isFocused: Bool
var body: some View {
Group {
if isShown {
Button(String(localized: "common.cancel"), action: action)
.focused($isFocused)
}
}
.task {
guard (try? await Task.sleep(for: delay)) != nil else { return }
isShown = true
// Focus was already lost by the time the button appears, and the
// focus engine is not guaranteed to pick up a late arrival.
guard (try? await Task.sleep(for: .milliseconds(100))) != nil else { return }
isFocused = true
}
}
}