Skip to content

Commit

Permalink
Fix window-restoration (sindresorhus#33)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Mortennn authored and dezinezync committed Dec 17, 2022
1 parent f9a0c10 commit 0344aa3
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions Sources/Preferences/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Cocoa

extension NSWindow.FrameAutosaveName {
static let preferences: NSWindow.FrameAutosaveName = "com.sindresorhus.Preferences.FrameAutosaveName"
}

public final class PreferencesWindowController: NSWindowController {
private let tabViewController = PreferencesTabViewController()

Expand Down Expand Up @@ -78,16 +82,28 @@ public final class PreferencesWindowController: NSWindowController {
/// - 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: PreferencePane.Identifier? = nil) {
if !window!.isVisible {
window?.center()
}

showWindow(self)
if let preferenceIdentifier = preferenceIdentifier {
tabViewController.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false)
} else {
tabViewController.restoreInitialTab()
}

showWindow(self)

restoreWindowPosition()

NSApp.activate(ignoringOtherApps: true)
}

private func restoreWindowPosition() {
guard let window = self.window,
let screenContainingWindow = window.screen
else { return }

let x = screenContainingWindow.visibleFrame.midX - window.frame.width / 2
let y = screenContainingWindow.visibleFrame.midY - window.frame.height / 2
window.setFrameOrigin(CGPoint(x: x, y: y))
window.setFrameUsingName(.preferences)
window.setFrameAutosaveName(.preferences)
}
}

0 comments on commit 0344aa3

Please sign in to comment.