Skip to content

Commit

Permalink
fixed preview panel
Browse files Browse the repository at this point in the history
  • Loading branch information
odrobnik committed Feb 14, 2013
1 parent e7b6d7f commit d606786
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
Expand Up @@ -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;

Expand Down
48 changes: 29 additions & 19 deletions Core/Source/OSX/NSWindowController+DTPanelControllerPresenting.m
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit d606786

Please sign in to comment.