Navigate to end-of-timeline button using keyboard. Activate with return or space.

This commit is contained in:
Brent Simmons committed 2026-12-05 11:23:19 -08:00
1 parent fa5a116cff
commit f4f46282ae
4 files changed
+118 -11

No files matched your search

@@ -40,6 +40,15 @@ final class MainTimelineViewController: UIViewController, UndoableCommandRunner
private var articlesPendingMarkReadOnScroll = [Article]()
/// Shown in the footer after the last row. Nil hides the footer’s button.
private var nextSidebarItemWithUnreadArticles: SidebarItem?
/// Down Arrow from the last article moves keyboard focus to the footer’s button, shown with a focus ring.
private var isNextUnreadButtonFocused = false {
didSet {
if isNextUnreadButtonFocused != oldValue {
updateVisibleNextUnreadFooters()
updateVisibleCellConfigurations()
}
}
}
/// When marking read on scroll, the footer after the last row is as tall as the area between the navigation bar and the toolbar, so every row can scroll under the navigation bar.
private var scrolledPastFooterHeight: CGFloat = 0
/// The article whose row was just swiped to change its status.
@@ -160,13 +169,29 @@ final class MainTimelineViewController: UIViewController, UndoableCommandRunner
// the WKWebViews built in hardware keyboard shortcuts, specifically the up and down arrow keys.
guard let current = UIResponder.currentFirstResponder, !(current is ArticleWebView) else { return nil }
return keyboardManager.keyCommands
guard isNextUnreadButtonFocused else {
return keyboardManager.keyCommands
}
// While the Next button has focus, its keys replace the commands that use the same keys.
let buttonKeyCommands = nextUnreadButtonKeyCommands
let buttonInputs = Set(buttonKeyCommands.compactMap { $0.input })
let otherKeyCommands = keyboardManager.keyCommands.filter { keyCommand in
guard let input = keyCommand.input else {
return true
}
return !(keyCommand.modifierFlags.isEmpty && buttonInputs.contains(input))
}
return buttonKeyCommands + otherKeyCommands
}
override var canBecomeFirstResponder: Bool {
true
}
// The selected row is accent-colored only while the timeline is first responder.
/// The selected row is accent-colored only while the timeline has keyboard focus — first responder, without the Next button focused.
var showsActiveSelection: Bool {
isFirstResponder && !isNextUnreadButtonFocused
}
@discardableResult override func becomeFirstResponder() -> Bool {
let didBecomeFirstResponder = super.becomeFirstResponder()
updateVisibleCellConfigurations()
@@ -364,6 +389,9 @@ final class MainTimelineViewController: UIViewController, UndoableCommandRunner
guard isViewLoaded else {
return
}
if resetScroll {
isNextUnreadButtonFocused = false
}
resetUI(resetScroll: resetScroll)
restoreSelectionIfNecessary(adjustScroll: false)
updateNextUnreadFooter()
@@ -383,6 +411,7 @@ final class MainTimelineViewController: UIViewController, UndoableCommandRunner
guard isViewLoaded, let collectionView, let dataSource else {
return
}
isNextUnreadButtonFocused = false
if let article = currentArticle,
let indexPath = dataSource.indexPath(for: article), let indexPaths = collectionView.indexPathsForSelectedItems {
@@ -518,12 +547,33 @@ final class MainTimelineViewController: UIViewController, UndoableCommandRunner
@objc func selectNextUp(_ sender: Any?) {
assert(coordinator != nil)
if isNextUnreadButtonFocused {
isNextUnreadButtonFocused = false
return
}
coordinator?.selectPrevArticle()
}
@objc func selectNextDown(_ sender: Any?) {
assert(coordinator != nil)
coordinator?.selectNextArticle()
guard let coordinator else {
return
}
// Down Arrow from the last article moves on to the Next button, when it’s showing.
if coordinator.currentArticle != nil, !coordinator.isNextArticleAvailable, nextSidebarItemWithUnreadArticles != nil {
isNextUnreadButtonFocused = true
scrollNextUnreadButtonToVisible()
return
}
coordinator.selectNextArticle()
}
@objc func activateNextUnreadButton(_ sender: Any?) {
selectNextSidebarItemWithUnreadArticles()
}
@objc func unfocusNextUnreadButton(_ sender: Any?) {
isNextUnreadButtonFocused = false
}
@objc func navigateToSidebar(_ sender: Any?) {
@@ -1003,23 +1053,27 @@ private extension MainTimelineViewController {
let listConfig = config
let section = NSCollectionLayoutSection.list(using: listConfig, layoutEnvironment: layoutEnvironment)
// Plain lists pin footers to the visible area. The footer belongs after the last row.
for boundarySupplementaryItem in section.boundarySupplementaryItems {
boundarySupplementaryItem.pinToVisibleBounds = false
}
/// Note to future self: apply insets that affect cell width
/// calculations (leading swipe actions with sidebar visible)
let sidebarOverlapWidth = layoutEnvironment.container.contentInsets.leading
// container.contentSize includes the width under the sidebar.
let remainingWidth = layoutEnvironment.container.contentSize.width - sidebarOverlapWidth
let leadingInset = remainingWidth > 0 ? sidebarOverlapWidth : 0
section.contentInsets = NSDirectionalEdgeInsets(
top: 0,
leading: remainingWidth > 0 ? sidebarOverlapWidth : 0,
leading: leadingInset,
bottom: 0,
trailing: 0
)
for boundarySupplementaryItem in section.boundarySupplementaryItems {
// Plain lists pin footers to the visible area. The footer belongs after the last row.
boundarySupplementaryItem.pinToVisibleBounds = false
// The section’s inset doesn’t apply to the footer, so it gets its own, to keep it out from under the sidebar.
boundarySupplementaryItem.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: leadingInset, bottom: 0, trailing: 0)
}
return section
}
layout.configuration.contentInsetsReference = .safeArea
@@ -1241,7 +1295,7 @@ private extension MainTimelineViewController {
/// The footer’s button shows when another feed or folder has unread articles.
func updateNextUnreadFooter() {
guard isViewLoaded, let collectionView else {
guard isViewLoaded else {
return
}
var sidebarItem: SidebarItem?
@@ -1250,11 +1304,42 @@ private extension MainTimelineViewController {
sidebarItem = nextSidebarItem
}
nextSidebarItemWithUnreadArticles = sidebarItem
if sidebarItem == nil {
isNextUnreadButtonFocused = false
}
updateVisibleNextUnreadFooters()
}
func updateVisibleNextUnreadFooters() {
guard isViewLoaded, let collectionView else {
return
}
for case let footerView as MainTimelineNextUnreadFooterView in collectionView.visibleSupplementaryViews(ofKind: UICollectionView.elementKindSectionFooter) {
configureNextUnreadFooter(footerView)
}
}
var nextUnreadButtonKeyCommands: [UIKeyCommand] {
let activateTitle = NSLocalizedString("Go to Next Feed or Folder", comment: "Keyboard command: activates the Next button after the last article")
let unfocusTitle = NSLocalizedString("Back to Articles", comment: "Keyboard command: moves keyboard focus from the Next button back to the articles")
let activateInputs = ["\r", " "]
var keyCommands = activateInputs.map { KeyboardManager.createKeyCommand(title: activateTitle, action: "activateNextUnreadButton:", input: $0, modifiers: []) }
keyCommands.append(KeyboardManager.createKeyCommand(title: unfocusTitle, action: "unfocusNextUnreadButton:", input: UIKeyCommand.inputEscape, modifiers: []))
return keyCommands
}
/// The footer can be a screen tall when marking read on scroll, so this scrolls to the button at its top.
func scrollNextUnreadButtonToVisible() {
guard let collectionView, collectionView.numberOfSections > 0, let attributes = collectionView.layoutAttributesForSupplementaryElement(ofKind: UICollectionView.elementKindSectionFooter, at: IndexPath(item: 0, section: 0)) else {
return
}
collectionView.scrollRectToVisible(CGRect(x: attributes.frame.minX, y: attributes.frame.minY, width: attributes.frame.width, height: 1), animated: false)
collectionView.layoutIfNeeded()
for case let footerView as MainTimelineNextUnreadFooterView in collectionView.visibleSupplementaryViews(ofKind: UICollectionView.elementKindSectionFooter) {
collectionView.scrollRectToVisible(footerView.convert(footerView.buttonFrame, to: collectionView), animated: true)
}
}
/// The footer’s height depends on the Mark as Read setting.
func invalidateLayoutForNextUnreadFooter() {
guard isViewLoaded, let collectionView else {
@@ -1265,6 +1350,7 @@ private extension MainTimelineViewController {
func configureNextUnreadFooter(_ footerView: MainTimelineNextUnreadFooterView) {
footerView.minimumHeight = markReadBehavior == .selectionAndScrolledPast ? scrolledPastFooterHeight : 0
footerView.showsKeyboardFocus = isNextUnreadButtonFocused
// “12 unread” only when unread counts are shown as numbers.
let showUnreadCount = AppDefaults.unreadCountDisplay == .count
guard let sidebarItem = nextSidebarItemWithUnreadArticles else {
@@ -1311,6 +1397,7 @@ private extension MainTimelineViewController {
guard let sidebarItem = nextSidebarItemWithUnreadArticles else {
return
}
isNextUnreadButtonFocused = false
coordinator?.selectFeed(sidebarItem, animations: [.navigation, .scroll])
}
}