From befc5efc7750477954ed58539bc71d0096a6654f Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 30 Dec 2017 12:45:10 -0800 Subject: [PATCH] Show feed names and favicons in timeline when appropriate. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I don’t quite like this. Maybe avatars are better. --- .../Timeline/Cell/TimelineCellData.swift | 8 ++-- .../Timeline/Cell/TimelineCellLayout.swift | 33 ++++++------- .../Cell/TimelineStringUtilities.swift | 9 ++-- .../Timeline/Cell/TimelineTableCellView.swift | 23 +++++++++ .../Timeline/TimelineViewController.swift | 47 ++++++++++++------- 5 files changed, 75 insertions(+), 45 deletions(-) diff --git a/Evergreen/MainWindow/Timeline/Cell/TimelineCellData.swift b/Evergreen/MainWindow/Timeline/Cell/TimelineCellData.swift index f06a64f2d..20e54fa7e 100644 --- a/Evergreen/MainWindow/Timeline/Cell/TimelineCellData.swift +++ b/Evergreen/MainWindow/Timeline/Cell/TimelineCellData.swift @@ -28,7 +28,7 @@ struct TimelineCellData { let featuredImage: NSImage? // image from within the article 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.text = timelineTruncatedSummary(article) @@ -51,13 +51,13 @@ struct TimelineCellData { attributedDateCache[self.dateString] = self.attributedDateString } - if let feed = article.feed { - self.feedName = timelineTruncatedFeedName(feed) + if let feedName = feedName { + self.feedName = timelineTruncatedFeedName(feedName) } else { self.feedName = "" } - if let s = attributedFeedNameCache[self.dateString] { + if let s = attributedFeedNameCache[self.feedName] { self.attributedFeedName = s } else { diff --git a/Evergreen/MainWindow/Timeline/Cell/TimelineCellLayout.swift b/Evergreen/MainWindow/Timeline/Cell/TimelineCellLayout.swift index 50f4dfac0..6fe73b9c8 100644 --- a/Evergreen/MainWindow/Timeline/Cell/TimelineCellLayout.swift +++ b/Evergreen/MainWindow/Timeline/Cell/TimelineCellLayout.swift @@ -10,11 +10,6 @@ import Cocoa import RSTextDrawing import RSCore -// title/text 1 date -// title/text 2 -// title/text 3 -// favicon feedname (optional line) - struct TimelineCellLayout { let width: CGFloat @@ -68,7 +63,7 @@ private func rectForDate(_ cellData: TimelineCellData, _ width: CGFloat, _ appea 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 { return NSZeroRect @@ -77,13 +72,13 @@ private func rectForFeedName(_ cellData: TimelineCellData, _ width: CGFloat, _ a let renderer = RSSingleLineRenderer(attributedTitle: cellData.attributedFeedName) var r = NSZeroRect r.size = renderer.size - r.origin.y = NSMaxY(titleRect) + appearance.titleBottomMargin + r.origin.y = NSMaxY(dateRect) + appearance.titleBottomMargin r.origin.x = appearance.boxLeftMargin - if let _ = cellData.favicon { - r.origin.x += appearance.faviconSize.width + appearance.faviconFeedNameSpacing - } - +// if let _ = cellData.favicon { +// r.origin.x += appearance.faviconSize.width + appearance.faviconFeedNameSpacing +// } + r.size.width = width - (r.origin.x + appearance.cellPadding.right) if r.size.width < 15 { @@ -93,7 +88,7 @@ private func rectForFeedName(_ cellData: TimelineCellData, _ width: CGFloat, _ a 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 { return NSZeroRect @@ -101,9 +96,9 @@ private func rectForFavicon(_ cellData: TimelineCellData, _ appearance: Timeline var r = NSZeroRect 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 } @@ -129,9 +124,9 @@ private func rectsForTitle(_ cellData: TimelineCellData, _ width: CGFloat, _ app private func rectForUnreadIndicator(_ cellData: TimelineCellData, _ appearance: TimelineCellAppearance, _ titleLine1Rect: NSRect) -> NSRect { - if cellData.read { - return NSZeroRect - } +// if cellData.read { +// return NSZeroRect +// } var r = NSZeroRect 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 (titleRect, titleLine1Rect) = rectsForTitle(cellData, width, appearance) let dateRect = rectForDate(cellData, width, appearance, titleRect) - let feedNameRect = rectForFeedName(cellData, width, appearance, titleRect) - let faviconRect = rectForFavicon(cellData, appearance, feedNameRect) + let feedNameRect = rectForFeedName(cellData, width, appearance, dateRect) let unreadIndicatorRect = rectForUnreadIndicator(cellData, appearance, titleLine1Rect) + let faviconRect = rectForFavicon(cellData, appearance, feedNameRect, unreadIndicatorRect) 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) diff --git a/Evergreen/MainWindow/Timeline/Cell/TimelineStringUtilities.swift b/Evergreen/MainWindow/Timeline/Cell/TimelineStringUtilities.swift index 17761b2a0..2d71bb19f 100644 --- a/Evergreen/MainWindow/Timeline/Cell/TimelineStringUtilities.swift +++ b/Evergreen/MainWindow/Timeline/Cell/TimelineStringUtilities.swift @@ -10,7 +10,7 @@ import Foundation import Data import RSParser -private let truncatedFeedNameCache = NSMutableDictionary() +private var truncatedFeedNameCache = [String: String]() private let truncatedTitleCache = NSMutableDictionary() private let normalizedTextCache = NSMutableDictionary() private let textCache = NSMutableDictionary() @@ -19,17 +19,16 @@ private let summaryCache = NSMutableDictionary() func timelineEmptyCaches() { - truncatedFeedNameCache.removeAllObjects() + truncatedFeedNameCache = [String: String]() truncatedTitleCache.removeAllObjects() normalizedTextCache.removeAllObjects() textCache.removeAllObjects() summaryCache.removeAllObjects() } -func timelineTruncatedFeedName(_ feed: Feed) -> String { +func timelineTruncatedFeedName(_ feedName: String) -> String { - let feedName = feed.nameForDisplay - if let cachedFeedName = truncatedFeedNameCache[feedName] as? String { + if let cachedFeedName = truncatedFeedNameCache[feedName] { return cachedFeedName } diff --git a/Evergreen/MainWindow/Timeline/Cell/TimelineTableCellView.swift b/Evergreen/MainWindow/Timeline/Cell/TimelineTableCellView.swift index 969c8c04d..8d651c76a 100644 --- a/Evergreen/MainWindow/Timeline/Cell/TimelineTableCellView.swift +++ b/Evergreen/MainWindow/Timeline/Cell/TimelineTableCellView.swift @@ -24,6 +24,14 @@ class TimelineTableCellView: NSTableCellView { 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 cellData: TimelineCellData! { didSet { @@ -75,6 +83,7 @@ class TimelineTableCellView: NSTableCellView { addSubviewAtInit(dateView, hidden: false) addSubviewAtInit(feedNameView, hidden: true) addSubviewAtInit(avatarImageView, hidden: true) + addSubviewAtInit(faviconImageView, hidden: true) } override init(frame frameRect: NSRect) { @@ -123,6 +132,7 @@ class TimelineTableCellView: NSTableCellView { dateView.rs_setFrameIfNotEqual(layoutRects.dateRect) feedNameView.rs_setFrameIfNotEqual(layoutRects.feedNameRect) avatarImageView.rs_setFrameIfNotEqual(layoutRects.avatarImageRect) + faviconImageView.rs_setFrameIfNotEqual(layoutRects.faviconRect) } 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() { updateTitleView() @@ -194,6 +216,7 @@ class TimelineTableCellView: NSTableCellView { updateFeedNameView() updateUnreadIndicator() updateAvatar() + updateFavicon() } private func updateAppearance() { diff --git a/Evergreen/MainWindow/Timeline/TimelineViewController.swift b/Evergreen/MainWindow/Timeline/TimelineViewController.swift index 3c31f3f72..cb84461b7 100644 --- a/Evergreen/MainWindow/Timeline/TimelineViewController.swift +++ b/Evergreen/MainWindow/Timeline/TimelineViewController.swift @@ -30,7 +30,22 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { var undoableCommands = [UndoableCommand]() 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 let timelineFontSizeKVOKey = "values.{AppDefaults.Key.timelineFontSize}" @@ -84,7 +99,8 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { cellAppearance = TimelineCellAppearance(theme: appDelegate.currentTheme, fontSize: fontSize) - tableView.rowHeight = calculateRowHeight() + updateRowHeights() + tableView.rowHeight = currentRowHeight tableView.target = self tableView.doubleAction = #selector(openArticleInBrowser(_:)) tableView.setDraggingSourceOperationMask(.copy, forLocal: false) @@ -124,9 +140,9 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { private func fontSizeDidChange() { cellAppearance = TimelineCellAppearance(theme: appDelegate.currentTheme, fontSize: fontSize) - let updatedRowHeight = calculateRowHeight() - if tableView.rowHeight != updatedRowHeight { - tableView.rowHeight = updatedRowHeight + updateRowHeights() + if tableView.rowHeight != currentRowHeight { + tableView.rowHeight = currentRowHeight tableView.reloadData() } } @@ -324,18 +340,23 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { // 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 prototypeID = "prototype" 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 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) return height } + private func updateRowHeights() { + + rowHeightWithFeedName = calculateRowHeight(showingFeedNames: true) + rowHeightWithoutFeedName = calculateRowHeight(showingFeedNames: false) + } } // MARK: - NSTableViewDataSource @@ -423,19 +444,11 @@ extension TimelineViewController: NSTableViewDelegate { cell.objectValue = article - let favicon = faviconFor(article) + let favicon = showFeedNames ? article.feed?.smallIcon : nil let avatar = avatarFor(article) let featuredImage = featuredImageFor(article) - cell.cellData = TimelineCellData(article: article, appearance: cellAppearance, showFeedName: showFeedNames, 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) + cell.cellData = TimelineCellData(article: article, appearance: cellAppearance, showFeedName: showFeedNames, feedName: article.feed?.nameForDisplay, favicon: favicon, avatar: avatar, featuredImage: featuredImage) } private func avatarFor(_ article: Article) -> NSImage? {