mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-04 03:08:43 +00:00
75 lines
2.4 KiB
Swift
75 lines
2.4 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?
|
||
|
||
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 layout() {
|
||
super.layout()
|
||
rowHeightDelegate?.timelineTableViewDidLayout(self)
|
||
}
|
||
|
||
override func viewDidEndLiveResize() {
|
||
super.viewDidEndLiveResize()
|
||
rowHeightDelegate?.timelineTableViewDidEndLiveResize(self)
|
||
}
|
||
}
|