From 397dc877af7149a396ebdd52220fbbd7a283b501 Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Sat, 15 Dec 2012 23:54:42 +0100 Subject: [PATCH] Use a block-based wrapper for NSAlert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This code isn’t ideal: We should make it a function instead of a class and the ‘info’ property should instead be done using a captured variable. --- .../DocumentWindow/src/DocumentCommand.mm | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/Frameworks/DocumentWindow/src/DocumentCommand.mm b/Frameworks/DocumentWindow/src/DocumentCommand.mm index 31ed15a9..815b7b75 100644 --- a/Frameworks/DocumentWindow/src/DocumentCommand.mm +++ b/Frameworks/DocumentWindow/src/DocumentCommand.mm @@ -21,25 +21,30 @@ #import #import -@interface OakShowCommandErrorDelegate : NSObject -{ - oak::uuid_t commandUUID; -} +@interface OakAlertBlockWrapper : NSObject +@property (nonatomic, retain) NSAlert* alert; +@property (nonatomic, retain) NSString* info; +@property (nonatomic, retain) void(^completionHandler)(OakAlertBlockWrapper*, NSInteger); @end -@implementation OakShowCommandErrorDelegate -- (id)initWithCommandUUID:(oak::uuid_t const&)anUUID +@implementation OakAlertBlockWrapper +- (void)showAlert:(NSAlert*)anAlert forWindow:(NSWindow*)aWindow completionHandler:(void(^)(OakAlertBlockWrapper*, NSInteger))aCallback { - if(self = [super init]) - commandUUID = anUUID; - return self; + self.completionHandler = aCallback; + self.alert = anAlert; + [self retain]; + + if(aWindow) + [anAlert beginSheetModalForWindow:aWindow modalDelegate:self didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:) contextInfo:NULL]; + else [self alertSheetDidEnd:anAlert returnCode:[anAlert runModal] contextInfo:NULL]; } -- (void)commandErrorDidEnd:(NSAlert*)alert returnCode:(NSInteger)returnCode contextInfo:(void*)info +- (void)alertSheetDidEnd:(NSAlert*)alert returnCode:(NSInteger)returnCode contextInfo:(void*)info { - if(returnCode == NSAlertSecondButtonReturn) - [[BundleEditor sharedInstance] revealBundleItem:bundles::lookup(commandUUID)]; - [alert release]; + self.completionHandler(self, returnCode); + self.completionHandler = nil; + self.alert = nil; + self.info = nil; [self release]; } @end @@ -341,8 +346,10 @@ void show_command_error (std::string const& message, oak::uuid_t const& uuid, NS [alert setInformativeText:[NSString stringWithCxxString:message] ?: @"No output"]; [alert addButtons:@"OK", @"Edit Command", nil]; - OakShowCommandErrorDelegate* delegate = [[OakShowCommandErrorDelegate alloc] initWithCommandUUID:uuid]; - if(window) - [alert beginSheetModalForWindow:window modalDelegate:delegate didEndSelector:@selector(commandErrorDidEnd:returnCode:contextInfo:) contextInfo:NULL]; - else [delegate commandErrorDidEnd:alert returnCode:[alert runModal] contextInfo:NULL]; + OakAlertBlockWrapper* wrapper = [[OakAlertBlockWrapper new] autorelease]; + wrapper.info = [NSString stringWithCxxString:uuid]; + [wrapper showAlert:alert forWindow:window completionHandler:^(OakAlertBlockWrapper* aWrapper, NSInteger button){ + if(button == NSAlertSecondButtonReturn) + [[BundleEditor sharedInstance] revealBundleItem:bundles::lookup(to_s(aWrapper.info))]; + }]; }