Change split view state restoration from user defaults to standard window restoration mechanism. I don't think you need to include the manual window frame autosave name stuff either because I think it's automatically handled by AppKit's default state restoration.

This commit is contained in:
Daniel Jalkut 2019-02-15 11:16:38 -05:00
parent 193c34b9c2
commit cd695848f0
3 changed files with 25 additions and 34 deletions

View File

@ -53,7 +53,6 @@ struct AppDefaults {
static let timelineSortDirection = "timelineSortDirection" static let timelineSortDirection = "timelineSortDirection"
static let detailFontSize = "detailFontSize" static let detailFontSize = "detailFontSize"
static let openInBrowserInBackground = "openInBrowserInBackground" static let openInBrowserInBackground = "openInBrowserInBackground"
static let mainWindowWidths = "mainWindowWidths"
static let refreshInterval = "refreshInterval" static let refreshInterval = "refreshInterval"
// Hidden prefs // Hidden prefs
@ -120,15 +119,6 @@ struct AppDefaults {
} }
} }
static var mainWindowWidths: [Int]? {
get {
return UserDefaults.standard.object(forKey: Key.mainWindowWidths) as? [Int]
}
set {
UserDefaults.standard.set(newValue, forKey: Key.mainWindowWidths)
}
}
static var refreshInterval: RefreshInterval { static var refreshInterval: RefreshInterval {
get { get {
let rawValue = UserDefaults.standard.integer(forKey: Key.refreshInterval) let rawValue = UserDefaults.standard.integer(forKey: Key.refreshInterval)

View File

@ -576,7 +576,6 @@ private extension AppDelegate {
func saveState() { func saveState() {
inspectorWindowController?.saveState() inspectorWindowController?.saveState()
mainWindowController?.saveState()
} }
func updateSortMenuItems() { func updateSortMenuItems() {

View File

@ -11,12 +11,11 @@ import Articles
import Account import Account
import RSCore import RSCore
class MainWindowController : NSWindowController, NSUserInterfaceValidations { class MainWindowController : NSWindowController, NSUserInterfaceValidations, NSWindowDelegate {
@IBOutlet var toolbarDelegate: MainWindowToolbarDelegate? @IBOutlet var toolbarDelegate: MainWindowToolbarDelegate?
private var sharingServicePickerDelegate: NSSharingServicePickerDelegate? private var sharingServicePickerDelegate: NSSharingServicePickerDelegate?
private let windowAutosaveName = NSWindow.FrameAutosaveName("MainWindow")
static var didPositionWindowOnFirstRun = false static var didPositionWindowOnFirstRun = false
private var currentFeedOrFolder: AnyObject? = nil { private var currentFeedOrFolder: AnyObject? = nil {
@ -43,7 +42,6 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
window?.titleVisibility = .hidden window?.titleVisibility = .hidden
} }
window?.setFrameUsingName(windowAutosaveName, force: true)
if AppDefaults.isFirstRun && !MainWindowController.didPositionWindowOnFirstRun { if AppDefaults.isFirstRun && !MainWindowController.didPositionWindowOnFirstRun {
if let window = window { if let window = window {
@ -56,9 +54,6 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
} }
detailSplitViewItem?.minimumThickness = CGFloat(MainWindowController.detailViewMinimumThickness) detailSplitViewItem?.minimumThickness = CGFloat(MainWindowController.detailViewMinimumThickness)
restoreSplitViewState()
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminate(_:)), name: NSApplication.willTerminateNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressDidChange(_:)), name: .AccountRefreshDidBegin, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressDidChange(_:)), name: .AccountRefreshDidBegin, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressDidChange(_:)), name: .AccountRefreshDidFinish, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressDidChange(_:)), name: .AccountRefreshDidFinish, object: nil)
@ -75,12 +70,6 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
// MARK: - API // MARK: - API
func saveState() {
saveSplitViewState()
}
func selectedObjectsInSidebar() -> [AnyObject]? { func selectedObjectsInSidebar() -> [AnyObject]? {
return sidebarViewController?.selectedObjects return sidebarViewController?.selectedObjects
@ -88,10 +77,12 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
// MARK: - Notifications // MARK: - Notifications
@objc func applicationWillTerminate(_ note: Notification) { func window(_ window: NSWindow, willEncodeRestorableState state: NSCoder) {
saveSplitViewState(to: state)
}
saveState() func window(_ window: NSWindow, didDecodeRestorableState state: NSCoder) {
window?.saveFrame(usingName: windowAutosaveName) restoreSplitViewState(from: state)
} }
@objc func refreshProgressDidChange(_ note: Notification) { @objc func refreshProgressDidChange(_ note: Notification) {
@ -397,6 +388,8 @@ extension MainWindowController : ScriptingMainWindowController {
private extension MainWindowController { private extension MainWindowController {
private static let mainWindowWidthsStateKey = "mainWindowWidths"
var splitViewController: NSSplitViewController? { var splitViewController: NSSplitViewController? {
guard let viewController = contentViewController else { guard let viewController = contentViewController else {
return nil return nil
@ -583,7 +576,7 @@ private extension MainWindowController {
} }
func saveSplitViewState() { func saveSplitViewState(to coder: NSCoder) {
// TODO: Update this for multiple windows. // TODO: Update this for multiple windows.
@ -592,16 +585,25 @@ private extension MainWindowController {
} }
let widths = splitView.arrangedSubviews.map{ Int(floor($0.frame.width)) } let widths = splitView.arrangedSubviews.map{ Int(floor($0.frame.width)) }
if AppDefaults.mainWindowWidths != widths { coder.encode(widths, forKey: MainWindowController.mainWindowWidthsStateKey)
AppDefaults.mainWindowWidths = widths
}
} }
func restoreSplitViewState() { func arrayOfIntFromCoder(_ coder: NSCoder, withKey: String) -> [Int]? {
let decodedFloats: [Int]?
do {
decodedFloats = try coder.decodeTopLevelObject(forKey: MainWindowController.mainWindowWidthsStateKey) as? [Int]? ?? nil
}
catch {
decodedFloats = nil
}
return decodedFloats
}
func restoreSplitViewState(from coder: NSCoder) {
// TODO: Update this for multiple windows. // TODO: Update this for multiple windows.
guard let splitView = splitViewController?.splitView, let widths = arrayOfIntFromCoder(coder, withKey: MainWindowController.mainWindowWidthsStateKey), widths.count == 3, let window = window else {
guard let splitView = splitViewController?.splitView, let widths = AppDefaults.mainWindowWidths, widths.count == 3, let window = window else {
return return
} }