mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-06 04:07:54 +00:00
153 lines
4.7 KiB
Swift
153 lines
4.7 KiB
Swift
//
|
||
// TimelineTableView.swift
|
||
// NetNewsWire
|
||
//
|
||
// Created by Brent Simmons on 10/11/16.
|
||
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
||
//
|
||
|
||
import AppKit
|
||
import RSCore
|
||
|
||
/// Measures row heights for rows about to be shown, so they draw at their measured heights.
|
||
@MainActor protocol TimelineTableViewRowHeightDelegate: AnyObject {
|
||
func timelineTableViewDidLayout(_ tableView: TimelineTableView)
|
||
func timelineTableViewDidEndLiveResize(_ tableView: TimelineTableView)
|
||
}
|
||
|
||
final class TimelineTableView: NSTableView {
|
||
|
||
weak var keyboardDelegate: KeyboardDelegate?
|
||
weak var rowHeightDelegate: TimelineTableViewRowHeightDelegate?
|
||
|
||
/// 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 {
|
||
tile()
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 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
|
||
|
||
override func accessibilityLabel() -> String? {
|
||
return NSLocalizedString("Timeline", comment: "Timeline")
|
||
}
|
||
|
||
// MARK: - NSResponder
|
||
|
||
/// On macOS 27 the click that activates an inactive window doesn’t reach the table,
|
||
/// but the next click still counts it — so selecting an article would also open it.
|
||
/// Subtract clicks the table never received from `clickCount`.
|
||
/// <https://github.com/Ranchero-Software/NetNewsWire/issues/5584>
|
||
override func mouseDown(with event: NSEvent) {
|
||
let continuesClickSequence = event.clickCount == lastMouseDownClickCount + 1 && event.timestamp - lastMouseDownTimestamp <= NSEvent.doubleClickInterval
|
||
if !continuesClickSequence {
|
||
uncountedClicks = event.clickCount - 1
|
||
}
|
||
lastMouseDownClickCount = event.clickCount
|
||
lastMouseDownTimestamp = event.timestamp
|
||
|
||
guard uncountedClicks > 0, let adjustedEvent = NSEvent.mouseEvent(with: event.type, location: event.locationInWindow, modifierFlags: event.modifierFlags, timestamp: event.timestamp, windowNumber: event.windowNumber, context: nil, eventNumber: event.eventNumber, clickCount: event.clickCount - uncountedClicks, pressure: event.pressure) else {
|
||
super.mouseDown(with: event)
|
||
return
|
||
}
|
||
super.mouseDown(with: adjustedEvent)
|
||
}
|
||
|
||
override func keyDown(with event: NSEvent) {
|
||
if keyboardDelegate?.keydown(event, in: self) ?? false {
|
||
return
|
||
}
|
||
super.keyDown(with: event)
|
||
}
|
||
|
||
// MARK: - NSView
|
||
|
||
override var isOpaque: Bool {
|
||
return true
|
||
}
|
||
|
||
override func setFrameSize(_ newSize: NSSize) {
|
||
var spaceAfterRows = footerHeight
|
||
if scrollsPastLastRow, let clipView = enclosingScrollView?.contentView {
|
||
let clipBottom = convert(clipView.bounds, from: clipView).maxY
|
||
spaceAfterRows = max(spaceAfterRows, clipBottom - unobscuredTop)
|
||
}
|
||
super.setFrameSize(NSSize(width: newSize.width, height: max(newSize.height, rowsHeight + spaceAfterRows)))
|
||
layoutFooterView()
|
||
}
|
||
|
||
// MARK: - API
|
||
|
||
/// The top of the visible area not under the toolbar, in this view’s coordinates.
|
||
var unobscuredTop: CGFloat {
|
||
guard let clipView = enclosingScrollView?.contentView else {
|
||
return visibleRect.minY
|
||
}
|
||
let clipTop = convert(clipView.bounds, from: clipView).minY
|
||
guard let window else {
|
||
return clipTop
|
||
}
|
||
let toolbarBottom = convert(NSPoint(x: 0, y: window.contentLayoutRect.maxY), from: nil).y
|
||
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)
|
||
}
|
||
|
||
override func viewDidEndLiveResize() {
|
||
super.viewDidEndLiveResize()
|
||
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)
|
||
}
|
||
}
|