Skip to content

Commit

Permalink
Uncrustify the objective c for some consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
caseymrm committed Jan 28, 2020
1 parent df384d5 commit 1ea9c0e
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 338 deletions.
110 changes: 55 additions & 55 deletions NSImage+Resize.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
@interface ImageCache : NSObject
+ (ImageCache *)instance;
- (void)setImage:(NSImage *)image
forName:(NSString *)name
withHeight:(CGFloat)height;
forName:(NSString *)name
withHeight:(CGFloat)height;
- (NSImage *)getImageForName:(NSString *)name withHeight:(CGFloat)height;
@property(nonatomic, strong) NSCache *imageCache;
@end
Expand All @@ -14,80 +14,80 @@ - (NSImage *)getImageForName:(NSString *)name withHeight:(CGFloat)height;
@implementation ImageCache

+ (ImageCache *)instance {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[ImageCache alloc] init];
});
return instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[ImageCache alloc] init];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
self.imageCache = [[NSCache alloc] init];
}
return self;
self = [super init];
if (self) {
self.imageCache = [[NSCache alloc] init];
}
return self;
}

- (NSString *)keyForName:(NSString *)name withHeight:(CGFloat)height {
return [NSString stringWithFormat:@"%f/%@", height, name];
return [NSString stringWithFormat:@"%f/%@", height, name];
}

- (void)setImage:(NSImage *)image
forName:(NSString *)name
withHeight:(CGFloat)height {
[self.imageCache setObject:image
forKey:[self keyForName:name withHeight:height]];
forName:(NSString *)name
withHeight:(CGFloat)height {
[self.imageCache setObject:image
forKey:[self keyForName:name withHeight:height]];
}

- (NSImage *)getImageForName:(NSString *)name withHeight:(CGFloat)height {
return
[self.imageCache objectForKey:[self keyForName:name withHeight:height]];
return
[self.imageCache objectForKey:[self keyForName:name withHeight:height]];
}

@end

@implementation NSImage (Resize)

- (NSImage *)imageWithHeight:(CGFloat)height {
NSImage *image = self;
if (![image isValid]) {
NSLog(@"Can't resize invalid image");
return nil;
}
NSSize newSize =
NSMakeSize(image.size.width * height / image.size.height, height);
NSImage *newImage = [[NSImage alloc] initWithSize:newSize];
[newImage lockFocus];
[image setSize:newSize];
[[NSGraphicsContext currentContext]
setImageInterpolation:NSImageInterpolationDefault];
[image drawAtPoint:NSZeroPoint
fromRect:CGRectMake(0, 0, newSize.width, newSize.height)
operation:NSCompositingOperationCopy
fraction:1.0];
[newImage unlockFocus];
return newImage;
NSImage *image = self;
if (![image isValid]) {
NSLog(@"Can't resize invalid image");
return nil;
}
NSSize newSize =
NSMakeSize(image.size.width * height / image.size.height, height);
NSImage *newImage = [[NSImage alloc] initWithSize:newSize];
[newImage lockFocus];
[image setSize:newSize];
[[NSGraphicsContext currentContext]
setImageInterpolation:NSImageInterpolationDefault];
[image drawAtPoint:NSZeroPoint
fromRect:CGRectMake(0, 0, newSize.width, newSize.height)
operation:NSCompositingOperationCopy
fraction:1.0];
[newImage unlockFocus];
return newImage;
}

+ (NSImage *)imageFromName:(NSString *)name withHeight:(CGFloat)height {
if (name.length == 0) {
return nil;
}
NSImage *image =
[[ImageCache instance] getImageForName:name withHeight:height];
if (image != nil) {
return image;
}
if ([name hasPrefix:@"http"]) {
image = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:name]];
} else {
image = [NSImage imageNamed:name];
}
if (height > 0 && image.size.height > height) {
image = [image imageWithHeight:height];
}
[[ImageCache instance] setImage:image forName:name withHeight:height];
return image;
if (name.length == 0) {
return nil;
}
NSImage *image =
[[ImageCache instance] getImageForName:name withHeight:height];
if (image != nil) {
return image;
}
if ([name hasPrefix:@"http"]) {
image = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:name]];
} else {
image = [NSImage imageNamed:name];
}
if (height > 0 && image.size.height > height) {
image = [image imageWithHeight:height];
}
[[ImageCache instance] setImage:image forName:name withHeight:height];
return image;
}

@end
108 changes: 54 additions & 54 deletions alert.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,58 @@
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;
NSTextField *textfield =
[[NSTextField 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:[NSTextField class]]) {
continue;
}
[values addObject:((NSTextField *)subview).stringValue];
}
}
NSData *jsonData =
[NSJSONSerialization dataWithJSONObject:values options:0 error:nil];
NSString *jsonString =
[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
alertClicked(resp - NSAlertFirstButtonReturn, jsonString.UTF8String);
});
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;
NSTextField *textfield =
[[NSTextField 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:[NSTextField class]]) {
continue;
}
[values addObject:((NSTextField *)subview).stringValue];
}
}
NSData *jsonData =
[NSJSONSerialization dataWithJSONObject:values options:0 error:nil];
NSString *jsonString =
[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
alertClicked(resp - NSAlertFirstButtonReturn, jsonString.UTF8String);
});
}
Loading

0 comments on commit 1ea9c0e

Please sign in to comment.