From d6067863916361d37bc1b85f4f4152565f2176b1 Mon Sep 17 00:00:00 2001 From: Oliver Drobnik Date: Thu, 14 Feb 2013 09:53:29 +0100 Subject: [PATCH] fixed preview panel --- ...owController+DTPanelControllerPresenting.h | 6 ++- ...owController+DTPanelControllerPresenting.m | 48 +++++++++++-------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/Core/Source/OSX/NSWindowController+DTPanelControllerPresenting.h b/Core/Source/OSX/NSWindowController+DTPanelControllerPresenting.h index 22555ae8..cfa64b9f 100644 --- a/Core/Source/OSX/NSWindowController+DTPanelControllerPresenting.h +++ b/Core/Source/OSX/NSWindowController+DTPanelControllerPresenting.h @@ -24,13 +24,15 @@ @property (nonatomic, readonly, strong) NSWindowController *modalPanelController; /** - Presents the panel modally + Presents the panel modally, the panel controller is being retained. @param panelController A window controller for the sheet, usually an NSWindowController with an NSPanel as window. */ - (void)presentModalPanelController:(NSWindowController *)panelController; /** - Dismisses a currently presented modal panel. + Dismisses a currently presented modal panel. The panel controller is being released after the out animation has finished. + + You can presentModalPanelController: another panel controller right after dismissing one. */ - (void)dismissModalPanelController; diff --git a/Core/Source/OSX/NSWindowController+DTPanelControllerPresenting.m b/Core/Source/OSX/NSWindowController+DTPanelControllerPresenting.m index 50b3ecbe..15e9400b 100644 --- a/Core/Source/OSX/NSWindowController+DTPanelControllerPresenting.m +++ b/Core/Source/OSX/NSWindowController+DTPanelControllerPresenting.m @@ -10,9 +10,24 @@ #import "NSWindowController+DTPanelControllerPresenting.h" static char DTPresentedViewControllerKey; +static char DTPresentedViewControllerDismissalQueueKey; @implementation NSWindowController (DTPanelControllerPresenting) +#pragma mark - Private Methods + +// called as a result of the sheed ending notification +- (void)_didFinishDismissingPanel:(NSNotification *)notification +{ + // remove notification + [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidEndSheetNotification object:self.window]; + + // dismiss the panel controller + objc_setAssociatedObject(self, &DTPresentedViewControllerDismissalQueueKey, nil, OBJC_ASSOCIATION_RETAIN); +} + +#pragma mark - Public Methods + - (void)presentModalPanelController:(NSWindowController *)panelController { NSWindowController *windowController = self.modalPanelController; @@ -29,33 +44,28 @@ - (void)presentModalPanelController:(NSWindowController *)panelController objc_setAssociatedObject(self, &DTPresentedViewControllerKey, panelController, OBJC_ASSOCIATION_RETAIN); } - -- (void)_didFinishDismissingPanel:(NSNotification *)notification -{ - // dismiss the panel controller - objc_setAssociatedObject(self, &DTPresentedViewControllerKey, nil, OBJC_ASSOCIATION_RETAIN); - - // remove notification - [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidEndSheetNotification object:self.window]; -} - - (void)dismissModalPanelController { NSWindowController *windowController = self.modalPanelController; if (!windowController) { + NSLog(@"%s called, but nothing to dismiss", (const char *)__PRETTY_FUNCTION__); return; } - - dispatch_async(dispatch_get_main_queue(), ^{ - // get notified if panel has been dismissed - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didFinishDismissingPanel:) name:NSWindowDidEndSheetNotification object:self.window]; - - // dismiss the panel - [windowController.window close]; - [NSApp endSheet:windowController.window]; - }); + + // retain it in the dismissal queue so that we can present a new one right after the out animation has finished + objc_setAssociatedObject(self, &DTPresentedViewControllerDismissalQueueKey, windowController, OBJC_ASSOCIATION_RETAIN); + + // get notified if panel has been dismissed + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didFinishDismissingPanel:) name:NSWindowDidEndSheetNotification object:self.window]; + + // dismiss the panel + [windowController.window close]; + [NSApp endSheet:windowController.window]; + + // free the reference + objc_setAssociatedObject(self, &DTPresentedViewControllerKey, nil, OBJC_ASSOCIATION_RETAIN); } - (NSWindowController *)modalPanelController