Conversation

Fahim Farook

Has anybody had luck getting a Catalyst app to restore window frame (size & position) after closing all windows of the app and then tapping the dock icon to launch the app again?

If I quit the app and re-launch, it remembers the frame fine using my current mechanism, but if I close all windows and re-launch from dock, it places the app at the default position rather than the previously remembered position.

I got this to work correctly for a #SwiftUI app under macOS after a bit of tinkering but that was using `NSApplicationDelegate` methods. Haven’t had much luck with `UIApplicationDelegate` methods for the Catalyst app so far because my previous method involved using `applicationShouldHandleReopen` which is not there in `UIApplicationDelegate` ….

So wondering if anybody has run into this before and fixed it before I do a deeper dive …

#macOS #Swift #Coding #Catalyst #SwiftUI
2
0
1

@f do you follow ‪@stroughtonsmith‬? He posts on related topics

1
0
0
@trode I used to follow @stroughtonsmith on Twitter but had no idea that he was on the Fediverse 🙂 Thanks for the pointer, Blake!
1
0
1
@marksmith Thank you 🙂 Will take a look see …
0
0
0

@f @trode you want something like this in your AppKit bundle, where you set a specific frame autosave name for the NSWindow (AppKit bundle a la https://github.com/steventroughtonsmith/CatalystPrefsWindow/blob/main/AppKitIntegration/AppKitController.swift)

1
0
0
@stroughtonsmith Thanks for the answer. I do have that (or similar mechanism) in place but that appears to work only for new launches.

If you close all windows after launching (and saving the frame properly) if you re-launch from the dock icon (where the app itself has not terminated), the new app window appears in the center of the screen instead of the last saved location.

But will try your specific code and get back on this since I might have missed a nuance and maybe your code does work for all cases? I hate to say something doesn’t work without trying it out … So will update once I have tested.
2
0
0

@f while I can't guarantee your specific use-case, these things generally need to be set from the callback I use in that project (triggered by private notification) as timing matters during window construction

1
0
1

@f I have been working on a Mac Catalyst app and thanks to your question just noticed it has the same re-launch problem. I'll be looking at it more in the morning, thanks.

2
0
0
@stevengharris Glad to highlight issues, even though I have no solutions 😛

I found one possible answer but it involves bridging to AppKit as mentioned here:

https://betterprogramming.pub/how-to-access-the-appkit-api-from-mac-catalyst-apps-2184527020b5

Haven’t tried it out yet though. I have small test app set up where I’ve been trying out different things. Haven’t had the chance to try the above approach yet, but if I do figure something out today, will let you know …
0
0
1
@stroughtonsmith I tested out your code and it works beautifully — thank you 🙂

I could have sworn I tried something similar before and it didn’t work but, I’m too lazy to go hunting for the code that didn’t work to see what the difference is …

On the plus side, your solution is way less code than my original code too. So a win overall. Thanks again!
0
0
1
@stevengharris There’s a solution provided by @stroughtonsmith in this same thread that worked for me. Just letting you know.

If you’d prefer to see the code in action, I can always send you the final code that worked for me. Let me know.
1
0
0

@f @stroughtonsmith I modeled what I'm doing off of his example, but I still see the problem. If I just launch the CatalystPrefsWindow (without the Preferences showing), move/resize, press the close button, and then re-open the window from the dock, it opens at the original position/size in the middle of the screen. If instead of using the close button, I switch to Xcode and terminate it from Xcode, then when I launch the app again from Xcode, it opens in the new position/size.

1
0
0

@stevengharris @f could be a few things going on here; Catalyst apps don't quit immediately when you quit them, they stay alive long enough to save state. Killing with Xcode will kill them outright. State restoration behavior is what can interfere with window frame, as all windows in Catalyst use state restoration by default (which you can turn off at the AppKit level if necessary)

1
0
0

@stroughtonsmith @f Thanks. (That prefs example and others have been a huge help.) If the Catalyst app is put in the background by selecting Xcode or another app, then it saves state properly, and can then be killed with Xcode and have state restored, which works fine. The case that is (I think) a problem is pressing the close button on the app itself. In that case, it always re-opens in the middle again with default size

2
0
0

@stevengharris @f when you hit the close box, you are intentionally throwing the state away — a new window is created on next launch. Using the frame autosave name through AppKit *should* take precedence, though

0
0
0
@stevengharris The implementation I created works fine for closing using the close button. Originally, if I quit the app then the frame would be saved but not if I used the close button and then re-launched the (unterminated) app from the dock icon.

In implementing a solution, what I noticed (at least the way I did it) was that if I kept a reference to the AppKit bridging plugin, the state would be saved correctly. But if I didn’t, then the state would not be saved on using the Close button. However, this could just be the way I implemented things …

Would be happy to share the code if it would help …
@stroughtonsmith
0
0
0