Skip to content

Commit

Permalink
dispatch if not mainthread, fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
fcjr committed Feb 18, 2021
1 parent 2a3db8c commit 3e4e48f
Showing 1 changed file with 58 additions and 24 deletions.
82 changes: 58 additions & 24 deletions internal/nsalert/alert.m
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
#import <stdlib.h>
#import <AppKit/NSAlert.h>

void info(char *title, char *message) {
void alert(char *title, char *message, NSAlertStyle style) {
NSString* titleString = [NSString stringWithCString:title encoding:[NSString defaultCStringEncoding]];
NSString* messageString = [NSString stringWithCString:message encoding:[NSString defaultCStringEncoding]];
if([NSThread isMainThread]) {
NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = titleString;
alert.informativeText = messageString;
alert.alertStyle = NSAlertStyleInformational;

NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = titleString;
alert.informativeText = messageString;
alert.alertStyle = NSAlertStyleInformational;
[alert runModal];
[alert release];
return;
} else {
dispatch_group_t wg = dispatch_group_create();
dispatch_group_enter(wg);
dispatch_async(dispatch_get_main_queue(), ^(void){
NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = titleString;
alert.informativeText = messageString;
alert.alertStyle = NSAlertStyleInformational;

[alert runModal];
[alert release];
[alert runModal];
[alert release];
dispatch_group_leave(wg);
return;
});
dispatch_group_wait(wg, DISPATCH_TIME_FOREVER);
}
}

void error(char *title, char *message) {
NSString* titleString = [NSString stringWithCString:title encoding:[NSString defaultCStringEncoding]];
NSString* messageString = [NSString stringWithCString:message encoding:[NSString defaultCStringEncoding]];

NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = titleString;
alert.informativeText = messageString;
alert.alertStyle = NSAlertStyleCritical;
void info(char *title, char *message) {
alert(title, message, NSAlertStyleInformational);
}

[alert runModal];
[alert release];
void error(char *title, char *message) {
alert(title, message, NSAlertStyleCritical);
}

bool question(char *title, char *message, char *defaultButton, char*alternateButton) {
Expand All @@ -33,14 +45,36 @@ bool question(char *title, char *message, char *defaultButton, char*alternateBut
NSString* defaultButtonString = [NSString stringWithCString:defaultButton encoding:[NSString defaultCStringEncoding]];
NSString* alternateButtonString = [NSString stringWithCString:alternateButton encoding:[NSString defaultCStringEncoding]];

NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = titleString;
alert.informativeText = messageString;
[alert addButtonWithTitle:defaultButtonString];
[alert addButtonWithTitle:alternateButtonString];
__block NSInteger button;
if([NSThread isMainThread]) {
NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = titleString;
alert.informativeText = messageString;
[alert addButtonWithTitle:defaultButtonString];
[alert addButtonWithTitle:alternateButtonString];

button = [alert runModal];
[alert release];
} else {
dispatch_group_t wg = dispatch_group_create();
dispatch_group_enter(wg);
dispatch_async(dispatch_get_main_queue(), ^(void){

NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = titleString;
alert.informativeText = messageString;
[alert addButtonWithTitle:defaultButtonString];
[alert addButtonWithTitle:alternateButtonString];

button = [alert runModal];
[alert release];

dispatch_group_leave(wg);
return;
});
dispatch_group_wait(wg, DISPATCH_TIME_FOREVER);
}

NSInteger button = [alert runModal];
[alert release];
if (button == NSAlertFirstButtonReturn) {
return true;
}
Expand Down

0 comments on commit 3e4e48f

Please sign in to comment.