From b91321722d70550a45f327db19fcabd2077de7a6 Mon Sep 17 00:00:00 2001 From: Michael Edlund Date: Mon, 22 Dec 2025 19:44:10 -0800 Subject: [PATCH] Preserve first responder position when children are replaced If a Control's children need to be rebuilt (for example, during ForEach updates when the underlying data changes), the first responder would always reset to the first selectable element. This caused jarring UX in dynamic lists where the selection would jump to the top after any update. With this proposed change, Control tracks which child index contained the first responder before removal. When adding new children with firstResponder == nil, it attempts to restore the first responder at that same index. If the index is out of bounds (e.g., the list got shorter after a deletion), it decrements until finding a valid selectable element. This provides intuitive behavior for: - Lists where items are deleted (selection stays near same position) - Dynamic content that refreshes (selection doesn't jump to top) - Any view rebuild that replaces children This change should be backwards compatible because it only changes behavior in cases where firstResponder is nil, which is when the selection would have been reset anyway. --- Sources/SwiftTUI/Controls/Control.swift | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Sources/SwiftTUI/Controls/Control.swift b/Sources/SwiftTUI/Controls/Control.swift index 4fff1e48..06b80d41 100644 --- a/Sources/SwiftTUI/Controls/Control.swift +++ b/Sources/SwiftTUI/Controls/Control.swift @@ -8,6 +8,9 @@ class Control: LayerDrawing { private var index: Int = 0 + // Track the index of the first responder child to restore it after rebuilds + private var preferredFirstResponderIndex: Int? + var window: Window? private(set) lazy var layer: Layer = makeLayer() @@ -22,6 +25,23 @@ class Control: LayerDrawing { children[i].index = i } if let window = root.window, window.firstResponder == nil { + // Try to restore first responder at the preferred index with decrement fallback + if let preferredIndex = preferredFirstResponderIndex { + var tryIndex = preferredIndex + while tryIndex >= 0 && tryIndex < children.count { + if let responder = children[tryIndex].firstSelectableElement { + window.firstResponder = responder + responder.becomeFirstResponder() + preferredFirstResponderIndex = nil // Clear after use + return + } + tryIndex -= 1 + } + // If we exhausted all indices, clear the preference and fall through + preferredFirstResponderIndex = nil + } + + // Fallback: use the newly added view's first selectable element if let responder = view.firstSelectableElement { window.firstResponder = responder responder.becomeFirstResponder() @@ -31,6 +51,9 @@ class Control: LayerDrawing { func removeSubview(at index: Int) { if children[index].isFirstResponder || root.window?.firstResponder?.isDescendant(of: children[index]) == true { + // Save the index before removing so we can try to restore it later + preferredFirstResponderIndex = index + root.window?.firstResponder?.resignFirstResponder() root.window?.firstResponder = selectableElement(above: index) ?? selectableElement(below: index) root.window?.firstResponder?.becomeFirstResponder()