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

[WIP] center window on first start only, then restore from user defaults #27

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 18 additions & 4 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-WIP-1"
}

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

Expand Down Expand Up @@ -60,12 +64,22 @@ public final class PreferencesWindowController: NSWindowController {
/// - 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.
public func show(preferencePane preferenceIdentifier: PreferencePaneIdentifier? = nil) {
if !window!.isVisible {
window?.center()
}

centerWindowOnFirstStart()
showWindow(self)
tabViewController.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false)
NSApp.activate(ignoringOtherApps: true)
}

private func centerWindowOnFirstStart() {
guard let window = self.window else {
return
}

// When setting the autosave name, the current position isn't stored immediately.
// Trying to read the frame values will fail, so we can center it.
window.setFrameAutosaveName(.preferences)
if window.setFrameUsingName(.preferences) == false {
window.center()
}
}
}