From 17860b72f5f0d451de9cccc471a1a6a671379d5a Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 16 Dec 2023 09:32:17 -0600 Subject: [PATCH] Change Mac state preservation to be NSSecureCoding compliant --- Mac/AppDefaults.swift | 4 +- Mac/AppDelegate.swift | 4 + .../Detail/DetailViewController.swift | 10 +-- .../Detail/DetailWebViewController.swift | 11 +-- Mac/MainWindow/Detail/DetailWindowState.swift | 33 ++++++++ Mac/MainWindow/MainWindowController.swift | 82 +++++++++---------- Mac/MainWindow/MainWindowState.swift | 50 +++++++++++ .../Sidebar/SidebarViewController.swift | 35 ++++---- .../Sidebar/SidebarWindowState.swift | 37 +++++++++ .../TimelineContainerViewController.swift | 12 +-- .../Timeline/TimelineViewController.swift | 51 ++++++------ .../Timeline/TimelineWindowState.swift | 41 ++++++++++ Shared/UserInfoKey.swift | 10 +-- 13 files changed, 263 insertions(+), 117 deletions(-) create mode 100644 Mac/MainWindow/Detail/DetailWindowState.swift create mode 100644 Mac/MainWindow/MainWindowState.swift create mode 100644 Mac/MainWindow/Sidebar/SidebarWindowState.swift create mode 100644 Mac/MainWindow/Timeline/TimelineWindowState.swift diff --git a/Mac/AppDefaults.swift b/Mac/AppDefaults.swift index d4b2ee784..7fffb41b9 100644 --- a/Mac/AppDefaults.swift +++ b/Mac/AppDefaults.swift @@ -74,9 +74,9 @@ final class AppDefaults { return true }() - var windowState: [AnyHashable : Any]? { + var windowState: Data? { get { - return UserDefaults.standard.object(forKey: Key.windowState) as? [AnyHashable : Any] + return UserDefaults.standard.object(forKey: Key.windowState) as? Data } set { UserDefaults.standard.set(newValue, forKey: Key.windowState) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index e8f3dd76b..c9fa513c4 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -302,6 +302,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, return false } + func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool { + return true + } + func applicationDidBecomeActive(_ notification: Notification) { fireOldTimers() } diff --git a/Mac/MainWindow/Detail/DetailViewController.swift b/Mac/MainWindow/Detail/DetailViewController.swift index f096d0f5c..26104663d 100644 --- a/Mac/MainWindow/Detail/DetailViewController.swift +++ b/Mac/MainWindow/Detail/DetailViewController.swift @@ -28,6 +28,10 @@ final class DetailViewController: NSViewController, WKUIDelegate { private lazy var regularWebViewController = createWebViewController() private var searchWebViewController: DetailWebViewController? + var windowState: DetailWindowState { + return currentWebViewController.windowState + } + private var currentWebViewController: DetailWebViewController! { didSet { let webview = currentWebViewController.view @@ -108,12 +112,6 @@ final class DetailViewController: NSViewController, WKUIDelegate { window.makeFirstResponderUnlessDescendantIsFirstResponder(currentWebViewController.webView) } - // MARK: State Restoration - - func saveState(to state: inout [AnyHashable : Any]) { - currentWebViewController.saveState(to: &state) - } - } // MARK: - DetailWebViewControllerDelegate diff --git a/Mac/MainWindow/Detail/DetailWebViewController.swift b/Mac/MainWindow/Detail/DetailWebViewController.swift index 2f82fed78..27bf97937 100644 --- a/Mac/MainWindow/Detail/DetailWebViewController.swift +++ b/Mac/MainWindow/Detail/DetailWebViewController.swift @@ -35,6 +35,10 @@ final class DetailWebViewController: NSViewController { } } + var windowState: DetailWindowState { + return DetailWindowState(isShowingExtractedArticle: isShowingExtractedArticle, windowScrollY: windowScrollY ?? 0) + } + var article: Article? { switch state { case .article(let article, _): @@ -205,13 +209,6 @@ final class DetailWebViewController: NSViewController { webView.scrollPageUp(sender) } - // MARK: State Restoration - - func saveState(to state: inout [AnyHashable : Any]) { - state[UserInfoKey.isShowingExtractedArticle] = isShowingExtractedArticle - state[UserInfoKey.articleWindowScrollY] = windowScrollY - } - } // MARK: - WKScriptMessageHandler diff --git a/Mac/MainWindow/Detail/DetailWindowState.swift b/Mac/MainWindow/Detail/DetailWindowState.swift new file mode 100644 index 000000000..3fdc0a58a --- /dev/null +++ b/Mac/MainWindow/Detail/DetailWindowState.swift @@ -0,0 +1,33 @@ +// +// DetailWindowState.swift +// NetNewsWire +// +// Created by Maurice Parker on 12/16/23. +// Copyright © 2023 Ranchero Software. All rights reserved. +// + +import Foundation + +class DetailWindowState: NSObject, NSSecureCoding { + + static var supportsSecureCoding = true + + let isShowingExtractedArticle: Bool + let windowScrollY: CGFloat + + internal init(isShowingExtractedArticle: Bool, windowScrollY: CGFloat) { + self.isShowingExtractedArticle = isShowingExtractedArticle + self.windowScrollY = windowScrollY + } + + required init?(coder: NSCoder) { + isShowingExtractedArticle = coder.decodeBool(forKey: "isShowingExtractedArticle") + windowScrollY = CGFloat(coder.decodeDouble(forKey: "windowScrollY")) + } + + func encode(with coder: NSCoder) { + coder.encode(isShowingExtractedArticle, forKey: "isShowingExtractedArticle") + coder.encode(Double(windowScrollY), forKey: "windowScrollY") + } + +} diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index a0b9932c5..76cf62b30 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -130,12 +130,14 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { } func saveStateToUserDefaults() { - AppDefaults.shared.windowState = savableState() + let data = try? NSKeyedArchiver.archivedData(withRootObject: savableState(), requiringSecureCoding: true) + AppDefaults.shared.windowState = data window?.saveFrame(usingName: windowAutosaveName) } func restoreStateFromUserDefaults() { - if let state = AppDefaults.shared.windowState { + if let data = AppDefaults.shared.windowState, + let state = try? NSKeyedUnarchiver.unarchivedObject(ofClass: MainWindowState.self, from: data) { restoreState(from: state) window?.setFrameUsingName(windowAutosaveName, force: true) } @@ -530,7 +532,7 @@ extension MainWindowController: NSWindowDelegate { } func window(_ window: NSWindow, didDecodeRestorableState coder: NSCoder) { - guard let state = try? coder.decodeTopLevelObject(forKey: UserInfoKey.windowState) as? [AnyHashable : Any] else { return } + guard let state = coder.decodeObject(of: MainWindowState.self, forKey: UserInfoKey.windowState) else { return } restoreState(from: state) } @@ -949,31 +951,39 @@ private extension MainWindowController { // MARK: - State Restoration - func savableState() -> [AnyHashable : Any] { - var state = [AnyHashable : Any]() - state[UserInfoKey.windowFullScreenState] = window?.styleMask.contains(.fullScreen) ?? false - saveSplitViewState(to: &state) - sidebarViewController?.saveState(to: &state) - timelineContainerViewController?.saveState(to: &state) - detailViewController?.saveState(to: &state) - return state + func savableState() -> MainWindowState { + let isFullScreen = window?.styleMask.contains(.fullScreen) ?? false + + let splitViewWidths: [Int] + if let splitView = splitViewController?.splitView { + splitViewWidths = splitView.arrangedSubviews.map{ Int(floor($0.frame.width)) } + } else { + splitViewWidths = [] + } + + let isSidebarHidden = sidebarSplitViewItem?.isCollapsed ?? false + + return MainWindowState(isFullScreen: isFullScreen, + splitViewWidths: splitViewWidths, + isSidebarHidden: isSidebarHidden, + sidebarWindowState: sidebarViewController?.windowState, + timelineWindowState: timelineContainerViewController?.windowState, + detailWindowState: detailViewController?.windowState) } - func restoreState(from state: [AnyHashable : Any]) { - if let fullScreen = state[UserInfoKey.windowFullScreenState] as? Bool, fullScreen { + func restoreState(from state: MainWindowState) { + if state.isFullScreen { window?.toggleFullScreen(self) } restoreSplitViewState(from: state) - sidebarViewController?.restoreState(from: state) + sidebarViewController?.restoreState(from: state.sidebarWindowState) - let articleWindowScrollY = state[UserInfoKey.articleWindowScrollY] as? CGFloat - restoreArticleWindowScrollY = articleWindowScrollY - timelineContainerViewController?.restoreState(from: state) - - let isShowingExtractedArticle = state[UserInfoKey.isShowingExtractedArticle] as? Bool ?? false + timelineContainerViewController?.restoreState(from: state.timelineWindowState) + restoreArticleWindowScrollY = state.detailWindowState?.windowScrollY + + let isShowingExtractedArticle = state.detailWindowState?.isShowingExtractedArticle as? Bool ?? false if isShowingExtractedArticle { - restoreArticleWindowScrollY = articleWindowScrollY startArticleExtractorForCurrentLink() } @@ -1237,29 +1247,17 @@ private extension MainWindowController { } } - func saveSplitViewState(to state: inout [AnyHashable : Any]) { - guard let splitView = splitViewController?.splitView else { - return - } - - let widths = splitView.arrangedSubviews.map{ Int(floor($0.frame.width)) } - state[MainWindowController.mainWindowWidthsStateKey] = widths - - state[UserInfoKey.isSidebarHidden] = sidebarSplitViewItem?.isCollapsed - } - - func restoreSplitViewState(from state: [AnyHashable : Any]) { + func restoreSplitViewState(from state: MainWindowState) { guard let splitView = splitViewController?.splitView, - let widths = state[MainWindowController.mainWindowWidthsStateKey] as? [Int], - widths.count == 3, - let window = window else { - return + state.splitViewWidths.count == 3, + let window = window else { + return } let windowWidth = Int(floor(window.frame.width)) let dividerThickness: Int = Int(splitView.dividerThickness) - let sidebarWidth: Int = widths[0] - let timelineWidth: Int = widths[1] + let sidebarWidth: Int = state.splitViewWidths[0] + let timelineWidth: Int = state.splitViewWidths[1] // Make sure the detail view has its minimum thickness, at least. if windowWidth < sidebarWidth + dividerThickness + timelineWidth + dividerThickness + MainWindowController.detailViewMinimumThickness { @@ -1268,11 +1266,9 @@ private extension MainWindowController { splitView.setPosition(CGFloat(sidebarWidth), ofDividerAt: 0) splitView.setPosition(CGFloat(sidebarWidth + dividerThickness + timelineWidth), ofDividerAt: 1) - - let isSidebarHidden = state[UserInfoKey.isSidebarHidden] as? Bool ?? false - - if !(sidebarSplitViewItem?.isCollapsed ?? false) && isSidebarHidden { - sidebarSplitViewItem?.isCollapsed = true + + Task { @MainActor in + sidebarSplitViewItem?.isCollapsed = state.isSidebarHidden } } diff --git a/Mac/MainWindow/MainWindowState.swift b/Mac/MainWindow/MainWindowState.swift new file mode 100644 index 000000000..b7fafd32b --- /dev/null +++ b/Mac/MainWindow/MainWindowState.swift @@ -0,0 +1,50 @@ +// +// MainWindowState.swift +// NetNewsWire +// +// Created by Maurice Parker on 12/16/23. +// Copyright © 2023 Ranchero Software. All rights reserved. +// + +import Foundation + +class MainWindowState: NSObject, NSSecureCoding { + + static var supportsSecureCoding = true + + let isFullScreen: Bool + let splitViewWidths: [Int] + let isSidebarHidden: Bool + let sidebarWindowState: SidebarWindowState? + let timelineWindowState: TimelineWindowState? + let detailWindowState: DetailWindowState? + + init(isFullScreen: Bool, splitViewWidths: [Int], isSidebarHidden: Bool, sidebarWindowState: SidebarWindowState? = nil, timelineWindowState: TimelineWindowState? = nil, detailWindowState: DetailWindowState? = nil) { + self.isFullScreen = isFullScreen + self.splitViewWidths = splitViewWidths + self.isSidebarHidden = isSidebarHidden + self.sidebarWindowState = sidebarWindowState + self.timelineWindowState = timelineWindowState + self.detailWindowState = detailWindowState + } + + required init?(coder: NSCoder) { + isFullScreen = coder.decodeBool(forKey: "isFullScreen") + splitViewWidths = coder.decodeObject(of: [NSArray.self, NSNumber.self], forKey: "splitViewWidths") as? [Int] ?? [] + isSidebarHidden = coder.decodeBool(forKey: "isSidebarHidden") + sidebarWindowState = coder.decodeObject(of: SidebarWindowState.self, forKey: "sidebarWindowState") + timelineWindowState = coder.decodeObject(of: TimelineWindowState.self, forKey: "timelineWindowState") + detailWindowState = coder.decodeObject(of: DetailWindowState.self, forKey: "detailWindowState") + } + + + func encode(with coder: NSCoder) { + coder.encode(isFullScreen, forKey: "isFullScreen") + coder.encode(splitViewWidths, forKey: "splitViewWidths") + coder.encode(isSidebarHidden, forKey: "isSidebarHidden") + coder.encode(sidebarWindowState, forKey: "sidebarWindowState") + coder.encode(timelineWindowState, forKey: "timelineWindowState") + coder.encode(detailWindowState, forKey: "detailWindowState") + } + +} diff --git a/Mac/MainWindow/Sidebar/SidebarViewController.swift b/Mac/MainWindow/Sidebar/SidebarViewController.swift index 43c625f10..41b329d7c 100644 --- a/Mac/MainWindow/Sidebar/SidebarViewController.swift +++ b/Mac/MainWindow/Sidebar/SidebarViewController.swift @@ -30,6 +30,12 @@ protocol SidebarDelegate: AnyObject { weak var splitViewItem: NSSplitViewItem? + var windowState: SidebarWindowState { + let expandedContainers = expandedTable.compactMap { $0.userInfo as? [String: String] } + let selectedFeeds = selectedFeeds.compactMap { $0.feedID?.userInfo as? [String: String] } + return SidebarWindowState(isReadFiltered: isReadFiltered, expandedContainers: expandedContainers, selectedFeeds: selectedFeeds) + } + private let rebuildTreeAndRestoreSelectionQueue = CoalescingQueue(name: "Rebuild Tree Queue", interval: 1.0) let treeControllerDelegate = WebFeedTreeControllerDelegate() lazy var treeController: TreeController = { @@ -96,25 +102,14 @@ protocol SidebarDelegate: AnyObject { // MARK: State Restoration - func saveState(to state: inout [AnyHashable : Any]) { - state[UserInfoKey.readFeedsFilterState] = isReadFiltered - state[UserInfoKey.containerExpandedWindowState] = expandedTable.map { $0.userInfo } - state[UserInfoKey.selectedFeedsState] = selectedFeeds.compactMap { $0.feedID?.userInfo } - } - - func restoreState(from state: [AnyHashable : Any]) { + func restoreState(from state: SidebarWindowState?) { + guard let state else { return } - if let containerExpandedWindowState = state[UserInfoKey.containerExpandedWindowState] as? [[AnyHashable: AnyHashable]] { - let containerIdentifiers = containerExpandedWindowState.compactMap( { ContainerIdentifier(userInfo: $0) }) - expandedTable = Set(containerIdentifiers) - } + let containerIdentifers = state.expandedContainers.compactMap( { ContainerIdentifier(userInfo: $0) }) + expandedTable = Set(containerIdentifers) - guard let selectedFeedsState = state[UserInfoKey.selectedFeedsState] as? [[AnyHashable: AnyHashable]] else { - return - } - - let selectedFeedIdentifiers = Set(selectedFeedsState.compactMap( { FeedIdentifier(userInfo: $0) })) - selectedFeedIdentifiers.forEach { treeControllerDelegate.addFilterException($0) } + let selectedFeedIdentifers = Set(state.selectedFeeds.compactMap( { FeedIdentifier(userInfo: $0) })) + selectedFeedIdentifers.forEach { treeControllerDelegate.addFilterException($0) } rebuildTreeAndReloadDataIfNeeded() @@ -122,7 +117,7 @@ protocol SidebarDelegate: AnyObject { func selectFeedsVisitor(node: Node) { if let feedID = (node.representedObject as? FeedIdentifiable)?.feedID { - if selectedFeedIdentifiers.contains(feedID) { + if selectedFeedIdentifers.contains(feedID) { selectIndexes.insert(outlineView.row(forItem: node) ) } } @@ -132,9 +127,7 @@ protocol SidebarDelegate: AnyObject { outlineView.selectRowIndexes(selectIndexes, byExtendingSelection: false) focus() - if let readFeedsFilterState = state[UserInfoKey.readFeedsFilterState] as? Bool { - isReadFiltered = readFeedsFilterState - } + isReadFiltered = state.isReadFiltered } // MARK: - Notifications diff --git a/Mac/MainWindow/Sidebar/SidebarWindowState.swift b/Mac/MainWindow/Sidebar/SidebarWindowState.swift new file mode 100644 index 000000000..2011362bb --- /dev/null +++ b/Mac/MainWindow/Sidebar/SidebarWindowState.swift @@ -0,0 +1,37 @@ +// +// SidebarWindowState.swift +// NetNewsWire +// +// Created by Maurice Parker on 12/16/23. +// Copyright © 2023 Ranchero Software. All rights reserved. +// + +import Foundation + +class SidebarWindowState: NSObject, NSSecureCoding { + + static var supportsSecureCoding = true + + let isReadFiltered: Bool + let expandedContainers: [[String: String]] + let selectedFeeds: [[String: String]] + + init(isReadFiltered: Bool, expandedContainers: [[String : String]], selectedFeeds: [[String : String]]) { + self.isReadFiltered = isReadFiltered + self.expandedContainers = expandedContainers + self.selectedFeeds = selectedFeeds + } + + required init?(coder: NSCoder) { + isReadFiltered = coder.decodeBool(forKey: "isReadFiltered") + expandedContainers = coder.decodeObject(of: [NSArray.self, NSDictionary.self, NSString.self], forKey: "expandedContainers") as? [[String: String]] ?? [] + selectedFeeds = coder.decodeObject(of: [NSArray.self, NSDictionary.self, NSString.self], forKey: "selectedFeeds") as? [[String: String]] ?? [] + } + + func encode(with coder: NSCoder) { + coder.encode(isReadFiltered, forKey: "isReadFiltered") + coder.encode(expandedContainers, forKey: "expandedContainers") + coder.encode(selectedFeeds, forKey: "selectedFeeds") + } + +} diff --git a/Mac/MainWindow/Timeline/TimelineContainerViewController.swift b/Mac/MainWindow/Timeline/TimelineContainerViewController.swift index 5fdf0ab12..ce435a642 100644 --- a/Mac/MainWindow/Timeline/TimelineContainerViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineContainerViewController.swift @@ -37,6 +37,10 @@ final class TimelineContainerViewController: NSViewController { view?.window?.recalculateKeyViewLoop() } } + + var windowState: TimelineWindowState? { + return currentTimelineViewController?.windowState + } weak var delegate: TimelineContainerViewControllerDelegate? @@ -127,11 +131,9 @@ final class TimelineContainerViewController: NSViewController { // MARK: State Restoration - func saveState(to state: inout [AnyHashable : Any]) { - regularTimelineViewController.saveState(to: &state) - } - - func restoreState(from state: [AnyHashable : Any]) { + func restoreState(from state: TimelineWindowState?) { + guard let state else { return } + regularTimelineViewController.restoreState(from: state) updateReadFilterButton() } diff --git a/Mac/MainWindow/Timeline/TimelineViewController.swift b/Mac/MainWindow/Timeline/TimelineViewController.swift index f19276b9f..f581e322d 100644 --- a/Mac/MainWindow/Timeline/TimelineViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineViewController.swift @@ -80,6 +80,24 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr } } + var windowState: TimelineWindowState { + let readArticlesFilterStateKeys = readFilterEnabledTable.keys.compactMap { $0.userInfo as? [String: String] } + let readArticlesFilterStateValues = readFilterEnabledTable.values.compactMap( { $0 }) + + if selectedArticles.count == 1 { + let path = selectedArticles.first!.pathUserInfo + return TimelineWindowState(readArticlesFilterStateKeys: readArticlesFilterStateKeys, + readArticlesFilterStateValues: readArticlesFilterStateValues, + selectedAccountID: path[ArticlePathKey.accountID] as? String, + selectedArticleID: path[ArticlePathKey.articleID] as? String) + } else { + return TimelineWindowState(readArticlesFilterStateKeys: readArticlesFilterStateKeys, + readArticlesFilterStateValues: readArticlesFilterStateValues, + selectedAccountID: nil, + selectedArticleID: nil) + } + + } weak var delegate: TimelineDelegate? var sharingServiceDelegate: NSSharingServiceDelegate? @@ -271,36 +289,21 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr // MARK: State Restoration - func saveState(to state: inout [AnyHashable : Any]) { - state[UserInfoKey.readArticlesFilterStateKeys] = readFilterEnabledTable.keys.compactMap { $0.userInfo } - state[UserInfoKey.readArticlesFilterStateValues] = readFilterEnabledTable.values.compactMap( { $0 }) - - if selectedArticles.count == 1 { - state[UserInfoKey.articlePath] = selectedArticles.first!.pathUserInfo - } - } - - func restoreState(from state: [AnyHashable : Any]) { - guard let readArticlesFilterStateKeys = state[UserInfoKey.readArticlesFilterStateKeys] as? [[AnyHashable: AnyHashable]], - let readArticlesFilterStateValues = state[UserInfoKey.readArticlesFilterStateValues] as? [Bool] else { - return - } - - for i in 0..