From c99e65b17c56c2b56154887bb534be3daa72badc Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Thu, 25 Aug 2016 14:05:16 +0200 Subject: [PATCH] =?UTF-8?q?Let=20OakHTMLOutputView=20handle=20the=20?= =?UTF-8?q?=E2=80=9CStop=20Task=3F=E2=80=9D=20warning=20sheet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also allows for the sheet to automatically close, should the command exit before the user selects an action. --- .../DocumentWindow/src/DocumentController.mm | 27 ++++++------------- Frameworks/HTMLOutput/src/HTMLOutput.h | 2 +- Frameworks/HTMLOutput/src/HTMLOutput.mm | 3 ++- Frameworks/HTMLOutput/src/OakHTMLOutputView.h | 2 +- .../HTMLOutput/src/OakHTMLOutputView.mm | 23 ++++++++++++++-- .../HTMLOutputWindow/src/HTMLOutputWindow.mm | 9 +++---- 6 files changed, 36 insertions(+), 30 deletions(-) diff --git a/Frameworks/DocumentWindow/src/DocumentController.mm b/Frameworks/DocumentWindow/src/DocumentController.mm index ab187b5d..9525c333 100644 --- a/Frameworks/DocumentWindow/src/DocumentController.mm +++ b/Frameworks/DocumentWindow/src/DocumentController.mm @@ -648,15 +648,13 @@ namespace { if(!self.htmlOutputInWindow && _runner && _runner->running()) { - NSAlert* alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:@"Stop “%@”?", [NSString stringWithCxxString:_runner->name()]] defaultButton:@"Stop" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"The job that the task is performing will not be completed."]; - OakShowAlertForWindow(alert, self.window, ^(NSInteger returnCode){ - if(returnCode == NSAlertDefaultReturn) /* "Stop" */ + [_htmlOutputView stopLoadingWithUserInteraction:YES completionHandler:^(BOOL didStop){ + if(didStop) { - [_htmlOutputView stopLoading]; _runner.reset(); [sender performSelector:@selector(performClose:) withObject:self afterDelay:0]; } - }); + }]; return NO; } @@ -1298,10 +1296,11 @@ namespace if([windows count] && [windows indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL* stop){ return (BOOL)!((HTMLOutputWindowController*)obj).running; }] == NSNotFound) { HTMLOutputWindowController* candidate = [windows firstObject]; - auto sheetResponseHandler = ^(NSInteger returnCode){ - if(returnCode == NSAlertDefaultReturn) /* "Stop" */ + + BOOL askUser = aCommand.output_reuse != output_reuse::abort_and_reuse_busy; + [candidate.htmlOutputView stopLoadingWithUserInteraction:askUser completionHandler:^(BOOL didStop){ + if(didStop) { - [candidate.htmlOutputView stopLoading]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ candidate.commandRunner->wait_for_command(); // Must not be called in main queue dispatch_async(dispatch_get_main_queue(), ^{ @@ -1313,17 +1312,7 @@ namespace { callback(NO); } - }; - - if(aCommand.output_reuse == output_reuse::reuse_busy) - { - NSAlert* alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:@"Stop “%@”?", [NSString stringWithCxxString:aCommand.name]] defaultButton:@"Stop" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"The job that the task is performing will not be completed."]; - OakShowAlertForWindow(alert, candidate.window, sheetResponseHandler); - } - else // output_reuse::abort_and_reuse_busy - { - sheetResponseHandler(NSAlertDefaultReturn); - } + }]; return; } } diff --git a/Frameworks/HTMLOutput/src/HTMLOutput.h b/Frameworks/HTMLOutput/src/HTMLOutput.h index d3142c5b..556c7eb7 100644 --- a/Frameworks/HTMLOutput/src/HTMLOutput.h +++ b/Frameworks/HTMLOutput/src/HTMLOutput.h @@ -4,7 +4,7 @@ extern NSString* const kCommandRunnerURLScheme; @interface OakHTMLOutputView : NSView - (void)loadRequest:(NSURLRequest*)aRequest environment:(std::map const&)anEnvironment autoScrolls:(BOOL)flag; -- (void)stopLoading; +- (void)stopLoadingWithUserInteraction:(BOOL)askUserFlag completionHandler:(void(^)(BOOL didStop))handler; - (void)loadHTMLString:(NSString*)someHTML; @property (nonatomic, getter = isRunningCommand, readonly) BOOL runningCommand; diff --git a/Frameworks/HTMLOutput/src/HTMLOutput.mm b/Frameworks/HTMLOutput/src/HTMLOutput.mm index 660d7f33..32f77ea3 100644 --- a/Frameworks/HTMLOutput/src/HTMLOutput.mm +++ b/Frameworks/HTMLOutput/src/HTMLOutput.mm @@ -1,4 +1,5 @@ #import "HTMLOutput.h" +#import #import #import #import @@ -119,6 +120,6 @@ NSURLRequest* URLRequestForCommandRunner (command::runner_ptr aRunner) NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://job/%ld", kCommandRunnerURLScheme, ++LastKey]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:6000]; [NSURLProtocol setProperty:pipe.fileHandleForReading forKey:@"fileHandle" inRequest:request]; [NSURLProtocol setProperty:@(aRunner->process_id()) forKey:@"processIdentifier" inRequest:request]; - // TODO Add a description (based on bundle item name) + [NSURLProtocol setProperty:to_ns(aRunner->name()) forKey:@"processName" inRequest:request]; return request; } diff --git a/Frameworks/HTMLOutput/src/OakHTMLOutputView.h b/Frameworks/HTMLOutput/src/OakHTMLOutputView.h index 482d3215..15b747da 100644 --- a/Frameworks/HTMLOutput/src/OakHTMLOutputView.h +++ b/Frameworks/HTMLOutput/src/OakHTMLOutputView.h @@ -3,7 +3,7 @@ PUBLIC @interface OakHTMLOutputView : HOBrowserView - (void)loadRequest:(NSURLRequest*)aRequest environment:(std::map const&)anEnvironment autoScrolls:(BOOL)flag; -- (void)stopLoading; +- (void)stopLoadingWithUserInteraction:(BOOL)askUserFlag completionHandler:(void(^)(BOOL didStop))handler; - (void)loadHTMLString:(NSString*)someHTML; @property (nonatomic, getter = isRunningCommand, readonly) BOOL runningCommand; diff --git a/Frameworks/HTMLOutput/src/OakHTMLOutputView.mm b/Frameworks/HTMLOutput/src/OakHTMLOutputView.mm index 6b222b44..a2115c37 100644 --- a/Frameworks/HTMLOutput/src/OakHTMLOutputView.mm +++ b/Frameworks/HTMLOutput/src/OakHTMLOutputView.mm @@ -3,6 +3,7 @@ #import "helpers/HOAutoScroll.h" #import "helpers/HOJSBridge.h" #import +#import #import extern NSString* const kCommandRunnerURLScheme; // from HTMLOutput.h @@ -31,9 +32,27 @@ extern NSString* const kCommandRunnerURLScheme; // from HTMLOutput.h [self.webView.mainFrame loadRequest:aRequest]; } -- (void)stopLoading +- (void)stopLoadingWithUserInteraction:(BOOL)askUserFlag completionHandler:(void(^)(BOOL didStop))handler { - [self.webView.mainFrame stopLoading]; + NSURLRequest* request = self.webView.mainFrame.dataSource.initialRequest; + if(askUserFlag && self.webView.mainFrame.dataSource.isLoading && [NSURLProtocol propertyForKey:@"processIdentifier" inRequest:request]) + { + NSString* commandName = [NSURLProtocol propertyForKey:@"processName" inRequest:request]; + + NSAlert* alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:@"Stop “%@”?", commandName] defaultButton:@"Stop" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"The job that the task is performing will not be completed."]; + OakShowAlertForWindow(alert, self.window, ^(NSInteger returnCode){ + if(returnCode == NSAlertDefaultReturn) /* "Stop" */ + [self.webView.mainFrame stopLoading]; + if(handler) + handler(returnCode == NSAlertDefaultReturn); + }); + } + else + { + [self.webView.mainFrame stopLoading]; + if(handler) + handler(YES); + } } - (void)loadHTMLString:(NSString*)someHTML diff --git a/Frameworks/HTMLOutputWindow/src/HTMLOutputWindow.mm b/Frameworks/HTMLOutputWindow/src/HTMLOutputWindow.mm index b5861071..ae4df4d2 100644 --- a/Frameworks/HTMLOutputWindow/src/HTMLOutputWindow.mm +++ b/Frameworks/HTMLOutputWindow/src/HTMLOutputWindow.mm @@ -101,16 +101,13 @@ OAK_DEBUG_VAR(HTMLOutputWindow); if(!self.running) return YES; - NSAlert* alert = [NSAlert alertWithMessageText:@"Stop task before closing?" defaultButton:@"Stop Task" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"The job that the task is performing will not be completed."]; - OakShowAlertForWindow(alert, self.window, ^(NSInteger returnCode){ - D(DBF_HTMLOutputWindow, bug("close %s\n", BSTR(returnCode == NSAlertDefaultReturn));); - if(returnCode == NSAlertDefaultReturn) /* "Stop" */ + [_htmlOutputView stopLoadingWithUserInteraction:YES completionHandler:^(BOOL didStop){ + if(didStop) { [self.window orderOut:self]; - [self.htmlOutputView stopLoading]; [self.window close]; } - }); + }]; return NO; }