diff --git a/Sources/Preferences/PreferencesStyleController.swift b/Sources/Preferences/PreferencesStyleController.swift index b9bd354..40e1ca3 100644 --- a/Sources/Preferences/PreferencesStyleController.swift +++ b/Sources/Preferences/PreferencesStyleController.swift @@ -11,6 +11,6 @@ protocol PreferencesStyleController: AnyObject { } protocol PreferencesStyleControllerDelegate: AnyObject { - func activateTab(preferenceIdentifier: PreferencePaneIdentifier?, animated: Bool) + func activateTab(preferenceIdentifier: PreferencePaneIdentifier, animated: Bool) func activateTab(index: Int, animated: Bool) } diff --git a/Sources/Preferences/PreferencesTabViewController.swift b/Sources/Preferences/PreferencesTabViewController.swift index 656cf35..e491abb 100644 --- a/Sources/Preferences/PreferencesTabViewController.swift +++ b/Sources/Preferences/PreferencesTabViewController.swift @@ -1,7 +1,7 @@ import Cocoa final class PreferencesTabViewController: NSViewController, PreferencesStyleControllerDelegate { - private var activeTab: Int! + private var activeTab: Int? private var preferencePanes = [PreferencePane]() internal var preferencePanesCount: Int { return preferencePanes.count @@ -54,19 +54,12 @@ final class PreferencesTabViewController: NSViewController, PreferencesStyleCont window.toolbar = toolbar } - func activateTab(preferencePane: PreferencePane?, animated: Bool) { - guard let preference = preferencePane else { - return activateTab(index: 0, animated: animated) - } - - activateTab(preferenceIdentifier: preference.preferencePaneIdentifier, animated: animated) + func activateTab(preferencePane: PreferencePane, animated: Bool) { + activateTab(preferenceIdentifier: preferencePane.preferencePaneIdentifier, animated: animated) } - func activateTab(preferenceIdentifier: PreferencePaneIdentifier?, animated: Bool) { - guard - let preferenceIdentifier = preferenceIdentifier, - let index = (preferencePanes.firstIndex { $0.preferencePaneIdentifier == preferenceIdentifier }) - else { + func activateTab(preferenceIdentifier: PreferencePaneIdentifier, animated: Bool) { + guard let index = (preferencePanes.firstIndex { $0.preferencePaneIdentifier == preferenceIdentifier }) else { return activateTab(index: 0, animated: animated) } @@ -91,6 +84,12 @@ final class PreferencesTabViewController: NSViewController, PreferencesStyleCont } } + func restoreInitialTab() { + if activeTab == nil { + activateTab(index: 0, animated: false) + } + } + private func updateWindowTitle(tabIndex: Int) { self.window.title = { if preferencePanes.count > 1 { @@ -114,6 +113,12 @@ final class PreferencesTabViewController: NSViewController, PreferencesStyleCont } private func animateTabTransition(index: Int, animated: Bool) { + guard let activeTab = activeTab else { + assertionFailure("animateTabTransition called before a tab was displayed; transition only works from one tab to another") + immediatelyDisplayTab(index: index) + return + } + let fromViewController = children[activeTab] let toViewController = children[index] let options: NSViewController.TransitionOptions = animated && isAnimated diff --git a/Sources/Preferences/PreferencesWindowController.swift b/Sources/Preferences/PreferencesWindowController.swift index b4f0498..46a0c6b 100644 --- a/Sources/Preferences/PreferencesWindowController.swift +++ b/Sources/Preferences/PreferencesWindowController.swift @@ -74,15 +74,20 @@ public final class PreferencesWindowController: NSWindowController { /// /// - See `close()` to close the window again. /// - See `showWindow(_:)` to show the window without the convenience of activating the app. - /// - Note: Unless you need to open a specific pane, prefer not to pass a parameter at all - /// - Parameter preferencePane: Identifier of the preference pane to display. + /// - Note: Unless you need to open a specific pane, prefer not to pass a parameter at all or `nil`. + /// - Parameter preferencePane: Identifier of the preference pane to display, or `nil` to show the + /// tab that was open when the user last closed the window. public func show(preferencePane preferenceIdentifier: PreferencePaneIdentifier? = nil) { if !window!.isVisible { window?.center() } showWindow(self) - tabViewController.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false) + if let preferenceIdentifier = preferenceIdentifier { + tabViewController.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false) + } else { + tabViewController.restoreInitialTab() + } NSApp.activate(ignoringOtherApps: true) } }