Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore active preference when reopening window #26

Merged
merged 4 commits into from
Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Preferences/PreferencesStyleController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
29 changes: 17 additions & 12 deletions Sources/Preferences/PreferencesTabViewController.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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 {
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions Sources/Preferences/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public func show(preferencePane preferenceIdentifier: PreferencePaneIdentifier? = nil) {
public func show(preferencePane preferencePaneIdentifier: PreferencePaneIdentifier? = nil) {

?

I think we should be consistent in using preferencePane over just preference.

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)
}
}