mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-05 19:57:31 +00:00
110 lines
3.6 KiB
Swift
110 lines
3.6 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 empty space after the last row, so every row can scroll off the top.
|
||
var scrollsPastLastRow = false {
|
||
didSet {
|
||
if scrollsPastLastRow != oldValue {
|
||
tile()
|
||
}
|
||
}
|
||
}
|
||
|
||
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) {
|
||
guard scrollsPastLastRow, let clipView = enclosingScrollView?.contentView else {
|
||
super.setFrameSize(newSize)
|
||
return
|
||
}
|
||
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)))
|
||
}
|
||
|
||
// 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)
|
||
}
|
||
|
||
override func layout() {
|
||
super.layout()
|
||
rowHeightDelegate?.timelineTableViewDidLayout(self)
|
||
}
|
||
|
||
override func viewDidEndLiveResize() {
|
||
super.viewDidEndLiveResize()
|
||
rowHeightDelegate?.timelineTableViewDidEndLiveResize(self)
|
||
}
|
||
}
|