Show feed names and favicons in timeline when appropriate.

I don’t quite like this. Maybe avatars are better.
This commit is contained in:
Brent Simmons 2017-12-30 12:45:10 -08:00
parent f4b33c6afb
commit befc5efc77
5 changed files with 75 additions and 45 deletions

View File

@ -28,7 +28,7 @@ struct TimelineCellData {
let featuredImage: NSImage? // image from within the article let featuredImage: NSImage? // image from within the article
let read: Bool let read: Bool
init(article: Article, appearance: TimelineCellAppearance, showFeedName: Bool, favicon: NSImage?, avatar: NSImage?, featuredImage: NSImage?) { init(article: Article, appearance: TimelineCellAppearance, showFeedName: Bool, feedName: String?, favicon: NSImage?, avatar: NSImage?, featuredImage: NSImage?) {
self.title = timelineTruncatedTitle(article) self.title = timelineTruncatedTitle(article)
self.text = timelineTruncatedSummary(article) self.text = timelineTruncatedSummary(article)
@ -51,13 +51,13 @@ struct TimelineCellData {
attributedDateCache[self.dateString] = self.attributedDateString attributedDateCache[self.dateString] = self.attributedDateString
} }
if let feed = article.feed { if let feedName = feedName {
self.feedName = timelineTruncatedFeedName(feed) self.feedName = timelineTruncatedFeedName(feedName)
} }
else { else {
self.feedName = "" self.feedName = ""
} }
if let s = attributedFeedNameCache[self.dateString] { if let s = attributedFeedNameCache[self.feedName] {
self.attributedFeedName = s self.attributedFeedName = s
} }
else { else {

View File

@ -10,11 +10,6 @@ import Cocoa
import RSTextDrawing import RSTextDrawing
import RSCore import RSCore
// title/text 1 date
// title/text 2
// title/text 3
// favicon feedname (optional line)
struct TimelineCellLayout { struct TimelineCellLayout {
let width: CGFloat let width: CGFloat
@ -68,7 +63,7 @@ private func rectForDate(_ cellData: TimelineCellData, _ width: CGFloat, _ appea
return r return r
} }
private func rectForFeedName(_ cellData: TimelineCellData, _ width: CGFloat, _ appearance: TimelineCellAppearance, _ titleRect: NSRect) -> NSRect { private func rectForFeedName(_ cellData: TimelineCellData, _ width: CGFloat, _ appearance: TimelineCellAppearance, _ dateRect: NSRect) -> NSRect {
if !cellData.showFeedName { if !cellData.showFeedName {
return NSZeroRect return NSZeroRect
@ -77,13 +72,13 @@ private func rectForFeedName(_ cellData: TimelineCellData, _ width: CGFloat, _ a
let renderer = RSSingleLineRenderer(attributedTitle: cellData.attributedFeedName) let renderer = RSSingleLineRenderer(attributedTitle: cellData.attributedFeedName)
var r = NSZeroRect var r = NSZeroRect
r.size = renderer.size r.size = renderer.size
r.origin.y = NSMaxY(titleRect) + appearance.titleBottomMargin r.origin.y = NSMaxY(dateRect) + appearance.titleBottomMargin
r.origin.x = appearance.boxLeftMargin r.origin.x = appearance.boxLeftMargin
if let _ = cellData.favicon { // if let _ = cellData.favicon {
r.origin.x += appearance.faviconSize.width + appearance.faviconFeedNameSpacing // r.origin.x += appearance.faviconSize.width + appearance.faviconFeedNameSpacing
} // }
r.size.width = width - (r.origin.x + appearance.cellPadding.right) r.size.width = width - (r.origin.x + appearance.cellPadding.right)
if r.size.width < 15 { if r.size.width < 15 {
@ -93,7 +88,7 @@ private func rectForFeedName(_ cellData: TimelineCellData, _ width: CGFloat, _ a
return r return r
} }
private func rectForFavicon(_ cellData: TimelineCellData, _ appearance: TimelineCellAppearance, _ feedNameRect: NSRect) -> NSRect { private func rectForFavicon(_ cellData: TimelineCellData, _ appearance: TimelineCellAppearance, _ feedNameRect: NSRect, _ unreadIndicatorRect: NSRect) -> NSRect {
guard let _ = cellData.favicon, cellData.showFeedName else { guard let _ = cellData.favicon, cellData.showFeedName else {
return NSZeroRect return NSZeroRect
@ -101,9 +96,9 @@ private func rectForFavicon(_ cellData: TimelineCellData, _ appearance: Timeline
var r = NSZeroRect var r = NSZeroRect
r.size = appearance.faviconSize r.size = appearance.faviconSize
r.origin.x = appearance.boxLeftMargin r.origin.y = feedNameRect.origin.y
r = RSRectCenteredVerticallyInRect(r, feedNameRect) r = RSRectCenteredHorizontallyInRect(r, unreadIndicatorRect)
return r return r
} }
@ -129,9 +124,9 @@ private func rectsForTitle(_ cellData: TimelineCellData, _ width: CGFloat, _ app
private func rectForUnreadIndicator(_ cellData: TimelineCellData, _ appearance: TimelineCellAppearance, _ titleLine1Rect: NSRect) -> NSRect { private func rectForUnreadIndicator(_ cellData: TimelineCellData, _ appearance: TimelineCellAppearance, _ titleLine1Rect: NSRect) -> NSRect {
if cellData.read { // if cellData.read {
return NSZeroRect // return NSZeroRect
} // }
var r = NSZeroRect var r = NSZeroRect
r.size = NSSize(width: appearance.unreadCircleDimension, height: appearance.unreadCircleDimension) r.size = NSSize(width: appearance.unreadCircleDimension, height: appearance.unreadCircleDimension)
@ -156,9 +151,9 @@ func timelineCellLayout(_ width: CGFloat, cellData: TimelineCellData, appearance
// let dateRect = rectForDate(cellData, width, appearance) // let dateRect = rectForDate(cellData, width, appearance)
let (titleRect, titleLine1Rect) = rectsForTitle(cellData, width, appearance) let (titleRect, titleLine1Rect) = rectsForTitle(cellData, width, appearance)
let dateRect = rectForDate(cellData, width, appearance, titleRect) let dateRect = rectForDate(cellData, width, appearance, titleRect)
let feedNameRect = rectForFeedName(cellData, width, appearance, titleRect) let feedNameRect = rectForFeedName(cellData, width, appearance, dateRect)
let faviconRect = rectForFavicon(cellData, appearance, feedNameRect)
let unreadIndicatorRect = rectForUnreadIndicator(cellData, appearance, titleLine1Rect) let unreadIndicatorRect = rectForUnreadIndicator(cellData, appearance, titleLine1Rect)
let faviconRect = rectForFavicon(cellData, appearance, feedNameRect, unreadIndicatorRect)
let avatarImageRect = rectForAvatar(cellData, appearance, titleLine1Rect) let avatarImageRect = rectForAvatar(cellData, appearance, titleLine1Rect)
return TimelineCellLayout(width: width, faviconRect: faviconRect, feedNameRect: feedNameRect, dateRect: dateRect, titleRect: titleRect, unreadIndicatorRect: unreadIndicatorRect, avatarImageRect: avatarImageRect, paddingBottom: appearance.cellPadding.bottom) return TimelineCellLayout(width: width, faviconRect: faviconRect, feedNameRect: feedNameRect, dateRect: dateRect, titleRect: titleRect, unreadIndicatorRect: unreadIndicatorRect, avatarImageRect: avatarImageRect, paddingBottom: appearance.cellPadding.bottom)

View File

@ -10,7 +10,7 @@ import Foundation
import Data import Data
import RSParser import RSParser
private let truncatedFeedNameCache = NSMutableDictionary() private var truncatedFeedNameCache = [String: String]()
private let truncatedTitleCache = NSMutableDictionary() private let truncatedTitleCache = NSMutableDictionary()
private let normalizedTextCache = NSMutableDictionary() private let normalizedTextCache = NSMutableDictionary()
private let textCache = NSMutableDictionary() private let textCache = NSMutableDictionary()
@ -19,17 +19,16 @@ private let summaryCache = NSMutableDictionary()
func timelineEmptyCaches() { func timelineEmptyCaches() {
truncatedFeedNameCache.removeAllObjects() truncatedFeedNameCache = [String: String]()
truncatedTitleCache.removeAllObjects() truncatedTitleCache.removeAllObjects()
normalizedTextCache.removeAllObjects() normalizedTextCache.removeAllObjects()
textCache.removeAllObjects() textCache.removeAllObjects()
summaryCache.removeAllObjects() summaryCache.removeAllObjects()
} }
func timelineTruncatedFeedName(_ feed: Feed) -> String { func timelineTruncatedFeedName(_ feedName: String) -> String {
let feedName = feed.nameForDisplay if let cachedFeedName = truncatedFeedNameCache[feedName] {
if let cachedFeedName = truncatedFeedNameCache[feedName] as? String {
return cachedFeedName return cachedFeedName
} }

View File

@ -24,6 +24,14 @@ class TimelineTableCellView: NSTableCellView {
return imageView return imageView
}() }()
let faviconImageView: NSImageView = {
let imageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 16, height: 16))
imageView.imageScaling = .scaleProportionallyDown
imageView.animates = false
imageView.imageAlignment = .alignCenter
return imageView
}()
var cellAppearance: TimelineCellAppearance! var cellAppearance: TimelineCellAppearance!
var cellData: TimelineCellData! { var cellData: TimelineCellData! {
didSet { didSet {
@ -75,6 +83,7 @@ class TimelineTableCellView: NSTableCellView {
addSubviewAtInit(dateView, hidden: false) addSubviewAtInit(dateView, hidden: false)
addSubviewAtInit(feedNameView, hidden: true) addSubviewAtInit(feedNameView, hidden: true)
addSubviewAtInit(avatarImageView, hidden: true) addSubviewAtInit(avatarImageView, hidden: true)
addSubviewAtInit(faviconImageView, hidden: true)
} }
override init(frame frameRect: NSRect) { override init(frame frameRect: NSRect) {
@ -123,6 +132,7 @@ class TimelineTableCellView: NSTableCellView {
dateView.rs_setFrameIfNotEqual(layoutRects.dateRect) dateView.rs_setFrameIfNotEqual(layoutRects.dateRect)
feedNameView.rs_setFrameIfNotEqual(layoutRects.feedNameRect) feedNameView.rs_setFrameIfNotEqual(layoutRects.feedNameRect)
avatarImageView.rs_setFrameIfNotEqual(layoutRects.avatarImageRect) avatarImageView.rs_setFrameIfNotEqual(layoutRects.avatarImageRect)
faviconImageView.rs_setFrameIfNotEqual(layoutRects.faviconRect)
} }
override func updateLayer() { override func updateLayer() {
@ -187,6 +197,18 @@ class TimelineTableCellView: NSTableCellView {
// } // }
} }
private func updateFavicon() {
if let favicon = cellData.showFeedName ? cellData.favicon : nil {
faviconImageView.image = favicon
faviconImageView.isHidden = false
}
else {
faviconImageView.image = nil
faviconImageView.isHidden = true
}
}
private func updateSubviews() { private func updateSubviews() {
updateTitleView() updateTitleView()
@ -194,6 +216,7 @@ class TimelineTableCellView: NSTableCellView {
updateFeedNameView() updateFeedNameView()
updateUnreadIndicator() updateUnreadIndicator()
updateAvatar() updateAvatar()
updateFavicon()
} }
private func updateAppearance() { private func updateAppearance() {

View File

@ -30,7 +30,22 @@ class TimelineViewController: NSViewController, UndoableCommandRunner {
var undoableCommands = [UndoableCommand]() var undoableCommands = [UndoableCommand]()
private var cellAppearance: TimelineCellAppearance! private var cellAppearance: TimelineCellAppearance!
private var showFeedNames = false
private var showFeedNames = false {
didSet {
if showFeedNames != oldValue {
tableView.rowHeight = currentRowHeight
}
}
}
private var rowHeightWithFeedName: CGFloat = 0.0
private var rowHeightWithoutFeedName: CGFloat = 0.0
private var currentRowHeight: CGFloat {
return showFeedNames ? rowHeightWithFeedName : rowHeightWithoutFeedName
}
private var didRegisterForNotifications = false private var didRegisterForNotifications = false
private let timelineFontSizeKVOKey = "values.{AppDefaults.Key.timelineFontSize}" private let timelineFontSizeKVOKey = "values.{AppDefaults.Key.timelineFontSize}"
@ -84,7 +99,8 @@ class TimelineViewController: NSViewController, UndoableCommandRunner {
cellAppearance = TimelineCellAppearance(theme: appDelegate.currentTheme, fontSize: fontSize) cellAppearance = TimelineCellAppearance(theme: appDelegate.currentTheme, fontSize: fontSize)
tableView.rowHeight = calculateRowHeight() updateRowHeights()
tableView.rowHeight = currentRowHeight
tableView.target = self tableView.target = self
tableView.doubleAction = #selector(openArticleInBrowser(_:)) tableView.doubleAction = #selector(openArticleInBrowser(_:))
tableView.setDraggingSourceOperationMask(.copy, forLocal: false) tableView.setDraggingSourceOperationMask(.copy, forLocal: false)
@ -124,9 +140,9 @@ class TimelineViewController: NSViewController, UndoableCommandRunner {
private func fontSizeDidChange() { private func fontSizeDidChange() {
cellAppearance = TimelineCellAppearance(theme: appDelegate.currentTheme, fontSize: fontSize) cellAppearance = TimelineCellAppearance(theme: appDelegate.currentTheme, fontSize: fontSize)
let updatedRowHeight = calculateRowHeight() updateRowHeights()
if tableView.rowHeight != updatedRowHeight { if tableView.rowHeight != currentRowHeight {
tableView.rowHeight = updatedRowHeight tableView.rowHeight = currentRowHeight
tableView.reloadData() tableView.reloadData()
} }
} }
@ -324,18 +340,23 @@ class TimelineViewController: NSViewController, UndoableCommandRunner {
// MARK: - Cell Configuring // MARK: - Cell Configuring
private func calculateRowHeight() -> CGFloat { private func calculateRowHeight(showingFeedNames: Bool) -> CGFloat {
let longTitle = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" let longTitle = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
let prototypeID = "prototype" let prototypeID = "prototype"
let status = ArticleStatus(articleID: prototypeID, read: false, starred: false, userDeleted: false, dateArrived: Date()) let status = ArticleStatus(articleID: prototypeID, read: false, starred: false, userDeleted: false, dateArrived: Date())
let prototypeArticle = Article(accountID: prototypeID, articleID: prototypeID, feedID: prototypeID, uniqueID: prototypeID, title: longTitle, contentHTML: nil, contentText: nil, url: nil, externalURL: nil, summary: nil, imageURL: nil, bannerImageURL: nil, datePublished: nil, dateModified: nil, authors: nil, attachments: nil, status: status) let prototypeArticle = Article(accountID: prototypeID, articleID: prototypeID, feedID: prototypeID, uniqueID: prototypeID, title: longTitle, contentHTML: nil, contentText: nil, url: nil, externalURL: nil, summary: nil, imageURL: nil, bannerImageURL: nil, datePublished: nil, dateModified: nil, authors: nil, attachments: nil, status: status)
let prototypeCellData = TimelineCellData(article: prototypeArticle, appearance: cellAppearance, showFeedName: false, favicon: nil, avatar: nil, featuredImage: nil) let prototypeCellData = TimelineCellData(article: prototypeArticle, appearance: cellAppearance, showFeedName: showingFeedNames, feedName: "Prototype Feed Name", favicon: nil, avatar: nil, featuredImage: nil)
let height = timelineCellHeight(100, cellData: prototypeCellData, appearance: cellAppearance) let height = timelineCellHeight(100, cellData: prototypeCellData, appearance: cellAppearance)
return height return height
} }
private func updateRowHeights() {
rowHeightWithFeedName = calculateRowHeight(showingFeedNames: true)
rowHeightWithoutFeedName = calculateRowHeight(showingFeedNames: false)
}
} }
// MARK: - NSTableViewDataSource // MARK: - NSTableViewDataSource
@ -423,19 +444,11 @@ extension TimelineViewController: NSTableViewDelegate {
cell.objectValue = article cell.objectValue = article
let favicon = faviconFor(article) let favicon = showFeedNames ? article.feed?.smallIcon : nil
let avatar = avatarFor(article) let avatar = avatarFor(article)
let featuredImage = featuredImageFor(article) let featuredImage = featuredImageFor(article)
cell.cellData = TimelineCellData(article: article, appearance: cellAppearance, showFeedName: showFeedNames, favicon: favicon, avatar: avatar, featuredImage: featuredImage) cell.cellData = TimelineCellData(article: article, appearance: cellAppearance, showFeedName: showFeedNames, feedName: article.feed?.nameForDisplay, favicon: favicon, avatar: avatar, featuredImage: featuredImage)
}
private func faviconFor(_ article: Article) -> NSImage? {
guard let feed = article.feed else {
return nil
}
return appDelegate.faviconDownloader.favicon(for: feed)
} }
private func avatarFor(_ article: Article) -> NSImage? { private func avatarFor(_ article: Article) -> NSImage? {