Handle app termination in class method

It didn’t really make sense as an instance method since it iterate all instances, and we also want to execute code incase of no instances (save that fact as session data).
This commit is contained in:
Allan Odgaard
2013-01-10 02:07:04 +01:00
parent a7a633f993
commit 531e8a44f1
3 changed files with 17 additions and 25 deletions

View File

@@ -214,14 +214,6 @@ static NSString* const OakGlobalSessionInfo = @"OakGlobalSessionInfo";
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender
{
D(DBF_AppController_Documents, bug("%s\n", [NSApp windows].description.UTF8String););
for(NSWindow* window in [NSApp orderedWindows])
{
DocumentController* delegate = (DocumentController*)[window delegate];
if([delegate isKindOfClass:[DocumentController class]])
return [delegate applicationShouldTerminate:sender];
}
[DocumentController saveSessionIncludingUntitledDocuments:NO];
return NSTerminateNow;
return [DocumentController applicationShouldTerminate:sender];
}
@end

View File

@@ -21,6 +21,7 @@ PUBLIC @interface DocumentController : NSObject
+ (BOOL)restoreSession;
+ (BOOL)saveSessionIncludingUntitledDocuments:(BOOL)includeUntitled;
+ (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender;
- (void)showWindow:(id)sender;
- (void)openAndSelectDocument:(document::document_ptr const&)aDocument;
@@ -82,7 +83,6 @@ PUBLIC @interface DocumentController : NSObject
// ==============
+ (instancetype)controllerForDocument:(document::document_ptr const&)aDocument;
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender;
- (void)updateVariables:(std::map<std::string, std::string>&)env;
// Private (used by DocumentCommand.mm)

View File

@@ -31,6 +31,7 @@
namespace find_tags { enum { in_document = 1, in_selection, in_project, in_folder }; } // From AppController.h
static NSString* const OakDocumentPboardType = @"OakDocumentPboardType"; // dragndrop of tabs
static BOOL IsInShouldTerminateEventLoop = NO;
@interface DocumentController () <NSWindowDelegate, OakTabBarViewDelegate, OakTabBarViewDataSource, OakTextViewDelegate, OakFileBrowserDelegate>
@property (nonatomic) ProjectLayoutView* layoutView;
@@ -53,8 +54,6 @@ static NSString* const OakDocumentPboardType = @"OakDocumentPboardType"; // drag
@property (nonatomic) OakFilterWindowController* filterWindowController;
@property (nonatomic) NSUInteger fileChooserSourceIndex;
@property (nonatomic) BOOL applicationTerminationEventLoopRunning;
+ (void)scheduleSessionBackup:(id)sender;
- (void)makeTextViewFirstResponder:(id)sender;
@@ -365,9 +364,9 @@ namespace
size_t _count;
};
if(self.applicationTerminationEventLoopRunning)
if(IsInShouldTerminateEventLoop)
{
self.applicationTerminationEventLoopRunning = NO;
IsInShouldTerminateEventLoop = NO;
[NSApp replyToApplicationShouldTerminate:NO];
}
@@ -525,30 +524,31 @@ namespace
return NO;
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender
+ (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender
{
std::vector<document::document_ptr> documents;
for(NSWindow* window in [NSApp orderedWindows])
{
DocumentController* delegate = (DocumentController*)[window delegate];
if([delegate isKindOfClass:[DocumentController class]])
std::copy_if(delegate.documents.begin(), delegate.documents.end(), back_inserter(documents), [](document::document_ptr const& doc){ return doc->is_modified(); });
}
for(DocumentController* delegate in SortedControllers())
std::copy_if(delegate.documents.begin(), delegate.documents.end(), back_inserter(documents), [](document::document_ptr const& doc){ return doc->is_modified(); });
if(documents.empty())
{
[DocumentController saveSessionIncludingUntitledDocuments:NO];
return NSTerminateNow;
}
self.applicationTerminationEventLoopRunning = YES;
[self showCloseWarningUIForDocuments:documents completionHandler:^(BOOL canClose){
IsInShouldTerminateEventLoop = YES;
DocumentController* controller = [SortedControllers() firstObject];
[controller showCloseWarningUIForDocuments:documents completionHandler:^(BOOL canClose){
if(canClose)
[DocumentController saveSessionIncludingUntitledDocuments:NO];
if(self.applicationTerminationEventLoopRunning)
if(IsInShouldTerminateEventLoop)
[NSApp replyToApplicationShouldTerminate:canClose];
else if(canClose)
[NSApp terminate:self];
self.applicationTerminationEventLoopRunning = NO;
IsInShouldTerminateEventLoop = NO;
}];
return NSTerminateLater;