diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index 40367ae12..e348611f6 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -791,6 +791,14 @@ extension MainWindowController: TimelineContainerViewControllerDelegate { invalidateRestorableState() } + func timelineNextSidebarItemWithUnreadArticles(_: TimelineContainerViewController) -> SidebarItem? { + sidebarViewController?.nextSidebarItemWithUnreadArticles(wrappingToTop: true) + } + + func timelineRequestedSidebarItemSelection(_: TimelineContainerViewController, sidebarItem: SidebarItem) { + sidebarViewController?.selectFeed(sidebarItem) + } + } // MARK: - NSSearchFieldDelegate diff --git a/Mac/MainWindow/Timeline/TimelineContainerViewController.swift b/Mac/MainWindow/Timeline/TimelineContainerViewController.swift index 44de1b688..c861b3be7 100644 --- a/Mac/MainWindow/Timeline/TimelineContainerViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineContainerViewController.swift @@ -14,6 +14,8 @@ import Articles func timelineSelectionDidChange(_: TimelineContainerViewController, articles: [Article]?, mode: TimelineSourceMode) func timelineRequestedFeedSelection(_: TimelineContainerViewController, feed: Feed) func timelineInvalidatedRestorationState(_: TimelineContainerViewController) + func timelineNextSidebarItemWithUnreadArticles(_: TimelineContainerViewController) -> SidebarItem? + func timelineRequestedSidebarItemSelection(_: TimelineContainerViewController, sidebarItem: SidebarItem) } final class TimelineContainerViewController: NSViewController { @@ -207,6 +209,18 @@ extension TimelineContainerViewController: TimelineDelegate { func timelineRequestedSortChange(_: TimelineViewController, parameters: ArticleSortParameters) { sortParameters = parameters } + + /// Search results don’t show the footer. + func timelineNextSidebarItemWithUnreadArticles(_ timelineViewController: TimelineViewController) -> SidebarItem? { + guard mode(for: timelineViewController) == .regular else { + return nil + } + return delegate?.timelineNextSidebarItemWithUnreadArticles(self) + } + + func timelineRequestedSidebarItemSelection(_: TimelineViewController, sidebarItem: SidebarItem) { + delegate?.timelineRequestedSidebarItemSelection(self, sidebarItem: sidebarItem) + } } private extension TimelineContainerViewController { diff --git a/Mac/MainWindow/Timeline/TimelineNextUnreadFooterView.swift b/Mac/MainWindow/Timeline/TimelineNextUnreadFooterView.swift new file mode 100644 index 000000000..36e916906 --- /dev/null +++ b/Mac/MainWindow/Timeline/TimelineNextUnreadFooterView.swift @@ -0,0 +1,176 @@ +// +// TimelineNextUnreadFooterView.swift +// NetNewsWire +// +// Created by Brent Simmons on 12/5/26. +// + +import AppKit + +/// After the last row of the timeline: a button that goes to the next feed or folder with unread articles. +final class TimelineNextUnreadFooterView: NSView { + + var action: (() -> Void)? { + get { + button.action + } + set { + button.action = newValue + } + } + + private let button = TimelineNextUnreadButton() + private lazy var leadingConstraint = button.leadingAnchor.constraint(equalTo: leadingAnchor) + private lazy var trailingConstraint = button.trailingAnchor.constraint(equalTo: trailingAnchor) + private static let verticalPadding: CGFloat = 24.0 + + override init(frame frameRect: NSRect) { + super.init(frame: frameRect) + commonInit() + } + + required init?(coder: NSCoder) { + super.init(coder: coder) + commonInit() + } + + var title: String { + get { + button.title + } + set { + button.title = newValue + } + } + + /// Lines the button up with the content of the timeline’s rows. + func setHorizontalInsets(leading: CGFloat, trailing: CGFloat) { + leadingConstraint.constant = leading + trailingConstraint.constant = -trailing + } +} + +private extension TimelineNextUnreadFooterView { + + func commonInit() { + button.translatesAutoresizingMaskIntoConstraints = false + addSubview(button) + // The table sets this view’s frame, which can be zero-sized — zero-height while hidden, zero-width while the table’s columns change. + // These two give way instead of conflicting. The fitting height still comes from the bottom constraint. + let bottomConstraint = button.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -Self.verticalPadding) + bottomConstraint.priority = .required - 1 + trailingConstraint.priority = .required - 1 + NSLayoutConstraint.activate([ + button.topAnchor.constraint(equalTo: topAnchor, constant: Self.verticalPadding), + bottomConstraint, + leadingConstraint, + trailingConstraint + ]) + } +} + +/// A flat, rounded button, quieter than a push button. +private final class TimelineNextUnreadButton: NSView { + + var action: (() -> Void)? + + var title: String { + get { + titleLabel.stringValue + } + set { + titleLabel.stringValue = newValue + setAccessibilityLabel(newValue) + } + } + + private let titleLabel = NSTextField(labelWithString: "") + private var isPressed = false { + didSet { + needsDisplay = true + } + } + private static let verticalPadding: CGFloat = 8.0 + private static let horizontalPadding: CGFloat = 20.0 + private static let cornerRadius: CGFloat = 10.0 + /// Softer than regular text, but not so faint it looks disabled, as secondary text does. + private static let titleAlpha: CGFloat = 0.8 + + override init(frame frameRect: NSRect) { + super.init(frame: frameRect) + commonInit() + } + + required init?(coder: NSCoder) { + super.init(coder: coder) + commonInit() + } + + // MARK: - NSView + + override var wantsUpdateLayer: Bool { + true + } + + /// A faint fill, so it’s easy to ignore. Darker while pressed. + override func updateLayer() { + let fillColor: NSColor = isPressed ? .quaternaryLabelColor : .quinaryLabelColor + layer?.backgroundColor = fillColor.cgColor + } + + /// Clicks on the label go to the button. + override func hitTest(_ point: NSPoint) -> NSView? { + frame.contains(point) ? self : nil + } + + // MARK: - NSResponder + + override func mouseDown(with event: NSEvent) { + isPressed = true + } + + override func mouseDragged(with event: NSEvent) { + isPressed = bounds.contains(convert(event.locationInWindow, from: nil)) + } + + override func mouseUp(with event: NSEvent) { + let wasPressed = isPressed + isPressed = false + if wasPressed { + action?() + } + } + + // MARK: - Accessibility + + override func accessibilityPerformPress() -> Bool { + action?() + return true + } +} + +private extension TimelineNextUnreadButton { + + func commonInit() { + wantsLayer = true + layer?.cornerRadius = Self.cornerRadius + layer?.cornerCurve = .continuous + + setAccessibilityElement(true) + setAccessibilityRole(.button) + + titleLabel.font = .systemFont(ofSize: NSFont.systemFontSize) + titleLabel.textColor = .labelColor.withAlphaComponent(Self.titleAlpha) + titleLabel.alignment = .center + titleLabel.lineBreakMode = .byTruncatingTail + titleLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) + titleLabel.translatesAutoresizingMaskIntoConstraints = false + addSubview(titleLabel) + NSLayoutConstraint.activate([ + titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: Self.verticalPadding), + titleLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -Self.verticalPadding), + titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: Self.horizontalPadding), + titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -Self.horizontalPadding) + ]) + } +} diff --git a/Mac/MainWindow/Timeline/TimelineTableView.swift b/Mac/MainWindow/Timeline/TimelineTableView.swift index 4f4a192ec..7101293b6 100644 --- a/Mac/MainWindow/Timeline/TimelineTableView.swift +++ b/Mac/MainWindow/Timeline/TimelineTableView.swift @@ -20,7 +20,8 @@ final class TimelineTableView: NSTableView { weak var keyboardDelegate: KeyboardDelegate? weak var rowHeightDelegate: TimelineTableViewRowHeightDelegate? - /// Adds a visible area’s height of empty space after the last row, so every row can scroll off the top. + /// Adds a visible area’s height of space after the last row, so every row can scroll off the top. + /// The footer view, if any, is at the top of that space. var scrollsPastLastRow = false { didSet { if scrollsPastLastRow != oldValue { @@ -29,6 +30,19 @@ final class TimelineTableView: NSTableView { } } + /// After the last row. Its height is its fitting height, or zero when hidden. + var footerView: NSView? { + didSet { + oldValue?.removeFromSuperview() + if let footerView { + addSubview(footerView) + } + footerViewDidChange() + } + } + + private var footerHeight: CGFloat = 0 + private var lastMouseDownClickCount = 0 private var lastMouseDownTimestamp: TimeInterval = 0 private var uncountedClicks = 0 @@ -72,14 +86,13 @@ final class TimelineTableView: NSTableView { } override func setFrameSize(_ newSize: NSSize) { - guard scrollsPastLastRow, let clipView = enclosingScrollView?.contentView else { - super.setFrameSize(newSize) - return + var spaceAfterRows = footerHeight + if scrollsPastLastRow, let clipView = enclosingScrollView?.contentView { + let clipBottom = convert(clipView.bounds, from: clipView).maxY + spaceAfterRows = max(spaceAfterRows, clipBottom - unobscuredTop) } - let clipBottom = convert(clipView.bounds, from: clipView).maxY - let unobscuredHeight = clipBottom - unobscuredTop - let rowsHeight = numberOfRows > 0 ? rect(ofRow: numberOfRows - 1).maxY : 0 - super.setFrameSize(NSSize(width: newSize.width, height: max(newSize.height, rowsHeight + unobscuredHeight))) + super.setFrameSize(NSSize(width: newSize.width, height: max(newSize.height, rowsHeight + spaceAfterRows))) + layoutFooterView() } // MARK: - API @@ -97,8 +110,22 @@ final class TimelineTableView: NSTableView { return max(clipTop, toolbarBottom) } + /// Call after changing the footer view’s contents or visibility. + func footerViewDidChange() { + var height: CGFloat = 0 + if let footerView, !footerView.isHidden { + height = footerView.fittingSize.height + } + if height != footerHeight { + footerHeight = height + tile() + } + layoutFooterView() + } + override func layout() { super.layout() + layoutFooterView() rowHeightDelegate?.timelineTableViewDidLayout(self) } @@ -107,3 +134,19 @@ final class TimelineTableView: NSTableView { rowHeightDelegate?.timelineTableViewDidEndLiveResize(self) } } + +private extension TimelineTableView { + + var rowsHeight: CGFloat { + numberOfRows > 0 ? rect(ofRow: numberOfRows - 1).maxY : 0 + } + + /// Spans the same width as the rows’ cells, so the footer can line up with their content. + func layoutFooterView() { + guard let footerView else { + return + } + let cellFrame = numberOfRows > 0 ? frameOfCell(atColumn: 0, row: 0) : rect(ofColumn: 0) + footerView.frame = NSRect(x: cellFrame.minX, y: rowsHeight, width: cellFrame.width, height: footerHeight) + } +} diff --git a/Mac/MainWindow/Timeline/TimelineViewController.swift b/Mac/MainWindow/Timeline/TimelineViewController.swift index 91f01e279..4499d4ae9 100644 --- a/Mac/MainWindow/Timeline/TimelineViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineViewController.swift @@ -19,6 +19,8 @@ import RSWeb func timelineRequestedFeedSelection(_: TimelineViewController, feed: Feed) func timelineInvalidatedRestorationState(_: TimelineViewController) func timelineRequestedSortChange(_: TimelineViewController, parameters: ArticleSortParameters) + func timelineNextSidebarItemWithUnreadArticles(_: TimelineViewController) -> SidebarItem? + func timelineRequestedSidebarItemSelection(_: TimelineViewController, sidebarItem: SidebarItem) } enum TimelineShowFeedName: Sendable { @@ -72,6 +74,7 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr seedReadFilterForFolders() unreadCount = 0 articleKeysMarkedReadOnScroll.removeAll() + updateNextUnreadFooter() selectionDidChange(nil) if showsSearchResults { @@ -298,6 +301,9 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr private var articlesPendingMarkReadOnScroll = [Article]() /// Marking waits for a pause in scrolling, so one Undo restores what one stretch of scrolling marked. private static let markReadOnScrollDelay: TimeInterval = 0.5 + private let nextUnreadFooterView = TimelineNextUnreadFooterView() + /// Shown in the footer after the last row. Nil hides the footer. + private var nextSidebarItemWithUnreadArticles: SidebarItem? /// How much of a row must be under the toolbar or above it to count as scrolled past. private static let scrolledPastFraction: CGFloat = 0.75 @@ -354,6 +360,10 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr tableView.rowHeightDelegate = self tableView.style = .inset tableView.gridColor = .separatorColor + nextUnreadFooterView.action = { [weak self] in + self?.selectNextSidebarItemWithUnreadArticles() + } + updateNextUnreadFooterInstallation() standardColumn = tableView.tableColumns.first configureTableView(for: layout) @@ -384,6 +394,7 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr NotificationCenter.default.addObserver(self, selector: #selector(handleTimelineFeedNameAndDateAboveTitleDidChange(_:)), name: .timelineFeedNameAndDateAboveTitleDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleTimelineFadeReadArticlesDidChange(_:)), name: .timelineFadeReadArticlesDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleMarkReadBehaviorDidChange(_:)), name: .markReadBehaviorDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleUnreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) if let scrollView = tableView.enclosingScrollView { NotificationCenter.default.addObserver(self, selector: #selector(handleBoundsDidChange(_:)), name: NSView.boundsDidChangeNotification, object: scrollView.contentView) NotificationCenter.default.addObserver(self, selector: #selector(handleWillStartLiveScroll(_:)), name: NSScrollView.willStartLiveScrollNotification, object: scrollView) @@ -898,6 +909,11 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr markReadBehavior = AppDefaults.markReadBehavior } + /// The sidebar updates on the same notification, so the footer waits until it has. + @objc func handleUnreadCountDidChange(_ notification: Notification) { + queueUpdateNextUnreadFooter() + } + @objc func handleBoundsDidChange(_ notification: Notification) { guard markReadOnScroll else { return @@ -1558,6 +1574,56 @@ extension TimelineViewController: NSTableViewDelegate { } } +// MARK: - Next Unread Footer + +private extension TimelineViewController { + + func queueUpdateNextUnreadFooter() { + NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(updateNextUnreadFooterAfterDelay), object: nil) + perform(#selector(updateNextUnreadFooterAfterDelay), with: nil, afterDelay: 0) + } + + @objc func updateNextUnreadFooterAfterDelay() { + updateNextUnreadFooter() + } + + /// Only the standard layout has the footer. + func updateNextUnreadFooterInstallation() { + tableView.footerView = layout == .standard ? nextUnreadFooterView : nil + updateNextUnreadFooter() + } + + /// The footer shows when another feed or folder has unread articles. + func updateNextUnreadFooter() { + guard isViewLoaded, layout == .standard else { + return + } + var sidebarItem: SidebarItem? + if let representedObjects, !representedObjects.isEmpty, let nextSidebarItem = delegate?.timelineNextSidebarItemWithUnreadArticles(self) { + let isCurrentTimeline = representedObjects.contains { ($0 as? SidebarItem)?.sidebarItemID == nextSidebarItem.sidebarItemID } + if !isCurrentTimeline { + sidebarItem = nextSidebarItem + } + } + nextSidebarItemWithUnreadArticles = sidebarItem + + if let sidebarItem { + // No unread count — the sidebar, usually visible on Mac, already shows it. + let titleFormat = NSLocalizedString("Next: %@", comment: "Button after the last article: goes to the next feed or folder with unread articles") + nextUnreadFooterView.title = String(format: titleFormat, sidebarItem.nameForDisplay) + } + nextUnreadFooterView.isHidden = sidebarItem == nil + tableView.footerViewDidChange() + } + + func selectNextSidebarItemWithUnreadArticles() { + guard let nextSidebarItemWithUnreadArticles else { + return + } + delegate?.timelineRequestedSidebarItemSelection(self, sidebarItem: nextSidebarItemWithUnreadArticles) + } +} + // MARK: - Private private extension TimelineViewController { @@ -1706,6 +1772,7 @@ private extension TimelineViewController { updateShowIcons() tableView.reloadData() } + updateNextUnreadFooterInstallation() if tableView.selectedRow != -1 { tableView.scrollRowToVisible(tableView.selectedRow) } @@ -1861,6 +1928,8 @@ private extension TimelineViewController { let iconSize = AppDefaults.timelineIconSize cellAppearance = TimelineCellAppearance(showIcon: false, textSize: textSize, iconSize: iconSize, numberOfLines: numberOfLines, showsFullTitles: showFullTitles, feedNameAndDateAboveTitle: feedNameAndDateAboveTitle, showsLargeThumbnails: thumbnailsDisplay == .large) cellAppearanceWithIcon = TimelineCellAppearance(showIcon: true, textSize: textSize, iconSize: iconSize, numberOfLines: numberOfLines, showsFullTitles: showFullTitles, feedNameAndDateAboveTitle: feedNameAndDateAboveTitle, showsLargeThumbnails: thumbnailsDisplay == .large) + // The button’s leading edge lines up with the rows’ feed icons. + nextUnreadFooterView.setHorizontalInsets(leading: cellAppearance.boxLeftMargin, trailing: cellAppearance.cellPadding.right) } private func cellAppearanceSettingDidChange() {