-
Notifications
You must be signed in to change notification settings - Fork 20
/
alert.m
63 lines (58 loc) · 2.32 KB
/
alert.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#import <Cocoa/Cocoa.h>
#import "alert.h"
#import "EditableNSTextField.h"
void alertClicked(int, const char *);
void showAlert(const char *jsonString) {
NSDictionary *jsonDict = [NSJSONSerialization
JSONObjectWithData:[[NSString stringWithUTF8String:jsonString]
dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
NSAlert *alert = [NSAlert new];
alert.messageText = jsonDict[@"MessageText"];
alert.informativeText = jsonDict[@"InformativeText"];
NSArray *buttons = jsonDict[@"Buttons"];
if (![buttons isEqualTo:NSNull.null] && buttons.count > 0) {
for (NSString *label in buttons) {
[alert addButtonWithTitle:label];
}
}
NSView *accessoryView;
NSArray *inputs = jsonDict[@"Inputs"];
BOOL hasInputs = ![inputs isEqualTo:NSNull.null] && inputs.count > 0;
if (hasInputs) {
BOOL first = false;
int y = 30 * inputs.count;
accessoryView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 200, y)];
for (NSString *input in inputs) {
y -= 30;
EditableNSTextField *textfield =
[[EditableNSTextField alloc] initWithFrame:NSMakeRect(0, y, 200, 25)];
[textfield setPlaceholderString:input];
[accessoryView addSubview:textfield];
if (!first) {
[alert.window setInitialFirstResponder:textfield];
first = true;
}
}
[alert setAccessoryView:accessoryView];
}
[NSApp activateIgnoringOtherApps:YES];
NSInteger resp = [alert runModal];
NSMutableArray *values = [NSMutableArray new];
if (hasInputs) {
for (NSView *subview in accessoryView.subviews) {
if (![subview isKindOfClass:[EditableNSTextField class]]) {
continue;
}
[values addObject:((EditableNSTextField *)subview).stringValue];
}
}
NSData *jsonData =
[NSJSONSerialization dataWithJSONObject:values options:0 error:nil];
NSString *jsonString =
[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
alertClicked(resp - NSAlertFirstButtonReturn, jsonString.UTF8String);
});
}