2020-09-14 02:48:55 +00:00
|
|
|
//
|
|
|
|
// Transformers.m
|
|
|
|
// Ample
|
|
|
|
//
|
|
|
|
// Created by Kelvin Sherlock on 9/13/2020.
|
|
|
|
// Copyright © 2020 Kelvin Sherlock. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "Transformers.h"
|
|
|
|
|
2020-09-28 02:06:03 +00:00
|
|
|
#import <AppKit/NSColor.h>
|
|
|
|
|
2020-09-14 02:48:55 +00:00
|
|
|
@implementation FilePathTransformer
|
|
|
|
|
|
|
|
+ (Class)transformedValueClass {
|
|
|
|
return [NSString class];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (BOOL)allowsReverseTransformation {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)transformedValue:(id)value {
|
|
|
|
if (!value) return value;
|
|
|
|
|
|
|
|
return [(NSString *)value lastPathComponent];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation FileSizeTransformer
|
|
|
|
|
|
|
|
+ (Class)transformedValueClass {
|
|
|
|
return [NSString class];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (BOOL)allowsReverseTransformation {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)transformedValue:(id)value {
|
|
|
|
if (!value) return value;
|
|
|
|
if (![value respondsToSelector: @selector(integerValue)]) {
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"Value (%@) does not respond to -integerValue.",
|
|
|
|
[value class]];
|
|
|
|
}
|
|
|
|
NSInteger size = [(NSNumber *)value integerValue];
|
|
|
|
|
|
|
|
if (size < 0) return nil;
|
|
|
|
if (size < 1024*1024) return [NSString stringWithFormat: @"%.1fKB", (float)size / 1024];
|
|
|
|
if (size < 1024*1024*1024) return [NSString stringWithFormat: @"%.1fMB", (float)size / (1024*1024)];
|
|
|
|
|
|
|
|
return [NSString stringWithFormat: @"%.1fGB", (float)size / (1024*1024*1024)];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2020-09-28 02:06:03 +00:00
|
|
|
@implementation ValidColorTransformer
|
|
|
|
+ (BOOL)allowsReverseTransformation {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
+ (Class)transformedValueClass {
|
|
|
|
return [NSColor class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)transformedValue:(id)value {
|
|
|
|
BOOL valid = [(NSNumber *)value boolValue];
|
2020-10-04 15:47:50 +00:00
|
|
|
return valid ? nil : [NSColor systemRedColor];
|
2020-09-28 02:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2020-09-14 02:48:55 +00:00
|
|
|
|
|
|
|
void RegisterTransformers(void) {
|
|
|
|
|
|
|
|
NSValueTransformer *t;
|
|
|
|
t = [FileSizeTransformer new];
|
|
|
|
[NSValueTransformer setValueTransformer: t forName: @"FileSizeTransformer"];
|
|
|
|
|
|
|
|
t = [FilePathTransformer new];
|
|
|
|
[NSValueTransformer setValueTransformer: t forName: @"FilePathTransformer"];
|
|
|
|
|
2020-09-28 02:06:03 +00:00
|
|
|
t = [ValidColorTransformer new];
|
|
|
|
[NSValueTransformer setValueTransformer: t forName: @"ValidColorTransformer"];
|
2020-09-14 02:48:55 +00:00
|
|
|
}
|