|
1 | 1 | import Cocoa
|
2 | 2 |
|
| 3 | +extension NSWindow.FrameAutosaveName { |
| 4 | + static let preferences: NSWindow.FrameAutosaveName = "com.sindresorhus.Preferences.FrameAutosaveName" |
| 5 | +} |
| 6 | + |
3 | 7 | public final class PreferencesWindowController: NSWindowController {
|
4 | 8 | private let tabViewController = PreferencesTabViewController()
|
5 | 9 |
|
@@ -78,16 +82,28 @@ public final class PreferencesWindowController: NSWindowController {
|
78 | 82 | /// - Parameter preferencePane: Identifier of the preference pane to display, or `nil` to show the
|
79 | 83 | /// tab that was open when the user last closed the window.
|
80 | 84 | public func show(preferencePane preferenceIdentifier: PreferencePane.Identifier? = nil) {
|
81 |
| - if !window!.isVisible { |
82 |
| - window?.center() |
83 |
| - } |
84 |
| - |
85 |
| - showWindow(self) |
86 | 85 | if let preferenceIdentifier = preferenceIdentifier {
|
87 | 86 | tabViewController.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false)
|
88 | 87 | } else {
|
89 | 88 | tabViewController.restoreInitialTab()
|
90 | 89 | }
|
| 90 | + |
| 91 | + showWindow(self) |
| 92 | + |
| 93 | + restoreWindowPosition() |
| 94 | + |
91 | 95 | NSApp.activate(ignoringOtherApps: true)
|
92 | 96 | }
|
| 97 | + |
| 98 | + private func restoreWindowPosition() { |
| 99 | + guard let window = self.window, |
| 100 | + let screenContainingWindow = window.screen |
| 101 | + else { return } |
| 102 | + |
| 103 | + let x = screenContainingWindow.visibleFrame.midX - window.frame.width / 2 |
| 104 | + let y = screenContainingWindow.visibleFrame.midY - window.frame.height / 2 |
| 105 | + window.setFrameOrigin(CGPoint(x: x, y: y)) |
| 106 | + window.setFrameUsingName(.preferences) |
| 107 | + window.setFrameAutosaveName(.preferences) |
| 108 | + } |
93 | 109 | }
|
0 commit comments