Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any NSWindowRestoration examples?

I'm having some trouble implementing NSWindowRestoration (in 10.7 Lion). I'm not getting the protocol notifications.

Is there an example app with this implemented somewhere? I cannot find one on the Apple Developer site. Thanks!


Edit: The question marked as answer is helpful, but the problem in my case was that I was using a menubar-only application. I guess window restoration doesn't work with dockless apps yet. Snap!

like image 490
Yep Avatar asked Feb 21 '23 13:02

Yep


1 Answers

The class method + (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler as described by "El Developer" is only half of the solution.

The class that implements the method (and conforms to the NSWindowRegistration protocol) also has to be registered as the window's "Restoration Class". When the window is initially created, register it using the - (void)setRestorationClass:(Class <NSWindowRestoration>)restorationClass method.

e.g. for a window controller, for initialization:

_myWindowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"];
_myWindowController.window.restorationClass = self.class;
_myWindowController.window.identifier = @"MyWindow";

for restoration:

+ (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler {

    if ([identifier isEqualToString:@"MyWindow"]) {
        MyAppDelegate *appDelegate = (MyAppDelegate *)NSApplication.sharedApplication.delegate;
        NSWindow *myWindow = appDelegate.myWindowController.window;

        completionHandler(myWindow, nil);
    }
}
like image 87
Ryan Avatar answered Feb 28 '23 00:02

Ryan