sindresorhus / Settings

⚙ Add a settings window to your macOS app in minutes
MIT License
1.43k stars 100 forks source link

Fix window-restoration (PR #27) #33

Closed Mortennn closed 5 years ago

Mortennn commented 5 years ago

I have modified @DivineDominion's code a bit to get centering and restoring the preferences window to work.

  1. Call showWindow().
  2. Get the screen the window is located on and calculates the new coordinates.

window.center() does not work. I don't know why.

sindresorhus commented 5 years ago

@Mortennn Any thoughts on this approach vs https://github.com/sindresorhus/touch-bar-simulator/blob/23f7c1ca46a99afa532253564c006c03e1448c1f/Touch%20Bar%20Simulator/TouchBarWindow.swift#L121-L125 ?

Mortennn commented 5 years ago

I've change the code according to your approach.

setFrameOrigin(CGPoint(x: frame.origin.x, y: 100)) In my opinion, it is better to calculate the coordinates based on the screen the preferences window is located on than relying on a hard-coded value for the y-axis in case anything changes later on.

DivineDominion commented 5 years ago

In my tests, this worked great! Thanks @Mortennn

tonyarnold commented 3 years ago

Does this work still work under macOS 11? It's not restoring to the position I drag it to on next launch.

Should this be using NSWindowRestoration instead of the frame autosave name?

sindresorhus commented 3 years ago

I just tried the bundled example app on macOS 11.5.2 and it correctly preserves its position between launches.


Should this be using NSWindowRestoration instead of the frame autosave name?

It's not clear to me which of these solutions that are the most correct solution.

sindresorhus commented 3 years ago

Maybe try switching the order of these to ensure it's not some kind of race issue: https://github.com/sindresorhus/Preferences/blob/main/Sources/Preferences/PreferencesWindowController.swift#L98-L99