Save and restore split view state. Fix #134.
This commit is contained in:
parent
bfc57d6e7a
commit
2281c4a3d8
@ -27,6 +27,7 @@ final class AppDefaults {
|
||||
static let timelineSortDirection = "timelineSortDirection"
|
||||
static let detailFontSize = "detailFontSize"
|
||||
static let openInBrowserInBackground = "openInBrowserInBackground"
|
||||
static let mainWindowWidths = "mainWindowWidths"
|
||||
|
||||
// Hidden prefs
|
||||
static let showTitleOnMainWindow = "KafasisTitleMode"
|
||||
@ -86,6 +87,15 @@ final class AppDefaults {
|
||||
}
|
||||
}
|
||||
|
||||
var mainWindowWidths: [Int]? {
|
||||
get {
|
||||
return UserDefaults.standard.object(forKey: Key.mainWindowWidths) as? [Int]
|
||||
}
|
||||
set {
|
||||
UserDefaults.standard.set(newValue, forKey: Key.mainWindowWidths)
|
||||
}
|
||||
}
|
||||
|
||||
private init() {
|
||||
|
||||
AppDefaults.registerDefaults()
|
||||
|
@ -31,6 +31,8 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
|
||||
return window?.toolbar?.existingItem(withIdentifier: .Share)
|
||||
}
|
||||
|
||||
private static var detailViewMinimumThickness = 384
|
||||
|
||||
// MARK: - NSWindowController
|
||||
|
||||
override func windowDidLoad() {
|
||||
@ -53,8 +55,9 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
|
||||
}
|
||||
}
|
||||
|
||||
detailSplitViewItem?.minimumThickness = 384
|
||||
|
||||
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)
|
||||
@ -72,11 +75,10 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
|
||||
|
||||
func saveState() {
|
||||
|
||||
// TODO: save width of split view and anything else that should be saved.
|
||||
|
||||
|
||||
saveSplitViewState()
|
||||
}
|
||||
|
||||
|
||||
func selectedObjectsInSidebar() -> [AnyObject]? {
|
||||
|
||||
return sidebarViewController?.selectedObjects
|
||||
@ -86,6 +88,7 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
|
||||
|
||||
@objc func applicationWillTerminate(_ note: Notification) {
|
||||
|
||||
saveState()
|
||||
window?.saveFrame(usingName: windowAutosaveName)
|
||||
}
|
||||
|
||||
@ -483,5 +486,39 @@ private extension MainWindowController {
|
||||
window?.title = "\(appDelegate.appName!) (\(unreadCount))"
|
||||
}
|
||||
}
|
||||
|
||||
func saveSplitViewState() {
|
||||
|
||||
// TODO: Update this for multiple windows.
|
||||
|
||||
guard let splitView = splitViewController?.splitView else {
|
||||
return
|
||||
}
|
||||
|
||||
let widths = splitView.arrangedSubviews.map{ Int(floor($0.frame.width)) }
|
||||
AppDefaults.shared.mainWindowWidths = widths
|
||||
}
|
||||
|
||||
func restoreSplitViewState() {
|
||||
|
||||
// TODO: Update this for multiple windows.
|
||||
|
||||
guard let splitView = splitViewController?.splitView, let widths = AppDefaults.shared.mainWindowWidths, widths.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]
|
||||
|
||||
// Make sure the detail view has its mimimum thickness, at least.
|
||||
if windowWidth < sidebarWidth + dividerThickness + timelineWidth + dividerThickness + MainWindowController.detailViewMinimumThickness {
|
||||
return
|
||||
}
|
||||
|
||||
splitView.setPosition(CGFloat(sidebarWidth), ofDividerAt: 0)
|
||||
splitView.setPosition(CGFloat(sidebarWidth + dividerThickness + timelineWidth), ofDividerAt: 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user