Let OakHTMLOutputView handle the “Stop Task?” warning sheet

This also allows for the sheet to automatically close, should the command exit before the user selects an action.
This commit is contained in:
Allan Odgaard
2016-08-25 14:05:16 +02:00
parent 2c41cf8fa0
commit c99e65b17c
6 changed files with 36 additions and 30 deletions

View File

@@ -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;
}
}

View File

@@ -4,7 +4,7 @@ extern NSString* const kCommandRunnerURLScheme;
@interface OakHTMLOutputView : NSView
- (void)loadRequest:(NSURLRequest*)aRequest environment:(std::map<std::string, std::string> 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;

View File

@@ -1,4 +1,5 @@
#import "HTMLOutput.h"
#import <ns/ns.h>
#import <OakSystem/process.h>
#import <command/runner.h>
#import <oak/debug.h>
@@ -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;
}

View File

@@ -3,7 +3,7 @@
PUBLIC @interface OakHTMLOutputView : HOBrowserView
- (void)loadRequest:(NSURLRequest*)aRequest environment:(std::map<std::string, std::string> 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;

View File

@@ -3,6 +3,7 @@
#import "helpers/HOAutoScroll.h"
#import "helpers/HOJSBridge.h"
#import <OakFoundation/NSString Additions.h>
#import <OakAppKit/OakAppKit.h>
#import <oak/debug.h>
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

View File

@@ -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;
}