Skip to content

Commit cc25d58

Browse files
MortennnDivineDominion
authored andcommitted
Fix window-restoration (#33)
* Suggested approach: sindresorhus/touch-bar-simulator:Touch%20Bar%20Simulator/TouchBarWindow.swift@23f7c1c#L121-L125 * use `visibleFrame` instead of `frame` to calculate the right coordinates * Remove `window?.center()` and `showWindow()` which had no effect
1 parent 3abe974 commit cc25d58

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

Sources/Preferences/PreferencesWindowController.swift

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import Cocoa
22

3+
extension NSWindow.FrameAutosaveName {
4+
static let preferences: NSWindow.FrameAutosaveName = "com.sindresorhus.Preferences.FrameAutosaveName"
5+
}
6+
37
public final class PreferencesWindowController: NSWindowController {
48
private let tabViewController = PreferencesTabViewController()
59

@@ -78,16 +82,28 @@ public final class PreferencesWindowController: NSWindowController {
7882
/// - Parameter preferencePane: Identifier of the preference pane to display, or `nil` to show the
7983
/// tab that was open when the user last closed the window.
8084
public func show(preferencePane preferenceIdentifier: PreferencePane.Identifier? = nil) {
81-
if !window!.isVisible {
82-
window?.center()
83-
}
84-
85-
showWindow(self)
8685
if let preferenceIdentifier = preferenceIdentifier {
8786
tabViewController.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false)
8887
} else {
8988
tabViewController.restoreInitialTab()
9089
}
90+
91+
showWindow(self)
92+
93+
restoreWindowPosition()
94+
9195
NSApp.activate(ignoringOtherApps: true)
9296
}
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+
}
93109
}

0 commit comments

Comments
 (0)