From f4f46282ae4741fe9cb72ae8784384876e67825d Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 5 Dec 2026 11:23:19 -0800 Subject: [PATCH] Navigate to end-of-timeline button using keyboard. Activate with return or space. --- Shared/Localizable.xcstrings | 6 + iOS/MainTimeline/Cell/MainTimelineCell.swift | 4 +- .../MainTimelineNextUnreadFooterView.swift | 14 +++ .../MainTimelineViewController.swift | 105 ++++++++++++++++-- 4 files changed, 118 insertions(+), 11 deletions(-) diff --git a/Shared/Localizable.xcstrings b/Shared/Localizable.xcstrings index dfe4e7b41..a41640447 100644 --- a/Shared/Localizable.xcstrings +++ b/Shared/Localizable.xcstrings @@ -273,6 +273,9 @@ "Automatic" : { "comment" : "Automatic" }, + "Back to Articles" : { + "comment" : "Keyboard command: moves keyboard focus from the Next button back to the articles" + }, "Behavior" : { "comment" : "Behavior" }, @@ -727,6 +730,9 @@ "Go to Feed" : { "comment" : "Go to Feed" }, + "Go to Next Feed or Folder" : { + "comment" : "Keyboard command: activates the Next button after the last article" + }, "Go To Settings" : { "comment" : "Go To Settings" }, diff --git a/iOS/MainTimeline/Cell/MainTimelineCell.swift b/iOS/MainTimeline/Cell/MainTimelineCell.swift index 35aedc348..9fe7f8645 100644 --- a/iOS/MainTimeline/Cell/MainTimelineCell.swift +++ b/iOS/MainTimeline/Cell/MainTimelineCell.swift @@ -248,9 +248,9 @@ private extension MainTimelineCell { setNeedsLayout() } - /// Matches the feeds list: accent background (and white text) when the timeline is first responder, gray otherwise. + /// Matches the feeds list: accent background (and white text) when the timeline has keyboard focus, gray otherwise. func isActiveSelection(_ state: UICellConfigurationState) -> Bool { - state.isSelected && isInExpandedSplitView && enclosingViewController?.isFirstResponder == true + state.isSelected && isInExpandedSplitView && (enclosingViewController as? MainTimelineViewController)?.showsActiveSelection == true } func updateColors(active: Bool) { diff --git a/iOS/MainTimeline/MainTimelineNextUnreadFooterView.swift b/iOS/MainTimeline/MainTimelineNextUnreadFooterView.swift index e8d006cb4..5d2e524b4 100644 --- a/iOS/MainTimeline/MainTimelineNextUnreadFooterView.swift +++ b/iOS/MainTimeline/MainTimelineNextUnreadFooterView.swift @@ -13,6 +13,19 @@ final class MainTimelineNextUnreadFooterView: UICollectionReusableView { var action: (() -> Void)? + /// A focus ring, for when Down Arrow from the last article moves keyboard focus to the button. + var showsKeyboardFocus = false { + didSet { + button.configuration?.background.strokeColor = showsKeyboardFocus ? .tintColor : nil + button.configuration?.background.strokeWidth = showsKeyboardFocus ? Self.focusRingWidth : 0 + } + } + + /// The button’s frame in the footer’s coordinates. + var buttonFrame: CGRect { + button.frame + } + /// The footer is always tall enough for the button, and at least this tall. var minimumHeight: CGFloat { get { @@ -29,6 +42,7 @@ final class MainTimelineNextUnreadFooterView: UICollectionReusableView { private static let verticalPadding: CGFloat = 24.0 private static let horizontalPadding: CGFloat = 20.0 private static let buttonContentInsets = NSDirectionalEdgeInsets(top: 14.0, leading: 20.0, bottom: 14.0, trailing: 20.0) + private static let focusRingWidth: CGFloat = 2.0 override init(frame: CGRect) { super.init(frame: frame) diff --git a/iOS/MainTimeline/MainTimelineViewController.swift b/iOS/MainTimeline/MainTimelineViewController.swift index 2bd9d7c22..67fcae537 100644 --- a/iOS/MainTimeline/MainTimelineViewController.swift +++ b/iOS/MainTimeline/MainTimelineViewController.swift @@ -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]) } }