From 531e8a44f14da2a4e496e23886d9046dee641852 Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Thu, 10 Jan 2013 02:07:04 +0100 Subject: [PATCH] Handle app termination in class method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- .../TextMate/src/AppController Documents.mm | 10 +------ .../DocumentWindow/src/DocumentController.h | 2 +- .../DocumentWindow/src/DocumentController.mm | 30 +++++++++---------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/Applications/TextMate/src/AppController Documents.mm b/Applications/TextMate/src/AppController Documents.mm index f37d8236..aa182274 100644 --- a/Applications/TextMate/src/AppController Documents.mm +++ b/Applications/TextMate/src/AppController Documents.mm @@ -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 diff --git a/Frameworks/DocumentWindow/src/DocumentController.h b/Frameworks/DocumentWindow/src/DocumentController.h index bef2e36a..5b58721a 100644 --- a/Frameworks/DocumentWindow/src/DocumentController.h +++ b/Frameworks/DocumentWindow/src/DocumentController.h @@ -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&)env; // Private (used by DocumentCommand.mm) diff --git a/Frameworks/DocumentWindow/src/DocumentController.mm b/Frameworks/DocumentWindow/src/DocumentController.mm index ae509e4b..e3a71909 100644 --- a/Frameworks/DocumentWindow/src/DocumentController.mm +++ b/Frameworks/DocumentWindow/src/DocumentController.mm @@ -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"; // drag’n’drop of tabs +static BOOL IsInShouldTerminateEventLoop = NO; @interface DocumentController () @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 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;