mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-05 19:57:31 +00:00
177 lines
4.7 KiB
Swift
177 lines
4.7 KiB
Swift
//
|
||
// 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)
|
||
])
|
||
}
|
||
}
|