diff --git a/Mac/MainWindow/Sidebar/Cell/SidebarCell.swift b/Mac/MainWindow/Sidebar/Cell/SidebarCell.swift index 42aea739b..17a65c168 100644 --- a/Mac/MainWindow/Sidebar/Cell/SidebarCell.swift +++ b/Mac/MainWindow/Sidebar/Cell/SidebarCell.swift @@ -44,8 +44,7 @@ final class SidebarCell: NSTableCellView { set { if unreadCountView.unreadCount != newValue { unreadCountView.unreadCount = newValue - unreadCountView.isHidden = (newValue < 1) - needsLayout = true + updateUnreadCountView() } } } @@ -112,8 +111,15 @@ final class SidebarCell: NSTableCellView { layoutWith(layout) } + func updateUnreadCountView() { + unreadCountView.invalidateIntrinsicContentSize() + unreadCountView.needsDisplay = true + unreadCountView.isHidden = unreadCountView.unreadCountText == nil + needsLayout = true + } + override func accessibilityLabel() -> String? { - if unreadCount > 0 { + if unreadCount > 0 && AppDefaults.shared.unreadCountDisplay == .count { let unreadLabel = NSLocalizedString("unread", comment: "Unread label for accessibility") return "\(name) \(unreadCount) \(unreadLabel)" } else { diff --git a/Mac/MainWindow/Sidebar/Cell/SidebarCellLayout.swift b/Mac/MainWindow/Sidebar/Cell/SidebarCellLayout.swift index 9dbf1e9c1..0b539b48b 100644 --- a/Mac/MainWindow/Sidebar/Cell/SidebarCellLayout.swift +++ b/Mac/MainWindow/Sidebar/Cell/SidebarCellLayout.swift @@ -36,7 +36,7 @@ import RSCore rTextField = rTextField.centeredVertically(in: bounds) let unreadCountSize = unreadCountView.intrinsicContentSize - let unreadCountIsHidden = unreadCountView.unreadCount < 1 + let unreadCountIsHidden = unreadCountView.unreadCountText == nil var rUnread = NSRect.zero if !unreadCountIsHidden { diff --git a/Mac/MainWindow/Sidebar/SidebarViewController.swift b/Mac/MainWindow/Sidebar/SidebarViewController.swift index b86ddc251..e9da14699 100644 --- a/Mac/MainWindow/Sidebar/SidebarViewController.swift +++ b/Mac/MainWindow/Sidebar/SidebarViewController.swift @@ -93,6 +93,7 @@ extension Notification.Name { NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidInitialize(_:)), name: .UnreadCountDidInitialize, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleUnreadCountDisplaySettingDidChange(_:)), name: .unreadCountDisplaySettingDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(containerChildrenDidChange(_:)), name: .ChildrenDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(accountsDidChange(_:)), name: .UserDidAddAccount, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(accountsDidChange(_:)), name: .UserDidDeleteAccount, object: nil) @@ -226,6 +227,12 @@ extension Notification.Name { } } + @objc func handleUnreadCountDisplaySettingDidChange(_ notification: Notification) { + applyToAvailableCells { cell, _ in + cell.updateUnreadCountView() + } + } + @objc func containerChildrenDidChange(_ note: Notification) { rebuildTreeAndRestoreSelection() } @@ -844,6 +851,7 @@ private extension SidebarViewController { cell.cellAppearance = SidebarCellAppearance(rowSizeStyle: outlineView.effectiveRowSizeStyle) cell.name = nameFor(node) configureUnreadCount(cell, node) + cell.updateUnreadCountView() // A reused cell may predate a display setting change configureFavicon(cell, node) cell.shouldShowImage = node.representedObject is SmallIconProvider } diff --git a/Mac/MainWindow/Sidebar/UnreadCountView.swift b/Mac/MainWindow/Sidebar/UnreadCountView.swift index 82b09e586..9e19f30b4 100644 --- a/Mac/MainWindow/Sidebar/UnreadCountView.swift +++ b/Mac/MainWindow/Sidebar/UnreadCountView.swift @@ -52,8 +52,13 @@ final class UnreadCountView: NSView { needsDisplay = true } } - var unreadCountString: String { - return unreadCount < 1 ? "" : "\(unreadCount.formatted())" + /// Text for the unread count, or nil when nothing should be drawn. + var unreadCountText: String? { + AppDefaults.shared.unreadCountDisplay.text(for: unreadCount) + } + + private var showsDot: Bool { + AppDefaults.shared.unreadCountDisplay.showsDot(for: unreadCount) } var isSelected: Bool = false { @@ -83,8 +88,12 @@ final class UnreadCountView: NSView { override var intrinsicContentSize: NSSize { if !intrinsicContentSizeIsValid { var size = NSSize.zero - if unreadCount > 0 { - size = textSize() + if showsDot { + // Same height as a count, so the row lays out the same either way. + size.width = UnreadIndicatorView.unreadCircleDimension + Appearance.padding.left + Appearance.padding.right + size.height = textSize(Self.digitForHeight).height + Appearance.padding.top + Appearance.padding.bottom + } else if let unreadCountText { + size = textSize(unreadCountText) size.width += (Appearance.padding.left + Appearance.padding.right) size.height += (Appearance.padding.top + Appearance.padding.bottom) } @@ -102,27 +111,23 @@ final class UnreadCountView: NSView { intrinsicContentSizeIsValid = false } - private static var textSizeCache = [Int: NSSize]() + private static var textSizeCache = [String: NSSize]() - private func textSize() -> NSSize { - if unreadCount < 1 { - return NSSize.zero - } - - if let cachedSize = UnreadCountView.textSizeCache[unreadCount] { + private func textSize(_ text: String) -> NSSize { + if let cachedSize = UnreadCountView.textSizeCache[text] { return cachedSize } - var size = unreadCountString.size(withAttributes: textAttributes) + var size = text.size(withAttributes: textAttributes) size.height = ceil(size.height) size.width = ceil(size.width) - UnreadCountView.textSizeCache[unreadCount] = size + UnreadCountView.textSizeCache[text] = size return size } - private func textRect() -> NSRect { - let size = textSize() + private func textRect(_ text: String) -> NSRect { + let size = textSize(text) var r = NSRect.zero r.size = size r.origin.x = (bounds.maxX - Appearance.padding.right) - r.size.width @@ -130,13 +135,29 @@ final class UnreadCountView: NSView { return r } + private static let digitForHeight = "0" + + /// Drawn without the pill. + private func drawDot() { + let dimension = UnreadIndicatorView.unreadCircleDimension + let r = NSRect(x: (bounds.maxX - Appearance.padding.right) - dimension, y: bounds.midY - (dimension / 2.0), width: dimension, height: dimension) + let color = isSelected ? NSColor.white : NSColor.secondaryLabelColor + color.setFill() + NSBezierPath(ovalIn: r).fill() + } + override func draw(_ dirtyRect: NSRect) { + if showsDot { + drawDot() + return + } + let path = NSBezierPath(roundedRect: bounds, xRadius: Appearance.cornerRadius, yRadius: Appearance.cornerRadius) Appearance.backgroundColor.setFill() path.fill() - if unreadCount > 0 { - unreadCountString.draw(at: textRect().origin, withAttributes: textAttributes) + if let unreadCountText { + unreadCountText.draw(at: textRect(unreadCountText).origin, withAttributes: textAttributes) } } }