From 67e6c7364598fcc5ce9e26aa22100bc5eb8b460c Mon Sep 17 00:00:00 2001 From: Christian Tietze Date: Sat, 13 Apr 2019 08:13:52 +0200 Subject: [PATCH 1/3] restore last open tab when re-opening the window --- .../Preferences/PreferencesStyleController.swift | 2 +- .../Preferences/PreferencesTabViewController.swift | 13 +++---------- .../Preferences/PreferencesWindowController.swift | 4 +++- 3 files changed, 7 insertions(+), 12 deletions(-) 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 5e5880b..d29cb07 100644 --- a/Sources/Preferences/PreferencesTabViewController.swift +++ b/Sources/Preferences/PreferencesTabViewController.swift @@ -57,19 +57,12 @@ final class PreferencesTabViewController: NSViewController, PreferencesStyleCont window.toolbar = toolbar } - func activateTab(preference: PreferencePane?, animated: Bool) { - guard let preference = preference else { - return activateTab(index: 0, animated: animated) - } - + func activateTab(preference: PreferencePane, animated: Bool) { activateTab(preferenceIdentifier: preference.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) } diff --git a/Sources/Preferences/PreferencesWindowController.swift b/Sources/Preferences/PreferencesWindowController.swift index 65452b5..926eaea 100644 --- a/Sources/Preferences/PreferencesWindowController.swift +++ b/Sources/Preferences/PreferencesWindowController.swift @@ -65,7 +65,9 @@ public final class PreferencesWindowController: NSWindowController { } showWindow(self) - tabViewController.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false) + if let preferenceIdentifier = preferenceIdentifier { + tabViewController.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false) + } NSApp.activate(ignoringOtherApps: true) } } From 8c5dd03f827fef87e7da59d69fc15ccecda840c9 Mon Sep 17 00:00:00 2001 From: Christian Tietze Date: Sat, 13 Apr 2019 08:16:55 +0200 Subject: [PATCH 2/3] clarify what happens when you pass `nil` to `show` --- Sources/Preferences/PreferencesWindowController.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/Preferences/PreferencesWindowController.swift b/Sources/Preferences/PreferencesWindowController.swift index 926eaea..79989df 100644 --- a/Sources/Preferences/PreferencesWindowController.swift +++ b/Sources/Preferences/PreferencesWindowController.swift @@ -57,8 +57,9 @@ 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() From 4e3287bdf014f60b94af1ce4d30112cf97c77321 Mon Sep 17 00:00:00 2001 From: Christian Tietze Date: Sat, 20 Apr 2019 10:42:06 +0200 Subject: [PATCH 3/3] fix calling `show(preferencePane: nil)` not showing anything at all --- .../Preferences/PreferencesTabViewController.swift | 14 +++++++++++++- .../Preferences/PreferencesWindowController.swift | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Sources/Preferences/PreferencesTabViewController.swift b/Sources/Preferences/PreferencesTabViewController.swift index 31b1b54..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 @@ -84,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 { @@ -107,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 9c1af1f..46a0c6b 100644 --- a/Sources/Preferences/PreferencesWindowController.swift +++ b/Sources/Preferences/PreferencesWindowController.swift @@ -85,6 +85,8 @@ public final class PreferencesWindowController: NSWindowController { showWindow(self) if let preferenceIdentifier = preferenceIdentifier { tabViewController.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false) + } else { + tabViewController.restoreInitialTab() } NSApp.activate(ignoringOtherApps: true) }