From b1f6fbc05dde0414eaba5dc5a8f626e2c78200cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C5=93ur?= Date: Fri, 10 Mar 2023 15:05:21 +0800 Subject: [PATCH] fix: change badge font size, position and precision (#5184) --- macosx/BadgeView.mm | 11 +++++---- macosx/NSStringAdditions.h | 4 ++++ macosx/NSStringAdditions.mm | 48 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/macosx/BadgeView.mm b/macosx/BadgeView.mm index 9d5ee3e28..69637117d 100644 --- a/macosx/BadgeView.mm +++ b/macosx/BadgeView.mm @@ -42,7 +42,7 @@ typedef NS_ENUM(NSInteger, ArrowDirection) { _fAttributes = [[NSMutableDictionary alloc] initWithCapacity:3]; _fAttributes[NSForegroundColorAttributeName] = NSColor.whiteColor; _fAttributes[NSShadowAttributeName] = stringShadow; - _fAttributes[NSFontAttributeName] = [NSFont boldSystemFontOfSize:23.0]; + _fAttributes[NSFontAttributeName] = [NSFont boldSystemFontOfSize:26.0]; // DownloadBadge and UploadBadge should have the same size NSSize badgeSize = [NSImage imageNamed:@"DownloadBadge"].size; @@ -81,7 +81,8 @@ typedef NS_ENUM(NSInteger, ArrowDirection) { if (download) { NSImage* downloadBadge = [NSImage imageNamed:@"DownloadBadge"]; - [self badge:downloadBadge arrow:ArrowDirectionDown string:[NSString stringForSpeedAbbrev:self.fDownloadRate] atHeight:bottom]; + [self badge:downloadBadge arrow:ArrowDirectionDown string:[NSString stringForSpeedAbbrevCompact:self.fDownloadRate] + atHeight:bottom]; if (upload) { @@ -91,7 +92,7 @@ typedef NS_ENUM(NSInteger, ArrowDirection) { if (upload) { [self badge:[NSImage imageNamed:@"UploadBadge"] arrow:ArrowDirectionUp - string:[NSString stringForSpeedAbbrev:self.fUploadRate] + string:[NSString stringForSpeedAbbrevCompact:self.fUploadRate] atHeight:bottom]; } } @@ -105,8 +106,8 @@ typedef NS_ENUM(NSInteger, ArrowDirection) { //string is in center of image NSSize stringSize = [string sizeWithAttributes:self.fAttributes]; NSRect stringRect; - stringRect.origin.x = NSMidX(badgeRect) - stringSize.width * 0.5; - stringRect.origin.y = NSMidY(badgeRect) - stringSize.height * 0.5 + 1.0; //adjust for shadow + stringRect.origin.x = NSMidX(badgeRect) - stringSize.width * 0.5 + kArrowInset.width; // adjust for arrow + stringRect.origin.y = NSMidY(badgeRect) - stringSize.height * 0.5 + 1.0; // adjust for shadow stringRect.size = stringSize; [string drawInRect:stringRect withAttributes:self.fAttributes]; diff --git a/macosx/NSStringAdditions.h b/macosx/NSStringAdditions.h index 96219252d..7a35ae246 100644 --- a/macosx/NSStringAdditions.h +++ b/macosx/NSStringAdditions.h @@ -12,8 +12,12 @@ + (NSString*)stringForFileSize:(uint64_t)size; + (NSString*)stringForFilePartialSize:(uint64_t)partialSize fullSize:(uint64_t)fullSize; +// 4 significant digits + (NSString*)stringForSpeed:(CGFloat)speed; +// 4 significant digits + (NSString*)stringForSpeedAbbrev:(CGFloat)speed; +// 3 significant digits ++ (NSString*)stringForSpeedAbbrevCompact:(CGFloat)speed; + (NSString*)stringForRatio:(CGFloat)ratio; + (NSString*)percentString:(CGFloat)progress longDecimals:(BOOL)longDecimals; diff --git a/macosx/NSStringAdditions.mm b/macosx/NSStringAdditions.mm index 5bfad9994..a3dc93e66 100644 --- a/macosx/NSStringAdditions.mm +++ b/macosx/NSStringAdditions.mm @@ -11,6 +11,7 @@ @interface NSString (Private) + (NSString*)stringForSpeed:(CGFloat)speed kb:(NSString*)kb mb:(NSString*)mb gb:(NSString*)gb; ++ (NSString*)stringForSpeedCompact:(CGFloat)speed kb:(NSString*)kb mb:(NSString*)mb gb:(NSString*)gb; @end @@ -73,6 +74,11 @@ return [self stringForSpeed:speed kb:@"K" mb:@"M" gb:@"G"]; } ++ (NSString*)stringForSpeedAbbrevCompact:(CGFloat)speed +{ + return [self stringForSpeedCompact:speed kb:@"K" mb:@"M" gb:@"G"]; +} + + (NSString*)stringForRatio:(CGFloat)ratio { //N/A is different than libtransmission's @@ -188,4 +194,46 @@ } } ++ (NSString*)stringForSpeedCompact:(CGFloat)speed kb:(NSString*)kb mb:(NSString*)mb gb:(NSString*)gb +{ + if (speed < 99.95) // 0.0 KB/s to 99.9 KB/s + { + return [NSString localizedStringWithFormat:@"%.1f %@", speed, kb]; + } + if (speed < 999.95) // 100 KB/s to 999 KB/s + { + return [NSString localizedStringWithFormat:@"%.0f %@", speed, kb]; + } + + speed /= 1000.0; + + if (speed < 9.95) // 1.00 MB/s to 9.99 MB/s + { + return [NSString localizedStringWithFormat:@"%.2f %@", speed, mb]; + } + if (speed < 99.95) // 10.0 MB/s to 99.9 MB/s + { + return [NSString localizedStringWithFormat:@"%.1f %@", speed, mb]; + } + if (speed < 999.95) // 100 MB/s to 999 MB/s + { + return [NSString localizedStringWithFormat:@"%.0f %@", speed, mb]; + } + + speed /= 1000.0; + + if (speed < 9.95) // 1.00 GB/s to 9.99 GB/s + { + return [NSString localizedStringWithFormat:@"%.2f %@", speed, gb]; + } + if (speed < 99.95) // 10.0 GB/s to 99.9 GB/s + { + return [NSString localizedStringWithFormat:@"%.1f %@", speed, gb]; + } + else // insane speeds + { + return [NSString localizedStringWithFormat:@"%.0f %@", speed, gb]; + } +} + @end