Files
NetNewsWire/Mac/MainWindow/Timeline/TimelineContainerViewController.swift
T

303 lines
9.8 KiB
Swift
Raw Normal View History

//
2019-02-14 21:58:45 -08:00
// TimelineContainerViewController.swift
// NetNewsWire
//
// Created by Brent Simmons on 2/14/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import AppKit
import Account
import Articles
2025-11-18 13:25:47 -08:00
@MainActor protocol TimelineContainerViewControllerDelegate: AnyObject {
func timelineSelectionDidChange(_: TimelineContainerViewController, articles: [Article]?, mode: TimelineSourceMode)
2025-11-03 08:55:25 -08:00
func timelineRequestedFeedSelection(_: TimelineContainerViewController, feed: Feed)
func timelineInvalidatedRestorationState(_: TimelineContainerViewController)
}
2019-02-16 22:14:06 -08:00
final class TimelineContainerViewController: NSViewController {
2025-11-18 22:22:38 -08:00
@IBOutlet var viewOptionsPopUpButton: NSPopUpButton!
@IBOutlet var readFilteredButton: NSButton!
2026-10-09 22:14:45 -07:00
@IBOutlet var headerSeparator: NSBox?
@IBOutlet var containerViewTopToHeaderConstraint: NSLayoutConstraint?
2019-02-16 22:14:06 -08:00
@IBOutlet var containerView: TimelineContainerView!
private var layout = AppDefaults.timelineLayout {
2026-09-19 10:26:29 -07:00
didSet {
if layout != oldValue {
layoutDidChange()
}
}
}
// Column layout hides the sort/filter header, and this pins the timeline to the top instead.
private lazy var containerViewTopToViewConstraint = containerView.topAnchor.constraint(equalTo: view.topAnchor)
2019-02-18 12:38:14 -08:00
var currentTimelineViewController: TimelineViewController? {
didSet {
let view = currentTimelineViewController?.view
if containerView.contentView === view {
return
}
containerView.contentView = view
view?.window?.recalculateKeyViewLoop()
2019-02-18 12:38:14 -08:00
}
}
2025-11-05 11:22:10 -08:00
var windowState: TimelineWindowState? {
currentTimelineViewController?.windowState
2026-09-18 21:47:51 -07:00
}
/// This window’s sort. Both timelines (regular and search) follow it.
private(set) var sortParameters = TimelineContainerViewController.seedSortParameters {
2026-09-18 21:47:51 -07:00
didSet {
if sortParameters == oldValue {
return
}
regularTimelineViewController.sortParameters = sortParameters
searchTimelineViewController.sortParameters = sortParameters
updateViewOptionsPopUpButton()
delegate?.timelineInvalidatedRestorationState(self)
}
}
2019-02-18 12:38:14 -08:00
weak var delegate: TimelineContainerViewControllerDelegate?
var isReadFiltered: Bool? {
guard let currentTimelineViewController = currentTimelineViewController, mode(for: currentTimelineViewController) == .regular else { return nil }
return regularTimelineViewController.isReadFiltered
}
var isCleanUpAvailable: Bool {
guard let currentTimelineViewController = currentTimelineViewController, mode(for: currentTimelineViewController) == .regular else { return false }
return regularTimelineViewController.isCleanUpAvailable
}
2025-11-05 11:22:10 -08:00
lazy var regularTimelineViewController = {
2026-09-18 21:47:51 -07:00
let viewController = TimelineViewController(delegate: self)
viewController.sortParameters = sortParameters
viewController.layout = layout
2026-09-18 21:47:51 -07:00
return viewController
2019-02-16 22:14:06 -08:00
}()
private lazy var searchTimelineViewController: TimelineViewController = {
let viewController = TimelineViewController(delegate: self)
viewController.showsSearchResults = true
2026-09-18 21:47:51 -07:00
viewController.sortParameters = sortParameters
viewController.layout = layout
return viewController
2019-02-16 22:14:06 -08:00
}()
2026-09-17 21:57:13 -07:00
convenience init() {
self.init(nibName: "TimelineContainerView", bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
2019-02-17 19:38:17 -08:00
setRepresentedObjects(nil, mode: .regular)
showTimeline(for: .regular)
2025-11-05 11:22:10 -08:00
// The first item is the pop-up’s hidden title.
for menuItem in viewOptionsPopUpButton.itemArray.dropFirst() where !menuItem.isSeparatorItem {
makeMenuItemTitleLarger(menuItem)
}
updateViewOptionsPopUpButton()
2026-09-19 10:26:29 -07:00
updateHeaderVisibility()
NotificationCenter.default.addObserver(self, selector: #selector(handleUserDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil)
}
2025-11-05 11:22:10 -08:00
2026-09-19 10:26:29 -07:00
// MARK: - Notifications
@objc nonisolated func handleUserDefaultsDidChange(_ note: Notification) {
Task { @MainActor in
self.userDefaultsDidChange()
}
}
private func userDefaultsDidChange() {
layout = AppDefaults.timelineLayout
2026-09-19 10:26:29 -07:00
}
2026-09-18 21:47:51 -07:00
// MARK: - API
2025-11-05 11:22:10 -08:00
/// Choosing the current field keeps its direction. Choosing another starts with that field’s natural direction.
func sortBy(key: ArticleSortKey) {
if key == sortParameters.key {
return
}
sortParameters = ArticleSortParameters(key: key, direction: key.firstDirection)
}
2025-11-05 11:22:10 -08:00
func setSortDirection(_ direction: ComparisonResult) {
sortParameters = sortParameters.withDirection(direction)
2026-09-18 21:47:51 -07:00
}
2019-02-16 22:14:06 -08:00
2019-02-17 19:38:17 -08:00
func setRepresentedObjects(_ objects: [AnyObject]?, mode: TimelineSourceMode) {
timelineViewController(for: mode).representedObjects = objects
updateReadFilterButton()
2019-02-16 22:14:06 -08:00
}
func showTimeline(for mode: TimelineSourceMode) {
2019-02-18 12:38:14 -08:00
currentTimelineViewController = timelineViewController(for: mode)
}
func regularTimelineViewControllerHasRepresentedObjects(_ representedObjects: [AnyObject]?) -> Bool {
// Use this to find out if the regular timeline view already has the specified representedObjects.
// This is used in determining whether a search should end.
// The sidebar may think that the selection has changed, and therefore search should end —
// but it could be that the regular timeline already has these representedObjects,
// and therefore the selection hasn’t actually changed,
// and therefore search shouldn’t end.
// https://github.com/brentsimmons/NetNewsWire/issues/791
if representedObjects == nil && regularTimelineViewController.representedObjects == nil {
return true
}
guard let currentObjects = regularTimelineViewController.representedObjects, let representedObjects = representedObjects else {
return false
}
if currentObjects.count != representedObjects.count {
return false
}
for object in representedObjects {
2026-01-02 17:50:35 -08:00
if currentObjects.firstIndex(where: { $0 === object }) == nil {
return false
}
}
return true
2019-02-16 22:14:06 -08:00
}
2025-11-05 11:22:10 -08:00
2020-03-15 18:02:55 -05:00
func cleanUp() {
regularTimelineViewController.cleanUp()
}
2025-11-05 11:22:10 -08:00
func toggleReadFilter() {
regularTimelineViewController.toggleReadFilter()
updateReadFilterButton()
}
2025-11-05 11:22:10 -08:00
// MARK: State Restoration
2025-11-05 11:22:10 -08:00
func restoreState(from state: TimelineWindowState?) {
guard let state else { return }
2025-11-05 11:22:10 -08:00
2026-09-18 21:47:51 -07:00
// Sort first so the restored selection lands in the right row.
if let savedSortParameters = state.sortParameters {
sortParameters = savedSortParameters
2026-09-18 21:47:51 -07:00
}
regularTimelineViewController.restoreState(from: state)
updateReadFilterButton()
}
/// Restore state using legacy state restoration data.
///
/// TODO: Delete for NetNewsWire 7.
func restoreLegacyState(from state: [AnyHashable: Any]) {
regularTimelineViewController.restoreLegacyState(from: state)
updateReadFilterButton()
}
2019-02-16 22:14:06 -08:00
}
extension TimelineContainerViewController: TimelineDelegate {
func timelineSelectionDidChange(_ timelineViewController: TimelineViewController, selectedArticles: [Article]?) {
delegate?.timelineSelectionDidChange(self, articles: selectedArticles, mode: mode(for: timelineViewController))
2019-02-16 22:14:06 -08:00
}
2025-11-03 09:32:50 -08:00
func timelineRequestedFeedSelection(_: TimelineViewController, feed: Feed) {
delegate?.timelineRequestedFeedSelection(self, feed: feed)
}
2025-11-05 11:22:10 -08:00
func timelineInvalidatedRestorationState(_: TimelineViewController) {
delegate?.timelineInvalidatedRestorationState(self)
}
2025-11-05 11:22:10 -08:00
2026-09-19 10:26:29 -07:00
func timelineRequestedSortChange(_: TimelineViewController, parameters: ArticleSortParameters) {
sortParameters = parameters
}
2019-02-16 22:14:06 -08:00
}
private extension TimelineContainerViewController {
func makeMenuItemTitleLarger(_ menuItem: NSMenuItem) {
menuItem.attributedTitle = NSAttributedString(string: menuItem.title,
attributes: [NSAttributedString.Key.font: NSFont.controlContentFont(ofSize: NSFont.systemFontSize)])
}
2025-11-05 11:22:10 -08:00
2019-02-16 22:14:06 -08:00
func timelineViewController(for mode: TimelineSourceMode) -> TimelineViewController {
switch mode {
case .regular:
return regularTimelineViewController
case .search:
return searchTimelineViewController
}
}
func mode(for timelineViewController: TimelineViewController) -> TimelineSourceMode {
if timelineViewController === regularTimelineViewController {
return .regular
2026-01-02 11:57:55 -08:00
} else if timelineViewController === searchTimelineViewController {
return .search
}
assertionFailure("Expected timelineViewController to match either regular or search timelineViewController, but it doesn’t.")
return .regular // Should never get here.
}
2025-11-05 11:22:10 -08:00
// The pop-up’s items share the View menu’s actions, so their checkmarks and direction titles come from
// MainWindowController’s validation. Only the button title is set here.
2025-11-17 22:09:53 -08:00
func updateViewOptionsPopUpButton() {
2026-09-18 21:47:51 -07:00
guard isViewLoaded else {
return
}
viewOptionsPopUpButton.setTitle(sortParameters.key.localizedName)
}
2025-11-05 11:22:10 -08:00
/// For a window with no saved sort. Group By Feed, from before sorting was per window, became sorting by feed.
static var seedSortParameters: ArticleSortParameters {
if AppDefaults.timelineGroupByFeed {
return ArticleSortParameters(key: .feed, direction: .orderedAscending)
}
return ArticleSortParameters(key: .date, direction: AppDefaults.timelineSortDirection)
}
2025-11-05 11:22:10 -08:00
2026-09-19 10:26:29 -07:00
func layoutDidChange() {
regularTimelineViewController.layout = layout
searchTimelineViewController.layout = layout
2026-09-19 10:26:29 -07:00
updateHeaderVisibility()
}
func updateHeaderVisibility() {
let isHeaderHidden = layout == .column
viewOptionsPopUpButton.isHidden = isHeaderHidden
2026-10-09 22:14:45 -07:00
headerSeparator?.isHidden = isHeaderHidden
containerViewTopToHeaderConstraint?.isActive = !isHeaderHidden
2026-09-19 10:26:29 -07:00
containerViewTopToViewConstraint.isActive = isHeaderHidden
updateReadFilterButton()
}
func updateReadFilterButton() {
2026-09-19 10:26:29 -07:00
guard layout == .standard else {
readFilteredButton.isHidden = true
return
}
2020-05-02 13:25:48 -05:00
guard currentTimelineViewController == regularTimelineViewController else {
readFilteredButton.isHidden = true
return
}
2025-11-05 11:22:10 -08:00
guard let isReadFiltered = regularTimelineViewController.isReadFiltered else {
readFilteredButton.isHidden = true
return
}
2025-11-05 11:22:10 -08:00
readFilteredButton.isHidden = false
2025-11-05 11:22:10 -08:00
if isReadFiltered {
2025-11-19 10:38:18 -08:00
readFilteredButton.image = Assets.Images.filterActive
} else {
2025-11-19 10:38:18 -08:00
readFilteredButton.image = Assets.Images.filterInactive
}
}
2025-11-05 11:22:10 -08:00
}