Changeset 13411


Ignore:
Timestamp:
Jul 25, 2012, 12:46:49 PM (9 years ago)
Author:
livings124
Message:

On 10.8, use NSByteCountFormatter to keep file sizes consistent with the operating system. Also, on 10.8 don't use a number formatter to localize unsigned integers.

Location:
trunk/macosx
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/NSApplicationAdditions.h

    r13162 r13411  
    2525#define NSPopoverLion NSClassFromString(@"NSPopover")
    2626#define NSDataDetectorLion NSClassFromString(@"NSDataDetector")
     27#define NSByteCountFormatterMtLion NSClassFromString(@"NSByteCountFormatter")
     28#define NSUserNotificationMtLion NSClassFromString(@"NSUserNotification")
     29#define NSUserNotificationCenterMtLion NSClassFromString(@"NSUserNotificationCenter")
    2730
    2831@interface NSApplication (NSApplicationAdditions)
    2932
    3033- (BOOL) isOnLionOrBetter;
     34- (BOOL) isOnMountainLionOrBetter;
    3135
    3236@end
  • trunk/macosx/NSApplicationAdditions.m

    r13162 r13411  
    3232}
    3333
     34- (BOOL) isOnMountainLionOrBetter
     35{
     36        return floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_7;
     37}
     38
    3439@end
  • trunk/macosx/NSStringAdditions.m

    r13369 r13411  
    2323 *****************************************************************************/
    2424
     25#import "NSApplicationAdditions.h"
    2526#import "NSStringAdditions.h"
    2627
     
    3031@interface NSString (Private)
    3132
    32 + (NSString *) stringForFileSize: (uint64_t) size showUnitUnless: (NSString *) notAllowedUnit unitsUsed: (NSString **) unitUsed;
     33+ (NSString *) stringForFileSizeLion: (uint64_t) size showUnitUnless: (NSString *) notAllowedUnit unitsUsed: (NSString **) unitUsed;
    3334
    3435+ (NSString *) stringForSpeed: (CGFloat) speed kb: (NSString *) kb mb: (NSString *) mb gb: (NSString *) gb;
     
    4849}
    4950
    50 #warning use localizedStringWithFormat: directly in roardacted
     51#warning use localizedStringWithFormat: directly when 10.8-only
    5152+ (NSString *) formattedUInteger: (NSUInteger) value
    5253{
    53     static NSNumberFormatter * numberFormatter;
    54     static dispatch_once_t onceToken;
    55     dispatch_once(&onceToken, ^{
    56         numberFormatter = [[NSNumberFormatter alloc] init];
    57         [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
    58         [numberFormatter setMaximumFractionDigits: 0];
    59     });
    60    
    61     return [numberFormatter stringFromNumber: [NSNumber numberWithUnsignedInteger: value]];
    62 }
    63 
     54    if ([NSApp isOnMountainLionOrBetter])
     55        return [NSString localizedStringWithFormat: @"%lu", value];
     56    else
     57    {
     58        static NSNumberFormatter * numberFormatter;
     59        static dispatch_once_t onceToken;
     60        dispatch_once(&onceToken, ^{
     61            numberFormatter = [[NSNumberFormatter alloc] init];
     62            [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
     63            [numberFormatter setMaximumFractionDigits: 0];
     64        });
     65       
     66        return [numberFormatter stringFromNumber: [NSNumber numberWithUnsignedInteger: value]];
     67    }
     68}
     69
     70#warning should we take long long instead?
    6471+ (NSString *) stringForFileSize: (uint64_t) size
    6572{
    66     return [self stringForFileSize: size showUnitUnless: nil unitsUsed: nil];
    67 }
    68 
     73    if ([NSApp isOnMountainLionOrBetter])
     74        return [NSByteCountFormatterMtLion stringFromByteCount: size countStyle: NSByteCountFormatterCountStyleFile];
     75    else
     76        return [self stringForFileSizeLion: size showUnitUnless: nil unitsUsed: nil];
     77}
     78
     79#warning should we take long long instead?
    6980+ (NSString *) stringForFilePartialSize: (uint64_t) partialSize fullSize: (uint64_t) fullSize
    7081{
    71     NSString * units;
    72     NSString * fullString = [self stringForFileSize: fullSize showUnitUnless: nil unitsUsed: &units];
    73     NSString * partialString = [self stringForFileSize: partialSize showUnitUnless: units unitsUsed: nil];
     82    NSString * partialString, * fullString;
     83    if ([NSApp isOnMountainLionOrBetter])
     84    {
     85        NSByteCountFormatter *fileSizeFormatter = [[NSByteCountFormatterMtLion alloc] init];
     86       
     87        //only show units for the partial file size if it's different than the full file size's
     88        [fileSizeFormatter setIncludesCount: NO];
     89        const BOOL partialUnitsDifferent = ![[fileSizeFormatter stringFromByteCount: partialSize] isEqualToString: [fileSizeFormatter stringFromByteCount: fullSize]];
     90       
     91        [fileSizeFormatter setIncludesCount: YES];
     92        fullString = [fileSizeFormatter stringFromByteCount: fullSize];
     93       
     94        [fileSizeFormatter setIncludesUnit: partialUnitsDifferent];
     95        partialString = [fileSizeFormatter stringFromByteCount: partialSize];
     96       
     97        [fileSizeFormatter release];
     98    }
     99    else
     100    {
     101        NSString * units;
     102        fullString = [self stringForFileSizeLion: fullSize showUnitUnless: nil unitsUsed: &units];
     103        partialString = [self stringForFileSizeLion: partialSize showUnitUnless: units unitsUsed: nil];
     104    }
    74105   
    75106    return [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "file size string"), partialString, fullString];
     
    110141{
    111142    if (progress >= 1.0)
    112         return @"100%";
     143        return [NSString localizedStringWithFormat: @"%d%%", 100];
    113144    else if (longDecimals)
    114145        return [NSString localizedStringWithFormat: @"%.2f%%", tr_truncd(progress * 100.0, 2)];
     
    124155+ (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds maxFields: (NSUInteger) max
    125156{
    126     NSAssert(max > 0, @"Cannot generate a time string with no fields");
     157    NSParameterAssert(max > 0);
    127158   
    128159    NSMutableArray * timeArray = [NSMutableArray arrayWithCapacity: MIN(max, 5)];
     
    205236@implementation NSString (Private)
    206237
    207 + (NSString *) stringForFileSize: (uint64_t) size showUnitUnless: (NSString *) notAllowedUnit unitsUsed: (NSString **) unitUsed
     238+ (NSString *) stringForFileSizeLion: (uint64_t) size showUnitUnless: (NSString *) notAllowedUnit unitsUsed: (NSString **) unitUsed
    208239{
    209240    double convertedSize;
Note: See TracChangeset for help on using the changeset viewer.