Files
NetNewsWire/Mac/Preferences/Appearance/AppearancePreferencesViewController.swift
T

222 lines
7.2 KiB
Swift

//
// AppearancePreferencesViewController.swift
// NetNewsWire
//
// Created by Brent Simmons on 11/6/26.
//
import AppKit
import RSCore
final class AppearancePreferencesViewController: NSViewController {
@IBOutlet var appearancePopup: NSPopUpButton?
@IBOutlet var grayscaleIconsButton: NSButton?
@IBOutlet var sidebarShowFeedIconsButton: NSButton?
@IBOutlet var sidebarTextSizePopup: NSPopUpButton?
@IBOutlet var sidebarShowTodayButton: NSButton?
@IBOutlet var sidebarShowAllUnreadButton: NSButton?
@IBOutlet var sidebarShowStarredButton: NSButton?
@IBOutlet var articleTextSizeLabel: NSTextField?
@IBOutlet var articleTextSizePopup: NSPopUpButton?
@IBOutlet var articleThemePopup: NSPopUpButton?
convenience init() {
self.init(nibName: "AppearancePreferencesView", bundle: nil)
}
override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
override func viewDidLoad() {
super.viewDidLoad()
fixArticleTextSizeBaselineIfNeeded()
view.sizeToFittingSize()
}
override func viewWillAppear() {
super.viewWillAppear()
updateAppearancePopup()
updateGrayscaleIconsButton()
updateSidebarShowIconsButton()
updateSidebarTextSizePopup()
updateSmartFeedsButtons()
updateArticleThemePopup()
}
// MARK: - Notifications
@objc func handleApplicationWillBecomeActive(_ notification: Notification) {
updateArticleThemePopup()
}
@objc func handleArticleThemeNamesDidChange(_ notification: Notification) {
updateArticleThemePopup()
}
@objc func handleUserInterfaceColorPaletteDidUpdate(_ notification: Notification) {
updateAppearancePopup()
}
@objc func handleGrayscaleIconsDidChange(_ notification: Notification) {
updateGrayscaleIconsButton()
}
@objc func handleSidebarShowsIconsDidChange(_ notification: Notification) {
updateSidebarShowIconsButton()
}
@objc func handleSidebarTextSizeDidChange(_ notification: Notification) {
updateSidebarTextSizePopup()
}
@objc func handleSidebarSmartFeedsVisibilityDidChange(_ notification: Notification) {
updateSmartFeedsButtons()
}
// MARK: - Actions
@IBAction func appearancePopUpDidChange(_ sender: Any) {
guard let tag = appearancePopup?.selectedTag(), let colorPalette = UserInterfaceColorPalette(rawValue: tag) else {
return
}
AppDefaults.userInterfaceColorPalette = colorPalette
}
@IBAction func grayscaleIconsDidChange(_ sender: NSButton) {
AppDefaults.grayscaleIcons = sender.state == .on
}
@IBAction func sidebarShowIconsDidChange(_ sender: NSButton) {
AppDefaults.sidebarShowsIcons = sender.state == .on
}
@IBAction func sidebarTextSizePopUpDidChange(_ sender: Any) {
guard let tag = sidebarTextSizePopup?.selectedTag(), let textSize = SidebarTextSize(rawValue: tag) else {
return
}
AppDefaults.sidebarTextSize = textSize
}
@IBAction func sidebarShowTodayDidChange(_ sender: NSButton) {
AppDefaults.showTodayFeed = sender.state == .on
}
@IBAction func sidebarShowAllUnreadDidChange(_ sender: NSButton) {
AppDefaults.showUnreadFeed = sender.state == .on
}
@IBAction func sidebarShowStarredDidChange(_ sender: NSButton) {
AppDefaults.showStarredFeed = sender.state == .on
}
@IBAction func showThemesFolder(_ sender: Any) {
let url = URL(fileURLWithPath: ArticleThemesManager.shared.folderPath)
NSWorkspace.shared.open(url)
}
@IBAction func articleThemePopUpDidChange(_ sender: Any) {
guard let menuItem = articleThemePopup?.selectedItem else {
return
}
ArticleThemesManager.shared.currentThemeName = menuItem.title
updateArticleThemePopup()
}
}
// MARK: - Private
private extension AppearancePreferencesViewController {
func commonInit() {
NotificationCenter.default.addObserver(self, selector: #selector(handleApplicationWillBecomeActive(_:)), name: NSApplication.willBecomeActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleArticleThemeNamesDidChange(_:)), name: .ArticleThemeNamesDidChangeNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleUserInterfaceColorPaletteDidUpdate(_:)), name: .userInterfaceColorPaletteDidUpdate, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleGrayscaleIconsDidChange(_:)), name: .grayscaleIconsDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleSidebarShowsIconsDidChange(_:)), name: .sidebarShowsIconsDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleSidebarTextSizeDidChange(_:)), name: .sidebarTextSizeDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleSidebarSmartFeedsVisibilityDidChange(_:)), name: .sidebarSmartFeedsVisibilityDidChange, object: nil)
}
/// On macOS 15 the "Article Text Size:" label is vertically misaligned
/// with its popup. Replace the firstBaseline constraint with a
/// centerY constraint, which works.
func fixArticleTextSizeBaselineIfNeeded() {
if #available(macOS 26, *) {
return
}
guard let articleTextSizeLabel, let articleTextSizePopup, let superview = articleTextSizeLabel.superview else {
return
}
for constraint in superview.constraints {
let matchesBaseline =
constraint.firstAttribute == .firstBaseline &&
constraint.secondAttribute == .firstBaseline
let involvesPopup =
constraint.firstItem === articleTextSizePopup ||
constraint.secondItem === articleTextSizePopup
let involvesLabel =
constraint.firstItem === articleTextSizeLabel ||
constraint.secondItem === articleTextSizeLabel
if matchesBaseline && involvesPopup && involvesLabel {
superview.removeConstraint(constraint)
articleTextSizeLabel.centerYAnchor.constraint(equalTo: articleTextSizePopup.centerYAnchor).isActive = true
break
}
}
}
func updateAppearancePopup() {
appearancePopup?.selectItem(withTag: AppDefaults.userInterfaceColorPalette.rawValue)
}
func updateGrayscaleIconsButton() {
grayscaleIconsButton?.state = AppDefaults.grayscaleIcons ? .on : .off
}
func updateSidebarShowIconsButton() {
sidebarShowFeedIconsButton?.state = AppDefaults.sidebarShowsIcons ? .on : .off
}
func updateSidebarTextSizePopup() {
sidebarTextSizePopup?.selectItem(withTag: AppDefaults.sidebarTextSize.rawValue)
}
func updateSmartFeedsButtons() {
sidebarShowTodayButton?.state = AppDefaults.showTodayFeed ? .on : .off
sidebarShowAllUnreadButton?.state = AppDefaults.showUnreadFeed ? .on : .off
sidebarShowStarredButton?.state = AppDefaults.showStarredFeed ? .on : .off
}
func updateArticleThemePopup() {
guard let articleThemePopup, let menu = articleThemePopup.menu else {
return
}
menu.removeAllItems()
menu.addItem(NSMenuItem(title: ArticleTheme.defaultTheme.name, action: nil, keyEquivalent: ""))
menu.addItem(NSMenuItem.separator())
for themeName in ArticleThemesManager.shared.themeNames {
menu.addItem(NSMenuItem(title: themeName, action: nil, keyEquivalent: ""))
}
articleThemePopup.selectItem(withTitle: ArticleThemesManager.shared.currentThemeName)
if articleThemePopup.indexOfSelectedItem == -1 {
articleThemePopup.selectItem(withTitle: ArticleTheme.defaultTheme.name)
}
}
}